2024-04-26 16:03:32 -07:00
|
|
|
package repository_test
|
|
|
|
|
|
|
|
import (
|
2024-05-05 10:02:17 -07:00
|
|
|
"context"
|
2024-04-26 16:03:32 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRefreshTokenCreate(t *testing.T) {
|
|
|
|
conn, err := setupInMemoryDb()
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
client := repository.NewRefreshTokenRepository(conn)
|
2024-05-05 10:02:17 -07:00
|
|
|
rows, err := client.Create(context.Background(), "tester", "BadTokenDontUse")
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows == 0 {
|
|
|
|
t.Log("expected one row to come back but got 0")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefreshTokenGetByUsername(t *testing.T) {
|
|
|
|
conn, err := setupInMemoryDb()
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
client := repository.NewRefreshTokenRepository(conn)
|
2024-05-05 10:02:17 -07:00
|
|
|
rows, err := client.Create(context.Background(), "tester", "BadTokenDoNotUse")
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows != 1 {
|
|
|
|
t.Log("expected a row to be added but not the wrong value back")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
model, err := client.GetByUsername(context.Background(), "tester")
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if model.Username != "tester" {
|
|
|
|
t.Log("got the wrong user back")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefreshTokenDeleteById(t *testing.T) {
|
|
|
|
conn, err := setupInMemoryDb()
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
client := repository.NewRefreshTokenRepository(conn)
|
2024-05-05 10:02:17 -07:00
|
|
|
created, err := client.Create(context.Background(), "tester", "BadTokenDoNotUse")
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if created != 1 {
|
|
|
|
t.Log("Unexpected number back for rows created")
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
model, err := client.GetByUsername(context.Background(), "tester")
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
updated, err := client.DeleteById(context.Background(), model.ID)
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated != 1 {
|
|
|
|
t.Log("deleted the wrong number of records")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|