Skip to content

Commit eb2c809

Browse files
committed
98% marketplacecontroller coverage 55.87% overall coverage
1 parent f2befb5 commit eb2c809

File tree

3 files changed

+116
-22
lines changed

3 files changed

+116
-22
lines changed

__tests__/server.test.tsx

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ afterAll(async () => {
3030
});
3131

3232

33-
3433
describe('Server endpoint tests', () => {
3534
it('should pass this test request', async () => {
3635
const response = await request(app).get('/test');
@@ -103,7 +102,7 @@ describe('Server endpoint tests', () => {
103102
.set('Content-Type', 'application/json')
104103
.send({ _id, userId })
105104
.expect(200)
106-
.then((res) => expect(res.body._id).toBe(_id)); // @Denton might want to check more of these fields
105+
.then((res) => expect(res.body._id).toBe(_id));
107106
});
108107
});
109108
});
@@ -118,7 +117,6 @@ describe('Server endpoint tests', () => {
118117
.set('Cookie', [`ssid=${user.userId}`])
119118
.set('Accept', 'application/json')
120119
.send(projectToSave)
121-
122120
const _id: String = projObj.body._id;
123121
const project: String = projObj.body.project;
124122
const comments: String = projObj.body.comments;
@@ -136,19 +134,56 @@ describe('Server endpoint tests', () => {
136134
expect(res.body.published).toBe(true);
137135
});
138136
});
137+
it('responds with status of 500 and error if userId and cookie ssid do not match', async () => {
138+
const projObj: Response = await request(app).post('/getProjects').set('Accept', 'application/json').set('Cookie', [`ssid=${user.userId}`]).send({ userId: projectToSave.userId });
139+
const _id: String = projObj.body[0]._id;
140+
const project: String = projObj.body[0].project;
141+
const comments: String = projObj.body[0].comments;
142+
const username: String = projObj.body[0].username;
143+
const name: String = projObj.body[0].name;
144+
const userId: String = "ERROR";
145+
return request(app)
146+
.post('/publishProject')
147+
.set('Content-Type', 'application/json')
148+
.set('Cookie', [`ssid=${user.userId}`])
149+
.send({ _id, project, comments, userId, username, name })//_id, project, comments, userId, username, name
150+
.expect(500)
151+
.then((res) => {
152+
expect(res.body.err).not.toBeNull()
153+
});
154+
});
155+
it('responds with status of 500 and error if _id was not a valid mongo ObjectId', async () => {
156+
const projObj: Response = await request(app).post('/getProjects').set('Accept', 'application/json').set('Cookie', [`ssid=${user.userId}`]).send({ userId: projectToSave.userId });
157+
const _id: String = 'ERROR';
158+
const project: String = projObj.body[0].project;
159+
const comments: String = projObj.body[0].comments;
160+
const username: String = user.username;
161+
const name: String = projObj.body[0].name;
162+
const userId: String = user.userId;
163+
164+
return request(app)
165+
.post('/publishProject')
166+
.set('Content-Type', 'application/json')
167+
.set('Cookie', [`ssid=${user.userId}`])
168+
.send({ _id, project, comments, userId, username, name })//_id, project, comments, userId, username, name
169+
.expect(200)
170+
.then((res) => {
171+
expect(res.body._id).not.toEqual(_id)
172+
});
173+
});
139174
});
140175
});
141176

