Files
upstream-rocketchat/apps/meteor/server/ufs/ufs-local.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
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (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

116 lines
3.3 KiB
TypeScript

import fs from 'node:fs';
import { expect } from 'chai';
import { before, beforeEach, afterEach, describe, it } from 'mocha';
import proxyquire from 'proxyquire';
import sinon from 'sinon';
import type { SinonStub } from 'sinon';
// import { Config } from './ufs-config';
// import { UploadFS } from './ufs';
import type { LocalStore as LocalStoreClass } from './ufs-local';
const fakeCollection = {
removeById: sinon.stub(),
findOne: sinon.stub(),
};
// Proxyquire chain to mock all Meteor dependencies
const ufsFilterMock = proxyquire.noCallThru().load('./ufs-filter', {
'meteor/meteor': {},
'meteor/check': {},
'meteor/mongo': {},
'meteor/npm-mongo': {},
});
const { UploadFS } = proxyquire.noCallThru().load('./ufs', {
'meteor/meteor': {},
'meteor/check': {},
'meteor/mongo': {},
'meteor/npm-mongo': {},
'./ufs-filter': ufsFilterMock,
'./ufs-store': {
Store: Object,
},
});
// Create the UploadFS mock object as a callable function with properties
// function UploadFS() {
// return UploadFS;
// }
// Object.assign(UploadFS, {
// store: {},
// config: new Config(),
// addStore: sinon.stub(),
// getStore: sinon.stub().returns(undefined),
// getTempFilePath: sinon.stub().returns('/tmp/mockfile'),
// });
// The module must export { UploadFS: ... }
const ufsMock = { UploadFS };
const ufsMockModule = { UploadFS };
const ufsIndexMock = { UploadFS };
const ufsStoreMockRaw = proxyquire.noCallThru().load('./ufs-store', {
'meteor/meteor': {},
'meteor/check': {},
'meteor/mongo': {},
'meteor/npm-mongo': {},
'./ufs': ufsMockModule,
'./ufs-filter': ufsFilterMock,
'./index': ufsIndexMock,
});
// Patch ufsStoreMock to export all possible ways (default, named, etc)
const ufsStoreMock = Object.assign({}, ufsStoreMockRaw, { default: ufsStoreMockRaw, ufsStoreMock: ufsStoreMockRaw });
const localStoreProxy = proxyquire.noCallThru().load('./ufs-local', {
// 'mkdirp': sinon.stub(),
'meteor/meteor': {},
'meteor/check': {},
'meteor/mongo': {},
'meteor/npm-mongo': {},
'./ufs': ufsMock,
'./ufs-store': ufsStoreMock,
'./index': ufsIndexMock,
});
const { LocalStore } = localStoreProxy as {
LocalStore: typeof LocalStoreClass;
};
describe('LocalStore', () => {
let store: LocalStoreClass;
let unlinkStub: SinonStub;
before(() => {
fakeCollection.removeById.resolves();
fakeCollection.findOne.resolves({ _id: 'test', name: 'file.txt' });
store = new LocalStore({ name: 'test', collection: fakeCollection as any, path: '/tmp/ufs-local' });
});
beforeEach(() => {
unlinkStub = sinon.stub(fs.promises, 'unlink').resolves();
fakeCollection.removeById.resetHistory();
fakeCollection.findOne.resetHistory();
});
afterEach(() => {
unlinkStub.restore();
});
it('should not throw if file does not exist (ENOENT)', async () => {
unlinkStub.rejects(Object.assign(new Error('not found'), { code: 'ENOENT' }));
await expect(store.delete('test')).to.be.fulfilled;
// unlink is called twice: once for the temp file, once for the actual file
expect(unlinkStub.calledTwice).to.be.true;
});
it('should throw if unlink fails with non-ENOENT error', async () => {
unlinkStub.rejects(Object.assign(new Error('fail'), { code: 'EACCES' }));
await expect(store.delete('test')).to.be.rejectedWith('fail');
});
it('should call findOne to get file info', async () => {
await store.delete('test');
expect(fakeCollection.findOne.calledWith('test')).to.be.true;
});
});