-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(user): release username and email after activation account token …
…expires There is an extra safeguard in `ON CONFLICT` SQL to prevent duplicate users from being created if this has not been validated before.
- Loading branch information
Showing
3 changed files
with
317 additions
and
26 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { version as uuidVersion } from 'uuid'; | |
import password from 'models/password.js'; | ||
import user from 'models/user.js'; | ||
import orchestrator from 'tests/orchestrator.js'; | ||
import RequestBuilder from 'tests/request-builder'; | ||
|
||
beforeAll(async () => { | ||
await orchestrator.waitForAllServices(); | ||
|
@@ -11,6 +12,8 @@ beforeAll(async () => { | |
}); | ||
|
||
describe('POST /api/v1/users', () => { | ||
const usersRequestBuilder = new RequestBuilder('/api/v1/users'); | ||
|
||
describe('Anonymous user', () => { | ||
test('With unique and valid data', async () => { | ||
const response = await fetch(`${orchestrator.webserverUrl}/api/v1/users`, { | ||
|
@@ -208,6 +211,43 @@ describe('POST /api/v1/users', () => { | |
expect(secondResponseBody.key).toBe('username'); | ||
}); | ||
|
||
test('With "username" and "email" duplicated', async () => { | ||
const { response: firstResponse, responseBody: firstResponseBody } = await usersRequestBuilder.post({ | ||
username: 'SaMeUsErNaMe', | ||
email: '[email protected]', | ||
password: 'validpassword', | ||
}); | ||
|
||
expect.soft(firstResponse.status).toBe(201); | ||
|
||
await orchestrator.nukeUser(firstResponseBody); | ||
|
||
const activateAccountToken = await orchestrator.getLastActivateAccountToken(); | ||
await orchestrator.updateActivateAccountToken(activateAccountToken.id, { | ||
expires_at: new Date(Date.now() - 1000), | ||
}); | ||
|
||
const { response: secondResponse, responseBody: secondResponseBody } = await usersRequestBuilder.post({ | ||
username: 'sameUsername', | ||
email: '[email protected]', | ||
password: 'new-password', | ||
}); | ||
|
||
expect.soft(secondResponse.status).toBe(400); | ||
expect(secondResponseBody).toStrictEqual({ | ||
status_code: 400, | ||
name: 'ValidationError', | ||
message: 'O "username" informado já está sendo usado.', | ||
action: 'Ajuste os dados enviados e tente novamente.', | ||
error_location_code: 'MODEL:USER:VALIDATE_UNIQUE_USERNAME:ALREADY_EXISTS', | ||
key: 'username', | ||
error_id: secondResponseBody.error_id, | ||
request_id: secondResponseBody.request_id, | ||
}); | ||
expect(uuidVersion(secondResponseBody.error_id)).toBe(4); | ||
expect(uuidVersion(secondResponseBody.request_id)).toBe(4); | ||
}); | ||
|
||
test('With "username" missing', async () => { | ||
const response = await fetch(`${orchestrator.webserverUrl}/api/v1/users`, { | ||
method: 'POST', | ||
|
@@ -762,4 +802,178 @@ describe('POST /api/v1/users', () => { | |
expect(responseBody.key).toBe('object'); | ||
}); | ||
}); | ||
|
||
test('With a duplicate "username" for a user with expired activation token', async () => { | ||
const { response: firstResponse, responseBody: firstResponseBody } = await usersRequestBuilder.post({ | ||
username: 'ARepeatedUsername', | ||
email: '[email protected]', | ||
password: 'validpassword', | ||
}); | ||
|
||
expect.soft(firstResponse.status).toBe(201); | ||
|
||
const activateAccountToken = await orchestrator.getLastActivateAccountToken(); | ||
await orchestrator.updateActivateAccountToken(activateAccountToken.id, { | ||
expires_at: new Date(Date.now() - 1000), | ||
}); | ||
|
||
const { response: secondResponse, responseBody: secondResponseBody } = await usersRequestBuilder.post({ | ||
username: 'ARepeatedUSERNAME', | ||
email: '[email protected]', | ||
password: 'new-password', | ||
}); | ||
|
||
expect.soft(secondResponse.status).toBe(201); | ||
expect(secondResponseBody).toStrictEqual({ | ||
id: firstResponseBody.id, | ||
username: 'ARepeatedUSERNAME', | ||
description: '', | ||
features: ['read:activation_token'], | ||
tabcoins: 0, | ||
tabcash: 0, | ||
created_at: secondResponseBody.created_at, | ||
updated_at: secondResponseBody.updated_at, | ||
}); | ||
|
||
expect(new Date(secondResponseBody.created_at).getTime()).toBeGreaterThan( | ||
new Date(firstResponseBody.created_at).getTime(), | ||
); | ||
expect(new Date(secondResponseBody.updated_at).getTime()).toBeGreaterThan( | ||
new Date(firstResponseBody.updated_at).getTime(), | ||
); | ||
|
||
const userInDatabase = await user.findOneByUsername('ARepeatedUSERNAME'); | ||
const passwordsMatch = await password.compare('new-password', userInDatabase.password); | ||
|
||
expect(passwordsMatch).toBe(true); | ||
expect(userInDatabase.email).toBe('[email protected]'); | ||
}); | ||
|
||
test('With a duplicate "email" for a user with expired activation token', async () => { | ||
const { response: firstResponse, responseBody: firstResponseBody } = await usersRequestBuilder.post({ | ||
username: 'ARepeatedEmail', | ||
email: '[email protected]', | ||
password: 'validpassword', | ||
}); | ||
|
||
expect.soft(firstResponse.status).toBe(201); | ||
|
||
const activateAccountToken = await orchestrator.getLastActivateAccountToken(); | ||
await orchestrator.updateActivateAccountToken(activateAccountToken.id, { | ||
expires_at: new Date(Date.now() - 1000), | ||
}); | ||
|
||
const { response: secondResponse, responseBody: secondResponseBody } = await usersRequestBuilder.post({ | ||
username: 'ARepeatedEmail2', | ||
email: '[email protected]', | ||
password: 'new-password', | ||
}); | ||
|
||
expect.soft(secondResponse.status).toBe(201); | ||
expect(secondResponseBody).toStrictEqual({ | ||
id: firstResponseBody.id, | ||
username: 'ARepeatedEmail2', | ||
description: '', | ||
features: ['read:activation_token'], | ||
tabcoins: 0, | ||
tabcash: 0, | ||
created_at: secondResponseBody.created_at, | ||
updated_at: secondResponseBody.updated_at, | ||
}); | ||
|
||
expect(new Date(secondResponseBody.created_at).getTime()).toBeGreaterThan( | ||
new Date(firstResponseBody.created_at).getTime(), | ||
); | ||
expect(new Date(secondResponseBody.updated_at).getTime()).toBeGreaterThan( | ||
new Date(firstResponseBody.updated_at).getTime(), | ||
); | ||
|
||
const userInDatabase = await user.findOneByUsername('ARepeatedEmail2'); | ||
const passwordsMatch = await password.compare('new-password', userInDatabase.password); | ||
|
||
expect(passwordsMatch).toBe(true); | ||
expect(userInDatabase.email).toBe('[email protected]'); | ||
}); | ||
|
||
test('With a duplicate "username" for a nuked user with expired activation token', async () => { | ||
const { response: firstResponse, responseBody: firstResponseBody } = await usersRequestBuilder.post({ | ||
username: 'NukedSameUsername', | ||
email: '[email protected]', | ||
password: 'validpassword', | ||
}); | ||
|
||
expect.soft(firstResponse.status).toBe(201); | ||
|
||
await orchestrator.nukeUser(firstResponseBody); | ||
|
||
const activateAccountToken = await orchestrator.getLastActivateAccountToken(); | ||
await orchestrator.updateActivateAccountToken(activateAccountToken.id, { | ||
expires_at: new Date(Date.now() - 1000), | ||
}); | ||
|
||
const { response: secondResponse, responseBody: secondResponseBody } = await usersRequestBuilder.post({ | ||
username: 'NukedSameUsername', | ||
email: '[email protected]', | ||
password: 'new-password', | ||
}); | ||
|
||
expect.soft(secondResponse.status).toBe(400); | ||
expect(secondResponseBody).toStrictEqual({ | ||
status_code: 400, | ||
name: 'ValidationError', | ||
message: 'O "username" informado já está sendo usado.', | ||
action: 'Ajuste os dados enviados e tente novamente.', | ||
error_location_code: 'MODEL:USER:VALIDATE_UNIQUE_USERNAME:ALREADY_EXISTS', | ||
key: 'username', | ||
error_id: secondResponseBody.error_id, | ||
request_id: secondResponseBody.request_id, | ||
}); | ||
expect(uuidVersion(secondResponseBody.error_id)).toBe(4); | ||
expect(uuidVersion(secondResponseBody.request_id)).toBe(4); | ||
}); | ||
|
||
test('With "username" and "email" duplicated from different users, one with expired token and the other nuked', async () => { | ||
const { response: firstResponse, responseBody: firstResponseBody } = await usersRequestBuilder.post({ | ||
username: 'firstUserNuked', | ||
email: '[email protected]', | ||
password: 'validpassword', | ||
}); | ||
|
||
expect.soft(firstResponse.status).toBe(201); | ||
|
||
await orchestrator.nukeUser(firstResponseBody); | ||
|
||
const { response: secondResponse } = await usersRequestBuilder.post({ | ||
username: 'secondUserExpiredToken', | ||
email: '[email protected]', | ||
password: 'validpassword', | ||
}); | ||
|
||
expect.soft(secondResponse.status).toBe(201); | ||
|
||
const activateAccountToken = await orchestrator.getLastActivateAccountToken(); | ||
await orchestrator.updateActivateAccountToken(activateAccountToken.id, { | ||
expires_at: new Date(Date.now() - 1000), | ||
}); | ||
|
||
const { response: thirdResponse, responseBody: thirdResponseBody } = await usersRequestBuilder.post({ | ||
username: 'firstUserNuked', | ||
email: '[email protected]', | ||
password: 'new-password', | ||
}); | ||
|
||
expect.soft(thirdResponse.status).toBe(400); | ||
expect(thirdResponseBody).toStrictEqual({ | ||
status_code: 400, | ||
name: 'ValidationError', | ||
message: 'O "username" informado já está sendo usado.', | ||
action: 'Ajuste os dados enviados e tente novamente.', | ||
error_location_code: 'MODEL:USER:VALIDATE_UNIQUE_USERNAME:ALREADY_EXISTS', | ||
key: 'username', | ||
error_id: thirdResponseBody.error_id, | ||
request_id: thirdResponseBody.request_id, | ||
}); | ||
expect(uuidVersion(thirdResponseBody.error_id)).toBe(4); | ||
expect(uuidVersion(thirdResponseBody.request_id)).toBe(4); | ||
}); | ||
}); |
Oops, something went wrong.