Skip to content

Commit 2bb913d

Browse files
committed
Cleaning the auth test and apply prettier
1 parent 193d965 commit 2bb913d

File tree

2 files changed

+74
-154
lines changed

2 files changed

+74
-154
lines changed

__tests__/userAuth.test.ts

Lines changed: 70 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22
* @jest-environment node
33
*/
44

5-
6-
import marketplaceController from '../server/controllers/marketplaceController';
75
import app from '../server/server';
86
import mockData from '../mockData';
9-
import { profileEnd } from 'console';
107
import { Sessions, Users } from '../server/models/reactypeModels';
118
const request = require('supertest');
129
const mongoose = require('mongoose');
13-
const mockNext = jest.fn(); // Mock nextFunction
1410
const MONGO_DB = process.env.MONGO_DB_TEST;
15-
const { state, projectToSave, user } = mockData
16-
const PORT = 8080;
11+
const { user } = mockData;
1712

1813
const num = Math.floor(Math.random() * 1000);
1914

2015
beforeAll(async () => {
2116
await mongoose.connect(MONGO_DB, {
2217
useNewUrlParser: true,
23-
useUnifiedTopology: true,
18+
useUnifiedTopology: true
2419
});
2520
});
2621

2722
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.`);
23+
const result = await Users.deleteMany({
24+
_id: { $ne: '64f551e5b28d5292975e08c8' }
25+
}); //clear the users collection after tests are done except for the mockdata user account
26+
const result2 = await Sessions.deleteMany({
27+
cookieId: { $ne: '64f551e5b28d5292975e08c8' }
28+
});
29+
console.log(
30+
`${result.deletedCount} and ${result2.deletedCount} documents deleted.`
31+
);
3232
await mongoose.connection.close();
3333
});
3434

@@ -40,7 +40,8 @@ describe('User Authentication tests', () => {
4040
expect(response.text).toBe('test request is working');
4141
});
4242
});
43-
describe('/signup', ()=> {
43+
44+
describe('/signup', () => {
4445
describe('POST', () => {
4546
//testing new signup
4647
it('responds with status 200 and sessionId on valid new user signup', () => {
@@ -55,7 +56,7 @@ describe('User Authentication tests', () => {
5556
.expect(200)
5657
.then((res) => expect(res.body.sessionId).not.toBeNull());
5758
});
58-
59+
5960
it('responds with status 400 and json string on invalid new user signup (Already taken)', () => {
6061
return request(app)
6162
.post('/signup')
@@ -67,8 +68,9 @@ describe('User Authentication tests', () => {
6768
});
6869
});
6970
});
71+
7072
describe('/login', () => {
71-
// tests whether existing login information permits user to log in
73+
// tests whether existing login information permits user to log in
7274
describe('POST', () => {
7375
it('responds with status 200 and json object on verified user login', () => {
7476
return request(app)
@@ -80,152 +82,74 @@ describe('User Authentication tests', () => {
8082
.then((res) => expect(res.body.sessionId).toEqual(user.userId));
8183
});
8284
// if invalid username/password, should respond with status 400
83-
it('responds with status 400 and json string on invalid user login', () => {
85+
it('responds with status 400 and json string on invalid user login', () => {
8486
return request(app)
8587
.post('/login')
8688
.send({ username: 'wrongusername', password: 'wrongpassword' })
8789
.expect(400)
8890
.expect('Content-Type', /json/)
8991
.then((res) => expect(typeof res.body).toBe('string'));
9092
});
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-
// });
93+
it("returns the message 'No Username Input' when no username is entered", () => {
94+
return request(app)
95+
.post('/login')
96+
.send({
97+
username: '',
98+
password: 'Reactype123!@#',
99+
isFbOauth: false
100+
})
101+
.then((res) => expect(res.text).toBe('"No Username Input"'));
102+
});
110103

111-
// afterAll(async () => {
112-
// await mongoose.connection.close();
113-
// });
114-
// //for creating unqiue login credentials
115-
// const num = Math.floor(Math.random() * 1000);
104+
it("returns the message 'No Username Input' when no username is entered", () => {
105+
return request(app)
106+
.post('/login')
107+
.send({
108+
username: '',
109+
password: 'Reactype123!@#',
110+
isFbOauth: false
111+
})
112+
.then((res) => expect(res.text).toBe('"No Username Input"'));
113+
});
116114

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-
// });
115+
it("returns the message 'No Password Input' when no password is entered", () => {
116+
return request(app)
117+
.post('/login')
118+
.send({
119+
username: 'reactype123',
120+
password: '',
121+
isFbOauth: false
122+
})
123+
.then((res) => expect(res.text).toBe('"No Password Input"'));
124+
});
125125

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-
// });
126+
it("returns the message 'Invalid Username' when username does not exist", () => {
127+
return request(app)
128+
.post('/login')
129+
.send({
130+
username: 'l!b',
131+
password: 'test',
132+
isFbOauth: false
133+
})
134+
.then((res) => expect(res.text).toBe('"Invalid Username"'));
135+
});
136+
});
139137

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-
// });
138+
it("returns the message 'Incorrect Password' when password does not match", () => {
139+
return request(app)
140+
.post('/login')
141+
.send({
142+
username: 'test',
143+
password: 'test',
144+
isFbOauth: false
145+
})
146+
.then((res) => expect(res.text).toBe('"Incorrect Password"'));
147+
});
148+
});
149+
});
183150

184151
// 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-
// });
195152

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-
// });
206-
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-
// });
218-
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-
// });
229153
// // note that the username and password in this test are kept in the heroku database
230154
// // DO NOT CHANGE unless you have access to the heroku database
231155
// it("returns the message 'Success' when the user passes all auth checks", () => {
@@ -240,7 +164,6 @@ describe('User Authentication tests', () => {
240164
// });
241165

242166
// // // OAuth tests (currently inoperative)
243-
244167
// // xdescribe('Github oauth tests', () => {
245168
// // describe('/github/callback?code=', () => {
246169
// // describe('GET', () => {

server/routers/auth.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ router.get(
3333
res.cookie('ssid', req.user.id, {
3434
httpOnly: true,
3535
sameSite: 'none',
36-
secure: true,
36+
secure: true
3737
});
3838

3939
res.cookie('username', req.user.username, {
4040
httpOnly: true,
4141
sameSite: 'none',
42-
secure: true,
42+
secure: true
4343
});
4444
return res.redirect(API_BASE_URL);
4545
}
@@ -50,7 +50,6 @@ router.get(
5050
passport.authenticate('google', {
5151
scope: ['profile']
5252
})
53-
5453
);
5554

5655
router.get(
@@ -62,15 +61,13 @@ router.get(
6261
res.cookie('ssid', req.user.id, {
6362
httpOnly: true,
6463
sameSite: 'none',
65-
secure: true,
64+
secure: true
6665
});
6766

68-
69-
7067
res.cookie('username', req.user.username, {
7168
httpOnly: true,
7269
sameSite: 'none',
73-
secure: true,
70+
secure: true
7471
});
7572
return res.redirect(API_BASE_URL);
7673
}

0 commit comments

Comments
 (0)