test: apps.resolver

This commit is contained in:
Nicolas Meienberger 2022-08-10 22:09:23 +02:00
parent 4214b9e62d
commit 350b696bf7
8 changed files with 455 additions and 3 deletions

View file

@ -8,5 +8,3 @@ fi
pnpm -r test
pnpm -r lint:fix
docker stop test-db

View file

@ -8,7 +8,8 @@ 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';
import { installAppMutation, startAppMutation, stopAppMutation, uninstallAppMutation, updateAppConfigMutation, updateAppMutation } from '../../../test/mutations';
import { faker } from '@faker-js/faker';
jest.mock('fs');
jest.mock('child_process');
@ -235,3 +236,295 @@ describe('InstallApp', () => {
expect(data?.installApp).toBeUndefined();
});
});
describe('StartApp', () => {
let app1: AppInfo;
beforeEach(async () => {
const app1create = await createApp({ status: AppStatusEnum.STOPPED, installed: true });
app1 = app1create.appInfo;
// @ts-ignore
fs.__createMockFiles(app1create.MockFiles);
});
it('Can start app', async () => {
const user = await createUser();
const { data } = await gcall<{ startApp: TApp }>({
source: startAppMutation,
userId: user.id,
variableValues: { id: app1.id },
});
expect(data?.startApp.info.id).toBe(app1.id);
expect(data?.startApp.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<{ startApp: TApp }>({
source: startAppMutation,
userId: user.id,
variableValues: { id: 'not-existing' },
});
expect(errors?.[0].message).toBe('App not-existing not found');
expect(data?.startApp).toBeUndefined();
});
it("Should throw an error if user doesn't exist", async () => {
const { data, errors } = await gcall<{ startApp: TApp }>({
source: startAppMutation,
userId: 0,
variableValues: { id: app1.id },
});
expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
expect(data?.startApp).toBeUndefined();
});
it('Should throw an error if no userId is provided', async () => {
const { data, errors } = await gcall<{ startApp: TApp }>({
source: startAppMutation,
variableValues: { id: app1.id },
});
expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
expect(data?.startApp).toBeUndefined();
});
});
describe('StopApp', () => {
let app1: AppInfo;
beforeEach(async () => {
const app1create = await createApp({ status: AppStatusEnum.RUNNING, installed: true });
app1 = app1create.appInfo;
// @ts-ignore
fs.__createMockFiles(app1create.MockFiles);
});
it('Can stop app', async () => {
const user = await createUser();
const { data } = await gcall<{ stopApp: TApp }>({
source: stopAppMutation,
userId: user.id,
variableValues: { id: app1.id },
});
expect(data?.stopApp.info.id).toBe(app1.id);
expect(data?.stopApp.status).toBe(AppStatusEnum.STOPPED.toUpperCase());
});
it("Should return an error if app doesn't exist", async () => {
const user = await createUser();
const { data, errors } = await gcall<{ stopApp: TApp }>({
source: stopAppMutation,
userId: user.id,
variableValues: { id: 'not-existing' },
});
expect(errors?.[0].message).toBe('App not-existing not found');
expect(data?.stopApp).toBeUndefined();
});
it("Should throw an error if user doesn't exist", async () => {
const { data, errors } = await gcall<{ stopApp: TApp }>({
source: stopAppMutation,
userId: 0,
variableValues: { id: app1.id },
});
expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
expect(data?.stopApp).toBeUndefined();
});
it('Should throw an error if no userId is provided', async () => {
const { data, errors } = await gcall<{ stopApp: TApp }>({
source: stopAppMutation,
variableValues: { id: app1.id },
});
expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
expect(data?.stopApp).toBeUndefined();
});
});
describe('UninstallApp', () => {
let app1: AppInfo;
beforeEach(async () => {
const app1create = await createApp({ status: AppStatusEnum.STOPPED, installed: true });
app1 = app1create.appInfo;
// @ts-ignore
fs.__createMockFiles(app1create.MockFiles);
});
it('Should uninstall app', async () => {
const user = await createUser();
const { data } = await gcall<{ uninstallApp: TApp }>({
source: uninstallAppMutation,
userId: user.id,
variableValues: { id: app1.id },
});
expect(data?.uninstallApp.info.id).toBe(app1.id);
expect(data?.uninstallApp.status).toBe(AppStatusEnum.MISSING.toUpperCase());
});
it("Should return an error if app doesn't exist", async () => {
const user = await createUser();
const { data, errors } = await gcall<{ uninstallApp: TApp }>({
source: uninstallAppMutation,
userId: user.id,
variableValues: { id: 'not-existing' },
});
expect(errors?.[0].message).toBe('App not-existing not found');
expect(data?.uninstallApp).toBeUndefined();
});
it("Should throw an error if user doesn't exist", async () => {
const { data, errors } = await gcall<{ uninstallApp: TApp }>({
source: uninstallAppMutation,
userId: 0,
variableValues: { id: app1.id },
});
expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
expect(data?.uninstallApp).toBeUndefined();
});
it('Should throw an error if no userId is provided', async () => {
const { data, errors } = await gcall<{ uninstallApp: TApp }>({
source: uninstallAppMutation,
variableValues: { id: app1.id },
});
expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
expect(data?.uninstallApp).toBeUndefined();
});
});
describe('UpdateAppConfig', () => {
let app1: AppInfo;
beforeEach(async () => {
const app1create = await createApp({ status: AppStatusEnum.STOPPED, installed: true });
app1 = app1create.appInfo;
// @ts-ignore
fs.__createMockFiles(app1create.MockFiles);
});
it('Should update app config', async () => {
const user = await createUser();
const word = faker.random.word();
const { data } = await gcall<{ updateAppConfig: TApp }>({
source: updateAppConfigMutation,
userId: user.id,
variableValues: { input: { id: app1.id, form: { TEST_FIELD: word } } },
});
expect(data?.updateAppConfig.info.id).toBe(app1.id);
expect(data?.updateAppConfig.config.TEST_FIELD).toBe(word);
});
it("Should return an error if app doesn't exist", async () => {
const user = await createUser();
const { data, errors } = await gcall<{ updateAppConfig: TApp }>({
source: updateAppConfigMutation,
userId: user.id,
variableValues: { input: { id: 'not-existing', form: { TEST_FIELD: faker.random.word() } } },
});
expect(errors?.[0].message).toBe('App not-existing not found');
expect(data?.updateAppConfig).toBeUndefined();
});
it("Should throw an error if user doesn't exist", async () => {
const { data, errors } = await gcall<{ updateAppConfig: TApp }>({
source: updateAppConfigMutation,
userId: 0,
variableValues: { input: { id: app1.id, form: { TEST_FIELD: faker.random.word() } } },
});
expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
expect(data?.updateAppConfig).toBeUndefined();
});
it('Should throw an error if no userId is provided', async () => {
const { data, errors } = await gcall<{ updateAppConfig: TApp }>({
source: updateAppConfigMutation,
variableValues: { input: { id: app1.id, form: { TEST_FIELD: faker.random.word() } } },
});
expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
expect(data?.updateAppConfig).toBeUndefined();
});
});
describe('UpdateApp', () => {
let app1: AppInfo;
beforeEach(async () => {
const app1create = await createApp({ status: AppStatusEnum.STOPPED, installed: true });
app1 = app1create.appInfo;
// @ts-ignore
fs.__createMockFiles(app1create.MockFiles);
});
it('Should update app', async () => {
const user = await createUser();
const { data } = await gcall<{ updateApp: TApp }>({
source: updateAppMutation,
userId: user.id,
variableValues: { id: app1.id },
});
expect(data?.updateApp.info.id).toBe(app1.id);
expect(data?.updateApp.info.name).toBe(data?.updateApp.info.name);
});
it("Should return an error if app doesn't exist", async () => {
const user = await createUser();
const { data, errors } = await gcall<{ updateApp: TApp }>({
source: updateAppMutation,
userId: user.id,
variableValues: { id: 'not-existing' },
});
expect(errors?.[0].message).toBe('App not-existing not found');
expect(data?.updateApp).toBeUndefined();
});
it("Should throw an error if user doesn't exist", async () => {
const { data, errors } = await gcall<{ updateApp: TApp }>({
source: updateAppMutation,
userId: 0,
variableValues: { id: app1.id },
});
expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
expect(data?.updateApp).toBeUndefined();
});
it('Should throw an error if no userId is provided', async () => {
const { data, errors } = await gcall<{ updateApp: TApp }>({
source: updateAppMutation,
variableValues: { id: app1.id },
});
expect(errors?.[0].message).toBe('Access denied! You need to be authorized to perform this action!');
expect(data?.updateApp).toBeUndefined();
});
});

View file

@ -3,5 +3,15 @@ import 'graphql-import-node';
import { print } from 'graphql/language/printer';
import * as installApp from './installApp.graphql';
import * as startApp from './startApp.graphql';
import * as stopApp from './stopApp.graphql';
import * as uninstallApp from './uninstallApp.graphql';
import * as updateAppConfig from './updateAppConfig.graphql';
import * as updateApp from './updateApp.graphql';
export const installAppMutation = print(installApp);
export const startAppMutation = print(startApp);
export const stopAppMutation = print(stopApp);
export const uninstallAppMutation = print(uninstallApp);
export const updateAppConfigMutation = print(updateAppConfig);
export const updateAppMutation = print(updateApp);

View file

@ -0,0 +1,31 @@
# Write your query or mutation here
mutation StartApp($id: String!) {
startApp(id: $id) {
id
status
config
info {
id
available
port
name
description
version
author
source
categories
url_suffix
form_fields {
max
min
required
env_variable
}
requirements
}
updateInfo {
current
latest
}
}
}

View file

@ -0,0 +1,30 @@
mutation StopApp($id: String!) {
stopApp(id: $id) {
id
status
config
info {
id
available
port
name
description
version
author
source
categories
url_suffix
form_fields {
max
min
required
env_variable
}
requirements
}
updateInfo {
current
latest
}
}
}

View file

@ -0,0 +1,30 @@
mutation UninstallApp($id: String!) {
uninstallApp(id: $id) {
id
status
config
info {
id
available
port
name
description
version
author
source
categories
url_suffix
form_fields {
max
min
required
env_variable
}
requirements
}
updateInfo {
current
latest
}
}
}

View file

@ -0,0 +1,30 @@
mutation UpdateApp($id: String!) {
updateApp(id: $id) {
id
status
config
info {
id
available
port
name
description
version
author
source
categories
url_suffix
form_fields {
max
min
required
env_variable
}
requirements
}
updateInfo {
current
latest
}
}
}

View file

@ -0,0 +1,30 @@
mutation UpdateAppConfig($input: AppInputType!) {
updateAppConfig(input: $input) {
id
status
config
info {
id
available
port
name
description
version
author
source
categories
url_suffix
form_fields {
max
min
required
env_variable
}
requirements
}
updateInfo {
current
latest
}
}
}