2
2
* @jest -environment node
3
3
*/
4
4
5
-
6
- import marketplaceController from '../server/controllers/marketplaceController' ;
7
5
import app from '../server/server' ;
8
6
import mockData from '../mockData' ;
9
- import { profileEnd } from 'console' ;
10
7
import { Sessions , Users } from '../server/models/reactypeModels' ;
11
8
const request = require ( 'supertest' ) ;
12
9
const mongoose = require ( 'mongoose' ) ;
13
10
const mockNext = jest . fn ( ) ; // Mock nextFunction
14
- const MONGO_DB = process . env . MONGO_DB_TEST ;
15
- const { state , projectToSave , user } = mockData
11
+ const MONGO_DB = import . meta . env . MONGO_DB_TEST ;
12
+ const { user } = mockData ;
16
13
const PORT = 8080 ;
17
14
18
15
const num = Math . floor ( Math . random ( ) * 1000 ) ;
19
16
20
17
beforeAll ( async ( ) => {
21
18
await mongoose . connect ( MONGO_DB , {
22
19
useNewUrlParser : true ,
23
- useUnifiedTopology : true ,
20
+ useUnifiedTopology : true
24
21
} ) ;
25
22
} ) ;
26
23
27
24
afterAll ( async ( ) => {
28
-
29
- const result = await Users . deleteMany ( { _id : { $ne : '64f551e5b28d5292975e08c8' } } ) ; //clear the users collection after tests are done except for the mockdata user account
30
- const result2 = await Sessions . deleteMany ( { cookieId : { $ne : '64f551e5b28d5292975e08c8' } } ) ;
31
- console . log ( `${ result . deletedCount } and ${ result2 . deletedCount } documents deleted.` ) ;
25
+ const result = await Users . deleteMany ( {
26
+ _id : { $ne : '64f551e5b28d5292975e08c8' }
27
+ } ) ; //clear the users collection after tests are done except for the mockdata user account
28
+ const result2 = await Sessions . deleteMany ( {
29
+ cookieId : { $ne : '64f551e5b28d5292975e08c8' }
30
+ } ) ;
31
+ console . log (
32
+ `${ result . deletedCount } and ${ result2 . deletedCount } documents deleted.`
33
+ ) ;
32
34
await mongoose . connection . close ( ) ;
33
35
} ) ;
34
36
@@ -40,7 +42,7 @@ describe('User Authentication tests', () => {
40
42
expect ( response . text ) . toBe ( 'test request is working' ) ;
41
43
} ) ;
42
44
} ) ;
43
- describe ( '/signup' , ( ) => {
45
+ describe ( '/signup' , ( ) => {
44
46
describe ( 'POST' , ( ) => {
45
47
//testing new signup
46
48
it ( 'responds with status 200 and sessionId on valid new user signup' , ( ) => {
@@ -55,7 +57,7 @@ describe('User Authentication tests', () => {
55
57
. expect ( 200 )
56
58
. then ( ( res ) => expect ( res . body . sessionId ) . not . toBeNull ( ) ) ;
57
59
} ) ;
58
-
60
+
59
61
it ( 'responds with status 400 and json string on invalid new user signup (Already taken)' , ( ) => {
60
62
return request ( app )
61
63
. post ( '/signup' )
@@ -67,8 +69,9 @@ describe('User Authentication tests', () => {
67
69
} ) ;
68
70
} ) ;
69
71
} ) ;
72
+
70
73
describe ( '/login' , ( ) => {
71
- // tests whether existing login information permits user to log in
74
+ // tests whether existing login information permits user to log in
72
75
describe ( 'POST' , ( ) => {
73
76
it ( 'responds with status 200 and json object on verified user login' , ( ) => {
74
77
return request ( app )
@@ -80,183 +83,68 @@ describe('User Authentication tests', () => {
80
83
. then ( ( res ) => expect ( res . body . sessionId ) . toEqual ( user . userId ) ) ;
81
84
} ) ;
82
85
// if invalid username/password, should respond with status 400
83
- it ( 'responds with status 400 and json string on invalid user login' , ( ) => {
86
+ it ( 'responds with status 400 and json string on invalid user login' , ( ) => {
84
87
return request ( app )
85
88
. post ( '/login' )
86
89
. send ( { username : 'wrongusername' , password : 'wrongpassword' } )
87
90
. expect ( 400 )
88
91
. expect ( 'Content-Type' , / j s o n / )
89
92
. then ( ( res ) => expect ( typeof res . body ) . toBe ( 'string' ) ) ;
90
93
} ) ;
91
- } ) ;
92
- } ) ;
93
-
94
- } ) ;
95
-
96
-
97
-
98
- // import request from 'supertest';
99
- // import app from '../server/server';
100
- // import mockObj from '../mockData';
101
- // const user = mockObj.user;
102
- // import mongoose from 'mongoose';
103
- // const URI = process.env.MONGO_DB;
104
-
105
- // beforeAll(() => {
106
- // mongoose
107
- // .connect(URI, { useNewUrlParser: true }, { useUnifiedTopology: true })
108
- // .then(() => console.log('connected to test database'));
109
- // });
110
-
111
- // afterAll(async () => {
112
- // await mongoose.connection.close();
113
- // });
114
- // //for creating unqiue login credentials
115
- // const num = Math.floor(Math.random() * 1000);
116
-
117
- // describe('User authentication tests', () => {
118
- // //test connection to server
119
- // describe('initial connection test', () => {
120
- // it('should connect to the server', async () => {
121
- // const response = await request(app).get('/test');
122
- // expect(response.text).toEqual('test request is working');
123
- // });
124
- // });
125
-
126
- // xdescribe('POST', () => {
127
- // it('responds with status 200 and json object on valid new user signup', () => {
128
- // return request(app)
129
- // .post('/signup')
130
- // .set('Content-Type', 'application/json')
131
- // .send({
132
- // username: `supertest${num}`,
133
- // email: `test${num}@test .com`,
134
- // password: `${num}`
135
- // })
136
- // .expect(200)
137
- // .then((res) => expect(typeof res.body).toBe('object'));
138
- // });
139
-
140
- // it('responds with status 400 and json string on invalid new user signup', () => {
141
- // return request(app)
142
- // .post('/signup')
143
- // .send(user)
144
- // .set('Accept', 'application/json')
145
- // .expect('Content-Type', /json/)
146
- // .expect(400)
147
- // .then((res) => expect(typeof res.body).toBe('string'));
148
- // });
149
- // });
150
- // });
151
- // describe('/login', () => {
152
- // // tests whether existing login information permits user to log in
153
- // xdescribe('POST', () => {
154
- // it('responds with status 200 and json object on verified user login', () => {
155
- // return request(app)
156
- // .post('/login')
157
- // .set('Accept', 'application/json')
158
- // .send(user)
159
- // .expect(200)
160
- // .expect('Content-Type', /json/)
161
- // .then((res) => expect(res.body.sessionId).toEqual(user.userId));
162
- // });
163
- // // if invalid username/password, should respond with status 400
164
- // it('responds with status 400 and json string on invalid user login', () => {
165
- // return request(app)
166
- // .post('/login')
167
- // .send({ username: 'wrongusername', password: 'wrongpassword' })
168
- // .expect(400)
169
- // .expect('Content-Type', /json/)
170
- // .then((res) => expect(typeof res.body).toBe('string'));
171
- // });
172
- // it('responds with status 400 and json string on invalid new user signup', () => {
173
- // return request(app)
174
- // .post('/signup')
175
- // .send(user)
176
- // .set('Accept', 'application/json')
177
- // .expect('Content-Type', /json/)
178
- // .expect(400)
179
- // .then((res) => expect(typeof res.body).toBe('string'));
180
- // });
181
- // });
182
- // });
183
-
184
- // describe('sessionIsCreated', () => {
185
- // it("returns the message 'No Username Input' when no username is entered", () => {
186
- // return request(app)
187
- // .post('/login')
188
- // .send({
189
- // username: '',
190
- // password: 'Reactype123!@#',
191
- // isFbOauth: false
192
- // })
193
- // .then((res) => expect(res.text).toBe('"No Username Input"'));
194
- // });
195
-
196
- // it("returns the message 'No Password Input' when no password is entered", () => {
197
- // return request(app)
198
- // .post('/login')
199
- // .send({
200
- // username: 'reactype123',
201
- // password: '',
202
- // isFbOauth: false
203
- // })
204
- // .then((res) => expect(res.text).toBe('"No Password Input"'));
205
- // });
94
+ it ( "returns the message 'No Username Input' when no username is entered" , ( ) => {
95
+ return request ( app )
96
+ . post ( '/login' )
97
+ . send ( {
98
+ username : '' ,
99
+ password : 'Reactype123!@#' ,
100
+ isFbOauth : false
101
+ } )
102
+ . then ( ( res ) => expect ( res . text ) . toBe ( '"No Username Input"' ) ) ;
103
+ } ) ;
206
104
207
- // it("returns the message 'Invalid Username' when username does not exist", () => {
208
- // return request(app)
209
- // .post('/login')
210
- // .send({
211
- // username: 'l!b',
212
- // password: 'test',
213
- // isFbOauth: false
214
- // })
215
- // .then((res) => expect(res.text).toBe('"Invalid Username"'));
216
- // });
217
- // });
105
+ it ( "returns the message 'No Username Input' when no username is entered" , ( ) => {
106
+ return request ( app )
107
+ . post ( '/login' )
108
+ . send ( {
109
+ username : '' ,
110
+ password : 'Reactype123!@#' ,
111
+ isFbOauth : false
112
+ } )
113
+ . then ( ( res ) => expect ( res . text ) . toBe ( '"No Username Input"' ) ) ;
114
+ } ) ;
218
115
219
- // it("returns the message 'Incorrect Password' when password does not match", () => {
220
- // return request(app)
221
- // .post('/login')
222
- // .send({
223
- // username: 'test',
224
- // password: 'test',
225
- // isFbOauth: false
226
- // })
227
- // .then((res) => expect(res.text).toBe('"Incorrect Password"'));
228
- // });
229
- // // note that the username and password in this test are kept in the heroku database
230
- // // DO NOT CHANGE unless you have access to the heroku database
231
- // it("returns the message 'Success' when the user passes all auth checks", () => {
232
- // return request(app)
233
- // .post('/login')
234
- // .send({
235
- // username: 'test',
236
- // password: 'password1!',
237
- // isFbOauth: false
238
- // })
239
- // .then((res) => expect(res.body).toHaveProperty('sessionId'));
240
- // });
116
+ it ( "returns the message 'No Password Input' when no password is entered" , ( ) => {
117
+ return request ( app )
118
+ . post ( '/login' )
119
+ . send ( {
120
+ username : 'reactype123' ,
121
+ password : '' ,
122
+ isFbOauth : false
123
+ } )
124
+ . then ( ( res ) => expect ( res . text ) . toBe ( '"No Password Input"' ) ) ;
125
+ } ) ;
241
126
242
- // // // OAuth tests (currently inoperative)
127
+ it ( "returns the message 'Invalid Username' when username does not exist" , ( ) => {
128
+ return request ( app )
129
+ . post ( '/login' )
130
+ . send ( {
131
+ username : 'l!b' ,
132
+ password : 'test' ,
133
+ isFbOauth : false
134
+ } )
135
+ . then ( ( res ) => expect ( res . text ) . toBe ( '"Invalid Username"' ) ) ;
136
+ } ) ;
137
+ } ) ;
243
138
244
- // // xdescribe('Github oauth tests', () => {
245
- // // describe('/github/callback?code=', () => {
246
- // // describe('GET', () => {
247
- // // it('responds with status 400 and error message if no code received', () => {
248
- // // return request(server)
249
- // // .get('/github/callback?code=')
250
- // // .expect(400)
251
- // // .then((res) => {
252
- // // return expect(res.text).toEqual(
253
- // // '"Undefined or no code received from github.com"'
254
- // // );
255
- // // });
256
- // // });
257
- // // it('responds with status 400 if invalid code received', () => {
258
- // // return request(server).get('/github/callback?code=123456').expect(400);
259
- // // });
260
- // // });
261
- // // });
262
- // // });
139
+ it ( "returns the message 'Incorrect Password' when password does not match" , ( ) => {
140
+ return request ( app )
141
+ . post ( '/login' )
142
+ . send ( {
143
+ username : 'test' ,
144
+ password : 'test' ,
145
+ isFbOauth : false
146
+ } )
147
+ . then ( ( res ) => expect ( res . text ) . toBe ( '"Incorrect Password"' ) ) ;
148
+ } ) ;
149
+ } ) ;
150
+ } ) ;
0 commit comments