mirror of
https://gitgud.io/yats/libkiwi.git
synced 2026-06-19 17:45:25 -04:00
Refactoring n shit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package libkiwi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pquerna/otp/totp"
|
||||
)
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
user, pass := os.Getenv("TEST_USER"), os.Getenv("TEST_PASS")
|
||||
if user == "" || pass == "" {
|
||||
t.Log("TEST_USER and/or TEST_PASS not set in env. Skipping.\n")
|
||||
return
|
||||
}
|
||||
|
||||
kf, err := newTestKF()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
resp, err := kf.Login(ctx, Credentials{
|
||||
Username: user,
|
||||
Password: pass,
|
||||
Remember: false, // Don't wastefully create dangling sessions.
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
t.Logf("Login response: %+v\n", resp)
|
||||
|
||||
if strings.Contains(resp.Request.URL.Path, "two-step") {
|
||||
code, err := genTest2FACode()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := kf.TwoFactorAuth(ctx, resp, code)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var lr LoginResp
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&lr)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t.Logf("2FA response: %+v\n", lr)
|
||||
}
|
||||
}
|
||||
|
||||
func genTest2FACode() (uint32, error) {
|
||||
secret := os.Getenv("TEST_TOTP")
|
||||
if secret == "" {
|
||||
return 0, errors.New("TEST_TOTP not set in env.")
|
||||
}
|
||||
|
||||
codeStr, err := totp.GenerateCode(secret, time.Now())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
code, err := strconv.Atoi(codeStr)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return uint32(code), nil
|
||||
}
|
||||
Reference in New Issue
Block a user