Skip to content

Commit 073c776

Browse files
Simplify unit test mock struct
1 parent 3abcd89 commit 073c776

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

launch/launch_test.go

+38-45
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,39 @@ var (
4141
testPlatformInstanceID = uuid.MustParse("52166f98-f932-4ccf-ae71-e0ae10255e4f")
4242
testRegistrationID = uuid.MustParse("7b556115-9460-4f1e-835e-cb11a7301f7d")
4343
testLaunchWithDeploymentID = uuid.MustParse("65ec0a8c-48e2-423b-b6e0-d1143292d550")
44-
testStoreSvc *mockStoreSvc
44+
testJwkKey jwk.Key
4545
testSrvUrl string
4646
)
4747

4848
func TestMain(m *testing.M) {
4949
// Setup a mock JWK keyset and server
5050
key, _ := jwk.FromRaw([]byte(testJWTSecret))
51-
err := key.Set("kid", "testkey")
51+
err := key.Set(jwk.KeyIDKey, "testkey")
5252
if err != nil {
5353
panic(err)
5454
}
55-
err = key.Set("alg", "HS256")
55+
err = key.Set(jwk.AlgorithmKey, "HS256")
5656
if err != nil {
5757
panic(err)
5858
}
59+
keys := make([]jwk.Key, 0)
60+
61+
k, err := key.PublicKey()
62+
if err != nil {
63+
panic(err)
64+
}
65+
keys = append(keys, k)
66+
type jwkResponse struct {
67+
Keys []jwk.Key `json:"keys"`
68+
}
5969

6070
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61-
if r.URL.String() != "/canvaslms/api/lti/security/jwks" {
71+
if r.URL.String() != canvasTestJWKURL {
6272
w.WriteHeader(http.StatusInternalServerError)
6373
return
6474
}
65-
type response struct {
66-
Keys []jwk.Key `json:"keys"`
67-
}
68-
keys := make([]jwk.Key, 0)
6975

70-
k, err := key.PublicKey()
71-
if err != nil {
72-
panic(err)
73-
}
74-
keys = append(keys, k)
75-
resp := response{
76+
resp := jwkResponse{
7677
Keys: keys,
7778
}
7879
ks, _ := json.Marshal(resp)
@@ -82,13 +83,9 @@ func TestMain(m *testing.M) {
8283
panic(err)
8384
}
8485
}))
86+
defer srv.Close()
8587

86-
testStoreSvc = &mockStoreSvc{
87-
idTokenKey: key,
88-
jwkServerURL: srv.URL,
89-
server: srv,
90-
}
91-
88+
testJwkKey = key
9289
testSrvUrl = srv.URL
9390
happyPathPlatform = peregrine.Platform{
9491
ID: testPlatformID,
@@ -107,7 +104,7 @@ func TestHandleOidcLoginHappyPath(t *testing.T) {
107104
launchSvc := New(Config{
108105
JWTKeySecret: testJWTSecret,
109106
Issuer: testIssuer,
110-
}, testStoreSvc)
107+
}, &mockStoreSvc{})
111108

