Skip to content

Commit 008d307

Browse files
committed
All userAuth tests pass
1 parent e0cb31a commit 008d307

File tree

4 files changed

+69
-97
lines changed

4 files changed

+69
-97
lines changed

__tests__/userAuth.test.ts

Lines changed: 0 additions & 90 deletions
This file was deleted.

__tests__/users.test.ts renamed to __tests__/usersAuth.test.ts

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ import request from 'supertest';
55
import app from '../server/server';
66
import mockObj from '../mockData';
77
const user = mockObj.user;
8+
import { sessionIsCreated } from '../app/src/helperFunctions/auth';
89

910
//for creating unqiue login credentials
1011
const num = Math.floor(Math.random() * 1000);
12+
let username;
13+
let password;
14+
let isFbOauth;
1115

1216
describe('User authentication tests', () => {
1317
//test connection to server
14-
test('initial connection test', async () => {
15-
const response = await request(app).get('/test');
16-
expect(response.text).toEqual('test request is working');
18+
describe('initial connection test', () => {
19+
it('should connect to the server', async () => {
20+
const response = await request(app).get('/test');
21+
expect(response.text).toEqual('test request is working');
22+
});
1723
});
1824
//navigating to signup page should serve
1925
describe('/', () => {
@@ -83,6 +89,64 @@ describe('User authentication tests', () => {
8389
});
8490
});
8591
});
92+
93+
describe('sessionIsCreated', () => {
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 Password Input' when no password is entered", () => {
106+
return request(app)
107+
.post('/login')
108+
.send({
109+
username: 'reactype123',
110+
password: '',
111+
isFbOauth: false
112+
})
113+
.then((res) => expect(res.text).toBe('"No Password Input"'));
114+
});
115+
116+
it("returns the message 'Invalid Username' when username does not exist", () => {
117+
return request(app)
118+
.post('/login')
119+
.send({
120+
username: 'l!b',
121+
password: 'test',
122+
isFbOauth: false
123+
})
124+
.then((res) => expect(res.text).toBe('"Invalid Username"'));
125+
});
126+
});
127+
128+
it("returns the message 'Incorrect Password' when password does not match", () => {
129+
return request(app)
130+
.post('/login')
131+
.send({
132+
username: 'test',
133+
password: 'test',
134+
isFbOauth: false
135+
})
136+
.then((res) => expect(res.text).toBe('"Incorrect Password"'));
137+
});
138+
// note that the username and password in this test are kept in the heroku database
139+
// DO NOT CHANGE unless you have access to the heroku database
140+
it("returns the message 'Success' when the user passes all auth checks", () => {
141+
return request(app)
142+
.post('/login')
143+
.send({
144+
username: 'test',
145+
password: 'password1!',
146+
isFbOauth: false
147+
})
148+
.then((res) => expect(res.body).toHaveProperty('sessionId'));
149+
});
86150
});
87151

88152
// // // OAuth tests (currently inoperative)

app/src/helperFunctions/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const sessionIsCreated = (
1515
password,
1616
isFbOauth
1717
});
18-
const result = fetch(`${serverURL}/login`, {
18+
const result = fetch(`/login`, {
1919
method: 'POST',
2020
credentials: 'include',
2121
headers: {
@@ -47,7 +47,7 @@ export const newUserIsCreated = (
4747
email,
4848
password
4949
});
50-
const result = fetch(`${serverURL}/signup`, {
50+
const result = fetch(`/signup`, {
5151
method: 'POST',
5252
credentials: 'include',
5353
headers: {

server/controllers/userController.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const userController: UserController = {
4141
createUser: (req, res, next) => {
4242
let email, username, password;
4343
// use this condition for Oauth login
44-
console.log('this is the test body', req.body);
4544
if (res.locals.signUpType === 'oauth') {
4645
email = res.locals.githubEmail;
4746
username = email;
@@ -90,7 +89,6 @@ const userController: UserController = {
9089
}
9190
// if no error found when creating a new user, send back user ID in res.locals
9291
res.locals.id = newUser.id;
93-
console.log('no error in test');
9492
return next();
9593
}
9694
);

0 commit comments

Comments
 (0)