@@ -20,7 +20,7 @@ import * as request from "supertest";
20
20
import * as chai from "chai" ;
21
21
import { OIDCCreateSessionPayload } from "./iam-oidc-create-session-payload" ;
22
22
import { BUILTIN_INSTLLATION_ADMIN_USER_ID , TeamDB } from "@gitpod/gitpod-db/lib" ;
23
- import { TeamMemberInfo } from "@gitpod/gitpod-protocol" ;
23
+ import { TeamMemberInfo , User } from "@gitpod/gitpod-protocol" ;
24
24
const expect = chai . expect ;
25
25
26
26
@suite ( timeout ( 10000 ) )
@@ -33,20 +33,25 @@ class TestIamSessionApp {
33
33
protected knownSubjectID = "111" ;
34
34
protected knownEmail = "[email protected] " ;
35
35
36
+ protected knownUser : Partial < User > = {
37
+ id : "id-known-user" ,
38
+ identities : [ ] ,
39
+ } ;
40
+
36
41
protected userServiceMock : Partial < UserService > = {
37
42
createUser : ( params ) => {
38
43
return { id : "id-new-user" } as any ;
39
44
} ,
40
45
41
46
findUserForLogin : async ( params ) => {
42
47
if ( params . candidate ?. authId === this . knownSubjectID ) {
43
- return { id : "id-known-user" } as any ;
48
+ return this . knownUser as any ;
44
49
}
45
50
return undefined ;
46
51
} ,
47
52
findOrgOwnedUser : async ( params ) => {
48
53
if ( params . email === this . knownEmail ) {
49
- return { id : "id-known-user" } as any ;
54
+ return this . knownUser as any ;
50
55
}
51
56
return undefined ;
52
57
} ,
@@ -88,6 +93,7 @@ class TestIamSessionApp {
88
93
89
94
public before ( ) {
90
95
this . teamDbMock . memberships . clear ( ) ;
96
+ this . knownUser . identities = [ ] ;
91
97
92
98
const container = new Container ( ) ;
93
99
container . load (
@@ -231,6 +237,58 @@ class TestIamSessionApp {
231
237
expect ( this . teamDbMock . memberships . has ( BUILTIN_INSTLLATION_ADMIN_USER_ID ) ) . to . be . false ;
232
238
expect ( this . teamDbMock . memberships . has ( "id-new-user" ) ) . to . be . true ;
233
239
}
240
+
241
+ @test public async testSessionRequest_updates_existing_user ( ) {
242
+ const payload : OIDCCreateSessionPayload = { ...this . payload } ;
243
+ payload . claims . sub = this . knownSubjectID ; // `userServiceMock.findUserForLogin` will match this value
244
+
245
+ this . knownUser . identities = [
246
+ {
247
+ authId : payload . claims . sub ,
248
+ authProviderId : payload . claims . aud ,
249
+ authName : "Test User" ,
250
+ primaryEmail :
"[email protected] " ,
251
+ } ,
252
+ ] ;
253
+
254
+ let newEmail : string | undefined ;
255
+ this . userServiceMock . updateUserIdentity = async ( user , updatedIdentity ) => {
256
+ newEmail = updatedIdentity . primaryEmail ;
257
+ } ;
258
+
259
+ const result = await request ( this . app . create ( ) )
260
+ . post ( "/session" )
261
+ . set ( "Content-Type" , "application/json" )
262
+ . send ( JSON . stringify ( payload ) ) ;
263
+
264
+ expect ( result . statusCode , JSON . stringify ( result . body ) ) . to . equal ( 200 ) ;
265
+ expect ( newEmail , "update was not called" ) . not . to . be . undefined ;
266
+ expect ( newEmail ) . to . equal ( payload . claims . email ) ;
267
+ }
268
+
269
+ @test public async testSessionRequest_no_update_if_same_email ( ) {
270
+ this . knownUser . identities = [
271
+ {
272
+ authId : this . payload . claims . sub ,
273
+ authProviderId : this . payload . claims . aud ,
274
+ authName : "Test User" ,
275
+ primaryEmail : this . payload . claims . email ,
276
+ } ,
277
+ ] ;
278
+
279
+ let updateUserIdentityCalled = false ;
280
+ this . userServiceMock . updateUserIdentity = async ( ) => {
281
+ updateUserIdentityCalled = true ;
282
+ } ;
283
+
284
+ const result = await request ( this . app . create ( ) )
285
+ . post ( "/session" )
286
+ . set ( "Content-Type" , "application/json" )
287
+ . send ( JSON . stringify ( this . payload ) ) ;
288
+
289
+ expect ( result . statusCode , JSON . stringify ( result . body ) ) . to . equal ( 200 ) ;
290
+ expect ( updateUserIdentityCalled ) . to . be . false ;
291
+ }
234
292
}
235
293
236
294
module . exports = new TestIamSessionApp ( ) ;
0 commit comments