2022-08-02 01:40:57 -07:00
|
|
|
import path from 'path';
|
|
|
|
|
2022-07-20 07:24:03 -07:00
|
|
|
import express from 'express';
|
2022-08-02 01:40:57 -07:00
|
|
|
import { mocked } from 'jest-mock';
|
|
|
|
|
2022-07-20 07:24:03 -07:00
|
|
|
import * as utils from './shared/utils';
|
|
|
|
import * as testDb from './shared/testDb';
|
2022-08-02 01:40:57 -07:00
|
|
|
import {
|
|
|
|
executeCommand,
|
|
|
|
checkNpmPackageStatus,
|
|
|
|
hasPackageLoaded,
|
|
|
|
removePackageFromMissingList,
|
|
|
|
isNpmError,
|
2022-11-09 06:25:00 -08:00
|
|
|
} from '@/CommunityNodes/helpers';
|
|
|
|
import { findInstalledPackage, isPackageInstalled } from '@/CommunityNodes/packageModel';
|
|
|
|
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
|
|
|
|
import { InstalledPackages } from '@db/entities/InstalledPackages';
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2022-11-09 06:25:00 -08:00
|
|
|
import type { Role } from '@db/entities/Role';
|
2022-08-02 01:40:57 -07:00
|
|
|
import type { AuthAgent } from './shared/types';
|
2022-11-09 06:25:00 -08:00
|
|
|
import type { InstalledNodes } from '@db/entities/InstalledNodes';
|
2022-08-11 02:02:21 -07:00
|
|
|
import { COMMUNITY_PACKAGE_VERSION } from './shared/constants';
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-11-09 06:25:00 -08:00
|
|
|
jest.mock('@/CommunityNodes/helpers', () => {
|
2022-08-02 01:40:57 -07:00
|
|
|
return {
|
2022-11-09 06:25:00 -08:00
|
|
|
...jest.requireActual('@/CommunityNodes/helpers'),
|
2022-08-02 01:40:57 -07:00
|
|
|
checkNpmPackageStatus: jest.fn(),
|
|
|
|
executeCommand: jest.fn(),
|
|
|
|
hasPackageLoaded: jest.fn(),
|
|
|
|
isNpmError: jest.fn(),
|
|
|
|
removePackageFromMissingList: jest.fn(),
|
|
|
|
};
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-11-09 06:25:00 -08:00
|
|
|
jest.mock('@/CommunityNodes/packageModel', () => {
|
2022-08-02 01:40:57 -07:00
|
|
|
return {
|
2022-11-09 06:25:00 -08:00
|
|
|
...jest.requireActual('@/CommunityNodes/packageModel'),
|
2022-08-02 01:40:57 -07:00
|
|
|
isPackageInstalled: jest.fn(),
|
|
|
|
findInstalledPackage: jest.fn(),
|
|
|
|
};
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const mockedEmptyPackage = mocked(utils.emptyPackage);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
|
|
|
let app: express.Application;
|
|
|
|
let globalOwnerRole: Role;
|
2022-08-02 01:40:57 -07:00
|
|
|
let authAgent: AuthAgent;
|
2022-07-20 07:24:03 -07:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-01-27 02:19:47 -08:00
|
|
|
app = await utils.initTestServer({ endpointGroups: ['nodes'] });
|
2022-07-20 07:24:03 -07:00
|
|
|
|
|
|
|
globalOwnerRole = await testDb.getGlobalOwnerRole();
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
authAgent = utils.createAuthAgent(app);
|
|
|
|
|
|
|
|
utils.initConfigFile();
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.truncate(['InstalledNodes', 'InstalledPackages', 'User']);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
|
|
|
mocked(executeCommand).mockReset();
|
|
|
|
mocked(findInstalledPackage).mockReset();
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.terminate();
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
/**
|
|
|
|
* GET /nodes
|
|
|
|
*/
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('GET /nodes should respond 200 if no nodes are installed', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { data },
|
|
|
|
} = await authAgent(ownerShell).get('/nodes');
|
|
|
|
|
|
|
|
expect(statusCode).toBe(200);
|
|
|
|
expect(data).toHaveLength(0);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('GET /nodes should return list of one installed package and node', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
|
|
|
|
const { packageName } = await testDb.saveInstalledPackage(utils.installedPackagePayload());
|
|
|
|
await testDb.saveInstalledNode(utils.installedNodePayload(packageName));
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { data },
|
|
|
|
} = await authAgent(ownerShell).get('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
expect(statusCode).toBe(200);
|
|
|
|
expect(data).toHaveLength(1);
|
|
|
|
expect(data[0].installedNodes).toHaveLength(1);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('GET /nodes should return list of multiple installed packages and nodes', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const first = await testDb.saveInstalledPackage(utils.installedPackagePayload());
|
|
|
|
await testDb.saveInstalledNode(utils.installedNodePayload(first.packageName));
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const second = await testDb.saveInstalledPackage(utils.installedPackagePayload());
|
|
|
|
await testDb.saveInstalledNode(utils.installedNodePayload(second.packageName));
|
|
|
|
await testDb.saveInstalledNode(utils.installedNodePayload(second.packageName));
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { data },
|
|
|
|
} = await authAgent(ownerShell).get('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
expect(statusCode).toBe(200);
|
|
|
|
expect(data).toHaveLength(2);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const allNodes = data.reduce(
|
|
|
|
(acc: InstalledNodes[], cur: InstalledPackages) => acc.concat(cur.installedNodes),
|
|
|
|
[],
|
|
|
|
);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
expect(allNodes).toHaveLength(3);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('GET /nodes should not check for updates if no packages installed', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
await authAgent(ownerShell).get('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
expect(mocked(executeCommand)).toHaveBeenCalledTimes(0);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('GET /nodes should check for updates if packages installed', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
|
|
|
|
const { packageName } = await testDb.saveInstalledPackage(utils.installedPackagePayload());
|
|
|
|
await testDb.saveInstalledNode(utils.installedNodePayload(packageName));
|
|
|
|
|
|
|
|
await authAgent(ownerShell).get('/nodes');
|
|
|
|
|
|
|
|
expect(mocked(executeCommand)).toHaveBeenCalledWith('npm outdated --json', {
|
|
|
|
doNotHandleError: true,
|
|
|
|
});
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('GET /nodes should report package updates if available', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
|
|
|
|
const { packageName } = await testDb.saveInstalledPackage(utils.installedPackagePayload());
|
|
|
|
await testDb.saveInstalledNode(utils.installedNodePayload(packageName));
|
|
|
|
|
|
|
|
mocked(executeCommand).mockImplementationOnce(() => {
|
|
|
|
throw {
|
|
|
|
code: 1,
|
|
|
|
stdout: JSON.stringify({
|
|
|
|
[packageName]: {
|
2022-08-11 02:02:21 -07:00
|
|
|
current: COMMUNITY_PACKAGE_VERSION.CURRENT,
|
|
|
|
wanted: COMMUNITY_PACKAGE_VERSION.CURRENT,
|
|
|
|
latest: COMMUNITY_PACKAGE_VERSION.UPDATED,
|
2022-08-02 01:40:57 -07:00
|
|
|
location: path.join('node_modules', packageName),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
};
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
mocked(isNpmError).mockReturnValueOnce(true);
|
|
|
|
|
|
|
|
const {
|
|
|
|
body: { data },
|
|
|
|
} = await authAgent(ownerShell).get('/nodes');
|
|
|
|
|
2022-08-11 02:02:21 -07:00
|
|
|
expect(data[0].installedVersion).toBe(COMMUNITY_PACKAGE_VERSION.CURRENT);
|
|
|
|
expect(data[0].updateAvailable).toBe(COMMUNITY_PACKAGE_VERSION.UPDATED);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
/**
|
|
|
|
* POST /nodes
|
|
|
|
*/
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('POST /nodes should reject if package name is missing', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const { statusCode } = await authAgent(ownerShell).post('/nodes');
|
|
|
|
|
|
|
|
expect(statusCode).toBe(400);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('POST /nodes should reject if package is duplicate', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
|
|
|
|
mocked(findInstalledPackage).mockResolvedValueOnce(new InstalledPackages());
|
|
|
|
mocked(isPackageInstalled).mockResolvedValueOnce(true);
|
|
|
|
mocked(hasPackageLoaded).mockReturnValueOnce(true);
|
|
|
|
|
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { message },
|
|
|
|
} = await authAgent(ownerShell).post('/nodes').send({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
expect(statusCode).toBe(400);
|
|
|
|
expect(message).toContain('already installed');
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('POST /nodes should allow installing packages that could not be loaded', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
mocked(findInstalledPackage).mockResolvedValueOnce(new InstalledPackages());
|
|
|
|
mocked(hasPackageLoaded).mockReturnValueOnce(false);
|
|
|
|
mocked(checkNpmPackageStatus).mockResolvedValueOnce({ status: 'OK' });
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-11 02:02:21 -07:00
|
|
|
jest.spyOn(LoadNodesAndCredentials(), 'loadNpmModule').mockImplementationOnce(mockedEmptyPackage);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const { statusCode } = await authAgent(ownerShell).post('/nodes').send({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
expect(statusCode).toBe(200);
|
|
|
|
expect(mocked(removePackageFromMissingList)).toHaveBeenCalled();
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('POST /nodes should not install a banned package', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
mocked(checkNpmPackageStatus).mockResolvedValueOnce({ status: 'Banned' });
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { message },
|
|
|
|
} = await authAgent(ownerShell).post('/nodes').send({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
2022-08-02 01:40:57 -07:00
|
|
|
|
|
|
|
expect(statusCode).toBe(400);
|
|
|
|
expect(message).toContain('banned');
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
/**
|
|
|
|
* DELETE /nodes
|
|
|
|
*/
|
|
|
|
|
|
|
|
test('DELETE /nodes should not delete if package name is empty', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
|
|
|
|
const response = await authAgent(ownerShell).delete('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('DELETE /nodes should reject if package is not installed', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
|
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { message },
|
2022-08-30 02:54:50 -07:00
|
|
|
} = await authAgent(ownerShell).delete('/nodes').query({
|
2022-08-02 01:40:57 -07:00
|
|
|
name: utils.installedPackagePayload().packageName,
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
expect(statusCode).toBe(400);
|
|
|
|
expect(message).toContain('not installed');
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('DELETE /nodes should uninstall package', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const removeSpy = jest
|
|
|
|
.spyOn(LoadNodesAndCredentials(), 'removeNpmModule')
|
|
|
|
.mockImplementationOnce(jest.fn());
|
|
|
|
|
|
|
|
mocked(findInstalledPackage).mockImplementationOnce(mockedEmptyPackage);
|
|
|
|
|
2022-08-30 02:54:50 -07:00
|
|
|
const { statusCode } = await authAgent(ownerShell).delete('/nodes').query({
|
2022-08-02 01:40:57 -07:00
|
|
|
name: utils.installedPackagePayload().packageName,
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
expect(statusCode).toBe(200);
|
|
|
|
expect(removeSpy).toHaveBeenCalledTimes(1);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
/**
|
|
|
|
* PATCH /nodes
|
|
|
|
*/
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('PATCH /nodes should reject if package name is empty', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
|
|
|
|
const response = await authAgent(ownerShell).patch('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('PATCH /nodes reject if package is not installed', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { message },
|
|
|
|
} = await authAgent(ownerShell).patch('/nodes').send({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(statusCode).toBe(400);
|
|
|
|
expect(message).toContain('not installed');
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
test('PATCH /nodes should update a package', async () => {
|
|
|
|
const ownerShell = await testDb.createUserShell(globalOwnerRole);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
const updateSpy = jest
|
|
|
|
.spyOn(LoadNodesAndCredentials(), 'updateNpmModule')
|
|
|
|
.mockImplementationOnce(mockedEmptyPackage);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
mocked(findInstalledPackage).mockImplementationOnce(mockedEmptyPackage);
|
|
|
|
|
|
|
|
await authAgent(ownerShell).patch('/nodes').send({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
2022-08-02 01:40:57 -07:00
|
|
|
expect(updateSpy).toHaveBeenCalledTimes(1);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|