|
| 1 | +import { after, afterEach, describe } from 'mocha'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { LoginEvent } from '../models'; |
| 4 | +import { LoginEventController } from '@server/controllers/LoginEventController'; |
| 5 | +import { server } from '@server/index'; |
| 6 | +import { User } from '@server/models'; |
| 7 | +import { v4 as uuidv4 } from 'uuid'; |
| 8 | + |
| 9 | +describe('LoginEventController', () => { |
| 10 | + const controller = new LoginEventController(); |
| 11 | + let user1: User; |
| 12 | + |
| 13 | + before(async () => { |
| 14 | + user1 = await User.create({ |
| 15 | + uuid: uuidv4(), |
| 16 | + email: 'info@libretext.org', |
| 17 | + }); |
| 18 | + }); |
| 19 | + after(async () => { |
| 20 | + await User.destroy({ where: {} }); |
| 21 | + if (server?.listening) { |
| 22 | + server.close(); |
| 23 | + } |
| 24 | + }); |
| 25 | + afterEach(async () => { |
| 26 | + await LoginEvent.destroy({ where: {} }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('log', () => { |
| 30 | + it('should log event', async () => { |
| 31 | + await controller.log(user1.uuid); |
| 32 | + const foundLog = await LoginEvent.findOne({ where: { user_id: user1.uuid } }); |
| 33 | + expect(foundLog).to.not.be.null; |
| 34 | + }); |
| 35 | + it('should log event with timestamp specified', async () => { |
| 36 | + await controller.log(user1.uuid, new Date(1000)); |
| 37 | + const foundLog = await LoginEvent.findOne({ where: { user_id: user1.uuid } }); |
| 38 | + expect(foundLog).to.not.be.null; |
| 39 | + expect(foundLog?.get('timestamp')?.getTime()).to.deep.equal(1000); |
| 40 | + }); |
| 41 | + it('should not error on duplicate entry', async () => { |
| 42 | + const timestamp = new Date(); |
| 43 | + await controller.log(user1.uuid, timestamp); |
| 44 | + await controller.log(user1.uuid, timestamp); |
| 45 | + const foundLogs = await LoginEvent.findAll({ where: { user_id: user1.uuid } }); |
| 46 | + expect(foundLogs?.length).to.deep.equal(1); |
| 47 | + }); |
| 48 | + }); |
| 49 | +}); |
0 commit comments