Skip to content

Commit

Permalink
Fix build errors from adding the new method to the store interface
Browse files Browse the repository at this point in the history
  • Loading branch information
claudealdric committed Sep 29, 2024
1 parent 8936336 commit c93f3da
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 13 deletions.
12 changes: 12 additions & 0 deletions data/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ func (f *FileSystemStore) GetUsers() ([]models.User, error) {
return users, nil
}

func (f *FileSystemStore) ValidateUserCredentials(email, password string) bool {
user, err := f.GetUserByEmail(email)
if err != nil {
return false
}
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
if err != nil {
return false
}
return true
}

func (f *FileSystemStore) getTasksFromFile() ([]models.Task, error) {
var tasks []models.Task
err := f.decoder.Decode(&tasks)
Expand Down
66 changes: 66 additions & 0 deletions data/file_system_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,70 @@ func TestFileSystemStoreUsers(t *testing.T) {
assert.HasNoError(t, err)
assert.Equals(t, users, initialUsers)
})

t.Run("ValidateUserCredentials", func(t *testing.T) {
createUserDTO := models.NewCreateUserDTO(
"Claude Aldric",
"[email protected]",
"Caput Draconis",
)
hashedPassword, err := bcrypt.GenerateFromPassword(
[]byte(createUserDTO.Password),
bcrypt.DefaultCost,
)
assert.HasNoError(t, err)

initialUsers := []models.User{
models.User{
Id: 1,
Name: "Claude Aldric",
Email: "[email protected]",
Password: string(hashedPassword),
},
}
jsonUsers, err := utils.ConvertToJSON(initialUsers)
assert.HasNoError(t, err)

database, cleanDatabase := testutils.CreateTempFile(t, string(jsonUsers))
defer cleanDatabase()

store, err := data.NewFileSystemStore(database)
assert.HasNoError(t, err)

tests := []struct {
name string
email string
password string
want bool
}{
{
name: "when provided valid credentials",
email: "[email protected]",
password: "Caput Draconis",
want: true,
},
{
name: "when provided an invalid password",
email: "[email protected]",
password: "password",
want: false,
},
{
name: "when provided an invalid email",
email: "[email protected]",
password: "Caput Draconis",
want: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equals(
t,
store.ValidateUserCredentials(test.email, test.password),
test.want,
)
})
}
})
}
1 change: 0 additions & 1 deletion data/sqlite_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ func (s *SqliteStore) ValidateUserCredentials(email, password string) bool {
}
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
if err != nil {
log.Printf("error with the password validation: %v\n", err)
return false
}
return true
Expand Down
33 changes: 21 additions & 12 deletions testutils/mock_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ var initialMockStoreTasks = []models.Task{*models.NewTask(1, "Pack clothes")}
var forcedError = errors.New("forced error")

type mockStore struct {
CreateTaskCalls int
CreateUserCalls int
GetTaskByIdCalls int
GetTasksCalls int
GetUserByEmailCalls int
GetUsersCalls int
Tasks []models.Task
UpdateTaskCalls int
Users []models.User
lastTaskId int
lastUserId int
shouldForceError bool
CreateTaskCalls int
CreateUserCalls int
GetTaskByIdCalls int
GetTasksCalls int
GetUserByEmailCalls int
GetUsersCalls int
Tasks []models.Task
UpdateTaskCalls int
Users []models.User
ValidateUserCredentialsCalls int
lastTaskId int
lastUserId int
shouldForceError bool
}

func NewMockStore(shouldError bool) *mockStore {
Expand Down Expand Up @@ -142,6 +143,14 @@ func (m *mockStore) GetUsers() ([]models.User, error) {
return m.Users, nil
}

func (m *mockStore) ValidateUserCredentials(username, password string) bool {
m.ValidateUserCredentialsCalls++
if m.shouldForceError {
return false
}
return true
}

func (m *mockStore) getNewTaskId() int {
newTaskId := m.lastTaskId + 1
m.lastTaskId++
Expand Down

0 comments on commit c93f3da

Please sign in to comment.