@@ -18,6 +18,7 @@ import * as session from "express-session";
18
18
import * as request from "supertest" ;
19
19
20
20
import * as chai from "chai" ;
21
+ import { OIDCCreateSessionPayload } from "./iam-oidc-create-session-payload" ;
21
22
const expect = chai . expect ;
22
23
23
24
@suite ( timeout ( 10000 ) )
@@ -27,6 +28,38 @@ class TestIamSessionApp {
27
28
28
29
protected cookieName = "test-session-name" ;
29
30
31
+ protected knownSub = "111" ;
32
+
33
+ protected userServiceMock : Partial < UserService > = {
34
+ createUser : ( params ) => {
35
+ return { id : "id-new-user" } as any ;
36
+ } ,
37
+
38
+ findUserForLogin : ( params ) => {
39
+ if ( params . candidate ?. authId === this . knownSub ) {
40
+ return { id : "id-known-user" } as any ;
41
+ }
42
+ return undefined ;
43
+ } ,
44
+ } ;
45
+
46
+ protected payload : OIDCCreateSessionPayload = {
47
+ idToken : { } as any ,
48
+ claims : {
49
+ aud : "1234" ,
50
+
51
+ email_verified : true ,
52
+ family_name : "User" ,
53
+ given_name : "Test" ,
54
+ iss : "https://accounts.get.net" ,
55
+ locale : "de" ,
56
+ name : "Test User" ,
57
+ picture : "https://cdn.get.net/users/abc23" ,
58
+ sub : "1234567890" ,
59
+ hd : "test.net" ,
60
+ } ,
61
+ } ;
62
+
30
63
public before ( ) {
31
64
const container = new Container ( ) ;
32
65
container . load (
@@ -35,11 +68,7 @@ class TestIamSessionApp {
35
68
bind ( IamSessionApp ) . toSelf ( ) . inSingletonScope ( ) ;
36
69
bind ( Authenticator ) . toConstantValue ( < any > { } ) ; // unused
37
70
bind ( Config ) . toConstantValue ( < any > { } ) ; // unused
38
- bind ( UserService ) . toConstantValue ( < any > {
39
- createUser : ( ) => ( {
40
- id : "C0FFEE" ,
41
- } ) ,
42
- } ) ;
71
+ bind ( UserService ) . toConstantValue ( this . userServiceMock as any ) ;
43
72
} ) ,
44
73
) ;
45
74
this . app = container . get ( IamSessionApp ) ;
@@ -72,22 +101,56 @@ class TestIamSessionApp {
72
101
await request ( this . app . create ( ) )
73
102
. post ( "/session" )
74
103
. set ( "Content-Type" , "application/json" )
75
- . send ( JSON . stringify ( this . idToken ) ) ;
104
+ . send ( JSON . stringify ( this . payload ) ) ;
76
105
77
106
expect ( count , "sessions added" ) . to . equal ( 1 ) ;
78
107
}
79
108
80
- @test public async testSessionRequestResponsesWithSetCookie ( ) {
109
+ @test public async testSessionRequestResponsesWithSetCookie_createUser ( ) {
81
110
const result = await request ( this . app . create ( ) )
82
111
. post ( "/session" )
83
112
. set ( "Content-Type" , "application/json" )
84
- . send ( JSON . stringify ( this . idToken ) ) ;
113
+ . send ( JSON . stringify ( this . payload ) ) ;
85
114
86
- expect ( result . statusCode ) . to . equal ( 200 ) ;
115
+ expect ( result . statusCode , JSON . stringify ( result . body ) ) . to . equal ( 200 ) ;
116
+ expect ( result . body ?. userId ) . to . equal ( "id-new-user" ) ;
87
117
expect ( JSON . stringify ( result . get ( "Set-Cookie" ) ) ) . to . contain ( this . cookieName ) ;
88
118
}
89
119
90
- idToken = { } ;
120
+ @test public async testSessionRequestResponsesWithSetCookie_knownUser ( ) {
121
+ const payload = { ...this . payload } ;
122
+ payload . claims . sub = this . knownSub ;
123
+ const result = await request ( this . app . create ( ) )
124
+ . post ( "/session" )
125
+ . set ( "Content-Type" , "application/json" )
126
+ . send ( JSON . stringify ( payload ) ) ;
127
+
128
+ expect ( result . statusCode , JSON . stringify ( result . body ) ) . to . equal ( 200 ) ;
129
+ expect ( result . body ?. userId ) . to . equal ( "id-known-user" ) ;
130
+ expect ( JSON . stringify ( result . get ( "Set-Cookie" ) ) ) . to . contain ( this . cookieName ) ;
131
+ }
132
+
133
+ @test public async testInvalidPayload ( ) {
134
+ const cases = [
135
+ { claims : { sub : "" } , expectedMessage : "Subject is missing" } ,
136
+ { claims : { iss : "" } , expectedMessage : "Issuer is missing" } ,
137
+ { claims : { email : "" } , expectedMessage : "Email is missing" } ,
138
+ { claims : { name : "" } , expectedMessage : "Name is missing" } ,
139
+ ] ;
140
+ for ( const c of cases ) {
141
+ const payload = { ...this . payload } ;
142
+ payload . claims = { ...payload . claims , ...c . claims } ;
143
+
144
+ const sr = request ( this . app . create ( ) ) ;
145
+ const result = await sr
146
+ . post ( "/session" )
147
+ . set ( "Content-Type" , "application/json" )
148
+ . send ( JSON . stringify ( payload ) ) ;
149
+
150
+ expect ( result . statusCode , JSON . stringify ( result . body ) ) . to . equal ( 400 ) ;
151
+ expect ( result . body ?. message ) . to . equal ( c . expectedMessage ) ;
152
+ }
153
+ }
91
154
}
92
155
93
156
module . exports = new TestIamSessionApp ( ) ;
0 commit comments