WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit f96c611

Browse files
committed
fix(contact): update username using action param instead of config
1 parent ae115fd commit f96c611

File tree

6 files changed

+58
-20
lines changed

6 files changed

+58
-20
lines changed

schemas/contacts.challenge.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@
1212
},
1313
"contact": {
1414
"$ref": "common.json#/definitions/contact",
15-
"required": ["value"]
15+
"required": [
16+
"value"
17+
]
1618
},
1719
"i18nLocale": {
1820
"type": "string",
1921
"minLength": 1,
2022
"maxLength": 10
2123
},
2224
"metadata": {
23-
"type": "object"
25+
"type": "object",
26+
"properties": {
27+
"updateUsername": {
28+
"type": "boolean"
29+
}
30+
}
2431
}
2532
}
2633
}

schemas/contacts.remove.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
},
1313
"contact": {
1414
"$ref": "common.json#/definitions/contact"
15+
},
16+
"updateUsername": {
17+
"type": "boolean"
1518
}
1619
}
1720
}

src/actions/contacts/remove.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ const { getUserId } = require('../../utils/userData');
1414
* @apiParam (Payload) {String} username -
1515
*/
1616
module.exports = async function remove({ params }) {
17-
const userId = await getUserId.call(this, params.username);
18-
return contacts.remove.call(this, { contact: params.contact, userId });
17+
const { contact, updateUsername, username } = params;
18+
const userId = await getUserId.call(this, username);
19+
20+
return contacts.remove.call(this, {
21+
contact,
22+
updateUsername,
23+
userId,
24+
});
1925
};
2026

2127
module.exports.transports = [ActionTransport.amqp, ActionTransport.internal];

src/configs/verification-challenges.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,4 @@ exports.contacts = {
7979
max: 5,
8080
onlyOneVerifiedEmail: true,
8181
allowRemoveFirstContact: true,
82-
updateUsername: true,
8382
};

src/utils/contacts.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ async function challenge({ userId, contact, i18nLocale, metadata = {} }) {
166166
async function verifyEmail({ secret }) {
167167
const { redis, tokenManager } = this;
168168
const { metadata } = await tokenManager.verify(secret);
169-
const { userId, contact } = metadata;
169+
const { userId, contact, metadata: tokenMetadata } = metadata;
170+
const { updateUsername } = tokenMetadata;
170171
const lock = await this.dlock.manager.once(lockContact(contact.value));
171172
const key = redisKey(userId, USERS_CONTACTS, contact.value);
172173

@@ -175,7 +176,7 @@ async function verifyEmail({ secret }) {
175176
if (this.config.contacts.onlyOneVerifiedEmail) {
176177
await removeAllEmailContactsOfUser.call(this, pipe, userId, contact.value);
177178
}
178-
if (this.config.contacts.updateUsername) {
179+
if (updateUsername) {
179180
await replaceUserName.call(this, pipe, userId, contact.value);
180181
}
181182
pipe.hset(key, 'verified', 'true');
@@ -211,7 +212,7 @@ async function verify({ userId, contact, token }) {
211212
return redis.hgetall(key).then(parseObj);
212213
}
213214

214-
async function remove({ userId, contact }) {
215+
async function remove({ contact, updateUsername, userId }) {
215216
const { redis, tokenManager, log } = this;
216217
const key = redisKey(userId, USERS_CONTACTS, contact.value);
217218

@@ -241,7 +242,7 @@ async function remove({ userId, contact }) {
241242
pipe.del(key);
242243
pipe.srem(redisKey(userId, USERS_CONTACTS), contact.value);
243244

244-
if (this.config.contacts.updateUsername) {
245+
if (updateUsername) {
245246
pipe.hdel(USERS_USERNAME_TO_ID, contact.value);
246247
}
247248

test/suites/actions/contacts.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ describe('#user contacts', function registerSuite() {
256256
},
257257
metadata: {
258258
name: 'Test',
259+
updateUsername: true,
259260
},
260261
};
261262

@@ -291,23 +292,44 @@ describe('#user contacts', function registerSuite() {
291292
});
292293

293294
it('should remove username to userid mapping on contact removal', async function test() {
294-
const params = {
295-
username: this.testUser.id,
296-
contact: {
297-
298-
type: 'email',
295+
await this.users.dispatch('contacts.add', {
296+
params: {
297+
contact: {
298+
299+
type: 'email',
300+
},
301+
username: this.testUser.id,
299302
},
300-
};
301-
await this.users.dispatch('contacts.add', { params });
303+
});
302304
const amqpStub = sinon.stub(this.users.amqp, 'publish');
303305
amqpStub.withArgs('mailer.predefined').resolves({ queued: true });
304-
await this.users.dispatch('contacts.challenge', { params });
306+
await this.users.dispatch('contacts.challenge', {
307+
params: {
308+
contact: {
309+
310+
type: 'email',
311+
},
312+
metadata: {
313+
updateUsername: true,
314+
},
315+
username: this.testUser.id,
316+
},
317+
});
305318
const { ctx: { template: { token: { secret } } } } = amqpStub.args[0][1];
306319
await this.users.dispatch('contacts.verify-email', { params: { secret } });
307-
const userid = await this.users.redis.hget(USERS_USERNAME_TO_ID, params.contact.value);
320+
const userid = await this.users.redis.hget(USERS_USERNAME_TO_ID, '[email protected]');
308321
assert.notEqual(userid, null);
309-
await this.users.dispatch('contacts.remove', { params });
310-
const useridRemoved = await this.users.redis.hget(USERS_USERNAME_TO_ID, params.contact.value);
322+
await this.users.dispatch('contacts.remove', {
323+
params: {
324+
contact: {
325+
326+
type: 'email',
327+
},
328+
updateUsername: true,
329+
username: this.testUser.id,
330+
},
331+
});
332+
const useridRemoved = await this.users.redis.hget(USERS_USERNAME_TO_ID, '[email protected]');
311333
assert.equal(useridRemoved, null);
312334
});
313335
});

0 commit comments

Comments
 (0)