Feat: finish login and add test
This commit is contained in:
59
tests/login_test.go
Normal file
59
tests/login_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
type genLoginUrlPayload struct {
|
||||
LoginUrl string `json:"loginUrl"`
|
||||
}
|
||||
|
||||
type loginPayload struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
client := resty.New()
|
||||
|
||||
var payload genLoginUrlPayload
|
||||
resp, err := client.R().
|
||||
SetBody(`{"userId": "testuser1"}`).
|
||||
SetResult(&payload).
|
||||
Post("http://localhost:8080/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")}).
|
||||
Post(loginUrl.Scheme + "://" + loginUrl.Host + loginUrl.Path)
|
||||
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")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatal("refresh token not exist")
|
||||
}
|
||||
Reference in New Issue
Block a user