|
| 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 | +}); |
0 commit comments