1
1
import * as jwtDecode from 'jwt-decode' ;
2
- import { AccountsServer } from '../src/accounts-server' ;
2
+ import { AccountsServer , JwtData } from '../src/accounts-server' ;
3
3
import {
4
4
bcryptPassword ,
5
5
hashPassword ,
@@ -100,10 +100,9 @@ describe('AccountsServer', () => {
100
100
) ;
101
101
102
102
const res = await accountsServer . loginWithUser ( user , { } ) ;
103
- expect ( res . user ) . toEqual ( user ) ;
104
103
const { accessToken, refreshToken } = res . tokens ;
105
- const decodedAccessToken : any = jwtDecode ( accessToken ) ;
106
- expect ( decodedAccessToken . data . sessionId ) . toEqual ( 'sessionId' ) ;
104
+ const decodedAccessToken : { data : JwtData } = jwtDecode ( accessToken ) ;
105
+ expect ( decodedAccessToken . data . token ) . toBeTruthy ( ) ;
107
106
expect ( accessToken ) . toBeTruthy ( ) ;
108
107
expect ( refreshToken ) . toBeTruthy ( ) ;
109
108
} ) ;
@@ -114,9 +113,9 @@ describe('AccountsServer', () => {
114
113
const accountsServer = new AccountsServer (
115
114
{
116
115
db : {
117
- findSessionById : ( ) =>
116
+ findSessionByToken : ( ) =>
118
117
Promise . resolve ( {
119
- sessionId : '456' ,
118
+ id : '456' ,
120
119
valid : true ,
121
120
userId : '123' ,
122
121
} ) ,
@@ -146,9 +145,9 @@ describe('AccountsServer', () => {
146
145
const accountsServer = new AccountsServer (
147
146
{
148
147
db : {
149
- findSessionById : ( ) =>
148
+ findSessionByToken : ( ) =>
150
149
Promise . resolve ( {
151
- sessionId : '456' ,
150
+ id : '456' ,
152
151
valid : true ,
153
152
userId : '123' ,
154
153
} ) ,
@@ -218,9 +217,9 @@ describe('AccountsServer', () => {
218
217
const accountsServer = new AccountsServer (
219
218
{
220
219
db : {
221
- findSessionById : ( ) =>
220
+ findSessionByToken : ( ) =>
222
221
Promise . resolve ( {
223
- sessionId : '456' ,
222
+ id : '456' ,
224
223
valid : true ,
225
224
userId : '123' ,
226
225
} ) ,
@@ -243,9 +242,9 @@ describe('AccountsServer', () => {
243
242
const accountsServer = new AccountsServer (
244
243
{
245
244
db : {
246
- findSessionById : ( ) =>
245
+ findSessionByToken : ( ) =>
247
246
Promise . resolve ( {
248
- sessionId : '456' ,
247
+ id : '456' ,
249
248
valid : true ,
250
249
userId : '123' ,
251
250
} ) ,
@@ -276,9 +275,9 @@ describe('AccountsServer', () => {
276
275
const accountsServer = new AccountsServer (
277
276
{
278
277
db : {
279
- findSessionById : ( ) =>
278
+ findSessionByToken : ( ) =>
280
279
Promise . resolve ( {
281
- sessionId : '456' ,
280
+ id : '456' ,
282
281
valid : true ,
283
282
userId : '123' ,
284
283
} ) ,
@@ -306,9 +305,9 @@ describe('AccountsServer', () => {
306
305
const accountsServer = new AccountsServer (
307
306
{
308
307
db : {
309
- findSessionById : ( ) =>
308
+ findSessionByToken : ( ) =>
310
309
Promise . resolve ( {
311
- sessionId : '456' ,
310
+ id : '456' ,
312
311
valid : false ,
313
312
userId : '123' ,
314
313
} ) ,
@@ -341,7 +340,7 @@ describe('AccountsServer', () => {
341
340
const accountsServer = new AccountsServer (
342
341
{
343
342
db : {
344
- findSessionById : ( ) => Promise . reject ( '' ) ,
343
+ findSessionByToken : ( ) => Promise . reject ( '' ) ,
345
344
findUserById : ( ) => Promise . resolve ( user ) ,
346
345
} as any ,
347
346
tokenSecret : 'secret' ,
@@ -367,7 +366,7 @@ describe('AccountsServer', () => {
367
366
const accountsServer = new AccountsServer (
368
367
{
369
368
db : {
370
- findSessionById : ( ) =>
369
+ findSessionByToken : ( ) =>
371
370
Promise . resolve ( {
372
371
valid : false ,
373
372
} ) ,
@@ -402,9 +401,9 @@ describe('AccountsServer', () => {
402
401
const accountsServer = new AccountsServer (
403
402
{
404
403
db : {
405
- findSessionById : ( ) =>
404
+ findSessionByToken : ( ) =>
406
405
Promise . resolve ( {
407
- sessionId : '456' ,
406
+ id : '456' ,
408
407
valid : true ,
409
408
userId : '123' ,
410
409
} ) ,
@@ -504,9 +503,9 @@ describe('AccountsServer', () => {
504
503
const accountsServer = new AccountsServer (
505
504
{
506
505
db : {
507
- findSessionById : ( ) =>
506
+ findSessionByToken : ( ) =>
508
507
Promise . resolve ( {
509
- sessionId : '456' ,
508
+ id : '456' ,
510
509
valid : true ,
511
510
userId : '123' ,
512
511
} ) ,
@@ -528,7 +527,10 @@ describe('AccountsServer', () => {
528
527
'ip' ,
529
528
'user agent'
530
529
) ;
531
- expect ( updateSession . mock . calls [ 0 ] ) . toEqual ( [ '456' , 'ip' , 'user agent' ] ) ;
530
+ expect ( updateSession . mock . calls [ 0 ] ) . toEqual ( [
531
+ '456' ,
532
+ { ip : 'ip' , userAgent : 'user agent' } ,
533
+ ] ) ;
532
534
expect ( res . user ) . toEqual ( {
533
535
userId : '123' ,
534
536
username : 'username' ,
@@ -577,7 +579,7 @@ describe('AccountsServer', () => {
577
579
const accountsServer = new AccountsServer (
578
580
{
579
581
db : {
580
- findSessionById : ( ) => Promise . resolve ( null ) ,
582
+ findSessionByToken : ( ) => Promise . resolve ( null ) ,
581
583
} as any ,
582
584
tokenSecret : 'secret' ,
583
585
} ,
@@ -601,7 +603,7 @@ describe('AccountsServer', () => {
601
603
const accountsServer = new AccountsServer (
602
604
{
603
605
db : {
604
- findSessionById : ( ) =>
606
+ findSessionByToken : ( ) =>
605
607
Promise . resolve ( {
606
608
valid : false ,
607
609
} ) ,
@@ -628,9 +630,9 @@ describe('AccountsServer', () => {
628
630
const accountsServer = new AccountsServer (
629
631
{
630
632
db : {
631
- findSessionById : ( ) =>
633
+ findSessionByToken : ( ) =>
632
634
Promise . resolve ( {
633
- sessionId : '456' ,
635
+ id : '456' ,
634
636
valid : true ,
635
637
userId : '123' ,
636
638
} ) ,
@@ -690,7 +692,7 @@ describe('AccountsServer', () => {
690
692
const accountsServer = new AccountsServer (
691
693
{
692
694
db : {
693
- findSessionById : ( ) => Promise . resolve ( null ) ,
695
+ findSessionByToken : ( ) => Promise . resolve ( null ) ,
694
696
} as any ,
695
697
tokenSecret : 'secret' ,
696
698
} ,
@@ -708,7 +710,7 @@ describe('AccountsServer', () => {
708
710
const accountsServer = new AccountsServer (
709
711
{
710
712
db : {
711
- findSessionById : ( ) =>
713
+ findSessionByToken : ( ) =>
712
714
Promise . resolve ( {
713
715
valid : false ,
714
716
} ) ,
@@ -748,9 +750,9 @@ describe('AccountsServer', () => {
748
750
const accountsServer = new AccountsServer (
749
751
{
750
752
db : {
751
- findSessionById : ( ) =>
753
+ findSessionByToken : ( ) =>
752
754
Promise . resolve ( {
753
- sessionId : '456' ,
755
+ id : '456' ,
754
756
valid : true ,
755
757
userId : '123' ,
756
758
} ) ,
@@ -779,9 +781,9 @@ describe('AccountsServer', () => {
779
781
const accountsServer = new AccountsServer (
780
782
{
781
783
db : {
782
- findSessionById : ( ) =>
784
+ findSessionByToken : ( ) =>
783
785
Promise . resolve ( {
784
- sessionId : '456' ,
786
+ id : '456' ,
785
787
valid : false ,
786
788
userId : '123' ,
787
789
} ) ,
@@ -804,9 +806,9 @@ describe('AccountsServer', () => {
804
806
const accountsServer = new AccountsServer (
805
807
{
806
808
db : {
807
- findSessionById : ( ) =>
809
+ findSessionByToken : ( ) =>
808
810
Promise . resolve ( {
809
- sessionId : '456' ,
811
+ id : '456' ,
810
812
valid : true ,
811
813
userId : '123' ,
812
814
} ) ,
@@ -1148,14 +1150,9 @@ describe('AccountsServer', () => {
1148
1150
tokens : { sessionId : '001' , isImpersonated : true } ,
1149
1151
user : impersonatedUser ,
1150
1152
} ) ;
1151
- expect ( createSession ) . toHaveBeenCalledWith (
1152
- impersonatedUser . id ,
1153
- null ,
1154
- null ,
1155
- {
1156
- impersonatorUserId : user . id ,
1157
- }
1158
- ) ;
1153
+ expect ( createSession . mock . calls [ 0 ] [ 3 ] ) . toEqual ( {
1154
+ impersonatorUserId : user . id ,
1155
+ } ) ;
1159
1156
} ) ;
1160
1157
} ) ;
1161
1158
0 commit comments