1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

fix: make users emails case-insensitive (#804)

This commit is contained in:
Christopher Kolstad 2021-04-23 15:30:23 +02:00 committed by GitHub
parent 97d77d84cc
commit c729f514cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 3 deletions

View File

@ -28,10 +28,12 @@ const emptify = value => {
return value;
};
const safeToLower = (s?: string) => (s ? s.toLowerCase() : s);
const mapUserToColumns = (user: ICreateUser) => ({
name: user.name,
username: user.username,
email: user.email,
email: safeToLower(user.email),
image_url: user.imageUrl,
});
@ -114,7 +116,7 @@ class UserStore {
return query.where('id', q.id);
}
if (q.email) {
return query.where('email', q.email);
return query.where('email', safeToLower(q.email));
}
if (q.username) {
return query.where('username', q.username);

View File

@ -0,0 +1,15 @@
exports.up = function(db, cb) {
db.runSql(
`
DELETE FROM users WHERE id IN
(SELECT id FROM
(SELECT id, lower(email) as email, row_number() over (PARTITION BY lower(email) ORDER BY id desc) as Row FROM users) as dupes
WHERE email IS NOT NULL AND dupes.Row > 1);
UPDATE users SET email = LOWER(email);
`,
cb,
);
};
exports.down = function() {};

View File

@ -128,7 +128,7 @@ test.serial('should reset user after successful login', async t => {
test.serial('should only update specified fields on user', async t => {
const store = stores.userStore;
const email = 'userTobeUpdated@mail.com';
const email = 'usertobeupdated@mail.com';
const user = {
email,
username: 'test',
@ -143,3 +143,40 @@ test.serial('should only update specified fields on user', async t => {
t.deepEqual(storedUser.email, user.email);
t.deepEqual(storedUser.username, user.username);
});
test.serial('should always lowercase emails on inserts', async t => {
const store = stores.userStore;
const email = 'someCrazyCasingGoingOn@mail.com';
const user = {
email,
};
await store.upsert(user);
const storedUser = await store.get({ email });
t.deepEqual(storedUser.email, user.email.toLowerCase());
});
test.serial('should always lowercase emails on updates', async t => {
const store = stores.userStore;
const email = 'someCrazyCasingGoingOn@mail.com';
const user = {
email,
};
await store.upsert(user);
let storedUser = await store.get({ email });
t.deepEqual(storedUser.email, user.email.toLowerCase());
const updatedUser = {
id: storedUser.id,
email: 'SomeOtherCasing@hotmail.com',
};
await store.upsert(updatedUser);
storedUser = await store.get({ id: storedUser.id });
t.is(storedUser.email, updatedUser.email.toLowerCase());
});