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
125 lines
3.8 KiB
TypeScript
125 lines
3.8 KiB
TypeScript
import { expect } from 'chai';
|
|
import { describe, it } from 'mocha';
|
|
import proxyquire from 'proxyquire';
|
|
import sinon from 'sinon';
|
|
|
|
const mocks = {
|
|
utils: {
|
|
userCanAccessAvatar: sinon.stub(),
|
|
renderSVGLetters: sinon.stub(),
|
|
},
|
|
};
|
|
|
|
const { protectAvatarsWithFallback, protectAvatars } = proxyquire.noCallThru().load('./auth.ts', {
|
|
'../utils': mocks.utils,
|
|
});
|
|
|
|
describe('#protectAvatarsWithFallback()', () => {
|
|
const response = {
|
|
setHeader: sinon.spy(),
|
|
writeHead: sinon.spy(),
|
|
write: sinon.spy(),
|
|
end: sinon.spy(),
|
|
};
|
|
const next = sinon.spy();
|
|
|
|
afterEach(() => {
|
|
response.setHeader.resetHistory();
|
|
response.writeHead.resetHistory();
|
|
response.end.resetHistory();
|
|
next.resetHistory();
|
|
|
|
Object.values(mocks.utils).forEach((mock) => mock.reset());
|
|
});
|
|
|
|
it(`should write 404 to head if no url provided`, async () => {
|
|
await protectAvatarsWithFallback({}, response, next);
|
|
expect(next.called).to.be.false;
|
|
expect(response.setHeader.called).to.be.false;
|
|
expect(response.writeHead.calledWith(404)).to.be.true;
|
|
expect(response.end.calledOnce).to.be.true;
|
|
});
|
|
|
|
it(`should write 200 to head and write fallback to body (user avatar)`, async () => {
|
|
mocks.utils.renderSVGLetters.returns('fallback');
|
|
|
|
await protectAvatarsWithFallback({ url: '/jon' }, response, next);
|
|
expect(next.called).to.be.false;
|
|
expect(response.setHeader.called).to.be.false;
|
|
|
|
expect(response.writeHead.calledWith(200, { 'Content-Type': 'image/svg+xml' })).to.be.true;
|
|
expect(mocks.utils.renderSVGLetters.calledWith('jon')).to.be.true;
|
|
expect(response.write.calledWith('fallback')).to.be.true;
|
|
|
|
expect(response.end.calledOnce).to.be.true;
|
|
});
|
|
|
|
it(`should write 200 to head and write fallback to body (room avatar)`, async () => {
|
|
mocks.utils.renderSVGLetters.returns('fallback');
|
|
|
|
await protectAvatarsWithFallback({ url: '/room/jon' }, response, next);
|
|
expect(next.called).to.be.false;
|
|
expect(response.setHeader.called).to.be.false;
|
|
expect(response.writeHead.calledWith(200, { 'Content-Type': 'image/svg+xml' })).to.be.true;
|
|
expect(response.write.calledWith('fallback')).to.be.true;
|
|
expect(response.end.calledOnce).to.be.true;
|
|
});
|
|
|
|
it(`should call next if user can access avatar`, async () => {
|
|
mocks.utils.userCanAccessAvatar.returns(true);
|
|
const request = { url: '/jon' };
|
|
|
|
await protectAvatarsWithFallback(request, response, next);
|
|
expect(mocks.utils.userCanAccessAvatar.calledWith(request)).to.be.true;
|
|
expect(next.called).to.be.true;
|
|
});
|
|
});
|
|
|
|
describe('#protectAvatars()', () => {
|
|
const response = {
|
|
setHeader: sinon.spy(),
|
|
writeHead: sinon.spy(),
|
|
write: sinon.spy(),
|
|
end: sinon.spy(),
|
|
};
|
|
const next = sinon.spy();
|
|
|
|
afterEach(() => {
|
|
response.setHeader.resetHistory();
|
|
response.writeHead.resetHistory();
|
|
response.end.resetHistory();
|
|
next.resetHistory();
|
|
|
|
Object.values(mocks.utils).forEach((mock) => mock.reset());
|
|
});
|
|
|
|
it(`should write 404 to head if no url provided`, async () => {
|
|
await protectAvatars({}, response, next);
|
|
|
|
expect(next.called).to.be.false;
|
|
expect(response.setHeader.called).to.be.false;
|
|
expect(response.writeHead.calledWith(404)).to.be.true;
|
|
expect(response.end.calledOnce).to.be.true;
|
|
});
|
|
|
|
it(`should write 404 to head if access is denied`, async () => {
|
|
mocks.utils.userCanAccessAvatar.returns(false);
|
|
|
|
await protectAvatars({ url: '/room/jon' }, response, next);
|
|
|
|
expect(next.called).to.be.false;
|
|
expect(response.setHeader.called).to.be.false;
|
|
expect(response.writeHead.calledWith(404)).to.be.true;
|
|
expect(response.end.calledOnce).to.be.true;
|
|
});
|
|
|
|
it(`should call next if user can access avatar`, async () => {
|
|
mocks.utils.userCanAccessAvatar.returns(true);
|
|
const request = { url: '/jon' };
|
|
|
|
await protectAvatars(request, response, next);
|
|
expect(mocks.utils.userCanAccessAvatar.calledWith(request)).to.be.true;
|
|
expect(next.called).to.be.true;
|
|
});
|
|
});
|