Skip to content

Commit 9bcce33

Browse files
authored
fix: remove contact email from username (#618)
* fix: remove contact email from username * fix: test added * fix: test added * fix: docker * fix: docker * fix: test * fix: test * fix: test1 * fix: test assert was changed 22 node in ms-token
1 parent 8029b3b commit 9bcce33

5 files changed

Lines changed: 35 additions & 13 deletions

File tree

.mdeprc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if (os.platform() !== 'darwin') {
1414
} catch (e) { }
1515
}
1616

17-
exports.node = "20";
17+
exports.node = "22.13.1";
1818
exports.in_one = true;
1919
exports.auto_compose = true;
2020
exports.with_local_compose = true;

Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
FROM makeomatic/node:$NODE_VERSION
22

33
ENV NCONF_NAMESPACE=MS_USERS \
4-
NODE_ENV=$NODE_ENV \
5-
COREPACK_ENABLE_NETWORK=1
4+
NODE_ENV=$NODE_ENV
65

76
WORKDIR /src
87

@@ -12,7 +11,6 @@ RUN \
1211
apk --update --upgrade \
1312
add ca-certificates --virtual .buildDeps git ca-certificates openssl g++ make python3 linux-headers \
1413
&& update-ca-certificates \
15-
&& corepack install -g pnpm@9 \
1614
&& chown node:node /src \
1715
&& su node sh -c "cd /src && pnpm fetch --prod" \
1816
&& su node sh -c "rm -rf ~/.cache && pnpm store prune" \

src/utils/contacts.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { HttpStatusError } = require('@microfleet/validation');
33
const challengeAct = require('./challenges/challenge');
44
const redisKey = require('./key');
55
const handlePipeline = require('./pipeline-error');
6-
const { getInternalData, getUserId } = require('./userData');
6+
const { getUserId } = require('./userData');
77
const {
88
USERS_CONTACTS,
99
USERS_DEFAULT_CONTACT,
@@ -64,11 +64,8 @@ async function removeAllEmailContactsOfUser(redisPipe, userId, exceptEmail) {
6464
}
6565
}
6666

67-
async function replaceUserName(redisPipe, userId, verifiedEmail) {
67+
async function setUserName(redisPipe, userId, verifiedEmail) {
6868
const { config: { jwt: { defaultAudience } } } = this;
69-
const internalData = await getInternalData.call(this, userId);
70-
const username = internalData[USERS_USERNAME_FIELD];
71-
redisPipe.hdel(USERS_USERNAME_TO_ID, username);
7269
redisPipe.hset(USERS_USERNAME_TO_ID, verifiedEmail, userId);
7370
redisPipe.hset(redisKey(userId, USERS_DATA), USERS_USERNAME_FIELD, verifiedEmail);
7471
redisPipe.hset(redisKey(userId, USERS_METADATA, defaultAudience), USERS_USERNAME_FIELD, JSON.stringify(verifiedEmail));
@@ -176,7 +173,7 @@ async function verifyEmail({ secret }) {
176173
await removeAllEmailContactsOfUser.call(this, pipe, userId, contact.value);
177174
}
178175
if (this.config.contacts.updateUsername) {
179-
await replaceUserName.call(this, pipe, userId, contact.value);
176+
await setUserName.call(this, pipe, userId, contact.value);
180177
}
181178
pipe.hset(key, 'verified', 'true');
182179
metadata.contact.verified = true;
@@ -241,6 +238,10 @@ async function remove({ userId, contact }) {
241238
pipe.del(key);
242239
pipe.srem(redisKey(userId, USERS_CONTACTS), contact.value);
243240

241+
if (this.config.contacts.updateUsername) {
242+
pipe.hdel(USERS_USERNAME_TO_ID, contact.value);
243+
}
244+
244245
return pipe.exec().then(handlePipeline);
245246
}
246247

test/suites/actions/contacts.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { faker } = require('@faker-js/faker');
44
const sinon = require('sinon');
55
const { createMembers } = require('../../helpers/organization');
66
const { startService, clearRedis } = require('../../config');
7+
const { USERS_USERNAME_TO_ID } = require('../../../src/constants');
78

89
describe('#user contacts', function registerSuite() {
910
const audience = '*.localhost';
@@ -288,4 +289,25 @@ describe('#user contacts', function registerSuite() {
288289
assert.equal(name, params.metadata.name);
289290
amqpStub.restore();
290291
});
292+
293+
it('should remove username to userid mapping on contact removal', async function test() {
294+
const params = {
295+
username: this.testUser.username,
296+
contact: {
297+
value: 'email@mail.org',
298+
type: 'email',
299+
},
300+
};
301+
await this.users.dispatch('contacts.add', { params });
302+
const amqpStub = sinon.stub(this.users.amqp, 'publish');
303+
amqpStub.withArgs('mailer.predefined').resolves({ queued: true });
304+
await this.users.dispatch('contacts.challenge', { params });
305+
const { ctx: { template: { token: { secret } } } } = amqpStub.args[0][1];
306+
await this.users.dispatch('contacts.verify-email', { params: { secret } });
307+
const userid = await this.users.redis.hget(USERS_USERNAME_TO_ID, params.contact.value);
308+
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);
311+
assert.equal(useridRemoved, null);
312+
});
291313
});

test/suites/actions/invite.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ describe('#invite', function registerSuite() {
7676
password: '123',
7777
inviteToken: this.invitationToken,
7878
audience: '*.localhost',
79-
} }), {
80-
name: 'AssertionError',
81-
message: `Sanity check failed for "id" failed: "abnormal@yandex.ru" vs "${email}"`,
79+
} }), (err) => {
80+
assert.strictEqual(err.name, 'AssertionError');
81+
assert.match(err.message, /Sanity check failed for "id" failed: "abnormal@yandex.ru" vs "v@yandex.ru"/);
82+
return true;
8283
});
8384
});
8485

0 commit comments

Comments
 (0)