This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
/
std_headers_test.go
321 lines (287 loc) · 10.3 KB
/
std_headers_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package lti
import (
"testing"
"github.com/goalbook/goalbook-auth/auth/lti/types"
"net/url"
"bytes"
"fmt"
"io/ioutil"
)
var testPrimaryEmail = "mohammads@goalbookapp.com"
var testFullName = "Mo Samman"
var badCtxType = "badCtxType"
var badLTIMessage = "badLTIMessage"
var badLTIVersion = "badLTIVersion"
type LTINonStdHeaders struct {
LTIStdHeaders
NonStandardHeader string
}
func TestParseJsonBody(t *testing.T) {
var err error
var body string
var l *LTIStdHeaders
template := `{
"context_type": "%s",
"lti_message_type": "%s",
"lti_version": "%s",
"roles":"%s,%s",
"lis_person_contact_email_primary":"%s",
"lis_person_name_full": "%s"
}`
/*
Bad context type test
*/
body = fmt.Sprintf(template,
//types.CtxTypeGroup,
badCtxType,
types.LTIBasicMessage,
types.LTIVersion1_0,
types.SysAdmin,
types.InstAdmin,
testPrimaryEmail,
testFullName,
)
l, err = parseJsonBody(bytes.NewReader([]byte(body)))
if err == nil {
t.Errorf("Expected error: Bad context type should trigger error")
}
/*
Bad lti message type test
*/
body = fmt.Sprintf(template,
types.CtxTypeGroup,
badLTIMessage,
types.LTIVersion1_0,
types.SysAdmin,
types.InstAdmin,
testPrimaryEmail,
testFullName,
)
l, err = parseJsonBody(bytes.NewReader([]byte(body)))
if err == nil {
t.Errorf("Expected error: Bad message type should trigger error")
}
/*
Bad lti version type test
*/
body = fmt.Sprintf(template,
types.CtxTypeGroup,
types.LTIBasicMessage,
badLTIVersion,
types.SysAdmin,
types.InstAdmin,
testPrimaryEmail,
testFullName,
)
l, err = parseJsonBody(bytes.NewReader([]byte(body)))
if err == nil {
t.Errorf("Expected error: Bad version type should trigger error")
}
/*
Parse valid body test
*/
body = fmt.Sprintf(template,
types.CtxTypeGroup,
types.LTIBasicMessage,
types.LTIVersion1_0,
types.SysAdmin,
types.InstAdmin,
testPrimaryEmail,
testFullName,
)
l, err = parseJsonBody(bytes.NewReader([]byte(body)))
if err != nil {
t.Errorf("Unexpected error: body parse should have succeeded but got error '%s'", err)
}
if !l.ContextType.HasContextType(types.CtxTypeGroup) {
t.Errorf("Bad marshal: Actual context type %s should be %s", l.ContextType, types.CtxTypeGroup)
}
if l.LTIMessageType != types.LTIMessage(types.LTIBasicMessage) {
t.Errorf("Bad marshal: Actual message type %s should be %s", l.LTIMessageType, types.LTIBasicMessage)
}
if l.LTIVersion != types.LTIVersion(types.LTIVersion1_0) {
t.Errorf("Bad marshal: Actual message type %s should be %s", l.LTIVersion, types.LTIVersion1_0)
}
if l.LISPersonPrimaryEmail != testPrimaryEmail {
t.Errorf("Bad marshal: Actual primary email %s should be %s", l.LISPersonPrimaryEmail, testPrimaryEmail)
}
if l.LISPersonFullName != testFullName {
t.Errorf("Bad marshal: Actual person full name %s should be %s", l.LISPersonFullName, testFullName)
}
if !l.Roles.HasSystemRole(types.SysAdmin) {
t.Errorf("Bad marshal: Of the present roles, %s, missing %s system role", l.Roles.GetSystemRoles(), types.SysAdmin)
}
if !l.Roles.HasInstitutionRole(types.InstAdmin) {
t.Errorf("Bad marshal: Of the present roles, %s, missing %s institution role", l.Roles.GetInstitutionRoles(), types.InstAdmin)
}
if len(l.Roles.GetContextRoles()) != 0 {
t.Errorf("Bad marshal: There should be no context roles present, but", l.Roles.GetContextRoles())
}
if len(l.Roles.GetUndefinedRoles()) != 0 {
t.Errorf("Bad marshal: There should be no undefined roles present, but", l.Roles.GetUndefinedRoles())
}
}
func TestParseUrlEncodedForm(t *testing.T) {
var l *LTIStdHeaders
var err error
vals := url.Values{}
vals.Add(ContextType, badCtxType)
vals.Add(LISPersonPrimaryEmail, testPrimaryEmail)
vals.Add(LISPersonFullName, testFullName)
vals.Add(LTIMessageType, string(types.LTIBasicMessage))
vals.Add(LTIVersion, string(types.LTIVersion1_0))
vals.Add(Roles, fmt.Sprintf("%s,%s", types.SysAdmin, types.InstAdmin))
/*
Bad context type test
*/
l, err = parseUrlEncodedForm(vals)
if err == nil {
t.Logf("(IGNORING) Expected error: Bad context type should trigger error")
}
/*
Bad lti message type test
*/
vals.Del(ContextType)
vals.Add(ContextType, string(types.CtxTypeGroup))
vals.Del(LTIMessageType)
vals.Add(LTIMessageType, badLTIMessage)
l, err = parseUrlEncodedForm(vals)
if err == nil {
t.Logf("(IGNORING) Expected error: Bad LTI message type should trigger error")
}
/*
Bad lti version type test
*/
vals.Del(LTIMessageType)
vals.Add(LTIMessageType, string(types.LTIBasicMessage))
vals.Del(LTIVersion)
vals.Add(LTIVersion, badLTIVersion)
l, err = parseUrlEncodedForm(vals)
if err == nil {
t.Logf("(IGNORING) Expected error: Bad LTI version type should trigger error")
}
/*
Parse valid body test
*/
vals.Del(LTIVersion)
vals.Add(LTIVersion, string(types.LTIVersion1_0))
l, err = parseUrlEncodedForm(vals)
if err != nil {
t.Errorf("Unexpected error: body parse should have succeeded but got error '%s'", err)
}
if !l.ContextType.HasContextType(types.CtxTypeGroup) {
t.Errorf("Bad marshal: Actual context type %s should be %s", l.ContextType, types.CtxTypeGroup)
}
if l.LTIMessageType != types.LTIMessage(types.LTIBasicMessage) {
t.Errorf("Bad marshal: Actual message type %s should be %s", l.LTIMessageType, types.LTIBasicMessage)
}
if l.LTIVersion != types.LTIVersion(types.LTIVersion1_0) {
t.Errorf("Bad marshal: Actual message type %s should be %s", l.LTIVersion, types.LTIVersion1_0)
}
if l.LISPersonPrimaryEmail != testPrimaryEmail {
t.Errorf("Bad marshal: Actual primary email %s should be %s", l.LISPersonPrimaryEmail, testPrimaryEmail)
}
if l.LISPersonFullName != testFullName {
t.Errorf("Bad marshal: Actual person full name %s should be %s", l.LISPersonFullName, testFullName)
}
if !l.Roles.HasSystemRole(types.SysAdmin) {
t.Errorf("Bad marshal: Of the present roles, %s, missing %s system role", l.Roles.GetSystemRoles(), types.SysAdmin)
}
if !l.Roles.HasInstitutionRole(types.InstAdmin) {
t.Errorf("Bad marshal: Of the present roles, %s, missing %s institution role", l.Roles.GetInstitutionRoles(), types.InstAdmin)
}
if len(l.Roles.GetContextRoles()) != 0 {
t.Errorf("Bad marshal: There should be no context roles present, but", l.Roles.GetContextRoles())
}
if len(l.Roles.GetUndefinedRoles()) != 0 {
t.Errorf("Bad marshal: There should be no undefined roles present, but", l.Roles.GetUndefinedRoles())
}
/*
Ignore custom and ext headers for now
*/
vals.Add("custom_lineitems_url", "line_items")
l, err = parseUrlEncodedForm(vals)
if err != nil {
t.Errorf("Unexpected error: body parse should have succeeded but got error '%s'", err)
}
}
func TestSerializeJsonBody(t *testing.T) {
roles := types.NewLTIRoles("")
roles.AddSystemRoles(types.SysAdmin)
roles.AddInstitutionRoles(types.InstAdmin)
ctxType, _ := types.NewLTIContextType(types.CtxTypeGroup)
ltiMsg, _ := types.NewLTIMessage(types.LTIBasicMessage)
ltiVer, _ := types.NewLTIVersion(types.LTIVersion1_0)
l := LTIStdHeaders{
ContextType: ctxType,
LISPersonPrimaryEmail: testPrimaryEmail,
LISPersonFullName: testFullName,
LTIMessageType: ltiMsg,
LTIVersion: ltiVer,
Roles: roles,
}
reader, err := serializeJsonBody(l)
if err != nil {
t.Errorf("Unexpected error: Serializing LTIStdHeaders failed with unexpected error '%s'", err)
}
b, err := ioutil.ReadAll(reader)
if err != nil {
t.Errorf("Unexpected error: Reading serialized LTIStdHeader failed with unexpected error '%s'", err)
}
if !bytes.Contains(b, []byte(`"context_type":"urn:lti:context-type:ims/lis/Group"`)) {
t.Errorf("Bad serialization: Context type serialized incorrectly.")
}
if !bytes.Contains(b, []byte(`"lis_person_name_full":"Mo Samman"`)) {
t.Errorf("Bad serialization: Person name full serialized incorrectly.")
}
if !bytes.Contains(b, []byte(`"lis_person_contact_email_primary":"mohammads@goalbookapp.com"`)) {
t.Errorf("Bad serialization: Person contact email serialized incorrectly.")
}
if !bytes.Contains(b, []byte(`"lti_message_type":"basic-lti-launch-request"`)) {
t.Errorf("Bad serialization: LTI message type serialized incorrectly.")
}
if !bytes.Contains(b, []byte(`"lti_version":"LTI-1p0"`)) {
t.Errorf("Bad serialization: LTI version serialized incorrectly.")
}
if !bytes.Contains(b, []byte(`"roles":"urn:lti:sysrole:ims/lis/Administrator,urn:lti:instrole:ims/lis/Administrator"`)) {
t.Errorf("Bad serialization: Roles serialized incorrectly.")
}
}
func TestSerializeUrlEncodedForm(t *testing.T) {
roles := types.NewLTIRoles("")
roles.AddSystemRoles(types.SysAdmin)
roles.AddInstitutionRoles(types.InstAdmin)
ctxType, _ := types.NewLTIContextType(types.CtxTypeGroup)
ltiMsg, _ := types.NewLTIMessage(types.LTIBasicMessage)
ltiVer, _ := types.NewLTIVersion(types.LTIVersion1_0)
l := LTIStdHeaders{
ContextType: ctxType,
LISPersonPrimaryEmail: testPrimaryEmail,
LISPersonFullName: testFullName,
LTIMessageType: ltiMsg,
LTIVersion: ltiVer,
Roles: roles,
}
vals, err := serializeUrlEncodedForm(l)
if err != nil {
t.Errorf("Unexpected error: Serializing LTIStdHeaders failed with unexpected error '%s'", err)
}
if vals.Get(ContextType) != string(types.CtxTypeGroup) {
t.Errorf("Bad serialization: Context type serialized as '%s' instead of '%s'.", vals.Get(ContextType), types.CtxTypeGroup)
}
if vals.Get(LISPersonFullName) != testFullName {
t.Errorf("Bad serialization: Person name full serialized as '%s' instead of '%s'.", vals.Get(LISPersonFullName), testFullName)
}
if vals.Get(LISPersonPrimaryEmail) != testPrimaryEmail {
t.Errorf("Bad serialization: Person contact email serialized as '%s' instead of '%s'.", vals.Get(LISPersonPrimaryEmail), testPrimaryEmail)
}
if vals.Get(LTIMessageType) != string(types.LTIBasicMessage) {
t.Errorf("Bad serialization: LTI message type serialized as '%s' instead of '%s'.", vals.Get(LTIMessageType), types.LTIBasicMessage)
}
if vals.Get(LTIVersion) != string(types.LTIVersion1_0) {
t.Errorf("Bad serialization: LTI version serialized as '%s' instead of '%s'.", vals.Get(LTIVersion), types.LTIVersion1_0)
}
if vals.Get(Roles) != "urn:lti:sysrole:ims/lis/Administrator,urn:lti:instrole:ims/lis/Administrator" {
t.Errorf("Bad serialization: Roles serialized as '%s' instead of '%s'.", vals.Get(Roles), fmt.Sprintf("%s,%s", types.SysAdmin, types.InstAdmin))
}
}