112109
resp, err := launchSvc.HandleOidcLogin(context.Background(), peregrine.OIDCLoginRequestParams{
113110
Issuer: canvasTestIssuer,
@@ -157,7 +154,7 @@ func TestHandleOidcLoginHappyPathWithLTIMessageHint(t *testing.T) {
157154
launchSvc := New(Config{
158155
JWTKeySecret: testJWTSecret,
159156
Issuer: testIssuer,
160-
}, testStoreSvc)
157+
}, &mockStoreSvc{})
161158

162159
resp, err := launchSvc.HandleOidcLogin(context.Background(), peregrine.OIDCLoginRequestParams{
163160
Issuer: canvasTestIssuer,
@@ -207,7 +204,7 @@ func TestHandleOidcLoginHappyPathWithDeploymentID(t *testing.T) {
207204
launchSvc := New(Config{
208205
JWTKeySecret: testJWTSecret,
209206
Issuer: testIssuer,
210-
}, testStoreSvc)
207+
}, &mockStoreSvc{})
211208

212209
resp, err := launchSvc.HandleOidcLogin(context.Background(), peregrine.OIDCLoginRequestParams{
213210
Issuer: canvasTestIssuer,
@@ -257,7 +254,7 @@ func TestHandleOidcLoginInvalidParams(t *testing.T) {
257254
launchSvc := New(Config{
258255
JWTKeySecret: testJWTSecret,
259256
Issuer: testIssuer,
260-
}, testStoreSvc)
257+
}, &mockStoreSvc{})
261258

262259
_, err := launchSvc.HandleOidcLogin(context.Background(), peregrine.OIDCLoginRequestParams{
263260
Issuer: canvasTestIssuer,
@@ -297,7 +294,7 @@ func TestHandleOidcLoginIncorrectIssuer(t *testing.T) {
297294
launchSvc := New(Config{
298295
JWTKeySecret: testJWTSecret,
299296
Issuer: testIssuer,
300-
}, testStoreSvc)
297+
}, &mockStoreSvc{})
301298

302299
_, err := launchSvc.HandleOidcLogin(context.Background(), peregrine.OIDCLoginRequestParams{
303300
Issuer: "https://canvas.instructure.com",
@@ -357,7 +354,7 @@ func TestHandleOidcCallbackHappyPath(t *testing.T) {
357354
launchSvc := New(Config{
358355
JWTKeySecret: testJWTSecret,
359356
Issuer: testIssuer,
360-
}, testStoreSvc)
357+
}, &mockStoreSvc{})
361358

362359
state, err := createLaunchState(launchSvc.config.Issuer, launchSvc.config.JWTKeySecret, testLaunchID)
363360
if err != nil {
@@ -380,7 +377,7 @@ func TestHandleOidcCallbackHappyPath(t *testing.T) {
380377
if err != nil {
381378
panic(err)
382379
}
383-
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testStoreSvc.idTokenKey))
380+
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testJwkKey))
384381
if err != nil {
385382
panic(err)
386383
}
@@ -403,7 +400,7 @@ func TestHandleOidcCallbackHappyPathWithLaunchDeploymentID(t *testing.T) {
403400
launchSvc := New(Config{
404401
JWTKeySecret: testJWTSecret,
405402
Issuer: testIssuer,
406-
}, testStoreSvc)
403+
}, &mockStoreSvc{})
407404

408405
state, err := createLaunchState(launchSvc.config.Issuer, launchSvc.config.JWTKeySecret, testLaunchWithDeploymentID)
409406
if err != nil {
@@ -426,7 +423,7 @@ func TestHandleOidcCallbackHappyPathWithLaunchDeploymentID(t *testing.T) {
426423
if err != nil {
427424
panic(err)
428425
}
429-
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testStoreSvc.idTokenKey))
426+
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testJwkKey))
430427
if err != nil {
431428
panic(err)
432429
}
@@ -449,7 +446,7 @@ func TestHandleOidcCallbackHappyPathWithPlatformInstanceID(t *testing.T) {
449446
launchSvc := New(Config{
450447
JWTKeySecret: testJWTSecret,
451448
Issuer: testIssuer,
452-
}, testStoreSvc)
449+
}, &mockStoreSvc{})
453450

454451
state, err := createLaunchState(launchSvc.config.Issuer, launchSvc.config.JWTKeySecret, testLaunchID)
455452
if err != nil {
@@ -475,7 +472,7 @@ func TestHandleOidcCallbackHappyPathWithPlatformInstanceID(t *testing.T) {
475472
if err != nil {
476473
panic(err)
477474
}
478-
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testStoreSvc.idTokenKey))
475+
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testJwkKey))
479476
if err != nil {
480477
panic(err)
481478
}
@@ -498,7 +495,7 @@ func TestHandleOidcCallbackInvalidState(t *testing.T) {
498495
launchSvc := New(Config{
499496
JWTKeySecret: testJWTSecret,
500497
Issuer: testIssuer,
501-
}, testStoreSvc)
498+
}, &mockStoreSvc{})
502499

