Skip to content

Commit 7a369cf

Browse files
Merge branch 'dev' of https://github.com/oslabs-beta/ReacType into dev
2 parents 19ffb0b + 2c887b6 commit 7a369cf

File tree

11 files changed

+39297
-38656
lines changed

11 files changed

+39297
-38656
lines changed

__tests__/userAuth.test.ts

Lines changed: 206 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,206 @@
1-
/**
2-
* @jest-environment node
3-
*/
4-
5-
import app from '../server/server';
6-
import mockData from '../mockData';
7-
import { Sessions, Users } from '../server/models/reactypeModels';
8-
const request = require('supertest');
9-
const mongoose = require('mongoose');
10-
const mockNext = jest.fn(); // Mock nextFunction
11-
const MONGO_DB = import.meta.env.MONGO_DB_TEST;
12-
const { user } = mockData;
13-
const PORT = 8080;
14-
15-
const num = Math.floor(Math.random() * 1000);
16-
17-
beforeAll(async () => {
18-
await mongoose.connect(MONGO_DB, {
19-
useNewUrlParser: true,
20-
useUnifiedTopology: true
21-
});
22-
});
23-
24-
afterAll(async () => {
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-
);
34-
await mongoose.connection.close();
35-
});
36-
37-
describe('User Authentication tests', () => {
38-
describe('initial connection test', () => {
39-
it('should connect to the server', async () => {
40-
const response = await request(app).get('/test');
41-
expect(response.status).toBe(200);
42-
expect(response.text).toBe('test request is working');
43-
});
44-
});
45-
describe('/signup', () => {
46-
describe('POST', () => {
47-
//testing new signup
48-
it('responds with status 200 and sessionId on valid new user signup', () => {
49-
return request(app)
50-
.post('/signup')
51-
.set('Content-Type', 'application/json')
52-
.send({
53-
username: `supertest${num}`,
54-
email: `test${num}@test.com`,
55-
password: `${num}`
56-
})
57-
.expect(200)
58-
.then((res) => expect(res.body.sessionId).not.toBeNull());
59-
});
60-
61-
it('responds with status 400 and json string on invalid new user signup (Already taken)', () => {
62-
return request(app)
63-
.post('/signup')
64-
.send(user)
65-
.set('Accept', 'application/json')
66-
.expect('Content-Type', /json/)
67-
.expect(400)
68-
.then((res) => expect(typeof res.body).toBe('string'));
69-
});
70-
});
71-
});
72-
73-
describe('/login', () => {
74-
// tests whether existing login information permits user to log in
75-
describe('POST', () => {
76-
it('responds with status 200 and json object on verified user login', () => {
77-
return request(app)
78-
.post('/login')
79-
.set('Accept', 'application/json')
80-
.send(user)
81-
.expect(200)
82-
.expect('Content-Type', /json/)
83-
.then((res) => expect(res.body.sessionId).toEqual(user.userId));
84-
});
85-
// if invalid username/password, should respond with status 400
86-
it('responds with status 400 and json string on invalid user login', () => {
87-
return request(app)
88-
.post('/login')
89-
.send({ username: 'wrongusername', password: 'wrongpassword' })
90-
.expect(400)
91-
.expect('Content-Type', /json/)
92-
.then((res) => expect(typeof res.body).toBe('string'));
93-
});
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-
});
104-
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-
});
115-
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-
});
126-
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-
});
138-
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-
});
1+
/**
2+
* @jest-environment node
3+
*/
4+
5+
import app from '../server/server';
6+
import mockData from '../mockData';
7+
import { Sessions, Users } from '../server/models/reactypeModels';
8+
const request = require('supertest');
9+
const mongoose = require('mongoose');
10+
const mockNext = jest.fn(); // Mock nextFunction
11+
const MONGO_DB = import.meta.env.MONGO_DB_TEST;
12+
const { user } = mockData;
13+
const PORT = 8080;
14+
15+
const num = Math.floor(Math.random() * 1000);
16+
17+
beforeAll(async () => {
18+
await mongoose.connect(MONGO_DB, {
19+
useNewUrlParser: true,
20+
useUnifiedTopology: true
21+
});
22+
});
23+
24+
afterAll(async () => {
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+
);
34+
await mongoose.connection.close();
35+
});
36+
37+
describe('User Authentication tests', () => {
38+
describe('initial connection test', () => {
39+
it('should connect to the server', async () => {
40+
const response = await request(app).get('/test');
41+
expect(response.status).toBe(200);
42+
expect(response.text).toBe('test request is working');
43+
});
44+
});
45+
describe('/signup', () => {
46+
describe('POST', () => {
47+
//testing new signup
48+
it('responds with status 200 and sessionId on valid new user signup', () => {
49+
return request(app)
50+
.post('/signup')
51+
.set('Content-Type', 'application/json')
52+
.send({
53+
username: `supertest${num}`,
54+
email: `test${num}@test.com`,
55+
password: `${num}`
56+
})
57+
.expect(200)
58+
.then((res) => expect(res.body.sessionId).not.toBeNull());
59+
});
60+
61+
it('responds with status 400 and json string on invalid new user signup (Already taken)', () => {
62+
return request(app)
63+
.post('/signup')
64+
.send(user)
65+
.set('Accept', 'application/json')
66+
.expect('Content-Type', /json/)
67+
.expect(400)
68+
.then((res) => expect(typeof res.body).toBe('string'));
69+
});
70+
});
71+
});
72+
73+
describe('/login', () => {
74+
// tests whether existing login information permits user to log in
75+
describe('POST', () => {
76+
it('responds with status 200 and json object on verified user login', () => {
77+
return request(app)
78+
.post('/login')
79+
.set('Accept', 'application/json')
80+
.send(user)
81+
.expect(200)
82+
.expect('Content-Type', /json/)
83+
.then((res) => expect(res.body.sessionId).toEqual(user.userId));
84+
});
85+
// if invalid username/password, should respond with status 400
86+
it('responds with status 400 and json string on invalid user login', () => {
87+
return request(app)
88+
.post('/login')
89+
.send({ username: 'wrongusername', password: 'wrongpassword' })
90+
.expect(400)
91+
.expect('Content-Type', /json/)
92+
.then((res) => expect(typeof res.body).toBe('string'));
93+
});
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+
});
104+
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+
});
115+
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+
});
126+
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+
});
138+
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+
151+
describe('/updatePassword', () => {
152+
describe('POST', () => {
153+
//testing update password
154+
const testUsername = `supertest${Date.now()}`;
155+
const testPassword = `password${Date.now()}`;
156+
it('responds with status 200 and json string on valid password update (Success)', () => {
157+
return request(app)
158+
.post('/updatePassword')
159+
.set('Content-Type', 'application/json')
160+
.send({
161+
username: testUsername,
162+
password: testPassword
163+
})
164+
.expect(200)
165+
.then((res) => expect(res.body.message).toBe('Success')); // might need to be res.text instead of res.body.message
166+
});
167+
168+
it('responds with status 400 and json string if no password is provided (Password is required.)', () => {
169+
return request(app)
170+
.post('/updatePassword')
171+
.send({ username: testUsername })
172+
.set('Accept', 'application/json')
173+
.expect('Content-Type', /json/)
174+
.expect(400)
175+
.then((res) => expect(res.body.error).toBe('Password is required.'));
176+
});
177+
178+
it('responds with status 404 and json string if user is not found (User not found.)', () => {
179+
return request(app)
180+
.post('/updatePassword')
181+
.send({ username: 'doesntexist', password: 'fakepassword' })
182+
.set('Accept', 'application/json')
183+
.expect('Content-Type', /json/)
184+
.expect(404)
185+
.then((res) => expect(res.body.error).toBe('User not found.'));
186+
});
187+
188+
it('responds with status 400 and json string the provided password is the same as the current password (New password must be different from the current password.)', () => {
189+
return request(app)
190+
.post('/updatePassword')
191+
.send({
192+
username: testUsername,
193+
password: testPassword
194+
})
195+
.set('Accept', 'application/json')
196+
.expect('Content-Type', /json/)
197+
.expect(400)
198+
.then((res) =>
199+
expect(res.body.error).toBe(
200+
'New password must be different from the current password.'
201+
)
202+
);
203+
});
204+
});
205+
});
206+
});

0 commit comments

Comments
 (0)