-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(LoginEvent): prevent duplicate entries (#104)
- Loading branch information
1 parent
afaecab
commit b8277cb
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { after, afterEach, describe } from 'mocha'; | ||
import { expect } from 'chai'; | ||
import { LoginEvent } from '../models'; | ||
import { LoginEventController } from '@server/controllers/LoginEventController'; | ||
import { server } from '@server/index'; | ||
import { User } from '@server/models'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
describe('LoginEventController', () => { | ||
const controller = new LoginEventController(); | ||
let user1: User; | ||
|
||
before(async () => { | ||
user1 = await User.create({ | ||
uuid: uuidv4(), | ||
email: '[email protected]', | ||
}); | ||
}); | ||
after(async () => { | ||
await User.destroy({ where: {} }); | ||
if (server?.listening) { | ||
server.close(); | ||
} | ||
}); | ||
afterEach(async () => { | ||
await LoginEvent.destroy({ where: {} }); | ||
}); | ||
|
||
describe('log', () => { | ||
it('should log event', async () => { | ||
await controller.log(user1.uuid); | ||
const foundLog = await LoginEvent.findOne({ where: { user_id: user1.uuid } }); | ||
expect(foundLog).to.not.be.null; | ||
}); | ||
it('should log event with timestamp specified', async () => { | ||
await controller.log(user1.uuid, new Date(1000)); | ||
const foundLog = await LoginEvent.findOne({ where: { user_id: user1.uuid } }); | ||
expect(foundLog).to.not.be.null; | ||
expect(foundLog?.get('timestamp')?.getTime()).to.deep.equal(1000); | ||
}); | ||
it('should not error on duplicate entry', async () => { | ||
const timestamp = new Date(); | ||
await controller.log(user1.uuid, timestamp); | ||
await controller.log(user1.uuid, timestamp); | ||
const foundLogs = await LoginEvent.findAll({ where: { user_id: user1.uuid } }); | ||
expect(foundLogs?.length).to.deep.equal(1); | ||
}); | ||
}); | ||
}); |