Debian Codegraph @

internal/login/login_test.go

raw symbols SCIP
1 package login
2
3 import (
4 "errors"
5 "fmt"
6 "net/http"
7 "net/http/httptest"
8 "testing"
9
10 "github.com/google/go-cmp/cmp"
11 "github.com/sirupsen/logrus"
12 "github.com/xperimental/nextcloud-exporter/internal/testutil"
13 )
14
15 func testClient(url string) *Client {
16 return &Client{
17 log: logrus.New(),
18 client: &http.Client{},
19 serverURL: url,
20 sleepFunc: func() {},
21 }
22 }
23
24 func testHandler(status int, body string) http.Handler {
25 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26 w.WriteHeader(status)
27 fmt.Fprintln(w, body)
28 })
29 }
30
31 func TestGetMajorVersion(t *testing.T) {
32 tt := []struct {
33 desc string
34 testHandler http.Handler
35 wantErr error
36 wantVersion int
37 }{
38 {
39 desc: "success",
40 testHandler: testHandler(http.StatusOK, `{"version": "18.0.2"}`),
41 wantVersion: 18,
42 },
43 {
44 desc: "parse error",
45 testHandler: testHandler(http.StatusOK, ``),
46 wantErr: errors.New("error decoding status: EOF"),
47 },
48 {
49 desc: "error version",
50 testHandler: testHandler(http.StatusOK, `{"version": "unparseable"}`),
51 wantErr: errors.New(`can not parse "unparseable" as version: strconv.Atoi: parsing "unparseable": invalid syntax`),
52 },
53 {
54 desc: "error http",
55 testHandler: testHandler(http.StatusInternalServerError, "test error"),
56 wantErr: errors.New("non-ok status: 500"),
57 },
58 }
59
60 for _, tc := range tt {
61 tc := tc
62 t.Run(tc.desc, func(t *testing.T) {
63 t.Parallel()
64
65 s := httptest.NewServer(tc.testHandler)
66 defer s.Close()
67 c := testClient(s.URL)
68
69 version, err := c.getMajorVersion()
70
71 if !testutil.EqualErrorMessage(err, tc.wantErr) {
72 t.Errorf("got error %q, want %q", err, tc.wantErr)
73 }
74
75 if err != nil {
76 return
77 }
78
79 if version != tc.wantVersion {
80 t.Errorf("got version %d, want %d", version, tc.wantVersion)
81 }
82 })
83 }
84 }
85
86 func TestGetLoginInfo(t *testing.T) {
87 tt := []struct {
88 desc string
89 testHandler http.Handler
90 wantErr error
91 wantInfo loginInfo
92 }{
93 {
94 desc: "success",
95 testHandler: testHandler(http.StatusOK, `{"login": "http://localhost/login", "poll": {"token": "token", "endpoint": "http://localhost/poll"}}`),
96 wantInfo: loginInfo{
97 LoginURL: "http://localhost/login",
98 PollInfo: pollInfo{
99 Token: "token",
100 Endpoint: "http://localhost/poll",
101 },
102 },
103 },
104 {
105 desc: "parse error",
106 testHandler: testHandler(http.StatusOK, ``),
107 wantErr: errors.New("error decoding login info: EOF"),
108 },
109 {
110 desc: "error http",
111 testHandler: testHandler(http.StatusInternalServerError, "test error"),
112 wantErr: errors.New("non-ok status: 500"),
113 },
114 }
115
116 for _, tc := range tt {
117 tc := tc
118 t.Run(tc.desc, func(t *testing.T) {
119 t.Parallel()
120
121 s := httptest.NewServer(tc.testHandler)
122 defer s.Close()
123 c := testClient(s.URL)
124
125 info, err := c.getLoginInfo()
126
127 if !testutil.EqualErrorMessage(err, tc.wantErr) {
128 t.Errorf("got error %q, want %q", err, tc.wantErr)
129 }
130
131 if err != nil {
132 return
133 }
134
135 if diff := cmp.Diff(info, tc.wantInfo); diff != "" {
136 t.Errorf("info differs: -got +want\n%s", diff)
137 }
138 })
139 }
140 }
141
142 func TestPollPassword(t *testing.T) {
143 tt := []struct {
144 desc string
145 testHandler http.Handler
146 pollInfo pollInfo
147 wantErr error
148 wantLogin Login
149 }{
150 {
151 desc: "success",
152 testHandler: testHandler(http.StatusOK, `{"loginName": "username", "appPassword": "password"}`),
153 wantLogin: Login{
154 Username: "username",
155 Password: "password",
156 },
157 },
158 {
159 desc: "parse error",
160 testHandler: testHandler(http.StatusOK, ``),
161 wantErr: errors.New("error decoding password info: EOF"),
162 },
163 }
164
165 for _, tc := range tt {
166 tc := tc
167 t.Run(tc.desc, func(t *testing.T) {
168 t.Parallel()
169
170 s := httptest.NewServer(tc.testHandler)
171 defer s.Close()
172 tc.pollInfo.Endpoint = s.URL
173
174 c := testClient("")
175 login, err := c.pollLogin(tc.pollInfo)
176
177 if !testutil.EqualErrorMessage(err, tc.wantErr) {
178 t.Errorf("got error %q, want %q", err, tc.wantErr)
179 }
180
181 if err != nil {
182 return
183 }
184
185 if diff := cmp.Diff(login, tc.wantLogin); diff != "" {
186 t.Errorf("login differs: -got +want\n%s", diff)
187 }
188 })
189 }
190 }