@@ -161,12 +161,155 @@ test('should be able to sign out', async t => {
161161 t . falsy ( session . user ) ;
162162} ) ;
163163
164- test ( 'should not be able to sign out if not signed in' , async t => {
165- const { app } = t . context ;
164+ test ( 'should be able to correct user id cookie' , async t => {
165+ const { app, u1 } = t . context ;
166+
167+ const signInRes = await request ( app . getHttpServer ( ) )
168+ . post ( '/api/auth/sign-in' )
169+ . send ( { email : u1 . email , password : '1' } )
170+ . expect ( 200 ) ;
171+
172+ const cookie = sessionCookie ( signInRes . headers ) ;
173+
174+ let session = await request ( app . getHttpServer ( ) )
175+ . get ( '/api/auth/session' )
176+ . set ( 'cookie' , cookie )
177+ . expect ( 200 ) ;
178+
179+ let userIdCookie = session . get ( 'Set-Cookie' ) ?. find ( c => {
180+ return c . startsWith ( `${ AuthService . userCookieName } =` ) ;
181+ } ) ;
182+
183+ t . true ( userIdCookie ?. startsWith ( `${ AuthService . userCookieName } =${ u1 . id } ` ) ) ;
184+
185+ session = await request ( app . getHttpServer ( ) )
186+ . get ( '/api/auth/session' )
187+ . set ( 'cookie' , `${ cookie } ;${ AuthService . userCookieName } =invalid_user_id` )
188+ . expect ( 200 ) ;
189+
190+ userIdCookie = session . get ( 'Set-Cookie' ) ?. find ( c => {
191+ return c . startsWith ( `${ AuthService . userCookieName } =` ) ;
192+ } ) ;
193+
194+ t . true ( userIdCookie ?. startsWith ( `${ AuthService . userCookieName } =${ u1 . id } ` ) ) ;
195+ t . is ( session . body . user . id , u1 . id ) ;
196+ } ) ;
197+
198+ // multiple accounts session tests
199+ test ( 'should be able to sign in another account in one session' , async t => {
200+ const { app, u1, auth } = t . context ;
201+
202+ const u2 = await auth . signUp ( 'u3@affine.pro' , '3' ) ;
203+
204+ // sign in u1
205+ const signInRes = await request ( app . getHttpServer ( ) )
206+ . post ( '/api/auth/sign-in' )
207+ . send ( { email : u1 . email , password : '1' } )
208+ . expect ( 200 ) ;
209+
210+ const cookie = sessionCookie ( signInRes . headers ) ;
211+
212+ // avoid create session at the exact same time, leads to same random session users order
213+ await new Promise ( resolve => setTimeout ( resolve , 1 ) ) ;
214+
215+ // sign in u2 in the same session
216+ await request ( app . getHttpServer ( ) )
217+ . post ( '/api/auth/sign-in' )
218+ . set ( 'cookie' , cookie )
219+ . send ( { email : u2 . email , password : '3' } )
220+ . expect ( 200 ) ;
221+
222+ // list [u1, u2]
223+ const sessions = await request ( app . getHttpServer ( ) )
224+ . get ( '/api/auth/sessions' )
225+ . set ( 'cookie' , cookie )
226+ . expect ( 200 ) ;
166227
228+ t . is ( sessions . body . users . length , 2 ) ;
229+ t . is ( sessions . body . users [ 0 ] . id , u1 . id ) ;
230+ t . is ( sessions . body . users [ 1 ] . id , u2 . id ) ;
231+
232+ // default to latest signed in user: u2
233+ let session = await request ( app . getHttpServer ( ) )
234+ . get ( '/api/auth/session' )
235+ . set ( 'cookie' , cookie )
236+ . expect ( 200 ) ;
237+
238+ t . is ( session . body . user . id , u2 . id ) ;
239+
240+ // switch to u1
241+ session = await request ( app . getHttpServer ( ) )
242+ . get ( '/api/auth/session' )
243+ . set ( 'cookie' , `${ cookie } ;${ AuthService . userCookieName } =${ u1 . id } ` )
244+ . expect ( 200 ) ;
245+
246+ t . is ( session . body . user . id , u1 . id ) ;
247+ } ) ;
248+
249+ test ( 'should be able to sign out multiple accounts in one session' , async t => {
250+ const { app, u1, auth } = t . context ;
251+
252+ const u2 = await auth . signUp ( 'u4@affine.pro' , '4' ) ;
253+
254+ // sign in u1
255+ const signInRes = await request ( app . getHttpServer ( ) )
256+ . post ( '/api/auth/sign-in' )
257+ . send ( { email : u1 . email , password : '1' } )
258+ . expect ( 200 ) ;
259+
260+ const cookie = sessionCookie ( signInRes . headers ) ;
261+
262+ await new Promise ( resolve => setTimeout ( resolve , 1 ) ) ;
263+
264+ // sign in u2 in the same session
167265 await request ( app . getHttpServer ( ) )
266+ . post ( '/api/auth/sign-in' )
267+ . set ( 'cookie' , cookie )
268+ . send ( { email : u2 . email , password : '4' } )
269+ . expect ( 200 ) ;
270+
271+ // sign out u2
272+ let signOut = await request ( app . getHttpServer ( ) )
273+ . get ( `/api/auth/sign-out?user_id=${ u2 . id } ` )
274+ . set ( 'cookie' , `${ cookie } ;${ AuthService . userCookieName } =${ u2 . id } ` )
275+ . expect ( 200 ) ;
276+
277+ // auto switch to u1 after sign out u2
278+ const userIdCookie = signOut . get ( 'Set-Cookie' ) ?. find ( c => {
279+ return c . startsWith ( `${ AuthService . userCookieName } =` ) ;
280+ } ) ;
281+
282+ t . true ( userIdCookie ?. startsWith ( `${ AuthService . userCookieName } =${ u1 . id } ` ) ) ;
283+
284+ // list [u1]
285+ const session = await request ( app . getHttpServer ( ) )
286+ . get ( '/api/auth/session' )
287+ . set ( 'cookie' , cookie )
288+ . expect ( 200 ) ;
289+
290+ t . is ( session . body . user . id , u1 . id ) ;
291+
292+ // sign in u2 in the same session
293+ await request ( app . getHttpServer ( ) )
294+ . post ( '/api/auth/sign-in' )
295+ . set ( 'cookie' , cookie )
296+ . send ( { email : u2 . email , password : '4' } )
297+ . expect ( 200 ) ;
298+
299+ // sign out all account in session
300+ signOut = await request ( app . getHttpServer ( ) )
168301 . get ( '/api/auth/sign-out' )
169- . expect ( HttpStatus . UNAUTHORIZED ) ;
302+ . set ( 'cookie' , cookie )
303+ . expect ( 200 ) ;
170304
171- t . assert ( true ) ;
305+ t . true (
306+ signOut
307+ . get ( 'Set-Cookie' )
308+ ?. some ( c => c . startsWith ( `${ AuthService . sessionCookieName } =;` ) )
309+ ) ;
310+ t . true (
311+ signOut
312+ . get ( 'Set-Cookie' )
313+ ?. some ( c => c . startsWith ( `${ AuthService . userCookieName } =;` ) )
314+ ) ;
172315} ) ;
0 commit comments