forked from raystack/compass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Muhammad Luthfi Fahlevi
committed
Nov 14, 2024
1 parent
c491f0f
commit 7a2e267
Showing
23 changed files
with
267 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ import ( | |
|
||
func TestContext(t *testing.T) { | ||
t.Run("should return passed user if exist in context", func(t *testing.T) { | ||
passedUser := user.User{UUID: "uuid", Email: "email"} | ||
passedUser := user.User{Email: "[email protected]"} | ||
userCtx := user.NewContext(context.Background(), passedUser) | ||
actual := user.FromContext(userCtx) | ||
if !cmp.Equal(passedUser, actual) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,18 +16,18 @@ func TestErrors(t *testing.T) { | |
testCases := []testCase{ | ||
{ | ||
Description: "not found error return correct error string", | ||
Err: user.NotFoundError{UUID: "uuid", Email: "email"}, | ||
ExpectedString: "could not find user with uuid \"uuid\" with email \"email\"", | ||
Err: user.NotFoundError{Email: "[email protected]"}, | ||
ExpectedString: "could not find user with email \"[email protected]\"", | ||
}, | ||
{ | ||
Description: "duplicate error return correct error string", | ||
Err: user.DuplicateRecordError{UUID: "uuid", Email: "email"}, | ||
ExpectedString: "duplicate user with uuid \"uuid\" with email \"email\"", | ||
Err: user.DuplicateRecordError{Email: "[email protected]"}, | ||
ExpectedString: "duplicate user with email \"[email protected]\"", | ||
}, | ||
{ | ||
Description: "invalid error return correct error string", | ||
Err: user.InvalidError{UUID: "uuid"}, | ||
ExpectedString: "empty field with uuid \"uuid\"", | ||
Err: user.InvalidError{Email: "[email protected]"}, | ||
ExpectedString: "empty field with email \"[email protected]\"", | ||
}, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,65 +12,35 @@ import ( | |
) | ||
|
||
func TestValidateUser(t *testing.T) { | ||
userID := "user-id" | ||
type testCase struct { | ||
Description string | ||
UUID string | ||
Email string | ||
Setup func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) | ||
Setup func(ctx context.Context, inputEmail string, userRepo *mocks.UserRepository) | ||
ExpectErr error | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
Description: "should return no user error when uuid is empty and email is optional", | ||
UUID: "", | ||
Description: "should return no user error when email is empty", | ||
ExpectErr: user.ErrNoUserInformation, | ||
}, | ||
{ | ||
Description: "should return user UUID when successfully found user UUID from DB", | ||
UUID: "a-uuid", | ||
Setup: func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) { | ||
userRepo.EXPECT().GetByUUID(ctx, inputUUID).Return(user.User{ID: "user-id", UUID: inputUUID}, nil) | ||
Description: "should return user ID when successfully found user Email from DB, or not found but can create the new one", | ||
Email: "[email protected]", | ||
Setup: func(ctx context.Context, inputEmail string, userRepo *mocks.UserRepository) { | ||
userRepo.EXPECT().GetOrInsertByEmail(ctx, &user.User{Email: inputEmail}).Return(userID, nil) | ||
}, | ||
ExpectErr: nil, | ||
}, | ||
{ | ||
Description: "should return user error if GetByUUID return nil error and empty ID", | ||
UUID: "a-uuid", | ||
Setup: func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) { | ||
userRepo.EXPECT().GetByUUID(ctx, inputUUID).Return(user.User{}, nil) | ||
Description: "should return user error if InsertByEmail return empty ID and error", | ||
Email: "[email protected]", | ||
Setup: func(ctx context.Context, inputEmail string, userRepo *mocks.UserRepository) { | ||
mockErr := errors.New("error get or insert user") | ||
userRepo.EXPECT().GetOrInsertByEmail(ctx, &user.User{Email: inputEmail}).Return("", mockErr) | ||
}, | ||
ExpectErr: errors.New("fetched user uuid from DB is empty"), | ||
}, | ||
{ | ||
Description: "should return user UUID when not found user UUID from DB but can create the new one without email", | ||
UUID: "an-email-success-create", | ||
Setup: func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) { | ||
userRepo.EXPECT().GetByUUID(ctx, inputUUID).Return(user.User{}, user.NotFoundError{UUID: inputUUID}) | ||
userRepo.EXPECT().UpsertByEmail(ctx, &user.User{UUID: inputUUID}).Return("some-id", nil) | ||
}, | ||
ExpectErr: nil, | ||
}, | ||
{ | ||
Description: "should return user UUID when not found user UUID from DB but can create the new one wit email", | ||
UUID: "an-uuid-error", | ||
Email: "an-email-success-create", | ||
Setup: func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) { | ||
userRepo.EXPECT().GetByUUID(ctx, inputUUID).Return(user.User{}, user.NotFoundError{UUID: inputUUID}) | ||
userRepo.EXPECT().UpsertByEmail(ctx, &user.User{UUID: inputUUID, Email: inputEmail}).Return("some-id", nil) | ||
}, | ||
ExpectErr: nil, | ||
}, | ||
{ | ||
Description: "should return error when not found user ID from DB but can't create the new one", | ||
UUID: "an-uuid-error", | ||
Email: "an-email", | ||
Setup: func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) { | ||
mockErr := errors.New("error upserting user") | ||
userRepo.EXPECT().GetByUUID(ctx, inputUUID).Return(user.User{}, mockErr) | ||
userRepo.EXPECT().UpsertByEmail(ctx, &user.User{UUID: inputUUID, Email: inputEmail}).Return("", mockErr) | ||
}, | ||
ExpectErr: errors.New("error upserting user"), | ||
ExpectErr: errors.New("error get or insert user"), | ||
}, | ||
} | ||
|
||
|
@@ -81,12 +51,12 @@ func TestValidateUser(t *testing.T) { | |
mockUserRepo := new(mocks.UserRepository) | ||
|
||
if tc.Setup != nil { | ||
tc.Setup(ctx, tc.UUID, tc.Email, mockUserRepo) | ||
tc.Setup(ctx, tc.Email, mockUserRepo) | ||
} | ||
|
||
userSvc := user.NewService(logger, mockUserRepo) | ||
|
||
_, err := userSvc.ValidateUser(ctx, tc.UUID, tc.Email) | ||
_, err := userSvc.ValidateUser(ctx, tc.Email) | ||
|
||
assert.Equal(t, tc.ExpectErr, err) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,13 @@ func TestValidate(t *testing.T) { | |
ExpectError: ErrNoUserInformation, | ||
}, | ||
{ | ||
Title: "should return error invalid if uuid is empty", | ||
Title: "should return error invalid if email is empty", | ||
User: &User{Provider: "provider"}, | ||
ExpectError: InvalidError{}, | ||
}, | ||
{ | ||
Title: "should return nil if user is valid", | ||
User: &User{UUID: "some-uuid", Provider: "provider"}, | ||
User: &User{Email: "[email protected]", Provider: "provider"}, | ||
ExpectError: nil, | ||
}, | ||
} | ||
|
Oops, something went wrong.