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 (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-17 14:31:14 +01:00
|
|
|
"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
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2020-07-27 20:43:47 +01:00
|
|
|
"storj.io/common/testrand"
|
2021-11-12 20:47:41 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2021-03-09 17:42:10 +00:00
|
|
|
"storj.io/storj/private/testredis"
|
2019-12-12 12:58:15 +00:00
|
|
|
"storj.io/storj/satellite/accounting"
|
|
|
|
"storj.io/storj/satellite/accounting/live"
|
2021-04-08 18:34:23 +01:00
|
|
|
"storj.io/storj/satellite/analytics"
|
2019-01-15 13:03:24 +00:00
|
|
|
"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"
|
2022-04-12 17:59:07 +01:00
|
|
|
"storj.io/storj/satellite/console/restkeys"
|
2019-03-02 15:22:20 +00:00
|
|
|
"storj.io/storj/satellite/mailservice"
|
2021-10-26 14:30:19 +01:00
|
|
|
"storj.io/storj/satellite/payments"
|
2020-05-18 10:17:05 +01:00
|
|
|
"storj.io/storj/satellite/payments/paymentsconfig"
|
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
2019-11-05 12:58:09 +00:00
|
|
|
"storj.io/storj/satellite/rewards"
|
2019-01-08 14:54:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGraphqlQuery(t *testing.T) {
|
2021-11-12 20:47:41 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{SatelliteCount: 1}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
db := sat.DB
|
2019-01-31 13:01:13 +00:00
|
|
|
log := zaptest.NewLogger(t)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
partnersService := rewards.NewPartnersService(
|
|
|
|
log.Named("partners"),
|
|
|
|
rewards.DefaultPartnersDB,
|
|
|
|
)
|
|
|
|
|
2021-04-08 18:34:23 +01:00
|
|
|
analyticsService := analytics.NewService(log, analytics.Config{}, "test-satellite")
|
|
|
|
|
2021-03-09 17:42:10 +00:00
|
|
|
redis, err := testredis.Mini(ctx)
|
2019-10-31 17:27:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-31 18:28:42 +00:00
|
|
|
defer ctx.Check(redis.Close)
|
2019-10-31 17:27:38 +00:00
|
|
|
|
2021-03-24 19:22:50 +00:00
|
|
|
cache, err := live.OpenCache(ctx, log.Named("cache"), live.Config{StorageBackend: "redis://" + redis.Addr() + "?db=0"})
|
2019-12-12 12:58:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-03 15:06:20 +00:00
|
|
|
projectLimitCache := accounting.NewProjectLimitCache(db.ProjectAccounting(), 0, 0, 0, accounting.ProjectLimitConfig{CacheCapacity: 100})
|
2020-09-09 20:20:44 +01:00
|
|
|
|
2021-12-14 13:49:33 +00:00
|
|
|
projectUsage := accounting.NewService(db.ProjectAccounting(), cache, projectLimitCache, *sat.Metabase.DB, 5*time.Minute, -10*time.Second)
|
2019-10-17 15:42:18 +01:00
|
|
|
|
2020-05-18 10:17:05 +01:00
|
|
|
// TODO maybe switch this test to testplanet to avoid defining config and Stripe service
|
|
|
|
pc := paymentsconfig.Config{
|
|
|
|
StorageTBPrice: "10",
|
|
|
|
EgressTBPrice: "45",
|
2021-10-20 23:54:34 +01:00
|
|
|
SegmentPrice: "0.0000022",
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
paymentsService, err := stripecoinpayments.NewService(
|
|
|
|
log.Named("payments.stripe:service"),
|
2020-10-08 18:14:09 +01:00
|
|
|
stripecoinpayments.NewStripeMock(
|
|
|
|
testrand.NodeID(),
|
|
|
|
db.StripeCoinPayments().Customers(),
|
|
|
|
db.Console().Users(),
|
|
|
|
),
|
2020-05-18 10:17:05 +01:00
|
|
|
pc.StripeCoinPayments,
|
|
|
|
db.StripeCoinPayments(),
|
|
|
|
db.Console().Projects(),
|
|
|
|
db.ProjectAccounting(),
|
|
|
|
pc.StorageTBPrice,
|
|
|
|
pc.EgressTBPrice,
|
2021-10-20 23:54:34 +01:00
|
|
|
pc.SegmentPrice,
|
2021-08-27 01:51:26 +01:00
|
|
|
pc.BonusRate)
|
2020-05-18 10:17:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
service, err := console.NewService(
|
2019-12-12 12:58:15 +00:00
|
|
|
log.Named("console"),
|
2019-01-24 16:26:36 +00:00
|
|
|
db.Console(),
|
2022-04-12 17:59:07 +01:00
|
|
|
restkeys.NewService(db.OIDC().OAuthTokens(), planet.Satellites[0].Config.RESTKeys),
|
2019-11-15 14:27:44 +00:00
|
|
|
db.ProjectAccounting(),
|
2019-12-12 12:58:15 +00:00
|
|
|
projectUsage,
|
2021-11-12 20:47:41 +00:00
|
|
|
sat.API.Buckets.Service,
|
2019-11-05 12:58:09 +00:00
|
|
|
partnersService,
|
2020-05-18 10:17:05 +01:00
|
|
|
paymentsService.Accounts(),
|
2022-04-28 03:54:56 +01:00
|
|
|
// TODO: do we need a payment deposit wallet here?
|
|
|
|
nil,
|
2021-04-08 18:34:23 +01:00
|
|
|
analyticsService,
|
2022-04-19 21:50:15 +01:00
|
|
|
consoleauth.NewService(consoleauth.Config{
|
|
|
|
TokenExpirationTime: 24 * time.Hour,
|
|
|
|
}, &consoleauth.Hmac{Secret: []byte("my-suppa-secret-key")}),
|
2021-11-18 18:55:37 +00:00
|
|
|
console.Config{
|
|
|
|
PasswordCost: console.TestPasswordCost,
|
|
|
|
DefaultProjectLimit: 5,
|
2022-06-05 23:41:38 +01:00
|
|
|
SessionDuration: time.Hour,
|
2021-11-18 18:55:37 +00:00
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
mailService, err := mailservice.New(log, &discardSender{}, "testdata")
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-05-17 13:29:35 +01:00
|
|
|
defer ctx.Check(mailService.Close)
|
2019-03-02 15:22:20 +00:00
|
|
|
|
|
|
|
rootObject := make(map[string]interface{})
|
|
|
|
rootObject["origin"] = "http://doesntmatter.com/"
|
|
|
|
rootObject[consoleql.ActivationPath] = "?activationToken="
|
2019-09-27 17:48:53 +01:00
|
|
|
rootObject[consoleql.LetUsKnowURL] = "letUsKnowURL"
|
|
|
|
rootObject[consoleql.ContactInfoURL] = "contactInfoURL"
|
|
|
|
rootObject[consoleql.TermsAndConditionsURL] = "termsAndConditionsURL"
|
2019-03-02 15:22:20 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
creator := consoleql.TypeCreator{}
|
2019-05-17 14:31:14 +01:00
|
|
|
err = creator.Create(log, service, mailService)
|
|
|
|
require.NoError(t, err)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
schema, err := graphql.NewSchema(graphql.SchemaConfig{
|
|
|
|
Query: creator.RootQuery(),
|
|
|
|
Mutation: creator.RootMutation(),
|
|
|
|
})
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
createUser := console.CreateUser{
|
2021-10-26 14:30:19 +01:00
|
|
|
FullName: "John",
|
|
|
|
ShortName: "",
|
|
|
|
Email: "mtest@mail.test",
|
|
|
|
Password: "123a123",
|
|
|
|
SignupPromoCode: "promo1",
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-03-19 17:55:43 +00:00
|
|
|
regToken, err := service.CreateRegToken(ctx, 2)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-03-19 17:55:43 +00:00
|
|
|
|
2021-02-04 18:16:49 +00:00
|
|
|
rootUser, err := service.CreateUser(ctx, createUser, regToken.Secret)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2021-10-26 14:30:19 +01:00
|
|
|
couponType, err := paymentsService.Accounts().Setup(ctx, rootUser.ID, rootUser.Email, rootUser.SignupPromoCode)
|
|
|
|
|
|
|
|
var signupCouponType payments.CouponType = payments.SignupCoupon
|
|
|
|
|
2020-05-18 10:17:05 +01:00
|
|
|
require.NoError(t, err)
|
2021-10-26 14:30:19 +01:00
|
|
|
assert.Equal(t, signupCouponType, couponType)
|
2020-05-18 10:17:05 +01:00
|
|
|
|
2019-02-05 17:22:17 +00:00
|
|
|
t.Run("Activation", func(t *testing.T) {
|
|
|
|
activationToken, err := service.GenerateActivationToken(
|
|
|
|
ctx,
|
|
|
|
rootUser.ID,
|
2019-06-18 01:28:40 +01:00
|
|
|
"mtest@mail.test",
|
2019-02-05 17:22:17 +00:00
|
|
|
)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2021-10-06 14:33:54 +01:00
|
|
|
_, err = service.ActivateAccount(ctx, activationToken)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-18 01:28:40 +01:00
|
|
|
rootUser.Email = "mtest@mail.test"
|
2019-02-05 17:22:17 +00:00
|
|
|
})
|
2019-01-30 15:04:40 +00:00
|
|
|
|
2021-07-13 18:21:16 +01:00
|
|
|
token, err := service.Token(ctx, console.AuthUser{Email: createUser.Email, Password: createUser.Password})
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
userCtx, err := service.TokenAuth(ctx, token, time.Now())
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
testQuery := func(t *testing.T, query string) interface{} {
|
|
|
|
result := graphql.Do(graphql.Params{
|
|
|
|
Schema: schema,
|
2022-06-05 23:41:38 +01:00
|
|
|
Context: userCtx,
|
2019-01-24 16:26:36 +00:00
|
|
|
RequestString: query,
|
2019-03-02 15:22:20 +00:00
|
|
|
RootObject: rootObject,
|
2019-01-24 16:26:36 +00:00
|
|
|
})
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
for _, err := range result.Errors {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-05-17 14:31:14 +01:00
|
|
|
require.False(t, result.HasErrors())
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
return result.Data
|
|
|
|
}
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
createdProject, err := service.CreateProject(userCtx, console.ProjectInfo{
|
2019-01-28 18:20:33 +00:00
|
|
|
Name: "TestProject",
|
2019-01-24 16:26:36 +00:00
|
|
|
})
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-03-27 12:33:32 +00:00
|
|
|
// "query {project(id:\"%s\"){id,name,members(offset:0, limit:50){user{fullName,shortName,email}},apiKeys{name,id,createdAt,projectID}}}"
|
2019-01-24 16:26:36 +00:00
|
|
|
t.Run("Project query base info", func(t *testing.T) {
|
|
|
|
query := fmt.Sprintf(
|
|
|
|
"query {project(id:\"%s\"){id,name,description,createdAt}}",
|
|
|
|
createdProject.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{})
|
|
|
|
project := data[consoleql.ProjectQuery].(map[string]interface{})
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
assert.Equal(t, createdProject.ID.String(), project[consoleql.FieldID])
|
|
|
|
assert.Equal(t, createdProject.Name, project[consoleql.FieldName])
|
|
|
|
assert.Equal(t, createdProject.Description, project[consoleql.FieldDescription])
|
2019-01-08 14:54:35 +00:00
|
|
|
|
|
|
|
createdAt := time.Time{}
|
2019-01-24 16:26:36 +00:00
|
|
|
err := createdAt.UnmarshalText([]byte(project[consoleql.FieldCreatedAt].(string)))
|
2019-01-08 14:54:35 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2019-02-06 14:48:30 +00:00
|
|
|
assert.True(t, createdProject.CreatedAt.Equal(createdAt))
|
2019-01-24 16:26:36 +00:00
|
|
|
})
|
|
|
|
|
2019-03-19 17:55:43 +00:00
|
|
|
regTokenUser1, err := service.CreateRegToken(ctx, 2)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-03-19 17:55:43 +00:00
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
user1, err := service.CreateUser(userCtx, console.CreateUser{
|
2019-10-29 14:24:16 +00:00
|
|
|
FullName: "Mickey Last",
|
|
|
|
ShortName: "Last",
|
|
|
|
Password: "123a123",
|
|
|
|
Email: "muu1@mail.test",
|
2021-02-04 18:16:49 +00:00
|
|
|
}, regTokenUser1.Secret)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
t.Run("Activation", func(t *testing.T) {
|
|
|
|
activationToken1, err := service.GenerateActivationToken(
|
|
|
|
ctx,
|
|
|
|
user1.ID,
|
2019-06-18 01:28:40 +01:00
|
|
|
"muu1@mail.test",
|
2019-02-05 17:22:17 +00:00
|
|
|
)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2021-10-06 14:33:54 +01:00
|
|
|
_, err = service.ActivateAccount(ctx, activationToken1)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-18 01:28:40 +01:00
|
|
|
user1.Email = "muu1@mail.test"
|
2019-02-05 17:22:17 +00:00
|
|
|
})
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-03-19 17:55:43 +00:00
|
|
|
regTokenUser2, err := service.CreateRegToken(ctx, 2)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-03-19 17:55:43 +00:00
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
user2, err := service.CreateUser(userCtx, console.CreateUser{
|
2019-10-29 14:24:16 +00:00
|
|
|
FullName: "Dubas Name",
|
|
|
|
ShortName: "Name",
|
|
|
|
Email: "muu2@mail.test",
|
|
|
|
Password: "123a123",
|
2021-02-04 18:16:49 +00:00
|
|
|
}, regTokenUser2.Secret)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
t.Run("Activation", func(t *testing.T) {
|
|
|
|
activationToken2, err := service.GenerateActivationToken(
|
|
|
|
ctx,
|
|
|
|
user2.ID,
|
2019-06-18 01:28:40 +01:00
|
|
|
"muu2@mail.test",
|
2019-02-05 17:22:17 +00:00
|
|
|
)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2021-10-06 14:33:54 +01:00
|
|
|
_, err = service.ActivateAccount(ctx, activationToken2)
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-18 01:28:40 +01:00
|
|
|
user2.Email = "muu2@mail.test"
|
2019-02-05 17:22:17 +00:00
|
|
|
})
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
users, err := service.AddProjectMembers(userCtx, createdProject.ID, []string{
|
2019-01-24 16:26:36 +00:00
|
|
|
user1.Email,
|
|
|
|
user2.Email,
|
|
|
|
})
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-03-06 15:42:19 +00:00
|
|
|
assert.Equal(t, 2, len(users))
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
t.Run("Project query team members", func(t *testing.T) {
|
|
|
|
query := fmt.Sprintf(
|
2019-08-12 11:22:32 +01:00
|
|
|
"query {project(id: \"%s\") {members( cursor: { limit: %d, search: \"%s\", page: %d, order: %d, orderDirection: %d } ) { projectMembers{ user { id, fullName, shortName, email, createdAt }, joinedAt }, search, limit, order, offset, pageCount, currentPage, totalCount } } }",
|
2019-01-24 16:26:36 +00:00
|
|
|
createdProject.ID.String(),
|
2019-08-12 11:22:32 +01:00
|
|
|
5,
|
|
|
|
"",
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
2)
|
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.ProjectQuery].(map[string]interface{})
|
2019-08-12 11:22:32 +01:00
|
|
|
members := project[consoleql.FieldMembers].(map[string]interface{})
|
|
|
|
projectMembers := members[consoleql.FieldProjectMembers].([]interface{})
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-08-12 11:22:32 +01:00
|
|
|
assert.Equal(t, 3, len(projectMembers))
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
testUser := func(t *testing.T, actual map[string]interface{}, expected *console.User) {
|
|
|
|
assert.Equal(t, expected.Email, actual[consoleql.FieldEmail])
|
2019-03-27 12:33:32 +00:00
|
|
|
assert.Equal(t, expected.FullName, actual[consoleql.FieldFullName])
|
|
|
|
assert.Equal(t, expected.ShortName, actual[consoleql.FieldShortName])
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
createdAt := time.Time{}
|
|
|
|
err := createdAt.UnmarshalText([]byte(actual[consoleql.FieldCreatedAt].(string)))
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
assert.NoError(t, err)
|
2019-02-06 14:48:30 +00:00
|
|
|
assert.True(t, expected.CreatedAt.Equal(createdAt))
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
var foundRoot, foundU1, foundU2 bool
|
|
|
|
|
2019-08-12 11:22:32 +01:00
|
|
|
for _, entry := range projectMembers {
|
2019-01-24 16:26:36 +00:00
|
|
|
member := entry.(map[string]interface{})
|
|
|
|
user := member[consoleql.UserType].(map[string]interface{})
|
|
|
|
|
|
|
|
id := user[consoleql.FieldID].(string)
|
|
|
|
switch id {
|
|
|
|
case rootUser.ID.String():
|
|
|
|
foundRoot = true
|
|
|
|
testUser(t, user, rootUser)
|
|
|
|
case user1.ID.String():
|
|
|
|
foundU1 = true
|
|
|
|
testUser(t, user, user1)
|
|
|
|
case user2.ID.String():
|
|
|
|
foundU2 = true
|
|
|
|
testUser(t, user, user2)
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
assert.True(t, foundRoot)
|
|
|
|
assert.True(t, foundU1)
|
|
|
|
assert.True(t, foundU2)
|
|
|
|
})
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
keyInfo1, _, err := service.CreateAPIKey(userCtx, createdProject.ID, "key1")
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
keyInfo2, _, err := service.CreateAPIKey(userCtx, createdProject.ID, "key2")
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
t.Run("Project query api keys", func(t *testing.T) {
|
|
|
|
query := fmt.Sprintf(
|
2019-09-12 15:19:30 +01:00
|
|
|
"query {project(id: \"%s\") {apiKeys( cursor: { limit: %d, search: \"%s\", page: %d, order: %d, orderDirection: %d } ) { apiKeys { id, name, createdAt, projectID }, search, limit, order, offset, pageCount, currentPage, totalCount } } }",
|
2019-01-24 16:26:36 +00:00
|
|
|
createdProject.ID.String(),
|
2019-09-12 15:19:30 +01:00
|
|
|
5,
|
|
|
|
"",
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
2)
|
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.ProjectQuery].(map[string]interface{})
|
2019-09-12 15:19:30 +01:00
|
|
|
keys := project[consoleql.FieldAPIKeys].(map[string]interface{})
|
|
|
|
apiKeys := keys[consoleql.FieldAPIKeys].([]interface{})
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-09-12 15:19:30 +01:00
|
|
|
assert.Equal(t, 2, len(apiKeys))
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
testAPIKey := func(t *testing.T, actual map[string]interface{}, expected *console.APIKeyInfo) {
|
|
|
|
assert.Equal(t, expected.Name, actual[consoleql.FieldName])
|
|
|
|
assert.Equal(t, expected.ProjectID.String(), actual[consoleql.FieldProjectID])
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
createdAt := time.Time{}
|
|
|
|
err := createdAt.UnmarshalText([]byte(actual[consoleql.FieldCreatedAt].(string)))
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
assert.NoError(t, err)
|
2019-02-06 14:48:30 +00:00
|
|
|
assert.True(t, expected.CreatedAt.Equal(createdAt))
|
2019-01-24 16:26:36 +00:00
|
|
|
}
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
var foundKey1, foundKey2 bool
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-09-12 15:19:30 +01:00
|
|
|
for _, entry := range apiKeys {
|
2019-01-24 16:26:36 +00:00
|
|
|
key := entry.(map[string]interface{})
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
id := key[consoleql.FieldID].(string)
|
|
|
|
switch id {
|
|
|
|
case keyInfo1.ID.String():
|
|
|
|
foundKey1 = true
|
|
|
|
testAPIKey(t, key, keyInfo1)
|
|
|
|
case keyInfo2.ID.String():
|
|
|
|
foundKey2 = true
|
|
|
|
testAPIKey(t, key, keyInfo2)
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
assert.True(t, foundKey1)
|
|
|
|
assert.True(t, foundKey2)
|
|
|
|
})
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
project2, err := service.CreateProject(userCtx, console.ProjectInfo{
|
2019-01-28 18:20:33 +00:00
|
|
|
Name: "Project2",
|
|
|
|
Description: "Test desc",
|
2019-01-24 16:26:36 +00:00
|
|
|
})
|
2019-05-17 14:31:14 +01:00
|
|
|
require.NoError(t, err)
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
t.Run("MyProjects query", func(t *testing.T) {
|
|
|
|
query := "query {myProjects{id,name,description,createdAt}}"
|
|
|
|
|
|
|
|
result := testQuery(t, query)
|
|
|
|
|
|
|
|
data := result.(map[string]interface{})
|
|
|
|
projectsList := data[consoleql.MyProjectsQuery].([]interface{})
|
|
|
|
|
|
|
|
assert.Equal(t, 2, len(projectsList))
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
testProject := func(t *testing.T, actual map[string]interface{}, expected *console.Project) {
|
|
|
|
assert.Equal(t, expected.Name, actual[consoleql.FieldName])
|
|
|
|
assert.Equal(t, expected.Description, actual[consoleql.FieldDescription])
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
createdAt := time.Time{}
|
|
|
|
err := createdAt.UnmarshalText([]byte(actual[consoleql.FieldCreatedAt].(string)))
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2019-02-06 14:48:30 +00:00
|
|
|
assert.True(t, expected.CreatedAt.Equal(createdAt))
|
2019-01-08 14:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
var foundProj1, foundProj2 bool
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
for _, entry := range projectsList {
|
|
|
|
project := entry.(map[string]interface{})
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
id := project[consoleql.FieldID].(string)
|
|
|
|
switch id {
|
|
|
|
case createdProject.ID.String():
|
|
|
|
foundProj1 = true
|
|
|
|
testProject(t, project, createdProject)
|
|
|
|
case project2.ID.String():
|
|
|
|
foundProj2 = true
|
|
|
|
testProject(t, project, project2)
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 14:54:35 +00:00
|
|
|
|
2021-01-21 18:19:37 +00:00
|
|
|
assert.True(t, foundProj1)
|
|
|
|
assert.True(t, foundProj2)
|
|
|
|
})
|
|
|
|
t.Run("OwnedProjects query", func(t *testing.T) {
|
|
|
|
query := fmt.Sprintf(
|
|
|
|
"query {ownedProjects( cursor: { limit: %d, page: %d } ) {projects{id, name, ownerId, description, createdAt, memberCount}, limit, offset, pageCount, currentPage, totalCount } }",
|
|
|
|
5,
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
|
|
|
|
result := testQuery(t, query)
|
|
|
|
|
|
|
|
data := result.(map[string]interface{})
|
|
|
|
projectsPage := data[consoleql.OwnedProjectsQuery].(map[string]interface{})
|
|
|
|
|
|
|
|
projectsList := projectsPage[consoleql.FieldProjects].([]interface{})
|
|
|
|
assert.Len(t, projectsList, 2)
|
|
|
|
|
|
|
|
assert.EqualValues(t, 1, projectsPage[consoleql.FieldCurrentPage])
|
|
|
|
assert.EqualValues(t, 0, projectsPage[consoleql.OffsetArg])
|
|
|
|
assert.EqualValues(t, 5, projectsPage[consoleql.LimitArg])
|
|
|
|
assert.EqualValues(t, 1, projectsPage[consoleql.FieldPageCount])
|
|
|
|
assert.EqualValues(t, 2, projectsPage[consoleql.FieldTotalCount])
|
|
|
|
|
|
|
|
testProject := func(t *testing.T, actual map[string]interface{}, expected *console.Project, expectedNumMembers int) {
|
|
|
|
assert.Equal(t, expected.Name, actual[consoleql.FieldName])
|
|
|
|
assert.Equal(t, expected.Description, actual[consoleql.FieldDescription])
|
|
|
|
|
|
|
|
createdAt := time.Time{}
|
|
|
|
err := createdAt.UnmarshalText([]byte(actual[consoleql.FieldCreatedAt].(string)))
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, expected.CreatedAt.Equal(createdAt))
|
|
|
|
|
|
|
|
assert.EqualValues(t, expectedNumMembers, actual[consoleql.FieldMemberCount])
|
|
|
|
}
|
|
|
|
|
|
|
|
var foundProj1, foundProj2 bool
|
|
|
|
|
|
|
|
for _, entry := range projectsList {
|
|
|
|
project := entry.(map[string]interface{})
|
|
|
|
|
|
|
|
id := project[consoleql.FieldID].(string)
|
|
|
|
switch id {
|
|
|
|
case createdProject.ID.String():
|
|
|
|
foundProj1 = true
|
|
|
|
testProject(t, project, createdProject, 3)
|
|
|
|
case project2.ID.String():
|
|
|
|
foundProj2 = true
|
|
|
|
testProject(t, project, project2, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:26:36 +00:00
|
|
|
assert.True(t, foundProj1)
|
|
|
|
assert.True(t, foundProj2)
|
|
|
|
})
|
2019-01-08 14:54:35 +00:00
|
|
|
})
|
|
|
|
}
|