1
1
import { MongoClient , ObjectId } from 'mongodb' ;
2
2
import { Book } from '../models/book' ;
3
3
import { connectToDatabase , collections } from '../database.js' ;
4
-
5
- const adminJWT = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NGQ0Yzk2NGYwZDA1NmVhNmJmMGYzZDgiLCJuYW1lIjoiT2xkU2Nob29sIEFsbGlnYXRvciIsImlzQWRtaW4iOnRydWUsImlhdCI6MTY5MTY2Njc4OCwiZXhwIjoxNzIzMjAyNzg4fQ.0ycGXmrPBBJC9f1_nhJ7Ypi0C1DjzcZ6NpQVvpDAnJM' ;
6
- const userJWT = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NGQ0Yzc1MDViZDQ4MzEwNWM0ODk5MWQiLCJuYW1lIjoiUm93ZHkgSHllbmEiLCJpYXQiOjE2OTE2NjY3ODgsImV4cCI6MTcyMzIwMjc4OH0.YCFLMDhF4R009QT3bOy_H90ocgpKRhIMdbtpOvO-s-c' ;
4
+ import request from 'supertest' ;
7
5
8
6
export const getBaseUrl = ( ) => `http://localhost:${ process . env . PORT } ` ;
9
7
@@ -32,6 +30,14 @@ export async function initializeTestData() {
32
30
users . admin ,
33
31
users . user1 ,
34
32
] ) ;
33
+
34
+ const adminResponse = await request ( getBaseUrl ( ) )
35
+ . get ( `/users/login/${ users . admin . name } ` ) ;
36
+ users . admin . jwt = adminResponse . body . jwt ;
37
+
38
+ const userResponse = await request ( getBaseUrl ( ) )
39
+ . get ( `/users/login/${ users . user1 . name } ` ) ;
40
+ users . user1 . jwt = userResponse . body . jwt ;
35
41
}
36
42
37
43
export async function cleanTestData ( ) {
@@ -47,11 +53,9 @@ export async function cleanTestData() {
47
53
48
54
export async function cleanDatabase ( ) {
49
55
return await Promise . all ( [
50
- collections ?. books ?. deleteOne ( { _id : books . sample . _id } ) ,
51
- collections ?. books ?. deleteOne ( { _id : books . oneCopy . _id } ) ,
52
- collections ?. books ?. deleteOne ( { _id : books . notAvailable . _id } ) ,
53
- collections . issueDetails ?. deleteMany ( { _id : new RegExp ( `^${ users . user1 . _id } ` ) } ) ,
54
- collections . reviews ?. deleteMany ( { name : users . user1 . name } )
56
+ collections ?. books ?. deleteMany ( ) ,
57
+ collections . issueDetails ?. deleteMany ( ) ,
58
+ collections . reviews ?. deleteMany ( )
55
59
] ) ;
56
60
}
57
61
@@ -60,12 +64,12 @@ export const users = {
60
64
_id : new ObjectId ( '64d4c964f0d056ea6bf0f3d8' ) ,
61
65
name : 'OldSchool Alligator' ,
62
66
isAdmin : true ,
63
- jwt : adminJWT ,
67
+ jwt : ''
64
68
} ,
65
69
user1 : {
66
70
_id : new ObjectId ( '64d4c7505bd483105c48991d' ) ,
67
71
name : 'Rowdy Hyena' ,
68
- jwt : userJWT ,
72
+ jwt : ''
69
73
}
70
74
} ;
71
75
0 commit comments