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

370 lines
11 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"
"github.com/graphql-go/graphql"
"github.com/skyrings/skyring-common/tools/uuid"
"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/storj/pkg/auth"
"storj.io/storj/private/currency"
"storj.io/storj/private/post"
"storj.io/storj/private/testcontext"
2019-01-24 16:26:36 +00:00
"storj.io/storj/satellite"
"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/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{}
}
2019-01-08 14:54:35 +00:00
func TestGrapqhlMutation(t *testing.T) {
2019-01-24 16:26:36 +00:00
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
2019-01-08 14:54:35 +00:00
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,
[]string{
"https://us-central-1.tardigrade.io/",
"https://asia-east-1.tardigrade.io/",
"https://europe-west-1.tardigrade.io/",
},
)
paymentsConfig := stripecoinpayments.Config{}
payments := stripecoinpayments.NewService(log, paymentsConfig, db.StripeCoinPayments(), db.Console().Projects(), db.ProjectAccounting(), 0, 0, 0)
2019-01-24 16:26:36 +00:00
service, err := console.NewService(
log,
&consoleauth.Hmac{Secret: []byte("my-suppa-secret-key")},
db.Console(),
db.ProjectAccounting(),
db.Rewards(),
partnersService,
payments.Accounts(),
console.TestPasswordCost,
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
}
refUserID := ""
2019-01-08 14:54:35 +00:00
_, err = db.Rewards().Create(ctx, &rewards.NewOffer{
Name: "Couchbase",
Description: "",
AwardCredit: currency.Cents(0),
InviteeCredit: currency.Cents(20),
RedeemableCap: 10,
AwardCreditDurationDays: 0,
InviteeCreditDurationDays: 14,
ExpiresAt: time.Now().UTC().Add(time.Hour * 1),
Status: rewards.Active,
Type: rewards.Partner,
})
require.NoError(t, err)
regToken, err := service.CreateRegToken(ctx, 1)
require.NoError(t, err)
rootUser, err := service.CreateUser(ctx, createUser, regToken.Secret, refUserID)
require.NoError(t, err)
require.Equal(t, createUser.PartnerID, rootUser.PartnerID.String())
2019-01-08 14:54:35 +00:00
activationToken, err := service.GenerateActivationToken(ctx, rootUser.ID, rootUser.Email)
require.NoError(t, err)
err = service.ActivateAccount(ctx, activationToken)
require.NoError(t, err)
2019-01-24 16:26:36 +00:00
token, err := service.Token(ctx, createUser.Email, createUser.Password)
require.NoError(t, err)
2019-01-24 16:26:36 +00:00
sauth, err := service.Authorize(auth.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{} {
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 {
assert.NoError(t, err)
}
require.False(t, result.HasErrors())
2019-01-24 16:26:36 +00:00
return result.Data
2019-01-08 14:54:35 +00:00
}
2019-01-24 16:26:36 +00:00
token, err = service.Token(ctx, rootUser.Email, createUser.Password)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
sauth, err = service.Authorize(auth.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
2019-01-24 16:26:36 +00:00
var projectID string
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
2019-01-24 16:26:36 +00:00
result := testQuery(t, query)
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
2019-01-24 16:26:36 +00:00
projectID = project[consoleql.FieldID].(string)
})
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
pID, err := uuid.Parse(projectID)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
project, err := service.GetProject(authCtx, *pID)
require.NoError(t, err)
require.Equal(t, rootUser.PartnerID, project.PartnerID)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
t.Run("Update project description mutation", func(t *testing.T) {
query := fmt.Sprintf(
"mutation {updateProjectDescription(id:\"%s\",description:\"%s\"){id,name,description}}",
project.ID.String(),
"",
)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
result := testQuery(t, query)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
data := result.(map[string]interface{})
proj := data[consoleql.UpdateProjectDescriptionMutation].(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, "", proj[consoleql.FieldDescription])
})
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, refUserID)
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, refUserID)
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
2019-01-24 16:26:36 +00:00
result := testQuery(t, query)
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
2019-01-24 16:26:36 +00:00
result := testQuery(t, query)
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
2019-01-24 16:26:36 +00:00
result := testQuery(t, query)
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.Parse(keyID)
require.NoError(t, err)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +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
2019-01-24 16:26:36 +00:00
result := testQuery(t, query)
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
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
2019-01-24 16:26:36 +00:00
result := testQuery(t, query)
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
data := result.(map[string]interface{})
proj := data[consoleql.DeleteProjectMutation].(map[string]interface{})
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
assert.Equal(t, project.Name, proj[consoleql.FieldName])
assert.Equal(t, project.ID.String(), proj[consoleql.FieldID])
2019-01-08 14:54:35 +00:00
2019-01-24 16:26:36 +00:00
_, err := service.GetProject(authCtx, project.ID)
assert.Error(t, err)
})
2019-01-08 14:54:35 +00:00
})
}