Skip to content

Commit 1e6fe53

Browse files
authored
Merge pull request #32 from oslabs-beta/liam/testing
Liam/testing
2 parents 21b6a95 + 79f1618 commit 1e6fe53

File tree

5 files changed

+183
-41
lines changed

5 files changed

+183
-41
lines changed

__tests__/projects.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const http = require('http')
1111
const {state, projectToSave } = mockData
1212

1313
// save and get projects endpoint testing
14-
xdescribe('Project endpoints tests', () => {
14+
describe('Project endpoints tests', () => {
1515
let server;
1616
beforeAll((done) => {
1717
server = http.createServer(app);
@@ -22,7 +22,7 @@ xdescribe('Project endpoints tests', () => {
2222
server.close(done);
2323
});
2424
// test saveProject endpoint
25-
xdescribe('/saveProject', () => {
25+
describe('/saveProject', () => {
2626
describe('/POST', () => {
2727
it('responds with a status of 200 and json object equal to project sent', () => {
2828
return request(server)
@@ -36,7 +36,7 @@ xdescribe('Project endpoints tests', () => {
3636
});
3737
});
3838
// test getProjects endpoint
39-
xdescribe('/getProjects', () => {
39+
describe('/getProjects', () => {
4040
describe('POST', () => {
4141
it('responds with status of 200 and json object equal to an array of user projects', () => {
4242
return request(server)
@@ -53,7 +53,7 @@ xdescribe('Project endpoints tests', () => {
5353
});
5454
});
5555
// test deleteProject endpoint
56-
xdescribe('/deleteProject', () => {
56+
describe('/deleteProject', () => {
5757
describe('DELETE', () => {
5858
const { name, userId } = projectToSave;
5959
it('responds with status of 200 and json object equal to deleted project', () => {

__tests__/server.test.tsx

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import marketplaceController from '../server/controllers/marketplaceController';
2+
import app from '../server/server';
3+
import mockData from '../mockData';
4+
const request = require('supertest');
5+
const mongoose = require('mongoose');
6+
const mockNext = jest.fn(); // Mock nextFunction
7+
const MONGO_DB = process.env.MONGO_DB_TEST;
8+
const { state, projectToSave, user } = mockData
9+
const PORT = 8080;
10+
11+
beforeAll(async () => {
12+
await mongoose.connect(MONGO_DB, {
13+
useNewUrlParser: true,
14+
useUnifiedTopology: true,
15+
});
16+
});
17+
18+
afterAll(async () => {
19+
await mongoose.connection.close();
20+
});
21+
22+
describe('Server endpoint tests', () => {
23+
it('should pass this test request', async () => {
24+
const response = await request(app).get('/test');
25+
expect(response.status).toBe(200);
26+
expect(response.text).toBe('test request is working');
27+
});
28+
describe('Marketplace endpoint testing', () => {
29+
it('get requests to /getMarketplaceProjects should return an array of projects', async () => {
30+
const response = await request(app).get('/getMarketplaceProjects');
31+
expect(response.status).toBe(200);
32+
expect(Array.isArray(response.body)).toBe(true);
33+
});
34+
it('the return array should be populated with project objects', async () => {
35+
const response = await request(app).get('/getMarketplaceProjects');
36+
expect(response.status).toBe(200);
37+
expect(Array.isArray(response.body)).toBe(true);
38+
});
39+
});
40+
// test saveProject endpoint
41+
describe('/saveProject', () => {
42+
describe('/POST', () => {
43+
it('responds with a status of 200 and json object equal to project sent', async () => {
44+
// const response = await request(app).post('/saveProject').set('Accept', 'application/json').send(projectToSave);
45+
// console.log(response);
46+
// console.log(response.body);
47+
// expect(response.status).toBe(200);
48+
return request(app)
49+
.post('/saveProject')
50+
.set('Accept', 'application/json')
51+
.send(projectToSave)
52+
.expect(200)
53+
.expect('Content-Type', /application\/json/)
54+
.then((res) => expect(res.body.name).toBe(projectToSave.name));
55+
});
56+
// });
57+
});
58+
// test getProjects endpoint
59+
describe('/getProjects', () => {
60+
describe('POST', () => {
61+
it('responds with status of 200 and json object equal to an array of user projects', () => {
62+
return request(app)
63+
.post('/getProjects')
64+
.set('Accept', 'application/json')
65+
.send({ userId: projectToSave.userId })
66+
.expect(200)
67+
.expect('Content-Type', /json/)
68+
.then((res) => {
69+
expect(Array.isArray(res.body)).toBeTruthy;
70+
expect(res.body[0].name).toBe(state.name);
71+
});
72+
});
73+
});
74+
});
75+
// test deleteProject endpoint
76+
describe('/deleteProject', () => {
77+
describe('DELETE', () => {
78+
it('responds with status of 200 and json object equal to deleted project', async () => {
79+
const response: Response = await request(app).post('/getProjects').set('Accept', 'application/json').send({ userId: projectToSave.userId });
80+
const _id: String = response.body[0]._id;
81+
const userId: String = user.username;
82+
console.log(_id, userId);
83+
return request(app)
84+
.delete('/deleteProject')
85+
.set('Content-Type', 'application/json')
86+
.send({ _id, userId })
87+
.expect(200)
88+
.then((res) => expect(res.body.name).toBe(projectToSave.name)); // @Denton might want to check more of these fields
89+
});
90+
});
91+
});
92+
});
93+
});
94+
95+
96+
97+
98+
99+
100+
101+
// describe('marketplaceController Middleware', () => {
102+
// describe('getProjects tests', () => {
103+
// it('should add the projects as an array to res.locals', () => {
104+
// const req = {};
105+
// const res = { locals: {} };
106+
// console.log(marketplaceController.getPublishedProjects);
107+
// console.log(typeof marketplaceController.getPublishedProjects);
108+
// marketplaceController.getPublishedProjects(req, res, mockNext);
109+
// expect(Array.isArray(res.locals.publishedProjects)).toBe(true);
110+
// expect(mockNext).toHaveBeenCalled();
111+
// });
112+
// });
113+
114+
115+
// it('should send an error response if there is an error in the middleware', () => {
116+
// const req = { user: { isAuthenticated: false } };
117+
// const res = mockResponse();
118+
119+
// marketplaceController.authenticateMiddleware(req, res, mockNext);
120+
121+
// expect(res.status).toHaveBeenCalledWith(500);
122+
// expect(res.json).toHaveBeenCalledWith({ err: 'Error in marketplaceController.getPublishedProjects, check server logs for details' });
123+
// });
124+
// });

mockData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const mockObj = {
55
username: 'test',
66
77
password: 'password1!',
8-
userId: '64513ce26f7de07139a03069'
8+
userId: '64f11a47e77f99bf34b08979'
99
},
1010

1111
state: {
@@ -29,7 +29,7 @@ const mockObj = {
2929

3030
projectToSave: {
3131
name: 'super test project',
32-
userId: '60469fc6c435891422b3a84c',
32+
userId: '64f11a47e77f99bf34b08979',
3333
username: 'test',
3434
project: {
3535
name: 'test',

server/controllers/projectController.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,35 @@ const projectController: ProjectController = {
6565
},
6666

6767

68-
// delete project from database **currently not integrated into app**
69-
deleteProject: (req, res, next) => {
68+
// delete project from database
69+
deleteProject: async (req, res, next) => {
7070
// pull project name and userId from req.body
7171
const { _id, userId } = req.body;
72-
Projects.findOneAndDelete({ _id, userId }, null, (err, deleted) => {
73-
if (err) {
74-
return next({
75-
log: `Error in projectController.deleteProject: ${err}`,
76-
message: {
77-
err: 'Error in projectController.deleteProject, check server logs for details'
78-
}
79-
});
80-
}
81-
res.locals.deleted = deleted;
82-
return next();
83-
});
72+
try {
73+
const response = await Projects.findOneAndDelete({ _id: _id, username: userId });
74+
res.locals.deleted = response;
75+
return next()
76+
} catch (err) {
77+
return next({
78+
log: `Error in projectController.deleteProject: ${err}`,
79+
message: {
80+
err: 'Error in projectController.deleteProject, check server logs for details'
81+
}
82+
});
83+
}
84+
// @Denton, rewrote the above syntax for async await, would be good to test this further
85+
// Projects.findOneAndDelete({ _id, userId }, null, (err, deleted) => {
86+
// if (err) {
87+
// return next({
88+
// log: `Error in projectController.deleteProject: ${err}`,
89+
// message: {
90+
// err: 'Error in projectController.deleteProject, check server logs for details'
91+
// }
92+
// });
93+
// }
94+
// res.locals.deleted = deleted;
95+
// return next();
96+
// });
8497
}
8598
};
8699
export default projectController;

server/controllers/sessionController.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,36 @@ dotenv.config();
77
// here we are cheching that the user making the request is login in and has a valid cookieId
88
const sessionController: SessionController = {
99
isLoggedIn: async (req, res, next) => {
10-
try {
11-
let cookieId;
12-
if (req.cookies) {
13-
// if the request cookies exist then it assigns it to cookieId
14-
cookieId = req.cookies.ssid;
10+
if (process.env.NODE_ENV === 'test') {
11+
// Skip authentication checks in test environment
12+
return next();
1513
} else {
16-
// else it creates a new cookieId for the user based on the userId
17-
cookieId = req.body.userId;
18-
}
14+
try {
15+
let cookieId;
16+
if (req.cookies) {
17+
// if the request cookies exist then it assigns it to cookieId
18+
cookieId = req.cookies.ssid;
19+
} else {
20+
// else it creates a new cookieId for the user based on the userId
21+
cookieId = req.body.userId;
22+
}
1923

20-
// find session from request session ID in mongodb
21-
const session = await Sessions.findOne({ cookieId });
24+
// find session from request session ID in mongodb
25+
const session = await Sessions.findOne({ cookieId });
2226

23-
if (!session) {
24-
return res.redirect('/');
25-
}
26-
return next();
27-
} catch (err) {
28-
return next({
29-
log: `Error in sessionController.isLoggedIn: ${err}`,
30-
message: {
31-
err: 'Error in sessionController.isLoggedIn, check server logs for details'
27+
if (!session) {
28+
return res.redirect('/');
29+
}
30+
return next();
31+
} catch (err) {
32+
return next({
33+
log: `Error in sessionController.isLoggedIn: ${err}`,
34+
message: {
35+
err: 'Error in sessionController.isLoggedIn, check server logs for details'
36+
}
37+
});
3238
}
33-
});
34-
}
39+
}
3540
},
3641
// startSession - create and save a new session into the database
3742
startSession: (req, res, next) => {

0 commit comments

Comments
 (0)