-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: memory stores used by test are now initialized with pointer va…
…lues It makes it easier to assert modification of an existing record without relying on `GetByID`
- Loading branch information
Showing
18 changed files
with
110 additions
and
136 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,14 @@ func Test_CreateFirstAccount(t *testing.T) { | |
hasher := infra.NewBCryptHasher() | ||
keygen := infra.NewKeyGenerator() | ||
|
||
createFirstAccount := func(existingUsers ...domain.User) (func(context.Context, command.CreateFirstAccountCommand) error, memory.UsersStore) { | ||
createFirstAccount := func(existingUsers ...*domain.User) (func(context.Context, command.CreateFirstAccountCommand) error, memory.UsersStore) { | ||
store := memory.NewUsersStore(existingUsers...) | ||
return command.CreateFirstAccount(store, store, hasher, keygen), store | ||
} | ||
|
||
t.Run("should do nothing if a user already exists", func(t *testing.T) { | ||
uc, store := createFirstAccount(domain.NewUser("[email protected]", "password", "apikey")) | ||
usr := domain.NewUser("[email protected]", "password", "apikey") | ||
uc, store := createFirstAccount(&usr) | ||
|
||
err := uc(ctx, command.CreateFirstAccountCommand{}) | ||
|
||
|
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,7 +16,7 @@ import ( | |
func Test_Login(t *testing.T) { | ||
hasher := infra.NewBCryptHasher() | ||
password, _ := hasher.Hash("password") // Sample password hash for the string "password" for tests | ||
login := func(existingUsers ...domain.User) func(context.Context, command.LoginCommand) (string, error) { | ||
login := func(existingUsers ...*domain.User) func(context.Context, command.LoginCommand) (string, error) { | ||
store := memory.NewUsersStore(existingUsers...) | ||
return command.Login(store, hasher) | ||
} | ||
|
@@ -42,7 +42,8 @@ func Test_Login(t *testing.T) { | |
}) | ||
|
||
t.Run("should complains if password does not match", func(t *testing.T) { | ||
uc := login(domain.NewUser("[email protected]", password, "apikey")) | ||
usr := domain.NewUser("[email protected]", password, "apikey") | ||
uc := login(&usr) | ||
_, err := uc(context.Background(), command.LoginCommand{ | ||
Email: "[email protected]", | ||
Password: "nobodycares", | ||
|
@@ -56,7 +57,7 @@ func Test_Login(t *testing.T) { | |
|
||
t.Run("should returns a valid user id if it succeeds", func(t *testing.T) { | ||
existingUser := domain.NewUser("[email protected]", password, "apikey") | ||
uc := login(existingUser) | ||
uc := login(&existingUser) | ||
uid, err := uc(context.Background(), command.LoginCommand{ | ||
Email: "[email protected]", | ||
Password: "password", | ||
|
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 |
---|---|---|
|
@@ -15,13 +15,13 @@ import ( | |
|
||
func Test_UpdateUser(t *testing.T) { | ||
hasher := infra.NewBCryptHasher() | ||
update := func(existingUsers ...domain.User) (func(context.Context, command.UpdateUserCommand) error, memory.UsersStore) { | ||
update := func(existingUsers ...*domain.User) func(context.Context, command.UpdateUserCommand) error { | ||
store := memory.NewUsersStore(existingUsers...) | ||
return command.UpdateUser(store, store, hasher), store | ||
return command.UpdateUser(store, store, hasher) | ||
} | ||
|
||
t.Run("should require valid inputs", func(t *testing.T) { | ||
uc, _ := update() | ||
uc := update() | ||
err := uc(context.Background(), command.UpdateUserCommand{}) | ||
|
||
testutil.ErrorIs(t, apperr.ErrNotFound, err) | ||
|
@@ -30,7 +30,7 @@ func Test_UpdateUser(t *testing.T) { | |
t.Run("should succeed if values are the same", func(t *testing.T) { | ||
passwordHash, _ := hasher.Hash("apassword") | ||
user := domain.NewUser("[email protected]", passwordHash, "anapikey") | ||
uc, store := update(user) | ||
uc := update(&user) | ||
|
||
err := uc(context.Background(), command.UpdateUserCommand{ | ||
ID: string(user.ID()), | ||
|
@@ -39,14 +39,13 @@ func Test_UpdateUser(t *testing.T) { | |
}) | ||
|
||
testutil.IsNil(t, err) | ||
|
||
user, _ = store.GetByID(context.Background(), user.ID()) | ||
testutil.HasNEvents(t, &user, 2) // 2 since bcrypt will produce different hashes | ||
testutil.EventIs[domain.UserPasswordChanged](t, &user, 1) | ||
}) | ||
|
||
t.Run("should update user if everything is good", func(t *testing.T) { | ||
user := domain.NewUser("[email protected]", "apassword", "anapikey") | ||
uc, store := update(user) | ||
uc := update(&user) | ||
|
||
err := uc(context.Background(), command.UpdateUserCommand{ | ||
ID: string(user.ID()), | ||
|
@@ -55,8 +54,9 @@ func Test_UpdateUser(t *testing.T) { | |
}) | ||
|
||
testutil.IsNil(t, err) | ||
|
||
user, _ = store.GetByID(context.Background(), user.ID()) | ||
testutil.HasNEvents(t, &user, 3) | ||
evt := testutil.EventIs[domain.UserEmailChanged](t, &user, 1) | ||
testutil.Equals(t, "[email protected]", string(evt.Email)) | ||
testutil.EventIs[domain.UserPasswordChanged](t, &user, 2) | ||
}) | ||
} |
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
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
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
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
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
Oops, something went wrong.