|
@@ -3,11 +3,17 @@ import { setupConnection, teardownConnection } from '../../../test/connection';
|
|
|
import fs from 'fs';
|
|
|
import { gcall } from '../../../test/gcall';
|
|
|
import App from '../app.entity';
|
|
|
-import { getAppQuery, listAppInfosQuery } from '../../../test/queries';
|
|
|
+import { getAppQuery, InstalledAppsQuery, listAppInfosQuery } from '../../../test/queries';
|
|
|
import { createApp } from './apps.factory';
|
|
|
import { AppInfo, AppStatusEnum, ListAppsResonse } from '../apps.types';
|
|
|
+import { createUser } from '../../auth/__tests__/user.factory';
|
|
|
+import User from '../../auth/user.entity';
|
|
|
+import { installAppMutation } from '../../../test/mutations';
|
|
|
|
|
|
jest.mock('fs');
|
|
|
+jest.mock('child_process');
|
|
|
+jest.mock('internal-ip');
|
|
|
+jest.mock('tcp-port-used');
|
|
|
|
|
|
type TApp = App & {
|
|
|
info: AppInfo;
|
|
@@ -30,6 +36,7 @@ beforeEach(async () => {
|
|
|
jest.resetAllMocks();
|
|
|
jest.restoreAllMocks();
|
|
|
await App.clear();
|
|
|
+ await User.clear();
|
|
|
});
|
|
|
|
|
|
describe('ListAppsInfos', () => {
|
|
@@ -97,3 +104,134 @@ describe('GetApp', () => {
|
|
|
expect(data?.getApp).toBeUndefined();
|
|
|
});
|
|
|
});
|
|
|
+
|
|
|
+describe('InstalledApps', () => {
|
|
|
+ let app1: AppInfo;
|
|
|
+
|
|
|
+ beforeEach(async () => {
|
|
|
+ const app1create = await createApp(true);
|
|
|
+ app1 = app1create.appInfo;
|
|
|
+ // @ts-ignore
|
|
|
+ fs.__createMockFiles(app1create.MockFiles);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('Can list installed apps', async () => {
|
|
|
+ const user = await createUser();
|
|
|
+
|
|
|
+ const { data } = await gcall<{ installedApps: TApp[] }>({ source: InstalledAppsQuery, userId: user.id });
|
|
|
+
|
|
|
+ expect(data?.installedApps.length).toBe(1);
|
|
|
+
|
|
|
+ const app = data?.installedApps[0];
|
|
|
+
|
|
|
+ expect(app?.id).toBe(app1.id);
|
|
|
+ expect(app?.info.author).toBe(app1.author);
|
|
|
+ expect(app?.info.name).toBe(app1.name);
|
|
|
+ });
|
|
|
+
|
|
|
+ it("Should return an error if user doesn't exist", async () => {
|
|
|
+ const { data, errors } = await gcall<{ installedApps: TApp[] }>({
|
|
|
+ source: InstalledAppsQuery,
|
|
|
+ userId: 1,
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
|
|
|
+ expect(data?.installedApps).toBeUndefined();
|
|
|
+ });
|
|
|
+
|
|
|
+ it('Should throw an error if no userId is provided', async () => {
|
|
|
+ const { data, errors } = await gcall<{ installedApps: TApp[] }>({
|
|
|
+ source: InstalledAppsQuery,
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
|
|
|
+ expect(data?.installedApps).toBeUndefined();
|
|
|
+ });
|
|
|
+});
|
|
|
+
|
|
|
+describe('InstallApp', () => {
|
|
|
+ let app1: AppInfo;
|
|
|
+
|
|
|
+ beforeEach(async () => {
|
|
|
+ const app1create = await createApp();
|
|
|
+ app1 = app1create.appInfo;
|
|
|
+ // @ts-ignore
|
|
|
+ fs.__createMockFiles(app1create.MockFiles);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('Can install app', async () => {
|
|
|
+ const user = await createUser();
|
|
|
+
|
|
|
+ const { data } = await gcall<{ installApp: TApp }>({
|
|
|
+ source: installAppMutation,
|
|
|
+ userId: user.id,
|
|
|
+ variableValues: { input: { id: app1.id, form: { TEST_FIELD: 'hello' } } },
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(data?.installApp.info.id).toBe(app1.id);
|
|
|
+ expect(data?.installApp.status).toBe(AppStatusEnum.RUNNING.toUpperCase());
|
|
|
+ });
|
|
|
+
|
|
|
+ it("Should return an error if app doesn't exist", async () => {
|
|
|
+ const user = await createUser();
|
|
|
+
|
|
|
+ const { data, errors } = await gcall<{ installApp: TApp }>({
|
|
|
+ source: installAppMutation,
|
|
|
+ userId: user.id,
|
|
|
+ variableValues: { input: { id: 'not-existing', form: { TEST_FIELD: 'hello' } } },
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(errors?.[0].message).toBe('App not-existing not found');
|
|
|
+ expect(data?.installApp).toBeUndefined();
|
|
|
+ });
|
|
|
+
|
|
|
+ it("Should throw an error if user doesn't exist", async () => {
|
|
|
+ const { data, errors } = await gcall<{ installApp: TApp }>({
|
|
|
+ source: installAppMutation,
|
|
|
+ variableValues: { input: { id: app1.id, form: { TEST_FIELD: 'hello' } } },
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
|
|
|
+ expect(data?.installApp).toBeUndefined();
|
|
|
+ });
|
|
|
+
|
|
|
+ it('Should throw an error if no userId is provided', async () => {
|
|
|
+ const { data, errors } = await gcall<{ installApp: TApp }>({
|
|
|
+ source: installAppMutation,
|
|
|
+ variableValues: { input: { id: app1.id, form: { TEST_FIELD: 'hello' } } },
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
|
|
|
+ expect(data?.installApp).toBeUndefined();
|
|
|
+ });
|
|
|
+
|
|
|
+ it('Should throw an error if a required field is missing in form', async () => {
|
|
|
+ const user = await createUser();
|
|
|
+
|
|
|
+ const { data, errors } = await gcall<{ installApp: TApp }>({
|
|
|
+ source: installAppMutation,
|
|
|
+ userId: user.id,
|
|
|
+ variableValues: { input: { id: app1.id, form: {} } },
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(errors?.[0].message).toBe(`Variable ${app1.form_fields?.[0].env_variable} is required`);
|
|
|
+ expect(data?.installApp).toBeUndefined();
|
|
|
+ });
|
|
|
+
|
|
|
+ it('Should throw an error if the requirements are not met', async () => {
|
|
|
+ const { appInfo, MockFiles } = await createApp(false, undefined, 400);
|
|
|
+ // @ts-ignore
|
|
|
+ fs.__createMockFiles(MockFiles);
|
|
|
+
|
|
|
+ const user = await createUser();
|
|
|
+
|
|
|
+ const { data, errors } = await gcall<{ installApp: TApp }>({
|
|
|
+ source: installAppMutation,
|
|
|
+ userId: user.id,
|
|
|
+ variableValues: { input: { id: appInfo.id, form: { TEST_FIELD: 'hello' } } },
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(errors?.[0].message).toBe(`App ${appInfo.id} requirements not met`);
|
|
|
+ expect(data?.installApp).toBeUndefined();
|
|
|
+ });
|
|
|
+});
|