2022-08-02 01:40:57 -07:00
|
|
|
import path from 'path';
|
|
|
|
import { mocked } from 'jest-mock';
|
2023-03-17 09:24:05 -07:00
|
|
|
import type { SuperAgentTest } from 'supertest';
|
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';
|
2023-03-17 09:24:05 -07:00
|
|
|
import type { User } from '@db/entities/User';
|
2022-11-09 06:25:00 -08:00
|
|
|
import type { InstalledNodes } from '@db/entities/InstalledNodes';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { NodeTypes } from '@/NodeTypes';
|
|
|
|
import { Push } from '@/push';
|
2023-03-17 09:24:05 -07:00
|
|
|
import { COMMUNITY_PACKAGE_VERSION } from './shared/constants';
|
|
|
|
import * as utils from './shared/utils';
|
|
|
|
import * as testDb from './shared/testDb';
|
2023-02-21 10:21:56 -08:00
|
|
|
|
|
|
|
const mockLoadNodesAndCredentials = utils.mockInstance(LoadNodesAndCredentials);
|
|
|
|
utils.mockInstance(NodeTypes);
|
|
|
|
utils.mockInstance(Push);
|
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
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
let ownerShell: User;
|
|
|
|
let authOwnerShellAgent: SuperAgentTest;
|
2022-07-20 07:24:03 -07:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
const app = await utils.initTestServer({ endpointGroups: ['nodes'] });
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const globalOwnerRole = await testDb.getGlobalOwnerRole();
|
|
|
|
ownerShell = await testDb.createUserShell(globalOwnerRole);
|
|
|
|
authOwnerShellAgent = utils.createAuthAgent(app)(ownerShell);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
|
|
|
utils.initConfigFile();
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
await testDb.truncate(['InstalledNodes', 'InstalledPackages']);
|
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
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('GET /nodes', () => {
|
|
|
|
test('should respond 200 if no nodes are installed', async () => {
|
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { data },
|
|
|
|
} = await authOwnerShellAgent.get('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(statusCode).toBe(200);
|
|
|
|
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 () => {
|
|
|
|
const { packageName } = await testDb.saveInstalledPackage(utils.installedPackagePayload());
|
|
|
|
await testDb.saveInstalledNode(utils.installedNodePayload(packageName));
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { data },
|
|
|
|
} = await authOwnerShellAgent.get('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(statusCode).toBe(200);
|
|
|
|
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 () => {
|
|
|
|
const first = await testDb.saveInstalledPackage(utils.installedPackagePayload());
|
|
|
|
await testDb.saveInstalledNode(utils.installedNodePayload(first.packageName));
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -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
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { data },
|
|
|
|
} = await authOwnerShellAgent.get('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(statusCode).toBe(200);
|
|
|
|
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 () => {
|
|
|
|
await authOwnerShellAgent.get('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(mocked(executeCommand)).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
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 () => {
|
|
|
|
const { packageName } = await testDb.saveInstalledPackage(utils.installedPackagePayload());
|
|
|
|
await testDb.saveInstalledNode(utils.installedNodePayload(packageName));
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerShellAgent.get('/nodes');
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(mocked(executeCommand)).toHaveBeenCalledWith('npm outdated --json', {
|
|
|
|
doNotHandleError: true,
|
|
|
|
});
|
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 () => {
|
|
|
|
const { packageName } = await testDb.saveInstalledPackage(utils.installedPackagePayload());
|
|
|
|
await testDb.saveInstalledNode(utils.installedNodePayload(packageName));
|
|
|
|
|
|
|
|
mocked(executeCommand).mockImplementationOnce(() => {
|
|
|
|
throw {
|
|
|
|
code: 1,
|
|
|
|
stdout: JSON.stringify({
|
|
|
|
[packageName]: {
|
|
|
|
current: COMMUNITY_PACKAGE_VERSION.CURRENT,
|
|
|
|
wanted: COMMUNITY_PACKAGE_VERSION.CURRENT,
|
|
|
|
latest: COMMUNITY_PACKAGE_VERSION.UPDATED,
|
|
|
|
location: path.join('node_modules', packageName),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
mocked(isNpmError).mockReturnValueOnce(true);
|
|
|
|
|
|
|
|
const {
|
|
|
|
body: { data },
|
|
|
|
} = await authOwnerShellAgent.get('/nodes');
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('POST /nodes', () => {
|
|
|
|
test('should reject if package name is missing', async () => {
|
|
|
|
const { statusCode } = await authOwnerShellAgent.post('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(statusCode).toBe(400);
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should reject if package is duplicate', async () => {
|
|
|
|
mocked(findInstalledPackage).mockResolvedValueOnce(new InstalledPackages());
|
|
|
|
mocked(isPackageInstalled).mockResolvedValueOnce(true);
|
|
|
|
mocked(hasPackageLoaded).mockReturnValueOnce(true);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { message },
|
|
|
|
} = await authOwnerShellAgent.post('/nodes').send({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
|
|
|
});
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(statusCode).toBe(400);
|
|
|
|
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 () => {
|
|
|
|
mocked(findInstalledPackage).mockResolvedValueOnce(new InstalledPackages());
|
|
|
|
mocked(hasPackageLoaded).mockReturnValueOnce(false);
|
|
|
|
mocked(checkNpmPackageStatus).mockResolvedValueOnce({ status: 'OK' });
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-24 09:04:26 -07:00
|
|
|
mockLoadNodesAndCredentials.installNpmModule.mockImplementationOnce(mockedEmptyPackage);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const { statusCode } = await authOwnerShellAgent.post('/nodes').send({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(statusCode).toBe(200);
|
|
|
|
expect(mocked(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 () => {
|
|
|
|
mocked(checkNpmPackageStatus).mockResolvedValueOnce({ status: 'Banned' });
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { message },
|
|
|
|
} = await authOwnerShellAgent.post('/nodes').send({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(statusCode).toBe(400);
|
|
|
|
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 () => {
|
|
|
|
const response = await authOwnerShellAgent.delete('/nodes');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
});
|
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 {
|
|
|
|
statusCode,
|
|
|
|
body: { message },
|
|
|
|
} = await authOwnerShellAgent.delete('/nodes').query({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
|
|
|
});
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(statusCode).toBe(400);
|
|
|
|
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 () => {
|
|
|
|
const removeSpy = mockLoadNodesAndCredentials.removeNpmModule.mockImplementationOnce(jest.fn());
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
mocked(findInstalledPackage).mockImplementationOnce(mockedEmptyPackage);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const { statusCode } = await authOwnerShellAgent.delete('/nodes').query({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
|
|
|
});
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(statusCode).toBe(200);
|
|
|
|
expect(removeSpy).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 () => {
|
|
|
|
const response = await authOwnerShellAgent.patch('/nodes');
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('reject if package is not installed', async () => {
|
|
|
|
const {
|
|
|
|
statusCode,
|
|
|
|
body: { message },
|
|
|
|
} = await authOwnerShellAgent.patch('/nodes').send({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
|
|
|
});
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(statusCode).toBe(400);
|
|
|
|
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 () => {
|
|
|
|
const updateSpy =
|
|
|
|
mockLoadNodesAndCredentials.updateNpmModule.mockImplementationOnce(mockedEmptyPackage);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
mocked(findInstalledPackage).mockImplementationOnce(mockedEmptyPackage);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerShellAgent.patch('/nodes').send({
|
|
|
|
name: utils.installedPackagePayload().packageName,
|
|
|
|
});
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
expect(updateSpy).toHaveBeenCalledTimes(1);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
});
|