2020-10-06 15:25:53 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package console_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-11-13 11:41:35 +00:00
|
|
|
"storj.io/common/storj"
|
2020-10-06 15:25:53 +01:00
|
|
|
"storj.io/common/testcontext"
|
2020-11-13 11:41:35 +00:00
|
|
|
"storj.io/common/testrand"
|
2020-10-09 14:40:12 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-10-06 15:25:53 +01:00
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 2},
|
|
|
|
func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
service := sat.API.Console.Service
|
|
|
|
|
|
|
|
up1Pro1, err := sat.API.DB.Console().Projects().Get(ctx, planet.Uplinks[0].Projects[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
up2Pro1, err := sat.API.DB.Console().Projects().Get(ctx, planet.Uplinks[1].Projects[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
up2User, err := sat.API.DB.Console().Users().Get(ctx, up2Pro1.OwnerID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NotEqual(t, up1Pro1.ID, up2Pro1.ID)
|
|
|
|
require.NotEqual(t, up1Pro1.OwnerID, up2Pro1.OwnerID)
|
|
|
|
|
2020-10-09 14:40:12 +01:00
|
|
|
authCtx1, err := sat.AuthenticatedContext(ctx, up1Pro1.OwnerID)
|
2020-10-06 15:25:53 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
authCtx2, err := sat.AuthenticatedContext(ctx, up2Pro1.OwnerID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("TestGetProject", func(t *testing.T) {
|
|
|
|
// Getting own project details should work
|
2020-10-09 14:40:12 +01:00
|
|
|
project, err := service.GetProject(authCtx1, up1Pro1.ID)
|
2020-10-06 15:25:53 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, up1Pro1.ID, project.ID)
|
|
|
|
|
|
|
|
// Getting someone else project details should not work
|
2020-10-09 14:40:12 +01:00
|
|
|
project, err = service.GetProject(authCtx1, up2Pro1.ID)
|
2020-10-06 15:25:53 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, project)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TestUpdateProject", func(t *testing.T) {
|
|
|
|
// Updating own project should work
|
2020-10-09 14:40:12 +01:00
|
|
|
updatedPro, err := service.UpdateProject(authCtx1, up1Pro1.ID, "newName", "TestUpdate")
|
2020-10-06 15:25:53 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, up1Pro1.Name, updatedPro.Name)
|
|
|
|
|
|
|
|
// Updating someone else project details should not work
|
2020-10-09 14:40:12 +01:00
|
|
|
updatedPro, err = service.UpdateProject(authCtx1, up2Pro1.ID, "newName", "TestUpdate")
|
2020-10-06 15:25:53 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, updatedPro)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TestAddProjectMembers", func(t *testing.T) {
|
|
|
|
// Adding members to own project should work
|
2020-10-09 14:40:12 +01:00
|
|
|
addedUsers, err := service.AddProjectMembers(authCtx1, up1Pro1.ID, []string{up2User.Email})
|
2020-10-06 15:25:53 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, addedUsers, 1)
|
|
|
|
require.Contains(t, addedUsers, up2User)
|
|
|
|
|
|
|
|
// Adding members to someone else project should not work
|
2020-10-09 14:40:12 +01:00
|
|
|
addedUsers, err = service.AddProjectMembers(authCtx1, up2Pro1.ID, []string{up2User.Email})
|
2020-10-06 15:25:53 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, addedUsers)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TestGetProjectMembers", func(t *testing.T) {
|
|
|
|
// Getting the project members of an own project that one is a part of should work
|
2020-10-09 14:40:12 +01:00
|
|
|
userPage, err := service.GetProjectMembers(authCtx1, up1Pro1.ID, console.ProjectMembersCursor{Page: 1, Limit: 10})
|
2020-10-06 15:25:53 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, userPage.ProjectMembers, 2)
|
|
|
|
|
|
|
|
// Getting the project members of a foreign project that one is a part of should work
|
|
|
|
userPage, err = service.GetProjectMembers(authCtx2, up1Pro1.ID, console.ProjectMembersCursor{Page: 1, Limit: 10})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, userPage.ProjectMembers, 2)
|
|
|
|
|
|
|
|
// Getting the project members of a foreign project that one is not a part of should not work
|
2020-10-09 14:40:12 +01:00
|
|
|
userPage, err = service.GetProjectMembers(authCtx1, up2Pro1.ID, console.ProjectMembersCursor{Page: 1, Limit: 10})
|
2020-10-06 15:25:53 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, userPage)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TestDeleteProjectMembers", func(t *testing.T) {
|
|
|
|
// Deleting project members of an own project should work
|
2020-10-09 14:40:12 +01:00
|
|
|
err := service.DeleteProjectMembers(authCtx1, up1Pro1.ID, []string{up2User.Email})
|
2020-10-06 15:25:53 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Deleting Project members of someone else project should not work
|
2020-10-09 14:40:12 +01:00
|
|
|
err = service.DeleteProjectMembers(authCtx1, up2Pro1.ID, []string{up2User.Email})
|
2020-10-06 15:25:53 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TestDeleteProject", func(t *testing.T) {
|
2020-10-09 14:40:12 +01:00
|
|
|
// Deleting the own project should not work before deleting the API-Key
|
|
|
|
err := service.DeleteProject(authCtx1, up1Pro1.ID)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
keys, err := service.GetAPIKeys(authCtx1, up1Pro1.ID, console.APIKeyCursor{Page: 1, Limit: 10})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, keys.APIKeys, 1)
|
|
|
|
|
|
|
|
err = service.DeleteAPIKeys(authCtx1, []uuid.UUID{keys.APIKeys[0].ID})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Deleting the own project should now work
|
|
|
|
err = service.DeleteProject(authCtx1, up1Pro1.ID)
|
2020-10-06 15:25:53 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Deleting someone else project should not work
|
2020-10-09 14:40:12 +01:00
|
|
|
err = service.DeleteProject(authCtx1, up2Pro1.ID)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
err = planet.Uplinks[1].CreateBucket(ctx, sat, "testbucket")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// deleting a project with a bucket should fail
|
|
|
|
err = service.DeleteProject(authCtx2, up2Pro1.ID)
|
2020-10-06 15:25:53 +01:00
|
|
|
require.Error(t, err)
|
2020-10-09 14:40:12 +01:00
|
|
|
require.Equal(t, "service error: project usage error: some buckets still exist", err.Error())
|
2020-10-06 15:25:53 +01:00
|
|
|
})
|
2020-11-05 16:16:55 +00:00
|
|
|
|
|
|
|
t.Run("TestChangeEmail", func(t *testing.T) {
|
|
|
|
const newEmail = "newEmail@example.com"
|
|
|
|
|
|
|
|
err = service.ChangeEmail(authCtx2, newEmail)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
userWithUpdatedEmail, err := service.GetUserByEmail(authCtx2, newEmail)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, newEmail, userWithUpdatedEmail.Email)
|
|
|
|
|
|
|
|
err = service.ChangeEmail(authCtx2, newEmail)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2020-11-13 11:41:35 +00:00
|
|
|
|
|
|
|
t.Run("TestGetAllBucketNames", func(t *testing.T) {
|
|
|
|
bucket1 := storj.Bucket{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
Name: "testBucket1",
|
|
|
|
ProjectID: up2Pro1.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket2 := storj.Bucket{
|
|
|
|
ID: testrand.UUID(),
|
|
|
|
Name: "testBucket2",
|
|
|
|
ProjectID: up2Pro1.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := sat.DB.Buckets().CreateBucket(authCtx1, bucket1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = sat.DB.Buckets().CreateBucket(authCtx1, bucket2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bucketNames, err := service.GetAllBucketNames(authCtx1, up2Pro1.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, bucket1.Name, bucketNames[0])
|
|
|
|
require.Equal(t, bucket2.Name, bucketNames[1])
|
|
|
|
})
|
2020-10-06 15:25:53 +01:00
|
|
|
})
|
|
|
|
}
|