2022-08-02 01:40:57 -07:00
|
|
|
import path from 'path';
|
2023-09-01 06:13:19 -07:00
|
|
|
|
2022-11-09 06:25:00 -08:00
|
|
|
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { Push } from '@/push';
|
2023-09-01 06:13:19 -07:00
|
|
|
import { CommunityPackageService } from '@/services/communityPackage.service';
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
import { COMMUNITY_PACKAGE_VERSION } from './shared/constants';
|
|
|
|
import * as testDb from './shared/testDb';
|
2023-09-01 06:13:19 -07:00
|
|
|
import {
|
|
|
|
mockInstance,
|
|
|
|
setupTestServer,
|
|
|
|
mockPackage,
|
|
|
|
mockNode,
|
|
|
|
mockPackageName,
|
|
|
|
} from './shared/utils';
|
|
|
|
|
|
|
|
import type { InstalledPackages } from '@db/entities/InstalledPackages';
|
|
|
|
import type { InstalledNodes } from '@db/entities/InstalledNodes';
|
|
|
|
import type { SuperAgentTest } from 'supertest';
|
2023-02-21 10:21:56 -08:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
const communityPackageService = mockInstance(CommunityPackageService);
|
|
|
|
const mockLoadNodesAndCredentials = mockInstance(LoadNodesAndCredentials);
|
|
|
|
mockInstance(Push);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
const testServer = setupTestServer({ endpointGroups: ['nodes'] });
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
const commonUpdatesProps = {
|
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
installedVersion: COMMUNITY_PACKAGE_VERSION.CURRENT,
|
|
|
|
updateAvailable: COMMUNITY_PACKAGE_VERSION.UPDATED,
|
|
|
|
};
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
const parsedNpmPackageName = {
|
|
|
|
packageName: 'test',
|
|
|
|
rawString: 'test',
|
|
|
|
};
|
2023-07-13 01:14:48 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
let authAgent: SuperAgentTest;
|
2022-07-20 07:24:03 -07:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
const ownerShell = await testDb.createOwner();
|
|
|
|
authAgent = testServer.authAgentFor(ownerShell);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('GET /nodes', () => {
|
|
|
|
test('should respond 200 if no nodes are installed', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
communityPackageService.getAllInstalledPackages.mockResolvedValue([]);
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
body: { data },
|
2023-09-01 06:13:19 -07:00
|
|
|
} = await authAgent.get('/nodes').expect(200);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(data).toHaveLength(0);
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should return list of one installed package and node', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
const pkg = mockPackage();
|
|
|
|
const node = mockNode(pkg.packageName);
|
|
|
|
pkg.installedNodes = [node];
|
|
|
|
communityPackageService.getAllInstalledPackages.mockResolvedValue([pkg]);
|
|
|
|
communityPackageService.matchPackagesWithUpdates.mockReturnValue([pkg]);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
body: { data },
|
2023-09-01 06:13:19 -07:00
|
|
|
} = await authAgent.get('/nodes').expect(200);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(data).toHaveLength(1);
|
|
|
|
expect(data[0].installedNodes).toHaveLength(1);
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should return list of multiple installed packages and nodes', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
const pkgA = mockPackage();
|
|
|
|
const nodeA = mockNode(pkgA.packageName);
|
|
|
|
|
|
|
|
const pkgB = mockPackage();
|
|
|
|
const nodeB = mockNode(pkgB.packageName);
|
|
|
|
const nodeC = mockNode(pkgB.packageName);
|
|
|
|
|
|
|
|
communityPackageService.getAllInstalledPackages.mockResolvedValue([pkgA, pkgB]);
|
|
|
|
|
|
|
|
communityPackageService.matchPackagesWithUpdates.mockReturnValue([
|
|
|
|
{
|
|
|
|
...commonUpdatesProps,
|
|
|
|
packageName: pkgA.packageName,
|
|
|
|
installedNodes: [nodeA],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...commonUpdatesProps,
|
|
|
|
packageName: pkgB.packageName,
|
|
|
|
installedNodes: [nodeB, nodeC],
|
|
|
|
},
|
|
|
|
]);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
body: { data },
|
2023-09-01 06:13:19 -07:00
|
|
|
} = await authAgent.get('/nodes').expect(200);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(data).toHaveLength(2);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const allNodes = data.reduce(
|
|
|
|
(acc: InstalledNodes[], cur: InstalledPackages) => acc.concat(cur.installedNodes),
|
|
|
|
[],
|
|
|
|
);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(allNodes).toHaveLength(3);
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should not check for updates if no packages installed', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
await authAgent.get('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
expect(communityPackageService.executeNpmCommand).not.toHaveBeenCalled();
|
2023-03-17 09:24:05 -07:00
|
|
|
});
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should check for updates if packages installed', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
communityPackageService.getAllInstalledPackages.mockResolvedValue([mockPackage()]);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
await authAgent.get('/nodes').expect(200);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
const args = ['npm outdated --json', { doNotHandleError: true }];
|
|
|
|
|
|
|
|
expect(communityPackageService.executeNpmCommand).toHaveBeenCalledWith(...args);
|
2022-08-02 01:40:57 -07:00
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should report package updates if available', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
const pkg = mockPackage();
|
|
|
|
communityPackageService.getAllInstalledPackages.mockResolvedValue([pkg]);
|
2023-03-17 09:24:05 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
communityPackageService.executeNpmCommand.mockImplementation(() => {
|
2023-03-17 09:24:05 -07:00
|
|
|
throw {
|
|
|
|
code: 1,
|
|
|
|
stdout: JSON.stringify({
|
2023-09-01 06:13:19 -07:00
|
|
|
[pkg.packageName]: {
|
2023-03-17 09:24:05 -07:00
|
|
|
current: COMMUNITY_PACKAGE_VERSION.CURRENT,
|
|
|
|
wanted: COMMUNITY_PACKAGE_VERSION.CURRENT,
|
|
|
|
latest: COMMUNITY_PACKAGE_VERSION.UPDATED,
|
2023-09-01 06:13:19 -07:00
|
|
|
location: path.join('node_modules', pkg.packageName),
|
2023-03-17 09:24:05 -07:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
communityPackageService.matchPackagesWithUpdates.mockReturnValue([
|
|
|
|
{
|
|
|
|
packageName: 'test',
|
|
|
|
installedNodes: [],
|
|
|
|
...commonUpdatesProps,
|
|
|
|
},
|
|
|
|
]);
|
2023-03-17 09:24:05 -07:00
|
|
|
|
|
|
|
const {
|
|
|
|
body: { data },
|
2023-09-01 06:13:19 -07:00
|
|
|
} = await authAgent.get('/nodes').expect(200);
|
|
|
|
|
|
|
|
const [returnedPkg] = data;
|
2023-03-17 09:24:05 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
expect(returnedPkg.installedVersion).toBe(COMMUNITY_PACKAGE_VERSION.CURRENT);
|
|
|
|
expect(returnedPkg.updateAvailable).toBe(COMMUNITY_PACKAGE_VERSION.UPDATED);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('POST /nodes', () => {
|
|
|
|
test('should reject if package name is missing', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
await authAgent.post('/nodes').expect(400);
|
2023-03-17 09:24:05 -07:00
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should reject if package is duplicate', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
communityPackageService.findInstalledPackage.mockResolvedValue(mockPackage());
|
|
|
|
communityPackageService.isPackageInstalled.mockResolvedValue(true);
|
|
|
|
communityPackageService.hasPackageLoaded.mockReturnValue(true);
|
|
|
|
communityPackageService.parseNpmPackageName.mockReturnValue(parsedNpmPackageName);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
body: { message },
|
2023-09-01 06:13:19 -07:00
|
|
|
} = await authAgent.post('/nodes').send({ name: mockPackageName() }).expect(400);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(message).toContain('already installed');
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should allow installing packages that could not be loaded', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
communityPackageService.findInstalledPackage.mockResolvedValue(mockPackage());
|
|
|
|
communityPackageService.hasPackageLoaded.mockReturnValue(false);
|
|
|
|
communityPackageService.checkNpmPackageStatus.mockResolvedValue({ status: 'OK' });
|
|
|
|
communityPackageService.parseNpmPackageName.mockReturnValue(parsedNpmPackageName);
|
|
|
|
mockLoadNodesAndCredentials.installNpmModule.mockResolvedValue(mockPackage());
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
await authAgent.post('/nodes').send({ name: mockPackageName() }).expect(200);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
expect(communityPackageService.removePackageFromMissingList).toHaveBeenCalled();
|
2022-08-02 01:40:57 -07:00
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should not install a banned package', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
communityPackageService.checkNpmPackageStatus.mockResolvedValue({ status: 'Banned' });
|
|
|
|
communityPackageService.parseNpmPackageName.mockReturnValue(parsedNpmPackageName);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
body: { message },
|
2023-09-01 06:13:19 -07:00
|
|
|
} = await authAgent.post('/nodes').send({ name: mockPackageName() }).expect(400);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(message).toContain('banned');
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('DELETE /nodes', () => {
|
|
|
|
test('should not delete if package name is empty', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
await authAgent.delete('/nodes').expect(400);
|
2023-03-17 09:24:05 -07:00
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should reject if package is not installed', async () => {
|
|
|
|
const {
|
|
|
|
body: { message },
|
2023-09-01 06:13:19 -07:00
|
|
|
} = await authAgent.delete('/nodes').query({ name: mockPackageName() }).expect(400);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(message).toContain('not installed');
|
2022-08-02 01:40:57 -07:00
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should uninstall package', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
communityPackageService.findInstalledPackage.mockResolvedValue(mockPackage());
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
await authAgent.delete('/nodes').query({ name: mockPackageName() }).expect(200);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
expect(mockLoadNodesAndCredentials.removeNpmModule).toHaveBeenCalledTimes(1);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('PATCH /nodes', () => {
|
|
|
|
test('should reject if package name is empty', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
await authAgent.patch('/nodes').expect(400);
|
2023-03-17 09:24:05 -07:00
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
test('should reject if package is not installed', async () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
body: { message },
|
2023-09-01 06:13:19 -07:00
|
|
|
} = await authAgent.patch('/nodes').send({ name: mockPackageName() }).expect(400);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(message).toContain('not installed');
|
2022-08-02 01:40:57 -07:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should update a package', async () => {
|
2023-09-01 06:13:19 -07:00
|
|
|
communityPackageService.findInstalledPackage.mockResolvedValue(mockPackage());
|
|
|
|
communityPackageService.parseNpmPackageName.mockReturnValue(parsedNpmPackageName);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
await authAgent.patch('/nodes').send({ name: mockPackageName() });
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-09-01 06:13:19 -07:00
|
|
|
expect(mockLoadNodesAndCredentials.updateNpmModule).toHaveBeenCalledTimes(1);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
});
|