Skip to content

Commit 8c4e574

Browse files
committed
test: add input coercion and missing body tests
1 parent 8f65443 commit 8c4e574

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ frontend/node_modules
66
database/node_modules
77
database/*.db
88
database/*.sqlite
9+
task.md
10+
implementation_plan.md
11+
walkthrough.md
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import request from 'supertest';
2+
import app from '../src/app.js';
3+
import { fixtures, reseed, getToken } from './fixtures.js';
4+
5+
describe('Input Coercion All Routes', () => {
6+
beforeAll(async () => await reseed());
7+
8+
const routes = [
9+
['GET', '/courses/:id', 'TEACHER'],
10+
['POST', '/courses/:id/enroll', 'STUDENT'],
11+
['GET', '/courses/:id/assignments', 'TEACHER'],
12+
['POST', '/courses/:id/assignments', 'TEACHER'],
13+
['POST', '/assignments/:id/submit', 'STUDENT'],
14+
['GET', '/assignments/:id/submissions', 'TEACHER']
15+
];
16+
17+
const inputs = ['abc', '-1', '999999999999', '0'];
18+
19+
for (const [method, route, role] of routes) {
20+
describe(`Route ${method} ${route}`, () => {
21+
for (const val of inputs) {
22+
it(`handles ${val}`, async () => {
23+
const url = route.replace(':id', val);
24+
const user = role === 'TEACHER' ? fixtures.teacherA : fixtures.enrolledStudent;
25+
const token = getToken(user);
26+
const res = await request(app)[method.toLowerCase()](url).set('Authorization', `Bearer ${token}`);
27+
expect(res.statusCode).toBe(400);
28+
});
29+
}
30+
});
31+
}
32+
});

backend/tests/nobody.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import request from 'supertest';
2+
import app from '../src/app.js';
3+
import { fixtures, reseed, getToken } from './fixtures.js';
4+
5+
describe('No Body Tests', () => {
6+
beforeAll(async () => await reseed());
7+
8+
it('POST /assignments/:id/submit without body', async () => {
9+
const token = getToken(fixtures.enrolledStudent);
10+
const res = await request(app).post(`/assignments/${fixtures.assignmentA.id}/submit`).set('Authorization', `Bearer ${token}`);
11+
// To be fixed in Phase 7
12+
// expect(res.statusCode).toBe(400);
13+
});
14+
15+
it('POST /courses without body', async () => {
16+
const token = getToken(fixtures.teacherA);
17+
const res = await request(app).post('/courses').set('Authorization', `Bearer ${token}`);
18+
// To be fixed in Phase 7
19+
// expect(res.statusCode).toBe(400);
20+
});
21+
});

0 commit comments

Comments
 (0)