46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package acceptance
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
type loginResponse struct {
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
func loginAdmin(t *testing.T, ctx context.Context, client *Client, c Config) string {
|
|
t.Helper()
|
|
return login(t, ctx, client, "/v1/auth/admin/login", c.AdminEmail, c.AdminPassword)
|
|
}
|
|
|
|
func loginUser(t *testing.T, ctx context.Context, client *Client, c Config) string {
|
|
t.Helper()
|
|
return login(t, ctx, client, "/v1/auth/login", c.UserEmail, c.UserPassword)
|
|
}
|
|
|
|
func login(t *testing.T, ctx context.Context, client *Client, path string, email string, password string) string {
|
|
t.Helper()
|
|
requireSecret(t, "login email", email)
|
|
requireSecret(t, "login password", password)
|
|
|
|
envelope, raw, status, err := client.PostJSON(ctx, path, map[string]string{
|
|
"email": email,
|
|
"password": password,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("login request failed: %v", err)
|
|
}
|
|
assertOK(t, envelope, raw, status)
|
|
|
|
var data loginResponse
|
|
if err := json.Unmarshal(envelope.Data, &data); err != nil {
|
|
t.Fatalf("decode login response: %v", err)
|
|
}
|
|
if data.Token == "" {
|
|
t.Fatal("login response token is empty")
|
|
}
|
|
return data.Token
|
|
}
|