Skip to content

Commit

Permalink
Create the ValidateUserCredentials method
Browse files Browse the repository at this point in the history
  • Loading branch information
claudealdric committed Sep 29, 2024
1 parent 77aed87 commit 5404a5f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
21 changes: 18 additions & 3 deletions data/init_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package data
import (
"database/sql"
"log"

"github.com/claudealdric/go-todolist-restful-api-server/models"
"golang.org/x/crypto/bcrypt"
)

func InitDb(db *sql.DB) {
Expand Down Expand Up @@ -39,11 +42,23 @@ func createUsersTable(db *sql.DB) {
}

func seedUsersTable(db *sql.DB) {
_, err := db.Exec(`
dto := models.NewCreateUserDTO(
"Claude Aldric",
"[email protected]",
"Caput Draconis",
)
hashedPassword, err := bcrypt.GenerateFromPassword(
[]byte(dto.Password),
bcrypt.DefaultCost,
)
if err != nil {
log.Fatalln("failed at hashing the password:", err)
}
_, err = db.Exec(`
insert into users (name, email, password)
select 'Claude Aldric', '[email protected]', 'Caput Draconis'
select ?, ?, ?
where not exists (select 1 from users)
`)
`, dto.Name, dto.Email, hashedPassword)
if err != nil {
log.Fatalln("failed seeding the users table:", err)
}
Expand Down
15 changes: 15 additions & 0 deletions data/sqlite_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package data

import (
"database/sql"
"log"

"github.com/claudealdric/go-todolist-restful-api-server/models"
"golang.org/x/crypto/bcrypt"
Expand Down Expand Up @@ -145,3 +146,17 @@ func (s *SqliteStore) UpdateTask(task *models.Task) (*models.Task, error) {

return updatedTask, nil
}

func (s *SqliteStore) ValidateUserCredentials(email, password string) bool {
user, err := s.GetUserByEmail(email)
if err != nil {
log.Printf("error retrieving user for validation: %v\n", err)
return false
}
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
}
62 changes: 62 additions & 0 deletions data/sqlite_store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package data

import (
"database/sql"
"os"
"testing"

_ "github.com/mattn/go-sqlite3"

"github.com/claudealdric/go-todolist-restful-api-server/testutils/assert"
)

func TestValidateUserCredentials(t *testing.T) {
dbFile := "../tmp/sqlite_store_test.db"
db, err := sql.Open("sqlite3", dbFile)
assert.HasNoError(t, err)
defer db.Close()
InitDb(db)
defer cleanSqliteDatabase(dbFile)
store := NewSqliteStore(db)

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,
)
})
}

}

func cleanSqliteDatabase(path string) {
os.Remove(path)
}
1 change: 1 addition & 0 deletions data/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ type Store interface {
CreateUser(dto *models.CreateUserDTO) (*models.User, error)
GetUserByEmail(email string) (*models.User, error)
GetUsers() ([]models.User, error)
ValidateUserCredentials(username, password string) bool
}

0 comments on commit 5404a5f

Please sign in to comment.