Files
upstream-rocketchat/apps/meteor/tests/unit/server/users/saveUserIdentity.spec.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

134 lines
4.8 KiB
TypeScript

import { expect } from 'chai';
import proxyquire from 'proxyquire';
import sinon from 'sinon';
// Create stubs for dependencies
const stubs = {
findOneUserById: sinon.stub(),
updateUsernameAndMessageOfMentionByIdAndOldUsername: sinon.stub(),
updateUsernameOfEditByUserId: sinon.stub(),
updateAllUsernamesByUserId: sinon.stub(),
updateDirectNameAndFnameByName: sinon.stub(),
updateUserReferences: sinon.stub(),
updateHistoryReferences: sinon.stub(),
setUsername: sinon.stub(),
setRealName: sinon.stub(),
validateName: sinon.stub(),
FileUpload: sinon.stub(),
};
const { saveUserIdentity } = proxyquire.noCallThru().load('../../../../server/lib/users/saveUserIdentity', {
'@rocket.chat/models': {
Users: {
findOneById: stubs.findOneUserById,
},
Messages: {
updateUsernameAndMessageOfMentionByIdAndOldUsername: stubs.updateUsernameAndMessageOfMentionByIdAndOldUsername,
updateUsernameOfEditByUserId: stubs.updateUsernameOfEditByUserId,
updateAllUsernamesByUserId: stubs.updateAllUsernamesByUserId,
},
Subscriptions: {
updateDirectNameAndFnameByName: stubs.updateDirectNameAndFnameByName,
},
VideoConference: {
updateUserReferences: stubs.updateUserReferences,
},
CallHistory: {
updateUserReferences: stubs.updateHistoryReferences,
},
},
'meteor/meteor': {
'Meteor': sinon.stub(),
'@global': true,
},
'../../database/utils': { onceTransactionCommitedSuccessfully: async (cb: any, _sess: any) => cb() },
'../media/file-upload': {
FileUpload: stubs.FileUpload,
},
'./setUsername': {
_setUsername: stubs.setUsername,
_setUsernameWithSession: () => stubs.setUsername,
},
'./setRealName': {
setRealName: stubs.setRealName,
},
'../rooms/updateGroupDMsName': {
updateGroupDMsName: sinon.stub(),
},
'../shared/validateName': {
validateName: stubs.validateName,
},
});
describe('Users - saveUserIdentity', () => {
beforeEach(() => {
// Reset stubs before each test
Object.values(stubs).forEach((stub) => stub.reset());
});
it('should return false if _id is not provided', async () => {
const result = await saveUserIdentity({ _id: undefined });
expect(stubs.findOneUserById.called).to.be.false;
expect(result).to.be.false;
});
it('should return false if user does not exist', async () => {
stubs.findOneUserById.returns(undefined);
const result = await saveUserIdentity({ _id: 'valid_id' });
expect(stubs.findOneUserById.calledWith('valid_id')).to.be.true;
expect(result).to.be.false;
});
it('should return false if username is not allowed', async () => {
stubs.findOneUserById.returns({ username: 'oldUsername' });
stubs.validateName.returns(false);
const result = await saveUserIdentity({ _id: 'valid_id', username: 'admin' });
expect(stubs.validateName.calledWith('admin')).to.be.true;
expect(result).to.be.false;
});
it('should return false if username is invalid or unavailable', async () => {
stubs.findOneUserById.returns({ username: 'oldUsername' });
stubs.validateName.returns(true);
stubs.setUsername.returns(false);
const result = await saveUserIdentity({ _id: 'valid_id', username: 'invalidUsername' });
expect(stubs.validateName.calledWith('invalidUsername')).to.be.true;
expect(stubs.setUsername.calledWith('valid_id', 'invalidUsername', { username: 'oldUsername' })).to.be.true;
expect(result).to.be.false;
});
it("should not update the username if it's not changed", async () => {
stubs.findOneUserById.returns({ username: 'oldUsername', name: 'oldName' });
stubs.validateName.returns(true);
stubs.setUsername.returns(true);
await saveUserIdentity({ _id: 'valid_id', username: 'oldUsername', name: 'oldName' });
expect(stubs.validateName.called).to.be.false;
expect(stubs.setUsername.called).to.be.false;
expect(stubs.updateUsernameOfEditByUserId.called).to.be.false;
expect(stubs.updateAllUsernamesByUserId.called).to.be.false;
expect(stubs.updateUsernameAndMessageOfMentionByIdAndOldUsername.called).to.be.false;
expect(stubs.updateDirectNameAndFnameByName.called).to.be.false;
expect(stubs.updateUserReferences.called).to.be.false;
expect(stubs.updateHistoryReferences.called).to.be.false;
});
it('should update Subscriptions, VideoConference and Call History if name changes', async () => {
stubs.findOneUserById.returns({ name: 'oldName', username: 'oldUsername' });
stubs.setRealName.returns(true);
const result = await saveUserIdentity({ _id: 'valid_id', name: 'name', username: 'oldUsername' });
expect(stubs.setUsername.called).to.be.false;
expect(stubs.setRealName.called).to.be.true;
expect(stubs.updateUsernameOfEditByUserId.called).to.be.false;
expect(stubs.updateDirectNameAndFnameByName.called).to.be.true;
expect(stubs.updateUserReferences.called).to.be.true;
expect(stubs.updateHistoryReferences.called).to.be.true;
expect(result).to.be.true;
});
});