@@ -6,6 +6,7 @@ import request from 'supertest';
66
77import { AuthModule , CurrentUser , Public , Session } from '../../core/auth' ;
88import { AuthService } from '../../core/auth/service' ;
9+ import { Models } from '../../models' ;
910import { createTestingApp } from '../utils' ;
1011
1112@Controller ( '/' )
@@ -35,6 +36,8 @@ let server!: any;
3536let auth ! : AuthService ;
3637let u1 ! : CurrentUser ;
3738
39+ let sessionId = '' ;
40+
3841test . before ( async t => {
3942 const { app } = await createTestingApp ( {
4043 imports : [ AuthModule ] ,
@@ -44,13 +47,10 @@ test.before(async t => {
4447 auth = app . get ( AuthService ) ;
4548 u1 = await auth . signUp ( 'u1@affine.pro' , '1' ) ;
4649
47- const db = app . get ( PrismaClient ) ;
48- await db . session . create ( {
49- data : {
50- id : '1' ,
51- } ,
52- } ) ;
53- await auth . createUserSession ( u1 . id , '1' ) ;
50+ const models = app . get ( Models ) ;
51+ const session = await models . session . create ( ) ;
52+ sessionId = session . id ;
53+ await auth . createUserSession ( u1 . id , sessionId ) ;
5454
5555 server = app . getHttpServer ( ) ;
5656 t . context . app = app ;
@@ -69,7 +69,7 @@ test('should be able to visit public api if not signed in', async t => {
6969test ( 'should be able to visit public api if signed in' , async t => {
7070 const res = await request ( server )
7171 . get ( '/public' )
72- . set ( 'Cookie' , `${ AuthService . sessionCookieName } =1 ` )
72+ . set ( 'Cookie' , `${ AuthService . sessionCookieName } =${ sessionId } ` )
7373 . expect ( HttpStatus . OK ) ;
7474
7575 t . is ( res . body . user . id , u1 . id ) ;
@@ -90,7 +90,7 @@ test('should not be able to visit private api if not signed in', async t => {
9090test ( 'should be able to visit private api if signed in' , async t => {
9191 const res = await request ( server )
9292 . get ( '/private' )
93- . set ( 'Cookie' , `${ AuthService . sessionCookieName } =1 ` )
93+ . set ( 'Cookie' , `${ AuthService . sessionCookieName } =${ sessionId } ` )
9494 . expect ( HttpStatus . OK ) ;
9595
9696 t . is ( res . body . user . id , u1 . id ) ;
@@ -100,10 +100,10 @@ test('should be able to parse session cookie', async t => {
100100 const spy = Sinon . spy ( auth , 'getUserSession' ) ;
101101 await request ( server )
102102 . get ( '/public' )
103- . set ( 'cookie' , `${ AuthService . sessionCookieName } =1 ` )
103+ . set ( 'cookie' , `${ AuthService . sessionCookieName } =${ sessionId } ` )
104104 . expect ( 200 ) ;
105105
106- t . deepEqual ( spy . firstCall . args , [ '1' , undefined ] ) ;
106+ t . deepEqual ( spy . firstCall . args , [ sessionId , undefined ] ) ;
107107 spy . restore ( ) ;
108108} ) ;
109109
@@ -112,17 +112,17 @@ test('should be able to parse bearer token', async t => {
112112
113113 await request ( server )
114114 . get ( '/public' )
115- . auth ( '1' , { type : 'bearer' } )
115+ . auth ( sessionId , { type : 'bearer' } )
116116 . expect ( 200 ) ;
117117
118- t . deepEqual ( spy . firstCall . args , [ '1' , undefined ] ) ;
118+ t . deepEqual ( spy . firstCall . args , [ sessionId , undefined ] ) ;
119119 spy . restore ( ) ;
120120} ) ;
121121
122122test ( 'should be able to refresh session if needed' , async t => {
123123 await t . context . app . get ( PrismaClient ) . userSession . updateMany ( {
124124 where : {
125- sessionId : '1' ,
125+ sessionId,
126126 } ,
127127 data : {
128128 expiresAt : new Date ( Date . now ( ) + 1000 * 60 * 60 /* expires in 1 hour */ ) ,
@@ -131,7 +131,7 @@ test('should be able to refresh session if needed', async t => {
131131
132132 const res = await request ( server )
133133 . get ( '/session' )
134- . set ( 'cookie' , `${ AuthService . sessionCookieName } =1 ` )
134+ . set ( 'cookie' , `${ AuthService . sessionCookieName } =${ sessionId } ` )
135135 . expect ( 200 ) ;
136136
137137 const cookie = res
0 commit comments