-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
b2c429d
commit 2aeffe8
Showing
3 changed files
with
113 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package usecase_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/josephspurrier/gocleanarchitecture/database" | ||
"github.com/josephspurrier/gocleanarchitecture/domain" | ||
"github.com/josephspurrier/gocleanarchitecture/usecase" | ||
) | ||
|
||
// TestCreateUser ensures user can be created. | ||
func TestCreateUser(t *testing.T) { | ||
// Test user creation. | ||
s := usecase.UserService{ | ||
UserRepo: database.NewUserRepo(new(database.MockService)), | ||
} | ||
u := new(domain.User) | ||
u.Email = "jdoe@example.com" | ||
u.Password = "Pa$$w0rd" | ||
err := s.CreateUser(u) | ||
AssertEqual(t, err, nil) | ||
|
||
// Test user creation fail. | ||
err = s.CreateUser(u) | ||
AssertEqual(t, err, domain.ErrUserAlreadyExist) | ||
|
||
// Test user query. | ||
uTest, err := s.User("jdoe@example.com") | ||
AssertEqual(t, err, nil) | ||
AssertEqual(t, uTest.Password, "Pa$$w0rd") | ||
|
||
// Test failed user query. | ||
_, err = s.User("bademail@example.com") | ||
AssertEqual(t, err, domain.ErrUserNotFound) | ||
} | ||
|
||
// TestAuthenticate ensures user can authenticate. | ||
func TestAuthenticate(t *testing.T) { | ||
// Test user creation. | ||
s := usecase.UserService{ | ||
UserRepo: database.NewUserRepo(new(database.MockService)), | ||
} | ||
u := new(domain.User) | ||
u.Email = "ssmith@example.com" | ||
u.Password = "Pa$$w0rd" | ||
err := s.CreateUser(u) | ||
AssertEqual(t, err, nil) | ||
|
||
// Test user authentication. | ||
err = s.Authenticate(u) | ||
AssertEqual(t, err, nil) | ||
|
||
// Test failed user authentication. | ||
u.Password = "BadPa$$w0rd" | ||
err = s.Authenticate(u) | ||
AssertEqual(t, err, domain.ErrUserPasswordNotMatch) | ||
|
||
// Test failed user authentication. | ||
u.Email = "bfranklin@example.com" | ||
err = s.Authenticate(u) | ||
AssertEqual(t, err, domain.ErrUserNotFound) | ||
} | ||
|
||
// TestUserFailures ensures user fails properly. | ||
func TestUserFailures(t *testing.T) { | ||
// Test user creation. | ||
db := new(database.MockService) | ||
s := usecase.UserService{ | ||
UserRepo: database.NewUserRepo(db), | ||
} | ||
|
||
db.WriteFail = true | ||
db.ReadFail = true | ||
|
||
u := new(domain.User) | ||
u.Email = "ssmith@example.com" | ||
u.Password = "Pa$$w0rd" | ||
err := s.CreateUser(u) | ||
AssertNotNil(t, err) | ||
|
||
// Test user authentication. | ||
err = s.Authenticate(u) | ||
AssertNotNil(t, err) | ||
|
||
// Test failed user query. | ||
_, err = s.User("favalon@example.com") | ||
AssertNotNil(t, err) | ||
|
||
// Test failed user authentication. | ||
u.Email = "bfranklin@example.com" | ||
err = s.Authenticate(u) | ||
AssertNotNil(t, 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package usecase_test | ||
|
||
import "testing" | ||
|
||
// AssertEqual throws an error if the two values are not equal. | ||
func AssertEqual(t *testing.T, actualValue interface{}, expectedValue interface{}) { | ||
if actualValue != expectedValue { | ||
t.Errorf("\n got: %v\nwant: %v", actualValue, expectedValue) | ||
} | ||
} | ||
|
||
// AssertNotNil throws an error if the value is nil. | ||
func AssertNotNil(t *testing.T, actualValue interface{}) { | ||
if actualValue == nil { | ||
t.Errorf("\n got: %v\ndidn't want: %v", actualValue, nil) | ||
} | ||
} |