-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth_test.go
More file actions
109 lines (86 loc) · 2.54 KB
/
auth_test.go
File metadata and controls
109 lines (86 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package auth
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/drycc/controller-sdk-go/api"
drycc "github.com/drycc/controller-sdk-go"
)
const keyFixture = "61a7907bf5b34659a14f96371fed2ebc"
type fakeHTTPServer struct {
}
func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
if req.URL.Path == "/v2/auth/login/" && req.Method == "POST" {
body, err := io.ReadAll(req.Body)
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusInternalServerError)
res.Write(nil)
}
if len(body) == 0 {
res.Header().Add("Location", fmt.Sprintf("/v2/login/drycc/?key=%s/?alias=test", keyFixture))
res.WriteHeader(http.StatusFound)
res.Write(nil)
} else {
res.WriteHeader(http.StatusFound)
res.Write([]byte(fmt.Sprintf(`{"key": "%s"}`, keyFixture)))
}
}
if req.URL.Path == "/v2/auth/token/61a7907bf5b34659a14f96371fed2ebc/" && req.Method == "GET" {
res.WriteHeader(http.StatusOK)
res.Write([]byte(`{"username":"test","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"}`))
return
}
fmt.Printf("Unrecognized URL %s\n", req.URL)
res.WriteHeader(http.StatusNotFound)
res.Write(nil)
}
func TestLogin(t *testing.T) {
t.Parallel()
handler := fakeHTTPServer{}
server := httptest.NewServer(&handler)
defer server.Close()
drycc, err := drycc.New(false, server.URL, "abc")
if err != nil {
t.Fatal(err)
}
actual, err := Login(drycc, "", "")
if err != nil {
t.Error(err)
}
expected := fmt.Sprintf("/v2/login/drycc/?key=%s/?alias=test", keyFixture)
if actual != expected {
t.Errorf("Expected %s, Got %s", expected, actual)
}
actual, err = Login(drycc, "admin", "admin")
if err != nil {
t.Error(err)
}
if actual != keyFixture {
t.Errorf("Expected %s, Got %s", expected, actual)
}
}
func TestToken(t *testing.T) {
t.Parallel()
handler := fakeHTTPServer{}
server := httptest.NewServer(&handler)
defer server.Close()
drycc, err := drycc.New(false, server.URL, "abc")
if err != nil {
t.Fatal(err)
}
expected := api.AuthTokenResponse{
Username: "test",
Token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
}
token, err := Token(drycc, keyFixture, "test")
if err != nil {
t.Error(err)
}
if token != expected {
t.Errorf("Expected %s, Got %s", expected, token)
}
}