-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_test.go
More file actions
201 lines (161 loc) · 5.29 KB
/
Copy pathclient_test.go
File metadata and controls
201 lines (161 loc) · 5.29 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package patientaccess
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"
)
var (
mux *http.ServeMux
client *Client
server *httptest.Server
)
func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
client = NewClient()
url, _ := url.Parse(server.URL)
url.Path = url.Path + "/api"
client.BaseURL = url
}
func teardown() {
server.Close()
}
func assertHttpMethod(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %+v, want %+v", got, want)
}
}
func assertEqual(t *testing.T, got, want interface{}) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
}
func assertError(t *testing.T, got, want error) {
t.Helper()
if got != want {
t.Errorf("got error %q, want %q", got, want)
}
}
func assertStrings(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
func TestAccessTokenExpiresIn(t *testing.T) {
tokenExpiresIn := time.Now().Add(time.Minute * 5).Format("2006-01-02T15:04:05.999999Z")
jsonBlob := fmt.Sprintf(`{"access_token": "28d5cf150df203a0002f48395e380dff", "expires_in": "%s"}`, tokenExpiresIn)
want := AccessToken {
Token: "28d5cf150df203a0002f48395e380dff",
ExpiresIn: 299,
}
var got AccessToken
err := json.Unmarshal([]byte(jsonBlob), &got)
if err != nil {
t.Errorf("Token unmarshaling error: %v", err)
}
assertEqual(t, got, want)
}
func TestGetToken(t *testing.T) {
setup()
defer teardown()
username := "roman"
password := "sikr3t"
tokenExpiresIn := time.Now().Add(time.Minute * 5).Format("2006-01-02T15:04:05.999999Z")
jsonBlob := fmt.Sprintf(`{"accessToken": {"access_token": "28d5cf150df203a0002f48395e380dff", "expires_in": "%s"}}`, tokenExpiresIn)
mux.HandleFunc("/api/authorization/signin", func(w http.ResponseWriter, r *http.Request) {
assertHttpMethod(t, r.Method, "POST")
want := map[string]string{
"username": username,
"password": password,
}
var got map[string]string
json.NewDecoder(r.Body).Decode(&got)
assertEqual(t, want, got)
fmt.Fprint(w, jsonBlob)
})
want := &AccessToken {
Token: "28d5cf150df203a0002f48395e380dff",
ExpiresIn: 299,
}
got, _ := client.GetToken(username, password)
assertEqual(t, got, want)
}
func TestGetTokenBadCredentials(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api/authorization/signin", func(w http.ResponseWriter, r *http.Request) {
assertHttpMethod(t, r.Method, "POST")
fmt.Fprint(w, `{"accessToken": null}"`)
})
_, err := client.GetToken("roman", "sikr3t")
assertError(t, err, ErrBadCredentials)
}
func TestGetTokenBadStatusCode(t *testing.T) {
setup()
defer teardown()
_, err := client.GetToken("roman", "sikr3t")
assertError(t, err, ErrBadStatusCode)
}
func TestURLPathJoining(t *testing.T) {
tc := []struct{
baseURL string
path string
want string
} {
{"https://t.co", "api/foo", "https://t.co/api/foo"},
{"https://t.co", "/api/foo", "https://t.co/api/foo"},
{"https://t.co/api", "api/foo", "https://t.co/api/api/foo"},
{"https://t.co/api", "/api/foo", "https://t.co/api/api/foo"},
{"https://t.co/api/", "/api/foo", "https://t.co/api/api/foo"},
}
for _, tt := range tc {
t.Run(tt.want, func(t *testing.T) {
u, _ := url.Parse(tt.baseURL)
got, _ := joinPaths(u, tt.path)
assertStrings(t, got.String(), tt.want)
})
}
}
func TestGetAppointments(t *testing.T) {
setup()
defer teardown()
token := "28d5cf150df203a0002f48395e380dff"
patientId := "15d0b1d1-d046-46f6-ae46-7814782fd536"
mux.HandleFunc("/api/Appointment/properties/hierarchy", func(w http.ResponseWriter, r *http.Request) {
assertHttpMethod(t, r.Method, "GET")
assertStrings(t, r.Header.Get("Authorization"), fmt.Sprintf("Bearer %s", token))
assertStrings(t, r.Header.Get("X-PatientId"), patientId)
fmt.Fprint(w, `{"slots":[{"slotType":{"id":"Default","name":"General","status":1,"isDefault":true}},{"slotType":{"id":"BLOOD TEST","name":"BLOOD TEST","status":1,"isDefault":false}}]}`)
})
want := []AppointmentSlot{
AppointmentSlot{
SlotType{Id: "Default", Name: "General"},
},
AppointmentSlot{
SlotType{Id: "BLOOD TEST", Name: "BLOOD TEST"},
},
}
got, _ := client.GetAppointmentSlots(token, patientId)
assertEqual(t, got, want)
}
func TestGetPatientId(t *testing.T) {
setup()
defer teardown()
token := "28d5cf150df203a0002f48395e380dff"
mux.HandleFunc("/api/Account/patients", func(w http.ResponseWriter, r *http.Request) {
assertHttpMethod(t, r.Method, "GET")
assertStrings(t, r.Header.Get("Authorization"), fmt.Sprintf("Bearer %s", token))
fmt.Fprint(w, `{"selfPatientId":"15d0b1d1-d046-46f6-ae46-7814782fd536"}`)
})
want := "15d0b1d1-d046-46f6-ae46-7814782fd536"
got, _ := client.GetPatientId(token)
assertStrings(t, got, want)
}