2022-08-02 01:40:57 -07:00
|
|
|
import path from 'path';
|
2023-09-01 06:13:19 -07:00
|
|
|
|
2024-08-27 07:44:32 -07:00
|
|
|
import type { InstalledNodes } from '@/databases/entities/installed-nodes';
|
2024-09-12 09:07:18 -07:00
|
|
|
import type { InstalledPackages } from '@/databases/entities/installed-packages';
|
2024-08-22 02:10:37 -07:00
|
|
|
import { LoadNodesAndCredentials } from '@/load-nodes-and-credentials';
|
2024-08-28 08:57:46 -07:00
|
|
|
import { CommunityPackagesService } from '@/services/community-packages.service';
|
2023-09-01 06:13:19 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
import { COMMUNITY_PACKAGE_VERSION } from './shared/constants';
|
2023-11-08 07:29:39 -08:00
|
|
|
import { createOwner } from './shared/db/users';
|
2024-05-31 00:40:03 -07:00
|
|
|
import type { SuperAgentTest } from './shared/types';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { setupTestServer, mockPackage, mockNode, mockPackageName } from './shared/utils';
|
|
|
|
import { mockInstance } from '../shared/mocking';
|
2023-09-01 06:13:19 -07:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
const communityPackagesService = mockInstance(CommunityPackagesService, {
|
|
|
|
hasMissingPackages: false,
|
|
|
|
});
|
|
|
|
mockInstance(LoadNodesAndCredentials);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
const testServer = setupTestServer({ endpointGroups: ['community-packages'] });
|
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-11-08 07:29:39 -08:00
|
|
|
const ownerShell = await createOwner();
|
2023-09-01 06:13:19 -07:00
|
|
|
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-10-09 07:09:23 -07:00
|
|
|
describe('GET /community-packages', () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should respond 200 if no nodes are installed', async () => {
|
2023-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.getAllInstalledPackages.mockResolvedValue([]);
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
body: { data },
|
2023-10-09 07:09:23 -07:00
|
|
|
} = await authAgent.get('/community-packages').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];
|
2023-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.getAllInstalledPackages.mockResolvedValue([pkg]);
|
|
|
|
communityPackagesService.matchPackagesWithUpdates.mockReturnValue([pkg]);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
body: { data },
|
2023-10-09 07:09:23 -07:00
|
|
|
} = await authAgent.get('/community-packages').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);
|
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.getAllInstalledPackages.mockResolvedValue([pkgA, pkgB]);
|
2023-09-01 06:13:19 -07:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.matchPackagesWithUpdates.mockReturnValue([
|
2023-09-01 06:13:19 -07:00
|
|
|
{
|
|
|
|
...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-10-09 07:09:23 -07:00
|
|
|
} = await authAgent.get('/community-packages').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-10-09 07:09:23 -07:00
|
|
|
await authAgent.get('/community-packages');
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
expect(communityPackagesService.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-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.getAllInstalledPackages.mockResolvedValue([mockPackage()]);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
await authAgent.get('/community-packages').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 }];
|
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
expect(communityPackagesService.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();
|
2023-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.getAllInstalledPackages.mockResolvedValue([pkg]);
|
2023-03-17 09:24:05 -07:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.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-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.matchPackagesWithUpdates.mockReturnValue([
|
2023-09-01 06:13:19 -07:00
|
|
|
{
|
|
|
|
packageName: 'test',
|
|
|
|
installedNodes: [],
|
|
|
|
...commonUpdatesProps,
|
|
|
|
},
|
|
|
|
]);
|
2023-03-17 09:24:05 -07:00
|
|
|
|
|
|
|
const {
|
|
|
|
body: { data },
|
2023-10-09 07:09:23 -07:00
|
|
|
} = await authAgent.get('/community-packages').expect(200);
|
2023-09-01 06:13:19 -07:00
|
|
|
|
|
|
|
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-10-09 07:09:23 -07:00
|
|
|
describe('POST /community-packages', () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should reject if package name is missing', async () => {
|
2023-10-09 07:09:23 -07:00
|
|
|
await authAgent.post('/community-packages').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-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.findInstalledPackage.mockResolvedValue(mockPackage());
|
|
|
|
communityPackagesService.isPackageInstalled.mockResolvedValue(true);
|
|
|
|
communityPackagesService.hasPackageLoaded.mockReturnValue(true);
|
|
|
|
communityPackagesService.parseNpmPackageName.mockReturnValue(parsedNpmPackageName);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
body: { message },
|
2023-10-09 07:09:23 -07:00
|
|
|
} = await authAgent.post('/community-packages').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-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.findInstalledPackage.mockResolvedValue(mockPackage());
|
|
|
|
communityPackagesService.hasPackageLoaded.mockReturnValue(false);
|
|
|
|
communityPackagesService.checkNpmPackageStatus.mockResolvedValue({ status: 'OK' });
|
|
|
|
communityPackagesService.parseNpmPackageName.mockReturnValue(parsedNpmPackageName);
|
2024-08-05 02:52:06 -07:00
|
|
|
communityPackagesService.installPackage.mockResolvedValue(mockPackage());
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
await authAgent.post('/community-packages').send({ name: mockPackageName() }).expect(200);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
expect(communityPackagesService.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-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.checkNpmPackageStatus.mockResolvedValue({ status: 'Banned' });
|
|
|
|
communityPackagesService.parseNpmPackageName.mockReturnValue(parsedNpmPackageName);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
const {
|
|
|
|
body: { message },
|
2023-10-09 07:09:23 -07:00
|
|
|
} = await authAgent.post('/community-packages').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-10-09 07:09:23 -07:00
|
|
|
describe('DELETE /community-packages', () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should not delete if package name is empty', async () => {
|
2023-10-09 07:09:23 -07:00
|
|
|
await authAgent.delete('/community-packages').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-10-09 07:09:23 -07:00
|
|
|
} = await authAgent
|
|
|
|
.delete('/community-packages')
|
|
|
|
.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-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.findInstalledPackage.mockResolvedValue(mockPackage());
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
await authAgent.delete('/community-packages').query({ name: mockPackageName() }).expect(200);
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2024-08-05 02:52:06 -07:00
|
|
|
expect(communityPackagesService.removePackage).toHaveBeenCalledTimes(1);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
describe('PATCH /community-packages', () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should reject if package name is empty', async () => {
|
2023-10-09 07:09:23 -07:00
|
|
|
await authAgent.patch('/community-packages').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-10-09 07:09:23 -07:00
|
|
|
} = await authAgent.patch('/community-packages').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-10-09 07:09:23 -07:00
|
|
|
communityPackagesService.findInstalledPackage.mockResolvedValue(mockPackage());
|
|
|
|
communityPackagesService.parseNpmPackageName.mockReturnValue(parsedNpmPackageName);
|
2022-07-20 07:24:03 -07:00
|
|
|
|
2023-10-09 07:09:23 -07:00
|
|
|
await authAgent.patch('/community-packages').send({ name: mockPackageName() });
|
2022-08-02 01:40:57 -07:00
|
|
|
|
2024-08-05 02:52:06 -07:00
|
|
|
expect(communityPackagesService.updatePackage).toHaveBeenCalledTimes(1);
|
2022-07-20 07:24:03 -07:00
|
|
|
});
|
|
|
|
});
|