Skip to content

Commit 8bf89e1

Browse files
committed
added tests
1 parent 5ac4eb4 commit 8bf89e1

6 files changed

Lines changed: 535 additions & 47 deletions

File tree

src/utils/webdav.utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export class WebDavUtils {
3636
}
3737
const decodedUrl = decodeURIComponent(requestUrl);
3838
const parsedPath = path.parse(decodedUrl);
39-
const parentPath = `/${path.dirname(decodedUrl).replace(/^(\/)|(\/)$/g, '')}/`.replaceAll('//', '/');
39+
let parentPath = path.dirname(decodedUrl);
40+
if (!parentPath.startsWith('/')) parentPath = '/'.concat(parentPath);
41+
if (!parentPath.endsWith('/')) parentPath = parentPath.concat('/');
4042

4143
const isFolder = requestUrl.endsWith('/');
4244

src/webdav/handlers/PUT.handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ export class PUTRequestHandler implements WebDavMethodHandler {
5555
})) as DriveFileItem;
5656
if (driveFileItem && driveFileItem.status === 'EXISTS') {
5757
webdavLogger.info(`File '${resource.name}' already exists in '${resource.path.dir}', trashing it before PUT`);
58+
await driveDatabaseManager.deleteFileById(driveFileItem.id);
5859
await trashService.trashItems({
5960
items: [{ type: resource.type, uuid: driveFileItem.uuid }],
6061
});
61-
await driveDatabaseManager.deleteFileById(driveFileItem.id);
6262
}
63-
} catch (_) {
63+
} catch {
6464
//noop
6565
}
6666

test/fixtures/drive-database.fixture.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,41 @@ import { DriveFolder } from '../../src/services/database/drive-folder/drive-fold
44
import { DriveDatabaseManager } from '../../src/services/database/drive-database-manager.service';
55
import { DriveFileRepository } from '../../src/services/database/drive-file/drive-file.repository';
66
import { DriveFolderRepository } from '../../src/services/database/drive-folder/drive-folder.repository';
7+
import { randomInt, randomUUID } from 'crypto';
78

