test(apps.service): copy file and replace variables coverage

This commit is contained in:
Nicolas Meienberger 2023-06-08 08:50:50 +02:00 committed by Nicolas Meienberger
parent ead2f23fa8
commit a728507882
4 changed files with 61 additions and 1 deletions

View file

@ -3,6 +3,7 @@ name: E2E Tests
on:
workflow_dispatch:
pull_request:
types: [opened, synchronize, reopened]
branches:
- release/*

View file

@ -57,7 +57,9 @@ class FsMock {
};
mkdirSync = (p: string) => {
this.mockFiles[p] = Object.create(null);
if (!this.mockFiles[p]) {
this.mockFiles[p] = [];
}
};
rmSync = (p: string) => {
@ -128,6 +130,18 @@ class FsMock {
},
writeFile: async (p: string, data: string | string[]) => {
this.mockFiles[p] = data;
const dir = path.dirname(p);
if (!this.mockFiles[dir]) {
this.mockFiles[dir] = [];
}
this.mockFiles[dir].push(path.basename(p));
},
mkdir: async (p: string) => {
if (!this.mockFiles[p]) {
this.mockFiles[p] = [];
}
},
readdir: async (p: string) => {
const files: string[] = [];
@ -146,6 +160,19 @@ class FsMock {
return files;
},
lstat: async (p: string) => {
return {
isDirectory: () => {
return this.mockFiles[p] instanceof Array;
},
};
},
readFile: async (p: string) => {
return this.mockFiles[p];
},
copyFile: async (source: string, destination: string) => {
this.mockFiles[destination] = this.mockFiles[source];
},
};
}

View file

@ -269,6 +269,7 @@ export const copyDataDir = async (id: string) => {
};
const processDir = async (path: string) => {
await fs.promises.mkdir(`/app/storage/app-data/${id}/data/${path}`, { recursive: true });
const files = await fs.promises.readdir(`/runtipi/apps/${id}/data/${path}`);
await Promise.all(

View file

@ -239,6 +239,37 @@ describe('Install app', () => {
// act & assert
await expect(AppsService.installApp(appConfig.id, {})).rejects.toThrowError();
});
it('should replace env variables in .templates files in data folder', async () => {
// arrange
const appConfig = createAppConfig({ form_fields: [{ env_variable: 'TEST', type: 'text', label: 'test', required: true }] });
await fs.promises.writeFile(`/runtipi/apps/${appConfig.id}/data/test.txt.template`, 'test {{TEST}}');
await fs.promises.writeFile(`/runtipi/apps/${appConfig.id}/data/test2.txt`, 'test {{TEST}}');
// act
await AppsService.installApp(appConfig.id, { TEST: 'test' });
// assert
const file = await fs.promises.readFile(`/app/storage/app-data/${appConfig.id}/data/test.txt`);
const file2 = await fs.promises.readFile(`/app/storage/app-data/${appConfig.id}/data/test2.txt`);
expect(file.toString()).toBe('test test');
expect(file2.toString()).toBe('test {{TEST}}');
});
it('should copy and replace env variables in deeply nested .templates files in data folder', async () => {
// arrange
const appConfig = createAppConfig({ form_fields: [{ env_variable: 'TEST', type: 'text', label: 'test', required: true }] });
await fs.promises.mkdir(`/runtipi/apps/${appConfig.id}/data/test`);
await fs.promises.mkdir(`/runtipi/apps/${appConfig.id}/data/test/test`);
await fs.promises.writeFile(`/runtipi/apps/${appConfig.id}/data/test/test/test.txt.template`, 'test {{TEST}}');
// act
await AppsService.installApp(appConfig.id, { TEST: 'test' });
// assert
const file = await fs.promises.readFile(`/app/storage/app-data/${appConfig.id}/data/test/test/test.txt`);
expect(file.toString()).toBe('test test');
});
});
describe('Uninstall app', () => {