142177
//test getMarketplaceProjects endpoint
143178
describe('/getMarketplaceProjects', () => {//most recent project should be the one from publishProject
179+
144180
describe('GET', () => {
145181
it('responds with status of 200 and json object equal to unpublished project', async () => {
146182
return request(app)
147183
.get('/getMarketplaceProjects')
148184
.set('Content-Type', 'application/json')
149185
.expect(200)
150186
.then((res) => {
151-
152187
expect(Array.isArray(res.body)).toBe(true);
153188
expect(res.body[0]._id).toBeTruthy;
154189
});
@@ -164,13 +199,7 @@ describe('Server endpoint tests', () => {
164199
const projObj = await request(app)
165200
.get('/getMarketplaceProjects')
166201
.set('Content-Type', 'application/json')
167-
console.log(projObj.body)
168-
// const _id: String = projObj.body._id;
169-
// const project: String = projObj.body.project;
170-
// const comments: String = projObj.body.comments;
171-
// const username: String = projObj.body.username;
172-
// const name: String = projObj.body.name;
173-
// const userId: String = user.userId;
202+
174203
return request(app)
175204
.get(`/cloneProject/${projObj.body[0]._id}`)
176205
.set('Cookie', [`ssid=${user.userId}`]) // Set the cookie
@@ -181,6 +210,21 @@ describe('Server endpoint tests', () => {
181210
expect(res.body.username).toBe(user.username);
182211
});
183212
});
213+
it('responds with status of 500 and error', async () => {
214+
215+
const projObj = await request(app)
216+
.get('/getMarketplaceProjects')
217+
.set('Content-Type', 'application/json')
218+
219+
return request(app)
220+
.get(`/cloneProject/${projObj.body[0]._id}`)
221+
.set('Cookie', [`ssid=${user.userId}`]) // Set the cookie
222+
.query({ username: [] })
223+
.expect(500)
224+
.then((res) => {
225+
expect(res.body.err).not.toBeNull()
226+
});
227+
});
184228
});
185229
});
186230

@@ -195,12 +239,51 @@ describe('Server endpoint tests', () => {
195239
.patch('/unpublishProject')
196240
.set('Content-Type', 'application/json')
197241
.set('Cookie', [`ssid=${user.userId}`])
198-
.send({ _id, userId })//_id, project, comments, userId, username, name
242+
.send({ _id, userId })
199243
.expect(200)
200244
.then((res) => {
201245
expect(res.body._id).toBe(_id)
202246
expect(res.body.published).toBe(false);
203-
}); // @Denton might want to check more of these fields
247+
});
248+
});
249+
it('responds with status of 500 and error if userId and cookie ssid do not match', async () => {
250+
const projObj: Response = await request(app).post('/getProjects').set('Accept', 'application/json').set('Cookie', [`ssid=${user.userId}`]).send({ userId: projectToSave.userId });
251+
const _id: String = projObj.body[0]._id;
252+
const project: String = projObj.body[0].project;
253+
const comments: String = projObj.body[0].comments;
254+
const username: String = projObj.body[0].username;
255+
const name: String = projObj.body[0].name;
256+
let userId: String = user.userId;
257+
await request(app)//publishing a project first
258+
.post('/publishProject')
259+
.set('Content-Type', 'application/json')
260+
.set('Cookie', [`ssid=${user.userId}`])
261+
.send({ _id, project, comments, userId, username, name })
262+
263+
264+
userId = "ERROR";
265+
return request(app)
266+
.patch('/unpublishProject')
267+
.set('Content-Type', 'application/json')
268+
.set('Cookie', [`ssid=${user.userId}`])
269+
.send({ _id, userId })
270+
.expect(500)
271+
.then((res) => {
272+
expect(res.body.err).not.toBeNull()
273+
});
274+
});
275+
it('responds with status of 500 and error if _id was not a string', async () => {
276+
const userId: String = user.userId;
277+
278+
return request(app)
279+
.patch('/unpublishProject')
280+
.set('Content-Type', 'application/json')
281+
.set('Cookie', [`ssid=${user.userId}`])
282+
.send({userId })
283+
.expect(500)
284+
.then((res) => {
285+
expect(res.body.err).not.toBeNull()
286+
});
204287
});
205288
});
206289
});

mockData.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import HTMLTypes from "./app/src/redux/HTMLTypes";
2+
13
const mockObj = {
24
//these user credentials were created via the signup page
35
//once connecting to new mongo cluster, you must create new login credentials to test
@@ -24,7 +26,12 @@ const mockObj = {
2426
rootComponents: [1],
2527
canvasFocus: { componentId: 1, childId: null },
2628
nextComponentId: 2,
27-
nextChildId: 1
29+
nextChildId: 1,
30+
nextTopSeparatorId: 1000,
31+
HTMLTypes: HTMLTypes, // left as is for now
32+
tailwind: false,
33+
stylesheet: '',
34+
codePreview: false,
2835
},
2936

3037
projectToSave: {
@@ -48,7 +55,12 @@ const mockObj = {
4855
rootComponents: [1],
4956
canvasFocus: { componentId: 1, childId: null },
5057
nextComponentId: 2,
51-
nextChildId: 1
58+
nextChildId: 1,
59+
nextTopSeparatorId: 1000,
60+
HTMLTypes: HTMLTypes, // left as is for now
61+
tailwind: false,
62+
stylesheet: '',
63+
codePreview: false,
5264
}
5365
},
5466
//The following is for graphQL

server/controllers/marketplaceController.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ const marketplaceController: MarketplaceController = {
4040
const { _id, project, comments, userId, username, name } = req.body;
4141
const createdAt = Date.now();
4242

43-
if (userId === req.cookies.ssid || process.env.NODE_ENV === 'test') {
44-
43+
if (userId === req.cookies.ssid) {
4544
if (mongoose.isValidObjectId(_id)) {
46-
4745
const noPub = {...project}
4846
delete noPub.published;
4947
delete noPub._id;
@@ -60,6 +58,7 @@ const marketplaceController: MarketplaceController = {
6058
res.locals.publishedProject = publishedProject;
6159
return next();
6260
}else{
61+
6362
const noId = {...project};
6463
delete noId._id; //removing the empty string _id from project
6564
delete noId.published;
@@ -70,7 +69,7 @@ const marketplaceController: MarketplaceController = {
7069
}
7170
}
7271
else {
73-
console.log('userId did not match')
72+
7473
// we should not expect a user to be able to access another user's id, but included error handling for unexpected errors
7574
return next({
7675
log: 'Error in marketplaceController.publishProject',
@@ -91,11 +90,11 @@ const marketplaceController: MarketplaceController = {
9190
const { _id, userId } = req.body;
9291
//check if req.cookies.ssid matches userId
9392

94-
if (userId === req.cookies.ssid || process.env.NODE_ENV === 'test' ) {
93+
if (userId === req.cookies.ssid ) {
9594
Projects.findOneAndUpdate({ _id }, {published: false}, { new: true }, (err, result) => {
96-
if (err) {
95+
if (err || result === null) {
9796
return next({
98-
log: `Error in marketplaceController.unpublishProject: ${err}`,
97+
log: `Error in marketplaceController.unpublishProject: ${err || null}`,
9998
message: {
10099
err: 'Error in marketplaceController.unpublishProject, check server logs for details'
101100
}

0 commit comments

Comments
 (0)