2021-11-08 09:58:41 +00:00
|
|
|
package administrator_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"jokes-bapak2-api/core/administrator"
|
|
|
|
"testing"
|
2021-11-08 12:39:29 +00:00
|
|
|
"time"
|
2021-11-08 09:58:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetUserID_Success(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
|
|
|
defer cancel()
|
2021-11-08 15:55:07 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-08 09:58:41 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
c, err := db.Acquire(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
defer c.Release()
|
|
|
|
|
|
|
|
_, err = c.Exec(
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx,
|
2021-11-08 09:58:41 +00:00
|
|
|
`INSERT INTO administrators (id, key, token, last_used) VALUES ($1, $2, $3, $4)`,
|
|
|
|
administratorsData...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
id, err := administrator.GetUserID(db, ctx, "very secure")
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id != 1 {
|
|
|
|
t.Error("id is not correct, want: 1, got:", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUserID_Failed(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
|
|
|
defer cancel()
|
2021-11-08 09:58:41 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
|
|
|
|
|
|
|
c, err := db.Acquire(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
defer c.Release()
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
id, err := administrator.GetUserID(db, ctx, "very secure")
|
2021-11-08 09:58:41 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Error("an error was expected, got:", id)
|
|
|
|
}
|
|
|
|
}
|