-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice_test.go
208 lines (164 loc) · 5.52 KB
/
service_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
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
202
203
204
205
206
207
208
package golibre_test
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/equalsgibson/golibre"
)
const (
validPassword string = "VALID_PASSWORD"
validEmail string = "EMAIL"
validJWTToken string = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMzQ1Njc4LTEyMzQtMTIzNC0xMjM0LTQ2MDRhNjdiN2FiNCIsImZpcnN0TmFtZSI6IlNvbWUiLCJsYXN0TmFtZSI6Ik9uZSIsImNvdW50cnkiOiJHQiIsInJlZ2lvbiI6ImV1MiIsInJvbGUiOiJwYXRpZW50IiwidW5pdHMiOjAsInByYWN0aWNlcyI6W10sImMiOjEsInMiOiJsbHUuYW5kcm9pZCIsImV4cCI6MTc0MjI5NDIwN30.ilRwCINRf6nQViQ9c0BLZD9x21qsiBx43EzMk1POTuk" // #nosec G101 Fake Token For Test
validPatientID golibre.PatientID = "87654321-4321-4321-4321-0242ac110002"
)
func authenticatedRequestMiddleware(t *testing.T, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer "+validJWTToken {
t.Logf("Authorization header was present, but JWT Token was invalid: %s", r.Header.Get("Authorization"))
notAuthenticated(w)
return
}
next.ServeHTTP(w, r)
})
}
func newTestServer(t *testing.T) *httptest.Server {
t.Helper()
testServeMux := http.NewServeMux()
// Private Endpoints
// -> User endpoint
testServeMux.Handle("/user", authenticatedRequestMiddleware(t, userHandler(t)))
// -> Account endpoint
testServeMux.Handle("/account", authenticatedRequestMiddleware(t, accountHandler(t)))
// -> Connections endpoint
testServeMux.Handle("/llu/connections", authenticatedRequestMiddleware(t, connectionHandler(t)))
// -> ConnectionGraph endpoint
testServeMux.Handle("/llu/connections/{patientID}/graph", authenticatedRequestMiddleware(t, connectionGraphHandler(t)))
// Public Endpoints
// -> Authentication endpoint
testServeMux.HandleFunc("/llu/auth/login", loginHandler(t))
testServeMux.HandleFunc("/", http.NotFound)
// Add the handlers to an unstarted server
srv := httptest.NewUnstartedServer(
testServeMux,
)
// Set the TLS Config to skip the verify step, as this is a local test and outside the scope of the requirements
srv.StartTLS()
srv.TLS.InsecureSkipVerify = true
// Return the server to the caller
return srv
}
func getTestServerAddress(testSrv *httptest.Server) string {
url := testSrv.URL
return strings.TrimPrefix(url, "https://")
}
func notAuthenticated(w http.ResponseWriter) {
response, err := os.ReadFile("./test_files/error/response/error_unauthenticated.json")
if err != nil {
panic(err)
}
// As of Go 1.17, Write() automatically sends http.StatusOK.
_, err = w.Write(response)
if err != nil {
panic(err)
}
}
func userHandler(t *testing.T) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
validResponse, err := os.ReadFile("./test_files/user/response/getUserData_200.json")
if err != nil {
t.Logf("Error while reading response file: %s", err.Error())
panic(err)
}
_, err = w.Write(validResponse)
if err != nil {
t.Logf("Error while writing response bytes: %s", err.Error())
panic(err)
}
})
}
func accountHandler(t *testing.T) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
validResponse, err := os.ReadFile("./test_files/account/response/getAccountDetails_200.json")
if err != nil {
t.Logf("Error while serving valid response: %s", err.Error())
panic(err)
}
_, err = w.Write(validResponse)
if err != nil {
panic(err)
}
})
}
func loginHandler(t *testing.T) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Validate that on login, we are not sending an Authorization header
if r.Header.Get("Authorization") != "" {
t.Logf("Authorization header was present, with value: %s", r.Header.Get("Authorization"))
notAuthenticated(w)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
t.Logf("Error reading Request Body: %s", err.Error())
notAuthenticated(w)
return
}
target := &golibre.Authentication{}
if err := json.Unmarshal(body, target); err != nil {
t.Logf("Error unmarshalling Request Body: %s", err.Error())
notAuthenticated(w)
return
}
if target.Email != validEmail || target.Password != validPassword {
t.Logf("Invalid email or password: E: %s, P: %s", target.Email, target.Password)
notAuthenticated(w)
return
}
validResponse, err := os.ReadFile("./test_files/client/response/login_successful.json")
if err != nil {
t.Logf("Error while serving valid response: %s", err.Error())
notAuthenticated(w)
return
}
_, err = w.Write(validResponse)
if err != nil {
panic(err)
}
})
}
func connectionHandler(t *testing.T) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
validResponse, err := os.ReadFile("./test_files/connection/response/getAllConnectionData_200.json")
if err != nil {
t.Logf("Error while serving valid response: %s", err.Error())
notAuthenticated(w)
return
}
_, err = w.Write(validResponse)
if err != nil {
panic(err)
}
})
}
func connectionGraphHandler(t *testing.T) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.PathValue("patientID") != string(validPatientID) {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
validResponse, err := os.ReadFile("./test_files/connection/response/getConnectionGraph_200.json")
if err != nil {
t.Logf("Error while serving valid response: %s", err.Error())
notAuthenticated(w)
return
}
_, err = w.Write(validResponse)
if err != nil {
panic(err)
}
})
}