storj/satellite/console/consoleweb/consoleql/mutation_test.go

401 lines
12 KiB
Go
Raw Normal View History

2019-01-24 16:26:36 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2019-01-08 14:54:35 +00:00
// See LICENSE for copying information.
2019-01-24 16:26:36 +00:00
package consoleql_test
2019-01-08 14:54:35 +00:00
import (
"context"
2019-01-08 14:54:35 +00:00
"fmt"
"testing"
"time"
2019-01-08 14:54:35 +00:00
"github.com/graphql-go/graphql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-01-31 13:01:13 +00:00
"go.uber.org/zap/zaptest"
2019-01-08 14:54:35 +00:00
"storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/common/uuid"
"storj.io/storj/private/post"
"storj.io/storj/private/testredis"
2019-01-24 16:26:36 +00:00
"storj.io/storj/satellite"
"storj.io/storj/satellite/accounting"
"storj.io/storj/satellite/accounting/live"
"storj.io/storj/satellite/analytics"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/console/consoleauth"
2019-01-24 16:26:36 +00:00
"storj.io/storj/satellite/console/consoleweb/consoleql"
"storj.io/storj/satellite/mailservice"
"storj.io/storj/satellite/payments/paymentsconfig"
"storj.io/storj/satellite/payments/stripecoinpayments"
"storj.io/storj/satellite/rewards"
2019-01-24 16:26:36 +00:00
"storj.io/storj/satellite/satellitedb/satellitedbtest"
2019-01-08 14:54:35 +00:00
)
// discardSender discard sending of an actual email.
type discardSender struct{}
// SendEmail immediately returns with nil error.
func (*discardSender) SendEmail(ctx context.Context, msg *post.Message) error {
return nil
}
// FromAddress returns empty post.Address.
func (*discardSender) FromAddress() post.Address {
return post.Address{}
}
func TestGraphqlMutation(t *testing.T) {
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
2019-01-31 13:01:13 +00:00
log := zaptest.NewLogger(t)
2019-01-08 14:54:35 +00:00
partnersService := rewards.NewPartnersService(
log.Named("partners"),
rewards.DefaultPartnersDB,
)
analyticsService := analytics.NewService(log, analytics.Config{}, "test-satellite")
redis, err := testredis.Mini(ctx)
satellite/accounting: refactor live accounting to hold current estimated totals live accounting used to be a cache to store writes before they are picked up during the tally iteration, after which the cache is cleared. This created a window in which users could potentially exceed the storage limit. This PR refactors live accounting to hold current estimations of space used per project. This should also reduce DB load since we no longer need to query the satellite DB when checking space used for limiting. The mechanism by which the new live accounting system works is as follows: During the upload of any segment, the size of that segment is added to its respective project total in live accounting. At the beginning of the tally iteration we record the current values in live accounting as `initialLiveTotals`. At the end of the tally iteration we again record the current totals in live accounting as `latestLiveTotals`. The metainfo loop observer in tally allows us to get the project totals from what it observed in metainfo DB which are stored in `tallyProjectTotals`. However, for any particular segment uploaded during the metainfo loop, the observer may or may not have seen it. Thus, we take half of the difference between `latestLiveTotals` and `initialLiveTotals`, and add that to the total that was found during tally and set that as the new live accounting total. Initially, live accounting was storing the total stored amount across all nodes rather than the segment size, which is inconsistent with how we record amounts stored in the project accounting DB, so we have refactored live accounting to record segment size Change-Id: Ie48bfdef453428fcdc180b2d781a69d58fd927fb
2019-10-31 17:27:38 +00:00
require.NoError(t, err)
defer ctx.Check(redis.Close)
satellite/accounting: refactor live accounting to hold current estimated totals live accounting used to be a cache to store writes before they are picked up during the tally iteration, after which the cache is cleared. This created a window in which users could potentially exceed the storage limit. This PR refactors live accounting to hold current estimations of space used per project. This should also reduce DB load since we no longer need to query the satellite DB when checking space used for limiting. The mechanism by which the new live accounting system works is as follows: During the upload of any segment, the size of that segment is added to its respective project total in live accounting. At the beginning of the tally iteration we record the current values in live accounting as `initialLiveTotals`. At the end of the tally iteration we again record the current totals in live accounting as `latestLiveTotals`. The metainfo loop observer in tally allows us to get the project totals from what it observed in metainfo DB which are stored in `tallyProjectTotals`. However, for any particular segment uploaded during the metainfo loop, the observer may or may not have seen it. Thus, we take half of the difference between `latestLiveTotals` and `initialLiveTotals`, and add that to the total that was found during tally and set that as the new live accounting total. Initially, live accounting was storing the total stored amount across all nodes rather than the segment size, which is inconsistent with how we record amounts stored in the project accounting DB, so we have refactored live accounting to record segment size Change-Id: Ie48bfdef453428fcdc180b2d781a69d58fd927fb
2019-10-31 17:27:38 +00:00
cache, err := live.OpenCache(ctx, log.Named("cache"), live.Config{StorageBackend: "redis://" + redis.Addr() + "?db=0"})
require.NoError(t, err)
satellite/accounting: add cache for getting project storage and bw limits This PR adds the following items: 1) an in-memory read-only cache thats stores project limit info for projectIDs This cache is stored in-memory since this is expected to be a small amount of data. In this implementation we are only storing in the cache projects that have been accessed. Currently for the largest Satellite (eu-west) there is about 4500 total projects. So storing the storage limit (int64) and the bandwidth limit (int64), this would end up being about 200kb (including the 32 byte project ID) if all 4500 projectIDs were in the cache. So this all fits in memory for the time being. At some point it may not as usage grows, but that seems years out. The cache is a read only cache. When requests come in to upload/download a file, we will read from the cache what the current limits are for that project. If the cache does not contain the projectID, it will get the info from the database (satellitedb project table), then add it to the cache. The only time the values in the cache are modified is when either a) the project ID is not in the cache, or b) the item in the cache has expired (default 10mins), then the data gets refreshed out of the database. This occurs by default every 10 mins. This means that if we update the usage limits in the database, that change might not show up in the cache for 10 mins which mean it will not be reflected to limit end users uploading/downloading files for that time period.. Change-Id: I3fd7056cf963676009834fcbcf9c4a0922ca4a8f
2020-09-09 20:20:44 +01:00
projectLimitCache := accounting.NewProjectLimitCache(db.ProjectAccounting(), 0, 0, accounting.ProjectLimitConfig{CacheCapacity: 100})
projectUsage := accounting.NewService(db.ProjectAccounting(), cache, projectLimitCache, 5*time.Minute, -10*time.Second)
// TODO maybe switch this test to testplanet to avoid defining config and Stripe service
pc := paymentsconfig.Config{
StorageTBPrice: "10",
EgressTBPrice: "45",
ObjectPrice: "0.0000022",
}
paymentsService, err := stripecoinpayments.NewService(
log.Named("payments.stripe:service"),
stripecoinpayments.NewStripeMock(
testrand.NodeID(),
db.StripeCoinPayments().Customers(),
db.Console().Users(),
),
pc.StripeCoinPayments,
db.StripeCoinPayments(),
db.Console().Projects(),
db.ProjectAccounting(),
pc.StorageTBPrice,
pc.EgressTBPrice,
pc.ObjectPrice,
pc.BonusRate)
require.NoError(t, err)
2019-01-24 16:26:36 +00:00
service, err := console.NewService(
log.Named("console"),
2019-01-24 16:26:36 +00:00
&consoleauth.Hmac{Secret: []byte("my-suppa-secret-key")},
db.Console(),
db.ProjectAccounting(),
projectUsage,
db.Buckets(),
partnersService,
paymentsService.Accounts(),
analyticsService,
console.Config{PasswordCost: console.TestPasswordCost, DefaultProjectLimit: 5},
2019-01-08 14:54:35 +00:00
)
require.NoError(t, err)
2019-01-24 16:26:36 +00:00
mailService, err := mailservice.New(log, &discardSender{}, "testdata")
require.NoError(t, err)
defer ctx.Check(mailService.Close)
rootObject := make(map[string]interface{})
rootObject["origin"] = "http://doesntmatter.com/"
rootObject[consoleql.ActivationPath] = "?activationToken="
rootObject[consoleql.SignInPath] = "login"
rootObject[consoleql.LetUsKnowURL] = "letUsKnowURL"
rootObject[consoleql.ContactInfoURL] = "contactInfoURL"
rootObject[consoleql.TermsAndConditionsURL] = "termsAndConditionsURL"
schema, err := consoleql.CreateSchema(log, service, mailService)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
createUser := console.CreateUser{
FullName: "John Roll",
ShortName: "Roll",
Email: "test@mail.test",
PartnerID: "120bf202-8252-437e-ac12-0e364bee852e",
Password: "123a123",
2019-01-08 14:54:35 +00:00
}
regToken, err := service.CreateRegToken(ctx, 1)
require.NoError(t, err)
rootUser, err := service.CreateUser(ctx, createUser, regToken.Secret)
require.NoError(t, err)
require.Equal(t, createUser.PartnerID, rootUser.PartnerID.String())
2019-01-08 14:54:35 +00:00
err = paymentsService.Accounts().Setup(ctx, rootUser.ID, rootUser.Email)
require.NoError(t, err)
activationToken, err := service.GenerateActivationToken(ctx, rootUser.ID, rootUser.Email)
require.NoError(t, err)
err = service.ActivateAccount(ctx, activationToken)
require.NoError(t, err)
token, err := service.Token(ctx, console.AuthUser{Email: createUser.Email, Password: createUser.Password})
require.NoError(t, err)
2019-01-24 16:26:36 +00:00
sauth, err := service.Authorize(consoleauth.WithAPIKey(ctx, []byte(token)))
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
authCtx := console.WithAuth(ctx, sauth)
testQuery := func(t *testing.T, query string) (interface{}, error) {
2019-01-24 16:26:36 +00:00
result := graphql.Do(graphql.Params{
Schema: schema,
Context: authCtx,
RequestString: query,
RootObject: rootObject,
2019-01-24 16:26:36 +00:00
})
for _, err := range result.Errors {
if err.OriginalError() != nil {
return nil, err
}
2019-01-24 16:26:36 +00:00
}
require.False(t, result.HasErrors())
2019-01-24 16:26:36 +00:00
return result.Data, nil
2019-01-08 14:54:35 +00:00
}
token, err = service.Token(ctx, console.AuthUser{Email: rootUser.Email, Password: createUser.Password})
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
sauth, err = service.Authorize(consoleauth.WithAPIKey(ctx, []byte(token)))
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
authCtx = console.WithAuth(ctx, sauth)
2019-01-08 14:54:35 +00:00
var projectIDField string
2019-01-24 16:26:36 +00:00
t.Run("Create project mutation", func(t *testing.T) {
projectInfo := console.ProjectInfo{
Name: "Project name",
Description: "desc",
}
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
query := fmt.Sprintf(
"mutation {createProject(input:{name:\"%s\",description:\"%s\"}){name,description,id,createdAt}}",
2019-01-24 16:26:36 +00:00
projectInfo.Name,
projectInfo.Description,
)
2019-01-08 14:54:35 +00:00
result, err := testQuery(t, query)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
data := result.(map[string]interface{})
project := data[consoleql.CreateProjectMutation].(map[string]interface{})
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
assert.Equal(t, projectInfo.Name, project[consoleql.FieldName])
assert.Equal(t, projectInfo.Description, project[consoleql.FieldDescription])
2019-01-08 14:54:35 +00:00
projectIDField = project[consoleql.FieldID].(string)
2019-01-24 16:26:36 +00:00
})
2019-01-08 14:54:35 +00:00
projectID, err := uuid.FromString(projectIDField)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
project, err := service.GetProject(authCtx, projectID)
require.NoError(t, err)
require.Equal(t, rootUser.PartnerID, project.PartnerID)
2019-01-08 14:54:35 +00:00
regTokenUser1, err := service.CreateRegToken(ctx, 1)
require.NoError(t, err)
2019-01-24 16:26:36 +00:00
user1, err := service.CreateUser(authCtx, console.CreateUser{
FullName: "User1",
Email: "u1@mail.test",
2019-01-24 16:26:36 +00:00
Password: "123a123",
}, regTokenUser1.Secret)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
t.Run("Activation", func(t *testing.T) {
activationToken1, err := service.GenerateActivationToken(
ctx,
user1.ID,
"u1@mail.test",
)
require.NoError(t, err)
err = service.ActivateAccount(ctx, activationToken1)
require.NoError(t, err)
user1.Email = "u1@mail.test"
})
2019-01-08 14:54:35 +00:00
regTokenUser2, err := service.CreateRegToken(ctx, 1)
require.NoError(t, err)
2019-01-24 16:26:36 +00:00
user2, err := service.CreateUser(authCtx, console.CreateUser{
FullName: "User1",
Email: "u2@mail.test",
2019-01-24 16:26:36 +00:00
Password: "123a123",
}, regTokenUser2.Secret)
require.NoError(t, err)
t.Run("Activation", func(t *testing.T) {
activationToken2, err := service.GenerateActivationToken(
ctx,
user2.ID,
"u2@mail.test",
)
require.NoError(t, err)
err = service.ActivateAccount(ctx, activationToken2)
require.NoError(t, err)
user2.Email = "u2@mail.test"
})
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
t.Run("Add project members mutation", func(t *testing.T) {
query := fmt.Sprintf(
"mutation {addProjectMembers(projectID:\"%s\",email:[\"%s\",\"%s\"]){id,name,members(cursor: { limit: 50, search: \"\", page: 1, order: 1, orderDirection: 2 }){projectMembers{joinedAt}}}}",
2019-01-24 16:26:36 +00:00
project.ID.String(),
user1.Email,
user2.Email,
)
2019-01-08 14:54:35 +00:00
result, err := testQuery(t, query)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
data := result.(map[string]interface{})
proj := data[consoleql.AddProjectMembersMutation].(map[string]interface{})
2019-01-08 14:54:35 +00:00
members := proj[consoleql.FieldMembers].(map[string]interface{})
projectMembers := members[consoleql.FieldProjectMembers].([]interface{})
2019-01-24 16:26:36 +00:00
assert.Equal(t, project.ID.String(), proj[consoleql.FieldID])
assert.Equal(t, project.Name, proj[consoleql.FieldName])
assert.Equal(t, 3, len(projectMembers))
2019-01-24 16:26:36 +00:00
})
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
t.Run("Delete project members mutation", func(t *testing.T) {
query := fmt.Sprintf(
"mutation {deleteProjectMembers(projectID:\"%s\",email:[\"%s\",\"%s\"]){id,name,members(cursor: { limit: 50, search: \"\", page: 1, order: 1, orderDirection: 2 }){projectMembers{user{id}}}}}",
2019-01-24 16:26:36 +00:00
project.ID.String(),
user1.Email,
user2.Email,
)
2019-01-08 14:54:35 +00:00
result, err := testQuery(t, query)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
data := result.(map[string]interface{})
proj := data[consoleql.DeleteProjectMembersMutation].(map[string]interface{})
2019-01-08 14:54:35 +00:00
members := proj[consoleql.FieldMembers].(map[string]interface{})
projectMembers := members[consoleql.FieldProjectMembers].([]interface{})
rootMember := projectMembers[0].(map[string]interface{})[consoleql.UserType].(map[string]interface{})
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
assert.Equal(t, project.ID.String(), proj[consoleql.FieldID])
assert.Equal(t, project.Name, proj[consoleql.FieldName])
assert.Equal(t, 1, len(members))
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
assert.Equal(t, rootUser.ID.String(), rootMember[consoleql.FieldID])
})
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
var keyID string
t.Run("Create api key mutation", func(t *testing.T) {
keyName := "key1"
query := fmt.Sprintf(
"mutation {createAPIKey(projectID:\"%s\",name:\"%s\"){key,keyInfo{id,name,projectID,partnerId}}}",
2019-01-24 16:26:36 +00:00
project.ID.String(),
keyName,
)
2019-01-08 14:54:35 +00:00
result, err := testQuery(t, query)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
data := result.(map[string]interface{})
createAPIKey := data[consoleql.CreateAPIKeyMutation].(map[string]interface{})
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
key := createAPIKey[consoleql.FieldKey].(string)
keyInfo := createAPIKey[consoleql.APIKeyInfoType].(map[string]interface{})
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
assert.NotEqual(t, "", key)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
assert.Equal(t, keyName, keyInfo[consoleql.FieldName])
assert.Equal(t, project.ID.String(), keyInfo[consoleql.FieldProjectID])
assert.Equal(t, rootUser.PartnerID.String(), keyInfo[consoleql.FieldPartnerID])
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
keyID = keyInfo[consoleql.FieldID].(string)
})
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
t.Run("Delete api key mutation", func(t *testing.T) {
id, err := uuid.FromString(keyID)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
info, err := service.GetAPIKeyInfo(authCtx, id)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
query := fmt.Sprintf(
"mutation {deleteAPIKeys(id:[\"%s\"]){name,projectID}}",
keyID,
2019-01-24 16:26:36 +00:00
)
2019-01-08 14:54:35 +00:00
result, err := testQuery(t, query)
require.NoError(t, err)
2019-01-24 16:26:36 +00:00
data := result.(map[string]interface{})
keyInfoList := data[consoleql.DeleteAPIKeysMutation].([]interface{})
2019-01-08 14:54:35 +00:00
for _, k := range keyInfoList {
keyInfo := k.(map[string]interface{})
assert.Equal(t, info.Name, keyInfo[consoleql.FieldName])
assert.Equal(t, project.ID.String(), keyInfo[consoleql.FieldProjectID])
}
2019-01-24 16:26:36 +00:00
})
2019-01-08 14:54:35 +00:00
const testName = "testName"
const testDescription = "test description"
const StorageLimit = "100"
const BandwidthLimit = "100"
t.Run("Update project mutation", func(t *testing.T) {
query := fmt.Sprintf(
"mutation {updateProject(id:\"%s\",projectFields:{name:\"%s\",description:\"%s\"},projectLimits:{storageLimit:\"%s\",bandwidthLimit:\"%s\"}){id,name,description}}",
project.ID.String(),
testName,
testDescription,
StorageLimit,
BandwidthLimit,
)
result, err := testQuery(t, query)
require.NoError(t, err)
data := result.(map[string]interface{})
proj := data[consoleql.UpdateProjectMutation].(map[string]interface{})
assert.Equal(t, project.ID.String(), proj[consoleql.FieldID])
assert.Equal(t, testName, proj[consoleql.FieldName])
assert.Equal(t, testDescription, proj[consoleql.FieldDescription])
})
2019-01-24 16:26:36 +00:00
t.Run("Delete project mutation", func(t *testing.T) {
query := fmt.Sprintf(
"mutation {deleteProject(id:\"%s\"){id,name}}",
projectID,
)
2019-01-08 14:54:35 +00:00
result, err := testQuery(t, query)
require.Error(t, err)
require.Nil(t, result)
require.Equal(t, console.ErrUnauthorized.New("not implemented").Error(), err.Error())
2019-01-24 16:26:36 +00:00
})
2019-01-08 14:54:35 +00:00
})
}