503500
_, err := launchSvc.HandleOidcCallback(context.Background(), peregrine.OIDCAuthenticationResponse{
504501
State: "",
@@ -514,7 +511,7 @@ func TestHandleOidcCallbackInvalidIDToken(t *testing.T) {
514511
launchSvc := New(Config{
515512
JWTKeySecret: testJWTSecret,
516513
Issuer: testIssuer,
517-
}, testStoreSvc)
514+
}, &mockStoreSvc{})
518515

519516
state, err := createLaunchState(launchSvc.config.Issuer, launchSvc.config.JWTKeySecret, testLaunchID)
520517
if err != nil {
@@ -535,7 +532,7 @@ func TestHandleOidcCallbackLaunchIDNotFound(t *testing.T) {
535532
launchSvc := New(Config{
536533
JWTKeySecret: testJWTSecret,
537534
Issuer: testIssuer,
538-
}, testStoreSvc)
535+
}, &mockStoreSvc{})
539536

540537
state, err := createLaunchState(launchSvc.config.Issuer, launchSvc.config.JWTKeySecret, testDeploymentID)
541538
if err != nil {
@@ -558,7 +555,7 @@ func TestHandleOidcCallbackLaunchIDNotFound(t *testing.T) {
558555
if err != nil {
559556
panic(err)
560557
}
561-
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testStoreSvc.idTokenKey))
558+
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testJwkKey))
562559
if err != nil {
563560
panic(err)
564561
}
@@ -601,7 +598,7 @@ func TestHandleOidcCallbackDeploymentUpsertFailure(t *testing.T) {
601598
if err != nil {
602599
panic(err)
603600
}
604-
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testStoreSvc.idTokenKey))
601+
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testJwkKey))
605602
if err != nil {
606603
panic(err)
607604
}
@@ -643,7 +640,7 @@ func TestHandleOidcCallbackWithUpdateLaunchFailure(t *testing.T) {
643640
if err != nil {
644641
panic(err)
645642
}
646-
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testStoreSvc.idTokenKey))
643+
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testJwkKey))
647644
if err != nil {
648645
panic(err)
649646
}
@@ -689,7 +686,7 @@ func TestHandleOidcCallbackWithUpsertPlatformInstanceFailure(t *testing.T) {
689686
if err != nil {
690687
panic(err)
691688
}
692-
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testStoreSvc.idTokenKey))
689+
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testJwkKey))
693690
if err != nil {
694691
panic(err)
695692
}
@@ -709,7 +706,7 @@ func TestHandleOidcCallbackInvalidSubjectClaim(t *testing.T) {
709706
launchSvc := New(Config{
710707
JWTKeySecret: testJWTSecret,
711708
Issuer: testIssuer,
712-
}, testStoreSvc)
709+
}, &mockStoreSvc{})
713710

714711
state, err := createLaunchState(launchSvc.config.Issuer, launchSvc.config.JWTKeySecret, testLaunchID)
715712
if err != nil {
@@ -734,7 +731,7 @@ func TestHandleOidcCallbackInvalidSubjectClaim(t *testing.T) {
734731
if err != nil {
735732
panic(err)
736733
}
737-
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testStoreSvc.idTokenKey))
734+
signedIdToken, err := jwt.Sign(tok, jwt.WithKey(jwa.HS256, testJwkKey))
738735
if err != nil {
739736
panic(err)
740737
}
@@ -749,11 +746,7 @@ func TestHandleOidcCallbackInvalidSubjectClaim(t *testing.T) {
749746
}
750747

751748
// mockStoreSvc mocks the data store service dependency
752-
type mockStoreSvc struct {
753-
idTokenKey jwk.Key
754-
jwkServerURL string
755-
server *httptest.Server
756-
}
749+
type mockStoreSvc struct{}
757750

758751
// UpsertPlatformInstanceByGUID should create a PlatformInstance if not existing returning PlatformInstance with ID
759752
func (s *mockStoreSvc) UpsertPlatformInstanceByGUID(ctx context.Context, instance peregrine.PlatformInstance) (peregrine.PlatformInstance, error) {

launch/utils_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func TestGetPlatformJWKs(t *testing.T) {
1818
t.Parallel()
1919
c := jwk.NewCache(context.Background())
20-
keySet, err := getPlatformJWKs(context.Background(), c, testSrvUrl+"/canvaslms/api/lti/security/jwks")
20+
keySet, err := getPlatformJWKs(context.Background(), c, testSrvUrl+canvasTestJWKURL)
2121
if err != nil {
2222
t.Fatalf("unexpected error: %v", err)
2323
}

0 commit comments

Comments
 (0)