Skip to content

Commit

Permalink
Fix isUserInAccount (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
alishakawaguchi authored Dec 6, 2023
1 parent a5b6844 commit 51dd7e4
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 1 deletion.
19 changes: 19 additions & 0 deletions backend/gen/go/db/api-keys.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions backend/gen/go/db/mock_Querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/gen/go/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions backend/services/mgmt/v1alpha1/user-account-service/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func (s *Service) IsUserInAccount(
if err != nil {
return nil, err
}

userId, err := nucleusdb.ToUuid(user.Msg.UserId)
if err != nil {
return nil, err
Expand All @@ -193,6 +194,18 @@ func (s *Service) IsUserInAccount(
if err != nil {
return nil, err
}
apiKeyCount, err := s.db.Q.IsUserInAccountApiKey(ctx, s.db.Db, db_queries.IsUserInAccountApiKeyParams{
AccountId: accountId,
UserId: userId,
})
if err != nil {
return nil, err
}
if apiKeyCount > 0 {
return connect.NewResponse(&mgmtv1alpha1.IsUserInAccountResponse{
Ok: apiKeyCount > 0,
}), nil
}
count, err := s.db.Q.IsUserInAccount(ctx, s.db.Db, db_queries.IsUserInAccountParams{
AccountId: accountId,
UserId: userId,
Expand Down
32 changes: 32 additions & 0 deletions backend/services/mgmt/v1alpha1/user-account-service/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ func Test_IsUserInAccount_True(t *testing.T) {
AccountId: accountUuid,
UserId: userUuid,
}).Return(int64(1), nil)
m.QuerierMock.On("IsUserInAccountApiKey", ctx, mock.Anything, db_queries.IsUserInAccountApiKeyParams{
AccountId: accountUuid,
UserId: userUuid,
}).Return(int64(0), nil)

resp, err := m.Service.IsUserInAccount(ctx, &connect.Request[mgmtv1alpha1.IsUserInAccountRequest]{Msg: &mgmtv1alpha1.IsUserInAccountRequest{AccountId: mockAccountId}})

Expand All @@ -276,6 +280,10 @@ func Test_IsUserInAccount_False(t *testing.T) {
AccountId: accountUuid,
UserId: userUuid,
}).Return(int64(0), nil)
m.QuerierMock.On("IsUserInAccountApiKey", ctx, mock.Anything, db_queries.IsUserInAccountApiKeyParams{
AccountId: accountUuid,
UserId: userUuid,
}).Return(int64(0), nil)

resp, err := m.Service.IsUserInAccount(ctx, &connect.Request[mgmtv1alpha1.IsUserInAccountRequest]{Msg: &mgmtv1alpha1.IsUserInAccountRequest{AccountId: mockAccountId}})

Expand All @@ -284,6 +292,26 @@ func Test_IsUserInAccount_False(t *testing.T) {
assert.Equal(t, false, resp.Msg.Ok)
}

func Test_IsUserInAccount_ApiKey(t *testing.T) {
m := createServiceMock(t, &Config{IsAuthEnabled: true})

ctx := getAuthenticatedCtxMock(mockAuthProvider)
userAssociation := getUserIdentityProviderAssociationMock(mockUserId, mockAuthProvider)
accountUuid, _ := nucleusdb.ToUuid(mockAccountId)
userUuid, _ := nucleusdb.ToUuid(mockUserId)
m.QuerierMock.On("GetUserAssociationByAuth0Id", ctx, mock.Anything, mockAuthProvider).Return(userAssociation, nil)
m.QuerierMock.On("IsUserInAccountApiKey", ctx, mock.Anything, db_queries.IsUserInAccountApiKeyParams{
AccountId: accountUuid,
UserId: userUuid,
}).Return(int64(1), nil)

resp, err := m.Service.IsUserInAccount(ctx, &connect.Request[mgmtv1alpha1.IsUserInAccountRequest]{Msg: &mgmtv1alpha1.IsUserInAccountRequest{AccountId: mockAccountId}})

assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, true, resp.Msg.Ok)
}

func Test_CreateTeamAccount(t *testing.T) {
m := createServiceMock(t, &Config{IsAuthEnabled: true})
mockTx := new(nucleusdb.MockTx)
Expand Down Expand Up @@ -649,6 +677,10 @@ func mockVerifyUserInAccount(ctx context.Context, querierMock *db_queries.MockQu
AccountId: accountUuid,
UserId: userUuid,
}).Return(int64(inAccount), nil)
querierMock.On("IsUserInAccountApiKey", ctx, mock.Anything, db_queries.IsUserInAccountApiKeyParams{
AccountId: accountUuid,
UserId: userUuid,
}).Return(int64(0), nil)
}

func mockVerifyTeamAccount(ctx context.Context, querierMock *db_queries.MockQuerier, accountUuid pgtype.UUID, isTeamAccount bool) {
Expand Down
7 changes: 7 additions & 0 deletions backend/sql/postgresql/queries/api-keys.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ SET key_value = $1,
updated_by_id = $3
WHERE id = $4
RETURNING *;


-- name: IsUserInAccountApiKey :one
SELECT count(apk.id) from neosync_api.account_api_keys apk
INNER JOIN neosync_api.accounts a ON a.id = apk.account_id
INNER JOIN neosync_api.users u ON u.id = apk.user_id
WHERE a.id = sqlc.arg('accountId') AND u.id = sqlc.arg('userId');
2 changes: 1 addition & 1 deletion cli/internal/cmds/neosync/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func NewCmd() *cobra.Command {
cmd.Flags().String("connection-id", "", "Connection id for sync source")
cmd.Flags().String("destination-connection-url", "", "Connection url for sync output")
cmd.Flags().String("destination-driver", "", "Connection driver for sync output")
cmd.Flags().String("config", "neosync.yaml", `Location of config file (default "neosync.yaml")`)
cmd.Flags().String("config", "", `Location of config file`)
return cmd
}

Expand Down

0 comments on commit 51dd7e4

Please sign in to comment.