This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
user_test.go
110 lines (97 loc) · 2.26 KB
/
user_test.go
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
110
package starling
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
var userTestCases = []struct {
name string
mock string
}{
{
name: "sample user",
mock: `{
"customerUid": "6d2aa528-b9d1-4083-ae7c-53d460cd8d88",
"expiresAt": "2018-04-03T09:33:01.137Z",
"authenticated": true,
"expiresInSeconds": 86373,
"scopes": [
"account:read",
"balance:read",
"address:read",
"address:edit",
"card:read",
"customer:read",
"mandate:read",
"mandate:delete",
"metadata:create",
"metadata:edit",
"payee:create",
"payee:delete",
"payee:edit",
"payee:read",
"pay-local:create",
"pay-foreign:create",
"transaction:read",
"transaction:edit",
"savings-goal:read",
"savings-goal:create",
"savings-goal:delete",
"savings-goal-transfer:read",
"savings-goal-transfer:create",
"savings-goal-transfer:delete"
]
}`,
},
{
name: "sample user and no scopes",
mock: `{
"customerUid": "6d2aa528-b9d1-4083-ae7c-53d460cd8d88",
"expiresAt": "2018-04-03T09:33:01.137Z",
"authenticated": true,
"expiresInSeconds": 86373,
"scopes": []
}`,
},
}
func TestCurrentUser(t *testing.T) {
for _, tc := range userTestCases {
t.Run(tc.name, func(st *testing.T) {
testCurrentUser(st, tc.name, tc.mock)
})
}
}
func testCurrentUser(t *testing.T, name, mock string) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/me", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
fmt.Fprint(w, mock)
})
got, _, err := client.CurrentUser(context.Background())
checkNoError(t, err)
want := &Identity{}
json.Unmarshal([]byte(mock), want)
if !reflect.DeepEqual(got, want) {
t.Error("should return an identity matching the mock response", cross)
}
}
func TestCurrentUserForbidden(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/api/v1/me", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusForbidden)
})
got, resp, err := client.CurrentUser(context.Background())
checkHasError(t, err)
if resp.StatusCode != http.StatusForbidden {
t.Error("should return HTTP 403 status")
}
if got != nil {
t.Error("should not return a user")
}
}