-
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.
Create the
ValidateUserCredentials
method
- Loading branch information
1 parent
77aed87
commit 5404a5f
Showing
4 changed files
with
96 additions
and
3 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 |
---|---|---|
|
@@ -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) { | ||
|
@@ -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) | ||
} | ||
|
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,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) | ||
} |
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