|
| 1 | +const request = require('supertest'); |
| 2 | +const app = require('../app'); |
| 3 | +const { User } = require('../models/User'); |
| 4 | +const { Registration } = require('../models/Registration'); |
| 5 | + |
| 6 | +const registrations = []; |
| 7 | + |
| 8 | +beforeAll(async () => { |
| 9 | + // Create registration entries |
| 10 | + registrations.push(await Registration.create()); |
| 11 | + registrations.push(await Registration.create()); |
| 12 | +}); |
| 13 | + |
| 14 | +afterAll(async () => { |
| 15 | + // Delete registration entries |
| 16 | + await Registration.remove({ guid: { $in: registrations } }, { multi: true }); |
| 17 | + |
| 18 | + // Delete new users |
| 19 | + await User.remove({ username: 'jester-register' }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('Get /register page', () => { |
| 23 | + test('With valid id', async () => { |
| 24 | + const response = await request(app).get(`/register/${registrations[0]}`); |
| 25 | + expect(response.statusCode).toBe(200); |
| 26 | + expect(response.text).toMatch('Pinpoint Registration'); |
| 27 | + }); |
| 28 | + |
| 29 | + |
| 30 | + test('With invalid id', async () => { |
| 31 | + const response = await request(app).get('/register/abc123'); |
| 32 | + expect(response.text).toBe('Registration Used, please request a new link.'); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('Use registration code', () => { |
| 37 | + test('With valid id', async () => { |
| 38 | + const response = await request(app).post(`/register/${registrations[0]}`) |
| 39 | + .type('form') |
| 40 | + .send({ username: 'jester-register' }) |
| 41 | + .send({ password: 'jester' }); |
| 42 | + expect(response.statusCode).toBe(200); |
| 43 | + expect(response.text).toBe('Register Successful'); |
| 44 | + expect((await User.find({ username: 'jester-register' })).length).toBe(1); |
| 45 | + }); |
| 46 | + |
| 47 | + |
| 48 | + test('With invalid id', async () => { |
| 49 | + const response = await request(app).post(`/register/abc123`) |
| 50 | + .type('form') |
| 51 | + .send({ username: 'jester-register2' }) |
| 52 | + .send({ password: 'jester' }); |
| 53 | + expect(response.statusCode).toBe(200); |
| 54 | + expect(response.text).toBe('Registration Used, please request a new link.'); |
| 55 | + }); |
| 56 | +}); |
0 commit comments