Files
backend/tests/01_login_test.go
Yi-Ting Shih 47f09b733a
All checks were successful
Go test / run-go-vet (push) Successful in 20s
Go test / check-swagger-up-to-date (push) Successful in 9s
Go test / run-go-test (push) Successful in 47s
Go test / cleanup-go-test (push) Successful in 4s
Go test / release-image (push) Successful in 3m35s
Refactor: migrate discordbot
2025-12-13 04:58:12 +08:00

71 lines
1.5 KiB
Go

package tests
import (
"net/http"
"net/url"
"testing"
)
type genLoginUrlPayload struct {
LoginUrl string `json:"loginUrl"`
}
type loginPayload struct {
Token string `json:"token"`
}
func Test_01_Login(t *testing.T) {
t.Run("check preshared key failed", func(t *testing.T) {
resp, err := client.R().
SetBody(`{"userId": "testuser1"}`).
Post("/bot/auth/gen-login-url")
if err != nil {
t.Fatal("request failed")
}
if resp.StatusCode() != http.StatusUnauthorized {
t.Fatal("preshared key check should failed")
}
})
var payload genLoginUrlPayload
resp, err := client.R().
SetBody(`{"userId": "testuser1"}`).
SetAuthToken("poop").
SetResult(&payload).
Post("/bot/auth/gen-login-url")
if err != nil || resp.StatusCode() != http.StatusOK {
t.Fatal("failed to get login url")
}
loginUrl, err := url.Parse(payload.LoginUrl)
if err != nil {
t.Logf("login-url: %s", payload.LoginUrl)
t.Fatal("failed to parse login url")
}
resp, err = client.R().
SetBody(loginPayload{Token: loginUrl.Query().Get("token")}).
SetAuthToken("poop").
Post("/auth/login")
if err != nil || resp.StatusCode() != http.StatusOK {
t.Fatal("failed to login")
}
for _, rawCookie := range resp.Header()["Set-Cookie"] {
cookie, err := http.ParseSetCookie(rawCookie)
if err != nil {
t.Fatal("failed to parse cookie")
}
if cookie.Name == "refresh_token" {
if len(cookie.Value) == 0 {
t.Fatal("empty refresh token")
}
client.SetCookie(cookie)
return
}
}
t.Fatal("refresh token not exist")
}