jokes-bapak2/api/core/administrator/verify_test.go

71 lines
1.4 KiB
Go
Raw Normal View History

package administrator_test
import (
"context"
"jokes-bapak2-api/core/administrator"
"testing"
2021-11-08 12:39:29 +00:00
"time"
)
func TestCheckKeyExists_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 12:39:29 +00:00
defer Flush()
c, err := db.Acquire(ctx)
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,
"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
key, err := administrator.CheckKeyExists(db, ctx, "very secure")
if err != nil {
t.Error("an error was thrown:", err)
}
if key != "not the real one" {
t.Error("key isn't not the real one, got:", key)
}
}
func TestCheckKeyExists_Failing(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()
defer Flush()
2021-11-08 12:39:29 +00:00
c, err := db.Acquire(ctx)
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,
"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
key, err := administrator.CheckKeyExists(db, ctx, "others")
if err != nil {
t.Error("an error was thrown:", err)
}
if key != "" {
t.Error("key is not empty, got:", key)
}
}