Skip to content

Commit

Permalink
fix(user): not allow numerical usernames. Partially fixes MA-176 (#233)
Browse files Browse the repository at this point in the history
* fix(user): not allow numerical usernames

* chore(user): add test
  • Loading branch information
WikiRik authored Feb 22, 2021
1 parent 76ab1f3 commit c8f6622
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const User = sequelize.define('user', {
if (!USERNAME_REGEX.test(value)) {
throw new Error('Username should only contain letters, numbers, dots, underscores and dashes.');
}
if (value.match(/^[0-9._-]+$/)) {
throw new Error('Username should have at least 1 letter.');
}
}
},
unique: true
Expand Down
18 changes: 18 additions & 0 deletions test/api/campaigns-submission.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ describe('Campaign submission', () => {
expect(res.body.errors).toHaveProperty('username');
});

test('should fail if the username has no letters', async () => {
const campaign = await generator.createCampaign();
const user = generator.generateUser({ username: '123' });

const res = await request({
uri: '/signup/' + campaign.url,
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: user
});

expect(res.statusCode).toEqual(422);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('errors');
expect(res.body.errors).toHaveProperty('username');
});

test('should fail if the first name is invalid', async () => {
const campaign = await generator.createCampaign();
const user = generator.generateUser({ first_name: '!@#!@#D' });
Expand Down

0 comments on commit c8f6622

Please sign in to comment.