Files
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 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

150 lines
4.9 KiB
TypeScript

import type { BannerPlatform, IBanner, IBannerDismiss } from '@rocket.chat/core-typings';
import { registerModel, BaseRaw } from '@rocket.chat/models';
import { expect } from 'chai';
import { afterEach, before, describe, it } from 'mocha';
import type { FindCursor, FindOptions } from 'mongodb';
import proxyquire from 'proxyquire';
import sinon from 'sinon';
const { BannerService } = proxyquire.noCallThru().load('../../../../../server/services/banner/service', {});
class BannerModel extends BaseRaw<any> {
findActiveByRoleOrId(
_roles: string[],
_platform: BannerPlatform,
_bannerId?: string,
_options?: FindOptions<IBanner>,
): FindCursor<IBanner> {
return {} as unknown as FindCursor<IBanner>;
}
}
class BannerDismissModel extends BaseRaw<any> {
findByUserIdAndBannerId(): FindCursor<Pick<IBannerDismiss, 'bannerId'>> {
return {} as unknown as FindCursor<Pick<IBannerDismiss, 'bannerId'>>;
}
}
class UserModel extends BaseRaw<any> {
override findOneById(): Promise<any> {
return Promise.resolve({});
}
}
function findCursorFactory<T>(items: T[]): FindCursor<T> {
return {
toArray: () => Promise.resolve(items),
} as unknown as FindCursor<T>;
}
const bannersModel = new BannerModel({ collection: () => ({}) } as unknown as any, 'banner');
const bannerDismissModel = new BannerDismissModel({ collection: () => ({}) } as unknown as any, 'banner_dismiss');
const userModel = new UserModel({ collection: () => ({}) } as unknown as any, 'user');
describe('Banners service', () => {
before(() => {
registerModel('IBannersModel', bannersModel);
registerModel('IBannersDismissModel', bannerDismissModel);
registerModel('IUsersModel', userModel);
});
afterEach(() => sinon.restore());
it('should be defined', () => {
const service = new BannerService();
expect(service).to.be.an('object');
});
describe('getBannersForUser', () => {
const FAKE_BANNER: IBanner = {
_id: 'fake-id',
view: {
appId: 'fake-app-id',
viewId: 'fake-view-id',
},
} as IBanner;
const A_SECOND_FAKE_BANNER: IBanner = {
_id: 'fake-id-2',
surface: 'modal',
view: {},
} as IBanner;
it('should return the banners a user has access to', async () => {
const service = new BannerService();
const findActiveByRoleOrIdMock = sinon.replace(
bannersModel,
'findActiveByRoleOrId',
sinon.fake.returns(findCursorFactory<IBanner>([FAKE_BANNER])),
);
const findByUserIdAndBannerIdMock = sinon.replace(
bannerDismissModel,
'findByUserIdAndBannerId',
sinon.fake.returns(findCursorFactory<Pick<IBannerDismiss, 'bannerId'>>([])),
);
sinon.replace(userModel, 'findOneById', sinon.fake.returns(Promise.resolve({})));
const banners = await service.getBannersForUser('a-fake-id', 'web');
expect(findActiveByRoleOrIdMock.callCount).to.be.equal(1);
expect(findByUserIdAndBannerIdMock.callCount).to.be.equal(1);
expect(banners).to.be.an('array');
expect(banners).to.have.lengthOf(1);
expect(banners[0].view.viewId).to.be.equal(FAKE_BANNER.view.viewId);
expect(banners[0].surface).to.be.equal('banner');
});
it('should return all the banners that were not dismissed', async () => {
const service = new BannerService();
const findActiveByRoleOrIdMock = sinon.replace(
bannersModel,
'findActiveByRoleOrId',
sinon.fake.returns(findCursorFactory<IBanner>([FAKE_BANNER])),
);
const findByUserIdAndBannerIdMock = sinon.replace(
bannerDismissModel,
'findByUserIdAndBannerId',
sinon.fake.returns(findCursorFactory<Pick<IBannerDismiss, 'bannerId'>>([{ bannerId: FAKE_BANNER._id }])),
);
sinon.replace(userModel, 'findOneById', sinon.fake.returns(Promise.resolve({})));
const banners = await service.getBannersForUser('a-fake-id', 'web');
expect(findActiveByRoleOrIdMock.callCount).to.be.equal(1);
expect(findByUserIdAndBannerIdMock.callCount).to.be.equal(1);
expect(banners).to.be.an('array');
expect(banners).to.have.lengthOf(0);
});
it('should use the _id as viewId if not set', async () => {
const service = new BannerService();
const findActiveByRoleOrIdMock = sinon.replace(
bannersModel,
'findActiveByRoleOrId',
sinon.fake.returns(findCursorFactory<IBanner>([A_SECOND_FAKE_BANNER])),
);
const findByUserIdAndBannerIdMock = sinon.replace(
bannerDismissModel,
'findByUserIdAndBannerId',
sinon.fake.returns(findCursorFactory<Pick<IBannerDismiss, 'bannerId'>>([])),
);
sinon.replace(userModel, 'findOneById', sinon.fake.returns(Promise.resolve({})));
const banners = await service.getBannersForUser('a-fake-id', 'web');
expect(findActiveByRoleOrIdMock.callCount).to.be.equal(1);
expect(findByUserIdAndBannerIdMock.callCount).to.be.equal(1);
expect(banners).to.be.an('array');
expect(banners).to.have.lengthOf(1);
expect(banners[0].view.viewId).to.be.equal(A_SECOND_FAKE_BANNER._id);
expect(banners[0].surface).to.be.equal('modal');
});
});
});