8-
export const getDriveFileDatabaseFixture = (payload: Partial<DriveFile> = {}): DriveFile => {
9-
// @ts-expect-error - We only mock the properties we need
10-
const object: DriveFile = {
11-
id: new Date().getTime(),
9+
export const getDriveFileDatabaseFixture = (): DriveFile => {
10+
const object: DriveFile = new DriveFile({
11+
id: randomInt(2000),
1212
name: `file_${new Date().getTime().toString()}`,
13-
uuid: `uuid_${new Date().getTime().toString()}`,
14-
relativePath: '',
13+
uuid: randomUUID(),
14+
relativePath: `file_${new Date().getTime().toString()}.txt`,
1515
createdAt: new Date(),
1616
updatedAt: new Date(),
1717
status: 'EXISTS',
1818
fileId: `file_id_${new Date().getTime().toString()}`,
19-
folderId: 0,
19+
folderId: randomInt(2000),
2020
bucket: new Date().getTime().toString(),
21-
size: 0,
22-
};
23-
24-
// @ts-expect-error - We only mock the properties we need
25-
return {
26-
...object,
27-
...payload,
28-
};
21+
size: randomInt(2000),
22+
folderUuid: randomUUID(),
23+
type: 'txt',
24+
});
25+
return object;
2926
};
3027

31-
export const getDriveFolderDatabaseFixture = (payload: Partial<DriveFolder> = {}): DriveFolder => {
32-
// @ts-expect-error - We only mock the properties we need
33-
const object: DriveFolder = {
34-
id: new Date().getTime(),
28+
export const getDriveFolderDatabaseFixture = (): DriveFolder => {
29+
const object: DriveFolder = new DriveFolder({
30+
id: randomInt(2000),
3531
name: `folder_${new Date().getTime().toString()}`,
36-
uuid: `uuid_${new Date().getTime().toString()}`,
32+
uuid: randomUUID(),
3733
relativePath: '',
3834
createdAt: new Date(),
3935
updatedAt: new Date(),
40-
};
36+
parentId: randomInt(2000),
37+
parentUuid: randomUUID(),
38+
status: 'EXISTS',
39+
});
4140

42-
// @ts-expect-error - We only mock the properties we need
43-
return {
44-
...object,
45-
...payload,
46-
};
41+
return object;
4742
};
4843

4944
export const getDriveDatabaseManager = (): DriveDatabaseManager => {

test/services/config.service.test.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { expect } from 'chai';
2-
import crypto, { randomUUID } from 'crypto';
2+
import crypto, { randomInt, randomUUID } from 'crypto';
33
import Sinon, { SinonSandbox } from 'sinon';
44
import fs from 'fs/promises';
55
import { ConfigService } from '../../src/services/config.service';
66
import { CryptoService } from '../../src/services/crypto.service';
7-
import { CLICredentials, LoginCredentials } from '../../src/types/command.types';
7+
import { CLICredentials, LoginCredentials, WebdavConfig } from '../../src/types/command.types';
88
import { UserFixture } from '../fixtures/auth.fixture';
99

1010
import { config } from 'dotenv';
@@ -154,4 +154,66 @@ describe('Config service', () => {
154154

155155
expect(stubMkdir).to.be.calledOnceWith(ConfigService.WEBDAV_SSL_CERTS_DIR);
156156
});
157+
158+
it('When webdav config options are saved, then they are written to a file', async () => {
159+
const webdavConfig: WebdavConfig = {
160+
port: String(randomInt(65000)),
161+
protocol: 'https',
162+
};
163+
const stringConfig = JSON.stringify(webdavConfig);
164+
165+
const fsStub = configServiceSandbox
166+
.stub(fs, 'writeFile')
167+
.withArgs(ConfigService.WEBDAV_CONFIGS_FILE, stringConfig)
168+
.resolves();
169+
170+
await ConfigService.instance.saveWebdavConfig(webdavConfig);
171+
expect(fsStub).to.be.calledWith(ConfigService.WEBDAV_CONFIGS_FILE, stringConfig);
172+
});
173+
174+
it('When webdav config options are read and exist, then they are read from a file', async () => {
175+
const webdavConfig: WebdavConfig = {
176+
port: String(randomInt(65000)),
177+
protocol: 'http',
178+
};
179+
const stringConfig = JSON.stringify(webdavConfig);
180+
181+
const fsStub = configServiceSandbox
182+
.stub(fs, 'readFile')
183+
.withArgs(ConfigService.WEBDAV_CONFIGS_FILE)
184+
.resolves(stringConfig);
185+
186+
const webdavConfigResult = await ConfigService.instance.readWebdavConfig();
187+
expect(webdavConfigResult).to.be.eql(webdavConfig);
188+
expect(fsStub).to.be.calledWith(ConfigService.WEBDAV_CONFIGS_FILE);
189+
});
190+
191+
it('When webdav config options are read but not exist, then they are returned from defaults', async () => {
192+
const defaultWebdavConfig: WebdavConfig = {
193+
port: ConfigService.WEBDAV_DEFAULT_PORT,
194+
protocol: ConfigService.WEBDAV_DEFAULT_PROTOCOL,
195+
};
196+
197+
const fsStub = configServiceSandbox
198+
.stub(fs, 'readFile')
199+
.withArgs(ConfigService.WEBDAV_CONFIGS_FILE)
200+
.resolves(undefined);
201+
202+
const webdavConfigResult = await ConfigService.instance.readWebdavConfig();
203+
expect(webdavConfigResult).to.be.eql(defaultWebdavConfig);
204+
expect(fsStub).to.be.calledWith(ConfigService.WEBDAV_CONFIGS_FILE);
205+
});
206+
207+
it('When webdav config options are read but an error is thrown, then they are returned from defaults', async () => {
208+
const defaultWebdavConfig: WebdavConfig = {
209+
port: ConfigService.WEBDAV_DEFAULT_PORT,
210+
protocol: ConfigService.WEBDAV_DEFAULT_PROTOCOL,
211+
};
212+
213+
const fsStub = configServiceSandbox.stub(fs, 'readFile').withArgs(ConfigService.WEBDAV_CONFIGS_FILE).rejects();
214+
215+
const webdavConfigResult = await ConfigService.instance.readWebdavConfig();
216+
expect(webdavConfigResult).to.be.eql(defaultWebdavConfig);
217+
expect(fsStub).to.be.calledWith(ConfigService.WEBDAV_CONFIGS_FILE);
218+
});
157219
});

0 commit comments

Comments
 (0)