2020-05-18 21:37:18 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package admin_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-10-11 12:39:08 +01:00
|
|
|
"io"
|
2020-05-18 21:37:18 +01:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-04-20 11:33:48 +01:00
|
|
|
"storj.io/common/memory"
|
2023-07-27 13:54:48 +01:00
|
|
|
"storj.io/common/storj"
|
2020-05-18 21:37:18 +01:00
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestUserGet(t *testing.T) {
|
2020-05-18 21:37:18 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-05-18 21:37:18 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
address := sat.Admin.Admin.Listener.Addr()
|
|
|
|
project := planet.Uplinks[0].Projects[0]
|
|
|
|
|
2020-10-02 23:40:15 +01:00
|
|
|
projLimit, err := sat.DB.Console().Users().GetProjectLimit(ctx, project.Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
link := "http://" + address.String() + "/api/users/" + project.Owner.Email
|
|
|
|
expectedBody := `{` +
|
2023-07-27 13:54:48 +01:00
|
|
|
fmt.Sprintf(`"user":{"id":"%s","fullName":"User uplink0_0","email":"%s","projectLimit":%d,"placement":%d},`, project.Owner.ID, project.Owner.Email, projLimit, storj.EveryCountry) +
|
2021-08-27 01:51:26 +01:00
|
|
|
fmt.Sprintf(`"projects":[{"id":"%s","name":"uplink0_0","description":"","ownerId":"%s"}]}`, project.ID, project.Owner.ID)
|
2020-05-18 21:37:18 +01:00
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
assertReq(ctx, t, link, http.MethodGet, "", http.StatusOK, expectedBody, planet.Satellites[0].Config.Console.AuthToken)
|
2020-05-18 21:37:18 +01:00
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
link = "http://" + address.String() + "/api/users/" + "user-not-exist@not-exist.test"
|
|
|
|
body := assertReq(ctx, t, link, http.MethodGet, "", http.StatusNotFound, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(body), "does not exist")
|
2020-05-18 21:37:18 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestUserAdd(t *testing.T) {
|
2020-05-18 21:37:18 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-05-18 21:37:18 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
email := "alice+2@mail.test"
|
|
|
|
|
|
|
|
body := strings.NewReader(fmt.Sprintf(`{"email":"%s","fullName":"Alice Test","password":"123a123"}`, email))
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://"+address.String()+"/api/users", body)
|
2020-05-18 21:37:18 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-05-18 21:37:18 +01:00
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
2020-05-18 21:37:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
|
|
|
var output console.User
|
|
|
|
|
|
|
|
err = json.Unmarshal(responseBody, &output)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
user, err := planet.Satellites[0].DB.Console().Users().Get(ctx, output.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, email, user.Email)
|
|
|
|
})
|
|
|
|
}
|
2020-05-20 10:20:04 +01:00
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestUserAdd_sameEmail(t *testing.T) {
|
2020-07-16 13:17:30 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-07-16 13:17:30 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
email := "alice+2@mail.test"
|
|
|
|
|
|
|
|
body := strings.NewReader(fmt.Sprintf(`{"email":"%s","fullName":"Alice Test","password":"123a123"}`, email))
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://"+address.String()+"/api/users", body)
|
2020-07-16 13:17:30 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-07-16 13:17:30 +01:00
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
2020-07-16 13:17:30 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
|
|
|
var output console.User
|
|
|
|
|
|
|
|
err = json.Unmarshal(responseBody, &output)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
user, err := planet.Satellites[0].DB.Console().Users().Get(ctx, output.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, email, user.Email)
|
|
|
|
|
|
|
|
// Add same user again, this should fail
|
|
|
|
body = strings.NewReader(fmt.Sprintf(`{"email":"%s","fullName":"Alice Test","password":"123a123"}`, email))
|
2021-08-03 12:24:21 +01:00
|
|
|
req, err = http.NewRequestWithContext(ctx, http.MethodPost, "http://"+address.String()+"/api/users", body)
|
2020-07-16 13:17:30 +01:00
|
|
|
require.NoError(t, err)
|
2020-08-13 13:40:05 +01:00
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
2020-07-16 13:17:30 +01:00
|
|
|
|
|
|
|
response, err = http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusConflict, response.StatusCode)
|
2021-10-01 12:36:41 +01:00
|
|
|
require.Equal(t, "application/json", response.Header.Get("Content-Type"))
|
2020-07-16 17:37:34 +01:00
|
|
|
require.NoError(t, response.Body.Close())
|
2020-07-16 13:17:30 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestUserUpdate(t *testing.T) {
|
2020-10-02 23:40:15 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-10-02 23:40:15 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
user, err := planet.Satellites[0].DB.Console().Users().GetByEmail(ctx, planet.Uplinks[0].Projects[0].Owner.Email)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
t.Run("OK", func(t *testing.T) {
|
2022-04-19 14:07:44 +01:00
|
|
|
// Update user data.
|
2021-10-07 11:42:25 +01:00
|
|
|
link := fmt.Sprintf("http://"+address.String()+"/api/users/%s", user.Email)
|
|
|
|
body := `{"email":"alice+2@mail.test", "shortName":"Newbie"}`
|
|
|
|
responseBody := assertReq(ctx, t, link, http.MethodPut, body, http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Len(t, responseBody, 0)
|
2020-10-02 23:40:15 +01:00
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
updatedUser, err := planet.Satellites[0].DB.Console().Users().Get(ctx, user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "alice+2@mail.test", updatedUser.Email)
|
|
|
|
require.Equal(t, user.FullName, updatedUser.FullName)
|
|
|
|
require.NotEqual(t, "Newbie", user.ShortName)
|
|
|
|
require.Equal(t, "Newbie", updatedUser.ShortName)
|
|
|
|
require.Equal(t, user.ID, updatedUser.ID)
|
|
|
|
require.Equal(t, user.Status, updatedUser.Status)
|
|
|
|
require.Equal(t, user.ProjectLimit, updatedUser.ProjectLimit)
|
|
|
|
|
2022-04-19 14:07:44 +01:00
|
|
|
// Update project limit.
|
2021-10-07 11:42:25 +01:00
|
|
|
link = "http://" + address.String() + "/api/users/alice+2@mail.test"
|
|
|
|
newLimit := 50
|
|
|
|
body = fmt.Sprintf(`{"projectLimit":%d}`, newLimit)
|
|
|
|
responseBody = assertReq(ctx, t, link, http.MethodPut, body, http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Len(t, responseBody, 0)
|
|
|
|
|
2022-04-19 14:07:44 +01:00
|
|
|
updatedUserProjectLimit, err := planet.Satellites[0].DB.Console().Users().Get(ctx, user.ID)
|
2021-10-07 11:42:25 +01:00
|
|
|
require.NoError(t, err)
|
2022-04-19 14:07:44 +01:00
|
|
|
require.Equal(t, updatedUser.Email, updatedUserProjectLimit.Email)
|
|
|
|
require.Equal(t, updatedUser.ID, updatedUserProjectLimit.ID)
|
|
|
|
require.Equal(t, updatedUser.Status, updatedUserProjectLimit.Status)
|
|
|
|
require.Equal(t, newLimit, updatedUserProjectLimit.ProjectLimit)
|
|
|
|
|
|
|
|
// Update paid tier status and usage.
|
|
|
|
link = "http://" + address.String() + "/api/users/alice+2@mail.test"
|
|
|
|
newUsageLimit := int64(1000)
|
|
|
|
body1 := fmt.Sprintf(`{"projectStorageLimit":%d, "projectBandwidthLimit":%d, "projectSegmentLimit":%d, "paidTierStr":"true"}`, newUsageLimit, newUsageLimit, newUsageLimit)
|
|
|
|
responseBody = assertReq(ctx, t, link, http.MethodPut, body1, http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Len(t, responseBody, 0)
|
|
|
|
|
|
|
|
updatedUserStatusAndUsageLimits, err := planet.Satellites[0].DB.Console().Users().Get(ctx, user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, updatedUser.Email, updatedUserStatusAndUsageLimits.Email)
|
|
|
|
require.Equal(t, updatedUser.ID, updatedUserStatusAndUsageLimits.ID)
|
|
|
|
require.Equal(t, updatedUser.Status, updatedUserStatusAndUsageLimits.Status)
|
|
|
|
require.True(t, updatedUserStatusAndUsageLimits.PaidTier)
|
|
|
|
require.Equal(t, newUsageLimit, updatedUserStatusAndUsageLimits.ProjectStorageLimit)
|
|
|
|
require.Equal(t, newUsageLimit, updatedUserStatusAndUsageLimits.ProjectBandwidthLimit)
|
|
|
|
require.Equal(t, newUsageLimit, updatedUserStatusAndUsageLimits.ProjectSegmentLimit)
|
2023-02-02 22:58:17 +00:00
|
|
|
|
2023-04-20 11:33:48 +01:00
|
|
|
var updateLimitsTests = []struct {
|
|
|
|
newStorageLimit memory.Size
|
|
|
|
newBandwidthLimit memory.Size
|
|
|
|
newSegmentLimit int64
|
|
|
|
useSizeString bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
15000,
|
|
|
|
25000,
|
|
|
|
35000,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
50 * memory.KB,
|
|
|
|
75 * memory.KB,
|
|
|
|
40000,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range updateLimitsTests {
|
|
|
|
// Update user limits and project limits (current and existing projects for a user).
|
|
|
|
link = "http://" + address.String() + "/api/users/alice+2@mail.test/limits"
|
|
|
|
jsonStr := `{"storage":"%d", "bandwidth":"%d", "segment":%d}`
|
|
|
|
if tt.useSizeString {
|
|
|
|
jsonStr = strings.Replace(jsonStr, "%d", "%v", 2)
|
|
|
|
}
|
|
|
|
body2 := fmt.Sprintf(jsonStr, tt.newStorageLimit, tt.newBandwidthLimit, tt.newSegmentLimit)
|
|
|
|
responseBody = assertReq(ctx, t, link, http.MethodPut, body2, http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Len(t, responseBody, 0)
|
|
|
|
|
|
|
|
// Get user limits returns new updated limits
|
|
|
|
link2 := "http://" + address.String() + "/api/users/alice+2@mail.test/limits"
|
|
|
|
expectedBody := `{` +
|
|
|
|
fmt.Sprintf(`"storage":%d,"bandwidth":%d,"segment":%d}`, tt.newStorageLimit, tt.newBandwidthLimit, tt.newSegmentLimit)
|
|
|
|
assertReq(ctx, t, link2, http.MethodGet, "", http.StatusOK, expectedBody, planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
userUpdatedLimits, err := planet.Satellites[0].DB.Console().Users().Get(ctx, user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tt.newStorageLimit.Int64(), userUpdatedLimits.ProjectStorageLimit)
|
|
|
|
require.Equal(t, tt.newBandwidthLimit.Int64(), userUpdatedLimits.ProjectBandwidthLimit)
|
|
|
|
require.Equal(t, tt.newSegmentLimit, userUpdatedLimits.ProjectSegmentLimit)
|
|
|
|
|
|
|
|
projectUpdatedLimits, err := planet.Satellites[0].DB.Console().Projects().Get(ctx, planet.Uplinks[0].Projects[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tt.newStorageLimit, *projectUpdatedLimits.StorageLimit)
|
|
|
|
require.Equal(t, tt.newBandwidthLimit, *projectUpdatedLimits.BandwidthLimit)
|
|
|
|
require.Equal(t, tt.newSegmentLimit, *projectUpdatedLimits.SegmentLimit)
|
|
|
|
}
|
2021-10-07 11:42:25 +01:00
|
|
|
})
|
2020-10-02 23:40:15 +01:00
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
t.Run("Not found", func(t *testing.T) {
|
|
|
|
link := "http://" + address.String() + "/api/users/user-not-exists@not-exists.test"
|
|
|
|
body := `{"email":"alice+2@mail.test", "shortName":"Newbie"}`
|
|
|
|
responseBody := assertReq(ctx, t, link, http.MethodPut, body, http.StatusNotFound, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(responseBody), "does not exist")
|
|
|
|
})
|
2023-06-01 23:09:13 +01:00
|
|
|
|
|
|
|
t.Run("Email already used", func(t *testing.T) {
|
|
|
|
link := fmt.Sprintf("http://"+address.String()+"/api/users/%s", "alice+2@mail.test")
|
|
|
|
body := `{"email":"alice+2@mail.test", "shortName":"Newbie"}`
|
|
|
|
responseBody := assertReq(ctx, t, link, http.MethodPut, body, http.StatusConflict, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(responseBody), "already exists")
|
|
|
|
})
|
2020-05-20 10:20:04 +01:00
|
|
|
})
|
|
|
|
}
|
2020-07-06 22:31:40 +01:00
|
|
|
|
2023-06-13 16:58:24 +01:00
|
|
|
func TestUpdateUsersUserAgent(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
db := planet.Satellites[0].DB
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
project := planet.Uplinks[0].Projects[0]
|
|
|
|
newUserAgent := "awesome user agent value"
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
body := strings.NewReader(fmt.Sprintf(`{"userAgent":"%s"}`, newUserAgent))
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, fmt.Sprintf("http://"+address.String()+"/api/users/%s/useragent", project.Owner.Email), body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
response, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, response.StatusCode)
|
|
|
|
require.NoError(t, response.Body.Close())
|
|
|
|
|
|
|
|
newUserAgentBytes := []byte(newUserAgent)
|
|
|
|
|
|
|
|
updatedUser, err := db.Console().Users().Get(ctx, project.Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, newUserAgentBytes, updatedUser.UserAgent)
|
|
|
|
|
|
|
|
updatedProject, err := db.Console().Projects().Get(ctx, project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, newUserAgentBytes, updatedProject.UserAgent)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Same UserAgent", func(t *testing.T) {
|
|
|
|
err := db.Console().Users().UpdateUserAgent(ctx, project.Owner.ID, []byte(newUserAgent))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
link := fmt.Sprintf("http://"+address.String()+"/api/users/%s/useragent", project.Owner.Email)
|
|
|
|
body := fmt.Sprintf(`{"userAgent":"%s"}`, newUserAgent)
|
|
|
|
responseBody := assertReq(ctx, t, link, http.MethodPatch, body, http.StatusBadRequest, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(responseBody), "new UserAgent is equal to existing users UserAgent")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-27 23:04:12 +01:00
|
|
|
func TestDisableMFA(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
user, err := planet.Satellites[0].DB.Console().Users().GetByEmail(ctx, planet.Uplinks[0].Projects[0].Owner.Email)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Enable MFA.
|
|
|
|
user.MFAEnabled = true
|
|
|
|
user.MFASecretKey = "randomtext"
|
|
|
|
user.MFARecoveryCodes = []string{"0123456789"}
|
2022-06-01 22:15:37 +01:00
|
|
|
|
|
|
|
secretKeyPtr := &user.MFASecretKey
|
|
|
|
|
|
|
|
err = planet.Satellites[0].DB.Console().Users().Update(ctx, user.ID, console.UpdateUserRequest{
|
|
|
|
MFAEnabled: &user.MFAEnabled,
|
|
|
|
MFASecretKey: &secretKeyPtr,
|
|
|
|
MFARecoveryCodes: &user.MFARecoveryCodes,
|
|
|
|
})
|
2022-05-27 23:04:12 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Ensure MFA is enabled.
|
|
|
|
updatedUser, err := planet.Satellites[0].DB.Console().Users().Get(ctx, user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, updatedUser.MFAEnabled)
|
|
|
|
|
|
|
|
// Disabling users MFA should work.
|
|
|
|
link := fmt.Sprintf("http://"+address.String()+"/api/users/%s/mfa", user.Email)
|
|
|
|
body := assertReq(ctx, t, link, http.MethodDelete, "", http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Len(t, body, 0)
|
|
|
|
|
|
|
|
// Ensure MFA is disabled.
|
|
|
|
updatedUser, err = planet.Satellites[0].DB.Console().Users().Get(ctx, user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, false, updatedUser.MFAEnabled)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-16 15:17:27 +00:00
|
|
|
func TestFreezeUnfreezeUser(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
userPreFreeze, err := planet.Satellites[0].DB.Console().Users().Get(ctx, planet.Uplinks[0].Projects[0].Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotZero(t, userPreFreeze.ProjectStorageLimit)
|
|
|
|
require.NotZero(t, userPreFreeze.ProjectBandwidthLimit)
|
|
|
|
|
|
|
|
projectPreFreeze, err := planet.Satellites[0].DB.Console().Projects().Get(ctx, planet.Uplinks[0].Projects[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotZero(t, projectPreFreeze.BandwidthLimit)
|
|
|
|
require.NotZero(t, projectPreFreeze.StorageLimit)
|
|
|
|
|
|
|
|
// freeze can be run multiple times. Test that doing so does not affect Unfreeze result.
|
|
|
|
link := fmt.Sprintf("http://"+address.String()+"/api/users/%s/freeze", userPreFreeze.Email)
|
|
|
|
body := assertReq(ctx, t, link, http.MethodPut, "", http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Len(t, body, 0)
|
|
|
|
|
|
|
|
link = fmt.Sprintf("http://"+address.String()+"/api/users/%s/freeze", userPreFreeze.Email)
|
|
|
|
body = assertReq(ctx, t, link, http.MethodPut, "", http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Len(t, body, 0)
|
|
|
|
|
|
|
|
userPostFreeze, err := planet.Satellites[0].DB.Console().Users().Get(ctx, userPreFreeze.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Zero(t, userPostFreeze.ProjectStorageLimit)
|
|
|
|
require.Zero(t, userPostFreeze.ProjectBandwidthLimit)
|
|
|
|
|
|
|
|
projectPostFreeze, err := planet.Satellites[0].DB.Console().Projects().Get(ctx, planet.Uplinks[0].Projects[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Zero(t, projectPostFreeze.BandwidthLimit.Int64())
|
|
|
|
require.Zero(t, projectPostFreeze.StorageLimit.Int64())
|
|
|
|
|
|
|
|
link = fmt.Sprintf("http://"+address.String()+"/api/users/%s/freeze", userPreFreeze.Email)
|
|
|
|
body = assertReq(ctx, t, link, http.MethodDelete, "", http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Len(t, body, 0)
|
|
|
|
|
|
|
|
unfrozenUser, err := planet.Satellites[0].DB.Console().Users().Get(ctx, userPreFreeze.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, userPreFreeze.ProjectStorageLimit, unfrozenUser.ProjectStorageLimit)
|
|
|
|
require.Equal(t, userPreFreeze.ProjectBandwidthLimit, unfrozenUser.ProjectBandwidthLimit)
|
|
|
|
|
|
|
|
unfrozenProject, err := planet.Satellites[0].DB.Console().Projects().Get(ctx, projectPreFreeze.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, projectPreFreeze.StorageLimit, unfrozenProject.StorageLimit)
|
|
|
|
require.Equal(t, projectPreFreeze.BandwidthLimit, unfrozenProject.BandwidthLimit)
|
|
|
|
|
|
|
|
body = assertReq(ctx, t, link, http.MethodDelete, "", http.StatusInternalServerError, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(body), "not frozen")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-21 18:09:54 +01:00
|
|
|
func TestWarnUnwarnUser(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
user, err := planet.Satellites[0].DB.Console().Users().Get(ctx, planet.Uplinks[0].Projects[0].Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = planet.Satellites[0].Admin.FreezeAccounts.Service.WarnUser(ctx, user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
freeze, warning, err := planet.Satellites[0].DB.Console().AccountFreezeEvents().GetAll(ctx, user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, freeze)
|
|
|
|
require.NotNil(t, warning)
|
|
|
|
|
|
|
|
link := fmt.Sprintf("http://"+address.String()+"/api/users/%s/warning", user.Email)
|
|
|
|
body := assertReq(ctx, t, link, http.MethodDelete, "", http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Len(t, body, 0)
|
|
|
|
|
|
|
|
freeze, warning, err = planet.Satellites[0].DB.Console().AccountFreezeEvents().GetAll(ctx, user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, freeze)
|
|
|
|
require.Nil(t, warning)
|
|
|
|
|
|
|
|
body = assertReq(ctx, t, link, http.MethodDelete, "", http.StatusInternalServerError, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(body), "user is not warned")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
func TestUserDelete(t *testing.T) {
|
2020-07-06 22:31:40 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2021-10-01 12:36:41 +01:00
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
2020-07-06 22:31:40 +01:00
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
user, err := planet.Satellites[0].DB.Console().Users().GetByEmail(ctx, planet.Uplinks[0].Projects[0].Owner.Email)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-27 23:04:12 +01:00
|
|
|
// Deleting the user should fail, as project exists.
|
2021-10-07 11:42:25 +01:00
|
|
|
link := fmt.Sprintf("http://"+address.String()+"/api/users/%s", user.Email)
|
|
|
|
body := assertReq(ctx, t, link, http.MethodDelete, "", http.StatusConflict, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Greater(t, len(body), 0)
|
2020-07-06 22:31:40 +01:00
|
|
|
|
|
|
|
err = planet.Satellites[0].DB.Console().Projects().Delete(ctx, planet.Uplinks[0].Projects[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-07 11:42:25 +01:00
|
|
|
// Deleting the user should pass, as no project exists for given user.
|
|
|
|
body = assertReq(ctx, t, link, http.MethodDelete, "", http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Len(t, body, 0)
|
2020-07-06 22:31:40 +01:00
|
|
|
|
2022-02-04 17:31:24 +00:00
|
|
|
// Deleting non-existing user returns Not Found.
|
2021-10-07 11:42:25 +01:00
|
|
|
body = assertReq(ctx, t, link, http.MethodDelete, "", http.StatusNotFound, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(body), "does not exist")
|
2020-07-06 22:31:40 +01:00
|
|
|
})
|
2020-07-16 13:17:30 +01:00
|
|
|
}
|
2023-07-27 13:54:48 +01:00
|
|
|
|
|
|
|
func TestSetUsersGeofence(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
StorageNodeCount: 0,
|
|
|
|
UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) {
|
|
|
|
config.Admin.Address = "127.0.0.1:0"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
db := planet.Satellites[0].DB
|
|
|
|
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
|
|
|
|
project := planet.Uplinks[0].Projects[0]
|
|
|
|
newPlacement := storj.EU
|
|
|
|
newPlacementStr := "EU"
|
|
|
|
link := fmt.Sprintf("http://"+address.String()+"/api/users/%s/geofence", project.Owner.Email)
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
body := fmt.Sprintf(`{"region":"%s"}`, newPlacementStr)
|
|
|
|
assertReq(ctx, t, link, http.MethodPatch, body, http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
|
|
|
|
updatedUser, err := db.Console().Users().Get(ctx, project.Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, newPlacement, updatedUser.DefaultPlacement)
|
|
|
|
|
|
|
|
// DELETE
|
|
|
|
assertReq(ctx, t, link, http.MethodDelete, "", http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
updatedUser, err = db.Console().Users().Get(ctx, project.Owner.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, storj.EveryCountry, updatedUser.DefaultPlacement)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Same Placement", func(t *testing.T) {
|
|
|
|
err := db.Console().Users().Update(ctx, project.Owner.ID, console.UpdateUserRequest{
|
|
|
|
Email: &project.Owner.Email,
|
|
|
|
DefaultPlacement: newPlacement,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
body := fmt.Sprintf(`{"region":"%s"}`, newPlacementStr)
|
|
|
|
responseBody := assertReq(ctx, t, link, http.MethodPatch, body, http.StatusBadRequest, "", planet.Satellites[0].Config.Console.AuthToken)
|
|
|
|
require.Contains(t, string(responseBody), "new placement is equal to user's current placement")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|