@@ -2,6 +2,7 @@ package auth
22
33import (
44 "fmt"
5+ "io"
56 "net/http"
67 "net/http/httptest"
78 "testing"
@@ -11,25 +12,37 @@ import (
1112 drycc "github.com/drycc/controller-sdk-go"
1213)
1314
15+ const keyFixture = "61a7907bf5b34659a14f96371fed2ebc"
16+
1417type fakeHTTPServer struct {
1518}
1619
1720func (f * fakeHTTPServer ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
1821 res .Header ().Add ("DRYCC_API_VERSION" , drycc .APIVersion )
1922
2023 if req .URL .Path == "/v2/auth/login/" && req .Method == "POST" {
21- res .Header ().Add ("Location" , "/v2/login/drycc/?key=fdbf3b34742e4ed2be4dfa848af13007/" )
22- res .WriteHeader (http .StatusFound )
23- res .Write (nil )
24- return
24+ body , err := io .ReadAll (req .Body )
25+
26+ if err != nil {
27+ fmt .Println (err )
28+ res .WriteHeader (http .StatusInternalServerError )
29+ res .Write (nil )
30+ }
31+ if len (body ) == 0 {
32+ res .Header ().Add ("Location" , fmt .Sprintf ("/v2/login/drycc/?key=%s/" , keyFixture ))
33+ res .WriteHeader (http .StatusFound )
34+ res .Write (nil )
35+ } else {
36+ res .WriteHeader (http .StatusFound )
37+ res .Write ([]byte (fmt .Sprintf (`{"key": "%s"}` , keyFixture )))
38+ }
2539 }
2640
27- if req .URL .Path == "/v2/auth/token/fdbf3b34742e4ed2be4dfa848af13007 /" && req .Method == "GET" {
41+ if req .URL .Path == "/v2/auth/token/61a7907bf5b34659a14f96371fed2ebc /" && req .Method == "GET" {
2842 res .WriteHeader (http .StatusOK )
2943 res .Write ([]byte (`{"username":"test","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"}` ))
3044 return
3145 }
32-
3346 fmt .Printf ("Unrecognized URL %s\n " , req .URL )
3447 res .WriteHeader (http .StatusNotFound )
3548 res .Write (nil )
@@ -47,16 +60,25 @@ func TestLogin(t *testing.T) {
4760 t .Fatal (err )
4861 }
4962
50- actual , err := Login (drycc )
63+ actual , err := Login (drycc , "" , "" )
5164
5265 if err != nil {
5366 t .Error (err )
5467 }
5568
56- expected := "/v2/login/drycc/?key=fdbf3b34742e4ed2be4dfa848af13007/"
69+ expected := fmt . Sprintf ( "/v2/login/drycc/?key=%s/" , keyFixture )
5770 if actual != expected {
5871 t .Errorf ("Expected %s, Got %s" , expected , actual )
5972 }
73+
74+ actual , err = Login (drycc , "admin" , "admin" )
75+ if err != nil {
76+ t .Error (err )
77+ }
78+ if actual != keyFixture {
79+ t .Errorf ("Expected %s, Got %s" , expected , actual )
80+ }
81+
6082}
6183
6284func TestToken (t * testing.T ) {
@@ -70,12 +92,12 @@ func TestToken(t *testing.T) {
7092 if err != nil {
7193 t .Fatal (err )
7294 }
73- expected := api.AuthLoginResponse {
95+ expected := api.AuthTokenResponse {
7496 Username : "test" ,
7597 Token : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" ,
7698 }
7799
78- token , err := Token (drycc , "fdbf3b34742e4ed2be4dfa848af13007" )
100+ token , err := Token (drycc , keyFixture )
79101
80102 if err != nil {
81103 t .Error (err )
0 commit comments