2021-11-08 09:58:41 +00:00
|
|
|
package administrator_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
)
|
|
|
|
|
|
|
|
var db *pgxpool.Pool
|
|
|
|
|
|
|
|
var administratorsData = []interface{}{
|
|
|
|
1, "very secure", "not the real one", time.Now().Format(time.RFC3339),
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
defer Teardown()
|
|
|
|
Setup()
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Setup() {
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Minute))
|
|
|
|
defer cancel()
|
|
|
|
|
2021-11-08 09:58:41 +00:00
|
|
|
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
db, err = pgxpool.ConnectConfig(ctx, poolConfig)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
conn, err := db.Acquire(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
2021-11-08 12:39:29 +00:00
|
|
|
tx, err := conn.Begin(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-11-08 12:39:29 +00:00
|
|
|
defer tx.Rollback(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
|
|
|
|
_, err = tx.Exec(
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx,
|
2021-11-08 09:58:41 +00:00
|
|
|
`CREATE TABLE IF NOT EXISTS administrators (
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
key VARCHAR(255) NOT NULL UNIQUE,
|
|
|
|
token TEXT,
|
|
|
|
last_used VARCHAR(255)
|
|
|
|
)`,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
err = tx.Commit(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Teardown() (err error) {
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Minute))
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
defer db.Close()
|
2021-11-08 15:55:07 +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 {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-08 12:39:29 +00:00
|
|
|
defer c.Release()
|
2021-11-08 09:58:41 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
tx, err := c.Begin(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-08 12:39:29 +00:00
|
|
|
defer tx.Rollback(ctx)
|
|
|
|
|
|
|
|
_, err = tx.Exec(ctx, "TRUNCATE TABLE submission RESTART IDENTITY CASCADE")
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-08 12:39:29 +00:00
|
|
|
_, err = tx.Exec(ctx, "TRUNCATE TABLE jokesbapak2 RESTART IDENTITY CASCADE")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.Exec(ctx, "TRUNCATE TABLE administrators RESTART IDENTITY CASCADE")
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
err = tx.Commit(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func Flush() error {
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
conn, err := db.Acquire(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
_, err = conn.Exec(ctx, "TRUNCATE TABLE administrators RESTART IDENTITY CASCADE")
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|