feature/17-automated_tests #21

Merged
niggl merged 56 commits from feature/17-automated_tests into dev 2020-12-10 19:36:08 +00:00
29 changed files with 2083 additions and 1055 deletions

4
jest.config.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};

View File

@ -1,12 +1,12 @@
import { config } from 'dotenv'; import { config } from 'dotenv';
config(); config();
export default { export default {
type: process.env.DB_TYPE, type: process.env.DB_TYPE,
host: process.env.DB_HOST, host: process.env.DB_HOST,
port: process.env.DB_PORT, port: process.env.DB_PORT,
username: process.env.DB_USER, username: process.env.DB_USER,
password: process.env.DB_PASSWORD, password: process.env.DB_PASSWORD,
database: process.env.DB_NAME, database: process.env.DB_NAME,
entities: ["src/models/entities/*.ts"] entities: ["src/models/entities/*.ts"]
}; };

View File

@ -1,67 +1,81 @@
{ {
"name": "@lfk/backend", "name": "@lfk/backend",
"version": "1.0.0", "version": "1.0.0",
"main": "src/app.ts", "main": "src/app.ts",
"repository": "https://git.odit.services/lfk/backend", "repository": "https://git.odit.services/lfk/backend",
"author": { "author": {
"name": "ODIT.Services", "name": "ODIT.Services",
"email": "info@odit.services", "email": "info@odit.services",
"url": "https://odit.services" "url": "https://odit.services"
}, },
"contributors": [ "contributors": [
{ {
"name": "Philipp Dormann", "name": "Philipp Dormann",
"email": "philipp@philippdormann.de", "email": "philipp@philippdormann.de",
"url": "https://philippdormann.de" "url": "https://philippdormann.de"
}, },
{ {
"name": "Nicolai Ort", "name": "Nicolai Ort",
"email": "info@nicolai-ort.com", "email": "info@nicolai-ort.com",
"url": "https://nicolai-ort.com" "url": "https://nicolai-ort.com"
} }
], ],
"license": "CC-BY-NC-SA-4.0", "license": "CC-BY-NC-SA-4.0",
"dependencies": { "dependencies": {
"argon2": "^0.27.0", "argon2": "^0.27.0",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"class-transformer": "^0.3.1", "class-transformer": "^0.3.1",
"class-validator": "^0.12.2", "class-validator": "^0.12.2",
"class-validator-jsonschema": "^2.0.3", "class-validator-jsonschema": "^2.0.3",
"consola": "^2.15.0", "consola": "^2.15.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"express": "^4.17.1", "express": "^4.17.1",
"helmet": "^4.2.0", "helmet": "^4.2.0",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"multer": "^1.4.2", "multer": "^1.4.2",
"mysql": "^2.18.1", "mysql": "^2.18.1",
"pg": "^8.5.1", "pg": "^8.5.1",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"routing-controllers": "^0.9.0-alpha.6", "routing-controllers": "^0.9.0-alpha.6",
"routing-controllers-openapi": "^2.1.0", "routing-controllers-openapi": "^2.1.0",
"swagger-ui-express": "^4.1.5", "swagger-ui-express": "^4.1.5",
"typeorm": "^0.2.29", "typeorm": "^0.2.29",
"typeorm-routing-controllers-extensions": "^0.2.0", "typeorm-routing-controllers-extensions": "^0.2.0",
"uuid": "^8.3.1" "uuid": "^8.3.1"
}, },
"devDependencies": { "devDependencies": {
"@types/cors": "^2.8.8", "@types/cors": "^2.8.8",
"@types/dotenv": "^8.2.0", "@types/dotenv": "^8.2.0",
"@types/express": "^4.17.9", "@types/express": "^4.17.9",
"@types/jsonwebtoken": "^8.5.0", "@types/jest": "^26.0.16",
"@types/multer": "^1.4.4", "@types/jsonwebtoken": "^8.5.0",
"@types/node": "^14.14.9", "@types/multer": "^1.4.4",
"@types/swagger-ui-express": "^4.1.2", "@types/node": "^14.14.9",
"@types/uuid": "^8.3.0", "@types/swagger-ui-express": "^4.1.2",
"nodemon": "^2.0.6", "@types/uuid": "^8.3.0",
"sqlite3": "^5.0.0", "axios": "^0.21.0",
"ts-node": "^9.0.0", "dotenv-safe": "^8.2.0",
"typedoc": "^0.19.2", "jest": "^26.6.3",
"typescript": "^4.1.2" "nodemon": "^2.0.6",
}, "sqlite3": "^5.0.0",
"scripts": { "ts-jest": "^26.4.4",
"dev": "nodemon src/app.ts", "ts-node": "^9.0.0",
"build": "tsc", "typedoc": "^0.19.2",
"docs": "typedoc --out docs src" "typescript": "^4.1.2"
} },
"scripts": {
"dev": "nodemon src/app.ts",
"build": "tsc",
"docs": "typedoc --out docs src",
"test": "jest",
"test:watch": "jest --watchAll"
},
"nodemonConfig": {
"ignore": [
"src/tests/*",
"docs/*"
],
"delay": "2500"
}
} }

View File

@ -1,22 +1,22 @@
import { config as configDotenv } from 'dotenv'; import { config as configDotenv } from 'dotenv';
configDotenv(); configDotenv();
export const config = { export const config = {
internal_port: parseInt(process.env.APP_PORT) || 4010, internal_port: parseInt(process.env.APP_PORT) || 4010,
development: process.env.NODE_ENV === "production", development: process.env.NODE_ENV === "production",
jwt_secret: process.env.JWT_SECRET || "secretjwtsecret", jwt_secret: process.env.JWT_SECRET || "secretjwtsecret",
phone_validation_countrycode: process.env.PHONE_COUNTRYCODE || "ZZ" phone_validation_countrycode: process.env.PHONE_COUNTRYCODE || "ZZ"
} }
let errors = 0 let errors = 0
if (typeof config.internal_port !== "number") { if (typeof config.internal_port !== "number") {
errors++ errors++
} }
if (typeof config.phone_validation_countrycode !== "string") { if (typeof config.phone_validation_countrycode !== "string") {
errors++ errors++
} }
if (config.phone_validation_countrycode.length !== 2) { if (config.phone_validation_countrycode.length !== 2) {
errors++ errors++
} }
if (typeof config.development !== "boolean") { if (typeof config.development !== "boolean") {
errors++ errors++
} }
export let e = errors export let e = errors

View File

@ -1,101 +1,101 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { RunnerGroupNeededError, RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors'; import { RunnerGroupNeededError, RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors';
import { RunnerGroupNotFoundError } from '../errors/RunnerGroupErrors'; import { RunnerGroupNotFoundError } from '../errors/RunnerGroupErrors';
import { CreateRunner } from '../models/actions/CreateRunner'; import { CreateRunner } from '../models/actions/CreateRunner';
import { UpdateRunner } from '../models/actions/UpdateRunner'; import { UpdateRunner } from '../models/actions/UpdateRunner';
import { Runner } from '../models/entities/Runner'; import { Runner } from '../models/entities/Runner';
import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { ResponseRunner } from '../models/responses/ResponseRunner'; import { ResponseRunner } from '../models/responses/ResponseRunner';
@JsonController('/runners') @JsonController('/runners')
//@Authorized('RUNNERS:read') //@Authorized('RUNNERS:read')
export class RunnerController { export class RunnerController {
private runnerRepository: Repository<Runner>; private runnerRepository: Repository<Runner>;
/** /**
* Gets the repository of this controller's model/entity. * Gets the repository of this controller's model/entity.
*/ */
constructor() { constructor() {
this.runnerRepository = getConnectionManager().get().getRepository(Runner); this.runnerRepository = getConnectionManager().get().getRepository(Runner);
} }
@Get() @Get()
@ResponseSchema(ResponseRunner, { isArray: true }) @ResponseSchema(ResponseRunner, { isArray: true })
@OpenAPI({ description: 'Lists all runners.' }) @OpenAPI({ description: 'Lists all runners.' })
async getAll() { async getAll() {
let responseRunners: ResponseRunner[] = new Array<ResponseRunner>(); let responseRunners: ResponseRunner[] = new Array<ResponseRunner>();
const runners = await this.runnerRepository.find({ relations: ['scans', 'group'] }); const runners = await this.runnerRepository.find({ relations: ['scans', 'group'] });
runners.forEach(runner => { runners.forEach(runner => {
responseRunners.push(new ResponseRunner(runner)); responseRunners.push(new ResponseRunner(runner));
}); });
return responseRunners; return responseRunners;
} }
@Get('/:id') @Get('/:id')
@ResponseSchema(ResponseRunner) @ResponseSchema(ResponseRunner)
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
@OnUndefined(RunnerNotFoundError) @OnUndefined(RunnerNotFoundError)
@OpenAPI({ description: 'Returns a runner of a specified id (if it exists)' }) @OpenAPI({ description: 'Returns a runner of a specified id (if it exists)' })
async getOne(@Param('id') id: number) { async getOne(@Param('id') id: number) {
let runner = await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] }) let runner = await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] })
if (!runner) { throw new RunnerNotFoundError(); } if (!runner) { throw new RunnerNotFoundError(); }
return new ResponseRunner(runner); return new ResponseRunner(runner);
} }
@Post() @Post()
@ResponseSchema(ResponseRunner) @ResponseSchema(ResponseRunner)
@ResponseSchema(RunnerGroupNeededError) @ResponseSchema(RunnerGroupNeededError)
@ResponseSchema(RunnerGroupNotFoundError) @ResponseSchema(RunnerGroupNotFoundError)
@OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' }) @OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createRunner: CreateRunner) { async post(@Body({ validate: true }) createRunner: CreateRunner) {
let runner; let runner;
try { try {
runner = await createRunner.toRunner(); runner = await createRunner.toRunner();
} catch (error) { } catch (error) {
throw error; throw error;
} }
runner = await this.runnerRepository.save(runner) runner = await this.runnerRepository.save(runner)
return new ResponseRunner(await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] })); return new ResponseRunner(await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] }));
} }
@Put('/:id') @Put('/:id')
@ResponseSchema(ResponseRunner) @ResponseSchema(ResponseRunner)
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(RunnerIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a runner object (id can't be changed)." }) @OpenAPI({ description: "Update a runner object (id can't be changed)." })
async put(@Param('id') id: number, @Body({ validate: true }) runner: UpdateRunner) { async put(@Param('id') id: number, @Body({ validate: true }) runner: UpdateRunner) {
let oldRunner = await this.runnerRepository.findOne({ id: id }, { relations: ['group'] }); let oldRunner = await this.runnerRepository.findOne({ id: id }, { relations: ['group'] });
if (!oldRunner) { if (!oldRunner) {
throw new RunnerNotFoundError(); throw new RunnerNotFoundError();
} }
if (oldRunner.id != runner.id) { if (oldRunner.id != runner.id) {
throw new RunnerIdsNotMatchingError(); throw new RunnerIdsNotMatchingError();
} }
await this.runnerRepository.update(oldRunner, await runner.toRunner()); await this.runnerRepository.update(oldRunner, await runner.toRunner());
return new ResponseRunner(await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] })); return new ResponseRunner(await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] }));
} }
@Delete('/:id') @Delete('/:id')
@ResponseSchema(ResponseRunner) @ResponseSchema(ResponseRunner)
@ResponseSchema(ResponseEmpty, { statusCode: 204 }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
@OnUndefined(204) @OnUndefined(204)
@OpenAPI({ description: 'Delete a specified runner (if it exists).' }) @OpenAPI({ description: 'Delete a specified runner (if it exists).' })
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
let runner = await this.runnerRepository.findOne({ id: id }); let runner = await this.runnerRepository.findOne({ id: id });
if (!runner) { return null; } if (!runner) { return null; }
const responseRunner = await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] }); const responseRunner = await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] });
if (!runner) { if (!runner) {
throw new RunnerNotFoundError(); throw new RunnerNotFoundError();
} }
await this.runnerRepository.delete(runner); await this.runnerRepository.delete(runner);
return new ResponseRunner(responseRunner); return new ResponseRunner(responseRunner);
} }
} }

View File

@ -1,123 +1,123 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors';
import { CreateRunnerOrganisation } from '../models/actions/CreateRunnerOrganisation'; import { CreateRunnerOrganisation } from '../models/actions/CreateRunnerOrganisation';
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation'; import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation'; import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation';
import { RunnerController } from './RunnerController'; import { RunnerController } from './RunnerController';
import { RunnerTeamController } from './RunnerTeamController'; import { RunnerTeamController } from './RunnerTeamController';
@JsonController('/organisations') @JsonController('/organisations')
//@Authorized('RUNNERS:read') //@Authorized('RUNNERS:read')
export class RunnerOrganisationController { export class RunnerOrganisationController {
private runnerOrganisationRepository: Repository<RunnerOrganisation>; private runnerOrganisationRepository: Repository<RunnerOrganisation>;
/** /**
* Gets the repository of this controller's model/entity. * Gets the repository of this controller's model/entity.
*/ */
constructor() { constructor() {
this.runnerOrganisationRepository = getConnectionManager().get().getRepository(RunnerOrganisation); this.runnerOrganisationRepository = getConnectionManager().get().getRepository(RunnerOrganisation);
} }
@Get() @Get()
@ResponseSchema(ResponseRunnerOrganisation, { isArray: true }) @ResponseSchema(ResponseRunnerOrganisation, { isArray: true })
@OpenAPI({ description: 'Lists all runnerOrganisations.' }) @OpenAPI({ description: 'Lists all runnerOrganisations.' })
async getAll() { async getAll() {
let responseTeams: ResponseRunnerOrganisation[] = new Array<ResponseRunnerOrganisation>(); let responseTeams: ResponseRunnerOrganisation[] = new Array<ResponseRunnerOrganisation>();
const runners = await this.runnerOrganisationRepository.find({ relations: ['address', 'contact', 'teams'] }); const runners = await this.runnerOrganisationRepository.find({ relations: ['address', 'contact', 'teams'] });
runners.forEach(runner => { runners.forEach(runner => {
responseTeams.push(new ResponseRunnerOrganisation(runner)); responseTeams.push(new ResponseRunnerOrganisation(runner));
}); });
return responseTeams; return responseTeams;
} }
@Get('/:id') @Get('/:id')
@ResponseSchema(ResponseRunnerOrganisation) @ResponseSchema(ResponseRunnerOrganisation)
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
@OnUndefined(RunnerOrganisationNotFoundError) @OnUndefined(RunnerOrganisationNotFoundError)
@OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' }) @OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' })
async getOne(@Param('id') id: number) { async getOne(@Param('id') id: number) {
let runnerOrg = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] }); let runnerOrg = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] });
if (!runnerOrg) { throw new RunnerOrganisationNotFoundError(); } if (!runnerOrg) { throw new RunnerOrganisationNotFoundError(); }
return new ResponseRunnerOrganisation(runnerOrg); return new ResponseRunnerOrganisation(runnerOrg);
} }
@Post() @Post()
@ResponseSchema(ResponseRunnerOrganisation) @ResponseSchema(ResponseRunnerOrganisation)
@OpenAPI({ description: 'Create a new runnerOrganisation object (id will be generated automagicly).' }) @OpenAPI({ description: 'Create a new runnerOrganisation object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) { async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) {
let runnerOrganisation; let runnerOrganisation;
try { try {
runnerOrganisation = await createRunnerOrganisation.toRunnerOrganisation(); runnerOrganisation = await createRunnerOrganisation.toRunnerOrganisation();
} catch (error) { } catch (error) {
throw error; throw error;
} }
runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation); runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation);
return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] })); return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }));
} }
@Put('/:id') @Put('/:id')
@ResponseSchema(ResponseRunnerOrganisation) @ResponseSchema(ResponseRunnerOrganisation)
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." }) @OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." })
async put(@Param('id') id: number, @EntityFromBody() runnerOrganisation: RunnerOrganisation) { async put(@Param('id') id: number, @EntityFromBody() runnerOrganisation: RunnerOrganisation) {
let oldRunnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }); let oldRunnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id });
if (!oldRunnerOrganisation) { if (!oldRunnerOrganisation) {
throw new RunnerOrganisationNotFoundError(); throw new RunnerOrganisationNotFoundError();
} }
if (oldRunnerOrganisation.id != runnerOrganisation.id) { if (oldRunnerOrganisation.id != runnerOrganisation.id) {
throw new RunnerOrganisationIdsNotMatchingError(); throw new RunnerOrganisationIdsNotMatchingError();
} }
await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation); await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation);
runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }); runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] });
return new ResponseRunnerOrganisation(runnerOrganisation); return new ResponseRunnerOrganisation(runnerOrganisation);
} }
@Delete('/:id') @Delete('/:id')
@ResponseSchema(ResponseRunnerOrganisation) @ResponseSchema(ResponseRunnerOrganisation)
@ResponseSchema(ResponseEmpty, { statusCode: 204 }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
@ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 }) @ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 })
@ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 }) @ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 })
@OnUndefined(204) @OnUndefined(204)
@OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' }) @OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' })
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
let organisation = await this.runnerOrganisationRepository.findOne({ id: id }); let organisation = await this.runnerOrganisationRepository.findOne({ id: id });
if (!organisation) { return null; } if (!organisation) { return null; }
let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organisation, { relations: ['address', 'contact', 'runners', 'teams'] }); let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organisation, { relations: ['address', 'contact', 'runners', 'teams'] });
if (!force) { if (!force) {
if (runnerOrganisation.teams.length != 0) { if (runnerOrganisation.teams.length != 0) {
throw new RunnerOrganisationHasTeamsError(); throw new RunnerOrganisationHasTeamsError();
} }
} }
const teamController = new RunnerTeamController() const teamController = new RunnerTeamController()
for (let team of runnerOrganisation.teams) { for (let team of runnerOrganisation.teams) {
await teamController.remove(team.id, true); await teamController.remove(team.id, true);
} }
if (!force) { if (!force) {
if (runnerOrganisation.runners.length != 0) { if (runnerOrganisation.runners.length != 0) {
throw new RunnerOrganisationHasRunnersError(); throw new RunnerOrganisationHasRunnersError();
} }
} }
const runnerController = new RunnerController() const runnerController = new RunnerController()
for (let runner of runnerOrganisation.runners) { for (let runner of runnerOrganisation.runners) {
await runnerController.remove(runner.id, true); await runnerController.remove(runner.id, true);
} }
const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation); const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation);
await this.runnerOrganisationRepository.delete(organisation); await this.runnerOrganisationRepository.delete(organisation);
return responseOrganisation; return responseOrganisation;
} }
} }

View File

@ -1,111 +1,111 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNotFoundError } from '../errors/RunnerTeamErrors'; import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNotFoundError } from '../errors/RunnerTeamErrors';
import { CreateRunnerTeam } from '../models/actions/CreateRunnerTeam'; import { CreateRunnerTeam } from '../models/actions/CreateRunnerTeam';
import { UpdateRunnerTeam } from '../models/actions/UpdateRunnerTeam'; import { UpdateRunnerTeam } from '../models/actions/UpdateRunnerTeam';
import { RunnerTeam } from '../models/entities/RunnerTeam'; import { RunnerTeam } from '../models/entities/RunnerTeam';
import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam'; import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam';
import { RunnerController } from './RunnerController'; import { RunnerController } from './RunnerController';
@JsonController('/teams') @JsonController('/teams')
//@Authorized('RUNNERS:read') //@Authorized('RUNNERS:read')
export class RunnerTeamController { export class RunnerTeamController {
private runnerTeamRepository: Repository<RunnerTeam>; private runnerTeamRepository: Repository<RunnerTeam>;
/** /**
* Gets the repository of this controller's model/entity. * Gets the repository of this controller's model/entity.
*/ */
constructor() { constructor() {
this.runnerTeamRepository = getConnectionManager().get().getRepository(RunnerTeam); this.runnerTeamRepository = getConnectionManager().get().getRepository(RunnerTeam);
} }
@Get() @Get()
@ResponseSchema(ResponseRunnerTeam, { isArray: true }) @ResponseSchema(ResponseRunnerTeam, { isArray: true })
@OpenAPI({ description: 'Lists all runnerTeams.' }) @OpenAPI({ description: 'Lists all runnerTeams.' })
async getAll() { async getAll() {
let responseTeams: ResponseRunnerTeam[] = new Array<ResponseRunnerTeam>(); let responseTeams: ResponseRunnerTeam[] = new Array<ResponseRunnerTeam>();
const runners = await this.runnerTeamRepository.find({ relations: ['parentGroup', 'contact'] }); const runners = await this.runnerTeamRepository.find({ relations: ['parentGroup', 'contact'] });
runners.forEach(runner => { runners.forEach(runner => {
responseTeams.push(new ResponseRunnerTeam(runner)); responseTeams.push(new ResponseRunnerTeam(runner));
}); });
return responseTeams; return responseTeams;
} }
@Get('/:id') @Get('/:id')
@ResponseSchema(ResponseRunnerTeam) @ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@OnUndefined(RunnerTeamNotFoundError) @OnUndefined(RunnerTeamNotFoundError)
@OpenAPI({ description: 'Returns a runnerTeam of a specified id (if it exists)' }) @OpenAPI({ description: 'Returns a runnerTeam of a specified id (if it exists)' })
async getOne(@Param('id') id: number) { async getOne(@Param('id') id: number) {
let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] }); let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] });
if (!runnerTeam) { throw new RunnerTeamNotFoundError(); } if (!runnerTeam) { throw new RunnerTeamNotFoundError(); }
return new ResponseRunnerTeam(runnerTeam); return new ResponseRunnerTeam(runnerTeam);
} }
@Post() @Post()
@ResponseSchema(ResponseRunnerTeam) @ResponseSchema(ResponseRunnerTeam)
@OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' }) @OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createRunnerTeam: CreateRunnerTeam) { async post(@Body({ validate: true }) createRunnerTeam: CreateRunnerTeam) {
let runnerTeam; let runnerTeam;
try { try {
runnerTeam = await createRunnerTeam.toRunnerTeam(); runnerTeam = await createRunnerTeam.toRunnerTeam();
} catch (error) { } catch (error) {
throw error; throw error;
} }
runnerTeam = await this.runnerTeamRepository.save(runnerTeam); runnerTeam = await this.runnerTeamRepository.save(runnerTeam);
runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] }); runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] });
return new ResponseRunnerTeam(runnerTeam); return new ResponseRunnerTeam(runnerTeam);
} }
@Put('/:id') @Put('/:id')
@ResponseSchema(ResponseRunnerTeam) @ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@ResponseSchema(RunnerTeamIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(RunnerTeamIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a runnerTeam object (id can't be changed)." }) @OpenAPI({ description: "Update a runnerTeam object (id can't be changed)." })
async put(@Param('id') id: number, @Body({ validate: true }) runnerTeam: UpdateRunnerTeam) { async put(@Param('id') id: number, @Body({ validate: true }) runnerTeam: UpdateRunnerTeam) {
let oldRunnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] }); let oldRunnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] });
if (!oldRunnerTeam) { if (!oldRunnerTeam) {
throw new RunnerTeamNotFoundError(); throw new RunnerTeamNotFoundError();
} }
if (oldRunnerTeam.id != runnerTeam.id) { if (oldRunnerTeam.id != runnerTeam.id) {
throw new RunnerTeamIdsNotMatchingError(); throw new RunnerTeamIdsNotMatchingError();
} }
await this.runnerTeamRepository.update(oldRunnerTeam, await runnerTeam.toRunnerTeam()); await this.runnerTeamRepository.update(oldRunnerTeam, await runnerTeam.toRunnerTeam());
return new ResponseRunnerTeam(await this.runnerTeamRepository.findOne({ id: runnerTeam.id }, { relations: ['parentGroup', 'contact'] })); return new ResponseRunnerTeam(await this.runnerTeamRepository.findOne({ id: runnerTeam.id }, { relations: ['parentGroup', 'contact'] }));
} }
@Delete('/:id') @Delete('/:id')
@ResponseSchema(ResponseRunnerTeam) @ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(ResponseEmpty, { statusCode: 204 }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
@ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 }) @ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 })
@OnUndefined(204) @OnUndefined(204)
@OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' }) @OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' })
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
let team = await this.runnerTeamRepository.findOne({ id: id }); let team = await this.runnerTeamRepository.findOne({ id: id });
if (!team) { return null; } if (!team) { return null; }
let runnerTeam = await this.runnerTeamRepository.findOne(team, { relations: ['parentGroup', 'contact', 'runners'] }); let runnerTeam = await this.runnerTeamRepository.findOne(team, { relations: ['parentGroup', 'contact', 'runners'] });
if (!force) { if (!force) {
if (runnerTeam.runners.length != 0) { if (runnerTeam.runners.length != 0) {
throw new RunnerTeamHasRunnersError(); throw new RunnerTeamHasRunnersError();
} }
} }
const runnerController = new RunnerController() const runnerController = new RunnerController()
for (let runner of runnerTeam.runners) { for (let runner of runnerTeam.runners) {
await runnerController.remove(runner.id, true); await runnerController.remove(runner.id, true);
} }
const responseTeam = new ResponseRunnerTeam(runnerTeam); const responseTeam = new ResponseRunnerTeam(runnerTeam);
await this.runnerTeamRepository.delete(team); await this.runnerTeamRepository.delete(team);
return responseTeam; return responseTeam;
} }
} }

View File

@ -1,88 +1,88 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors'; import { UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors';
import { UserGroupNotFoundError } from '../errors/UserGroupErrors'; import { UserGroupNotFoundError } from '../errors/UserGroupErrors';
import { CreateUser } from '../models/actions/CreateUser'; import { CreateUser } from '../models/actions/CreateUser';
import { User } from '../models/entities/User'; import { User } from '../models/entities/User';
import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';
@JsonController('/users') @JsonController('/users')
export class UserController { export class UserController {
private userRepository: Repository<User>; private userRepository: Repository<User>;
/** /**
* Gets the repository of this controller's model/entity. * Gets the repository of this controller's model/entity.
*/ */
constructor() { constructor() {
this.userRepository = getConnectionManager().get().getRepository(User); this.userRepository = getConnectionManager().get().getRepository(User);
} }
@Get() @Get()
@ResponseSchema(User, { isArray: true }) @ResponseSchema(User, { isArray: true })
@OpenAPI({ description: 'Lists all users.' }) @OpenAPI({ description: 'Lists all users.' })
getAll() { getAll() {
return this.userRepository.find(); return this.userRepository.find();
} }
@Get('/:id') @Get('/:id')
@ResponseSchema(User) @ResponseSchema(User)
@ResponseSchema(UserNotFoundError, { statusCode: 404 }) @ResponseSchema(UserNotFoundError, { statusCode: 404 })
@OnUndefined(UserNotFoundError) @OnUndefined(UserNotFoundError)
@OpenAPI({ description: 'Returns a user of a specified id (if it exists)' }) @OpenAPI({ description: 'Returns a user of a specified id (if it exists)' })
getOne(@Param('id') id: number) { getOne(@Param('id') id: number) {
return this.userRepository.findOne({ id: id }); return this.userRepository.findOne({ id: id });
} }
@Post() @Post()
@ResponseSchema(User) @ResponseSchema(User)
@ResponseSchema(UserGroupNotFoundError) @ResponseSchema(UserGroupNotFoundError)
@OpenAPI({ description: 'Create a new user object (id will be generated automagicly).' }) @OpenAPI({ description: 'Create a new user object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createUser: CreateUser) { async post(@Body({ validate: true }) createUser: CreateUser) {
let user; let user;
try { try {
user = await createUser.toUser(); user = await createUser.toUser();
} catch (error) { } catch (error) {
throw error; throw error;
} }
return this.userRepository.save(user); return this.userRepository.save(user);
} }
@Put('/:id') @Put('/:id')
@ResponseSchema(User) @ResponseSchema(User)
@ResponseSchema(UserNotFoundError, { statusCode: 404 }) @ResponseSchema(UserNotFoundError, { statusCode: 404 })
@ResponseSchema(UserIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(UserIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a user object (id can't be changed)." }) @OpenAPI({ description: "Update a user object (id can't be changed)." })
async put(@Param('id') id: number, @EntityFromBody() user: User) { async put(@Param('id') id: number, @EntityFromBody() user: User) {
let oldUser = await this.userRepository.findOne({ id: id }); let oldUser = await this.userRepository.findOne({ id: id });
if (!oldUser) { if (!oldUser) {
throw new UserNotFoundError(); throw new UserNotFoundError();
} }
if (oldUser.id != user.id) { if (oldUser.id != user.id) {
throw new UserIdsNotMatchingError(); throw new UserIdsNotMatchingError();
} }
await this.userRepository.update(oldUser, user); await this.userRepository.update(oldUser, user);
return user; return user;
} }
@Delete('/:id') @Delete('/:id')
@ResponseSchema(User) @ResponseSchema(User)
@ResponseSchema(ResponseEmpty, { statusCode: 204 }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
@OnUndefined(204) @OnUndefined(204)
@OpenAPI({ description: 'Delete a specified runner (if it exists).' }) @OpenAPI({ description: 'Delete a specified runner (if it exists).' })
async remove(@Param("id") id: number) { async remove(@Param("id") id: number) {
let user = await this.userRepository.findOne({ id: id }); let user = await this.userRepository.findOne({ id: id });
if (!user) { if (!user) {
return null; return null;
} }
await this.userRepository.delete(user); await this.userRepository.delete(user);
return user; return user;
} }
} }

View File

@ -1,87 +1,87 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors'; import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors';
import { CreateUserGroup } from '../models/actions/CreateUserGroup'; import { CreateUserGroup } from '../models/actions/CreateUserGroup';
import { UserGroup } from '../models/entities/UserGroup'; import { UserGroup } from '../models/entities/UserGroup';
import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';
@JsonController('/usergroups') @JsonController('/usergroups')
export class UserGroupController { export class UserGroupController {
private userGroupsRepository: Repository<UserGroup>; private userGroupsRepository: Repository<UserGroup>;
/** /**
* Gets the repository of this controller's model/entity. * Gets the repository of this controller's model/entity.
*/ */
constructor() { constructor() {
this.userGroupsRepository = getConnectionManager().get().getRepository(UserGroup); this.userGroupsRepository = getConnectionManager().get().getRepository(UserGroup);
} }
@Get() @Get()
@ResponseSchema(UserGroup, { isArray: true }) @ResponseSchema(UserGroup, { isArray: true })
@OpenAPI({ description: 'Lists all usergroups.' }) @OpenAPI({ description: 'Lists all usergroups.' })
getAll() { getAll() {
return this.userGroupsRepository.find(); return this.userGroupsRepository.find();
} }
@Get('/:id') @Get('/:id')
@ResponseSchema(UserGroup) @ResponseSchema(UserGroup)
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 }) @ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
@OnUndefined(UserGroupNotFoundError) @OnUndefined(UserGroupNotFoundError)
@OpenAPI({ description: 'Returns a usergroup of a specified id (if it exists)' }) @OpenAPI({ description: 'Returns a usergroup of a specified id (if it exists)' })
getOne(@Param('id') id: number) { getOne(@Param('id') id: number) {
return this.userGroupsRepository.findOne({ id: id }); return this.userGroupsRepository.findOne({ id: id });
} }
@Post() @Post()
@ResponseSchema(UserGroup) @ResponseSchema(UserGroup)
@ResponseSchema(UserGroupNotFoundError) @ResponseSchema(UserGroupNotFoundError)
@OpenAPI({ description: 'Create a new usergroup object (id will be generated automagicly).' }) @OpenAPI({ description: 'Create a new usergroup object (id will be generated automagicly).' })
async post(@Body({ validate: true }) createUserGroup: CreateUserGroup) { async post(@Body({ validate: true }) createUserGroup: CreateUserGroup) {
let userGroup; let userGroup;
try { try {
userGroup = await createUserGroup.toUserGroup(); userGroup = await createUserGroup.toUserGroup();
} catch (error) { } catch (error) {
throw error; throw error;
} }
return this.userGroupsRepository.save(userGroup); return this.userGroupsRepository.save(userGroup);
} }
@Put('/:id') @Put('/:id')
@ResponseSchema(UserGroup) @ResponseSchema(UserGroup)
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 }) @ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
@ResponseSchema(UserGroupIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(UserGroupIdsNotMatchingError, { statusCode: 406 })
@OpenAPI({ description: "Update a usergroup object (id can't be changed)." }) @OpenAPI({ description: "Update a usergroup object (id can't be changed)." })
async put(@Param('id') id: number, @EntityFromBody() userGroup: UserGroup) { async put(@Param('id') id: number, @EntityFromBody() userGroup: UserGroup) {
let oldUserGroup = await this.userGroupsRepository.findOne({ id: id }); let oldUserGroup = await this.userGroupsRepository.findOne({ id: id });
if (!oldUserGroup) { if (!oldUserGroup) {
throw new UserGroupNotFoundError() throw new UserGroupNotFoundError()
} }
if (oldUserGroup.id != userGroup.id) { if (oldUserGroup.id != userGroup.id) {
throw new UserGroupIdsNotMatchingError(); throw new UserGroupIdsNotMatchingError();
} }
await this.userGroupsRepository.update(oldUserGroup, userGroup); await this.userGroupsRepository.update(oldUserGroup, userGroup);
return userGroup; return userGroup;
} }
@Delete('/:id') @Delete('/:id')
@ResponseSchema(UserGroup) @ResponseSchema(UserGroup)
@ResponseSchema(ResponseEmpty, { statusCode: 204 }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
@OnUndefined(204) @OnUndefined(204)
@OpenAPI({ description: 'Delete a specified usergroup (if it exists).' }) @OpenAPI({ description: 'Delete a specified usergroup (if it exists).' })
async remove(@Param("id") id: number) { async remove(@Param("id") id: number) {
let group = await this.userGroupsRepository.findOne({ id: id }); let group = await this.userGroupsRepository.findOne({ id: id });
if (!group) { if (!group) {
return null; return null;
} }
await this.userGroupsRepository.delete(group); await this.userGroupsRepository.delete(group);
return group; return group;
} }
} }

View File

@ -1,84 +1,84 @@
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { config } from '../../config'; import { config } from '../../config';
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
import { Address } from '../entities/Address'; import { Address } from '../entities/Address';
import { GroupContact } from '../entities/GroupContact'; import { GroupContact } from '../entities/GroupContact';
export class CreateGroupContact { export class CreateGroupContact {
/** /**
* The contact's first name. * The contact's first name.
*/ */
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
firstname: string; firstname: string;
/** /**
* The contact's middle name. * The contact's middle name.
* Optional * Optional
*/ */
@IsOptional() @IsOptional()
@IsString() @IsString()
middlename?: string; middlename?: string;
/** /**
* The contact's last name. * The contact's last name.
*/ */
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
lastname: string; lastname: string;
/** /**
* The contact's address. * The contact's address.
* Optional * Optional
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()
address?: number; address?: number;
/** /**
* The contact's phone number. * The contact's phone number.
* Optional * Optional
*/ */
@IsOptional() @IsOptional()
@IsPhoneNumber(config.phone_validation_countrycode) @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string; phone?: string;
/** /**
* The contact's email address. * The contact's email address.
* Optional * Optional
*/ */
@IsOptional() @IsOptional()
@IsEmail() @IsEmail()
email?: string; email?: string;
/** /**
* Get's this participant's address from this.address. * Get's this participant's address from this.address.
*/ */
public async getAddress(): Promise<Address> { public async getAddress(): Promise<Address> {
if (this.address === undefined) { if (this.address === undefined) {
return null; return null;
} }
if (!isNaN(this.address)) { if (!isNaN(this.address)) {
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
if (!address) { throw new AddressNotFoundError; } if (!address) { throw new AddressNotFoundError; }
return address; return address;
} }
throw new AddressWrongTypeError; throw new AddressWrongTypeError;
} }
/** /**
* Creates a Address object based on this. * Creates a Address object based on this.
*/ */
public async toGroupContact(): Promise<GroupContact> { public async toGroupContact(): Promise<GroupContact> {
let contact: GroupContact = new GroupContact(); let contact: GroupContact = new GroupContact();
contact.firstname = this.firstname; contact.firstname = this.firstname;
contact.middlename = this.middlename; contact.middlename = this.middlename;
contact.lastname = this.lastname; contact.lastname = this.lastname;
contact.email = this.email; contact.email = this.email;
contact.phone = this.phone; contact.phone = this.phone;
contact.address = await this.getAddress(); contact.address = await this.getAddress();
return null; return null;
} }
} }

View File

@ -1,72 +1,72 @@
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { config } from '../../config'; import { config } from '../../config';
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
import { Address } from '../entities/Address'; import { Address } from '../entities/Address';
export abstract class CreateParticipant { export abstract class CreateParticipant {
/** /**
* The new participant's first name. * The new participant's first name.
*/ */
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
firstname: string; firstname: string;
/** /**
* The new participant's middle name. * The new participant's middle name.
* Optional. * Optional.
*/ */
@IsString() @IsString()
@IsOptional() @IsOptional()
middlename?: string; middlename?: string;
/** /**
* The new participant's last name. * The new participant's last name.
*/ */
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
lastname: string; lastname: string;
/** /**
* The new participant's phone number. * The new participant's phone number.
* Optional. * Optional.
*/ */
@IsString() @IsString()
@IsOptional() @IsOptional()
@IsPhoneNumber(config.phone_validation_countrycode) @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string; phone?: string;
/** /**
* The new participant's e-mail address. * The new participant's e-mail address.
* Optional. * Optional.
*/ */
@IsString() @IsString()
@IsOptional() @IsOptional()
@IsEmail() @IsEmail()
email?: string; email?: string;
/** /**
* The new participant's address. * The new participant's address.
* Must be of type number (address id). * Must be of type number (address id).
* Optional. * Optional.
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()
address?: number; address?: number;
/** /**
* Get's this participant's address from this.address. * Get's this participant's address from this.address.
*/ */
public async getAddress(): Promise<Address> { public async getAddress(): Promise<Address> {
if (this.address === undefined) { if (this.address === undefined) {
return null; return null;
} }
if (!isNaN(this.address)) { if (!isNaN(this.address)) {
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
if (!address) { throw new AddressNotFoundError; } if (!address) { throw new AddressNotFoundError; }
return address; return address;
} }
throw new AddressWrongTypeError; throw new AddressWrongTypeError;
} }
} }

View File

@ -1,120 +1,120 @@
import * as argon2 from "argon2"; import * as argon2 from "argon2";
import { IsEmail, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { IsEmail, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import * as uuid from 'uuid'; import * as uuid from 'uuid';
import { config } from '../../config'; import { config } from '../../config';
import { UsernameOrEmailNeededError } from '../../errors/UserErrors'; import { UsernameOrEmailNeededError } from '../../errors/UserErrors';
import { UserGroupNotFoundError } from '../../errors/UserGroupErrors'; import { UserGroupNotFoundError } from '../../errors/UserGroupErrors';
import { User } from '../entities/User'; import { User } from '../entities/User';
import { UserGroup } from '../entities/UserGroup'; import { UserGroup } from '../entities/UserGroup';
export class CreateUser { export class CreateUser {
/** /**
* The new user's first name. * The new user's first name.
*/ */
@IsString() @IsString()
firstname: string; firstname: string;
/** /**
* The new user's middle name. * The new user's middle name.
* Optinal. * Optinal.
*/ */
@IsString() @IsString()
@IsOptional() @IsOptional()
middlename?: string; middlename?: string;
/** /**
* The new user's last name. * The new user's last name.
*/ */
@IsString() @IsString()
lastname: string; lastname: string;
/** /**
* The new user's username. * The new user's username.
* You have to provide at least one of: {email, username}. * You have to provide at least one of: {email, username}.
*/ */
@IsOptional() @IsOptional()
@IsString() @IsString()
username?: string; username?: string;
/** /**
* The new user's email address. * The new user's email address.
* You have to provide at least one of: {email, username}. * You have to provide at least one of: {email, username}.
*/ */
@IsEmail() @IsEmail()
@IsString() @IsString()
@IsOptional() @IsOptional()
email?: string; email?: string;
/** /**
* The new user's phone number. * The new user's phone number.
* Optional * Optional
*/ */
@IsPhoneNumber(config.phone_validation_countrycode) @IsPhoneNumber(config.phone_validation_countrycode)
@IsOptional() @IsOptional()
phone?: string; phone?: string;
/** /**
* The new user's password. * The new user's password.
* This will of course not be saved in plaintext :) * This will of course not be saved in plaintext :)
*/ */
@IsString() @IsString()
password: string; password: string;
/** /**
* The new user's groups' id(s). * The new user's groups' id(s).
* You can provide either one groupId or an array of groupIDs. * You can provide either one groupId or an array of groupIDs.
* Optional. * Optional.
*/ */
@IsOptional() @IsOptional()
groupId?: number[] | number groupId?: number[] | number
//TODO: ProfilePics //TODO: ProfilePics
/** /**
* Converts this to a User Entity. * Converts this to a User Entity.
*/ */
public async toUser(): Promise<User> { public async toUser(): Promise<User> {
let newUser: User = new User(); let newUser: User = new User();
if (this.email === undefined && this.username === undefined) { if (this.email === undefined && this.username === undefined) {
throw new UsernameOrEmailNeededError(); throw new UsernameOrEmailNeededError();
} }
if (this.groupId) { if (this.groupId) {
if (!Array.isArray(this.groupId)) { if (!Array.isArray(this.groupId)) {
this.groupId = [this.groupId] this.groupId = [this.groupId]
} }
const groupIDs: number[] = this.groupId const groupIDs: number[] = this.groupId
let errors = 0 let errors = 0
const validateusergroups = async () => { const validateusergroups = async () => {
let foundgroups = [] let foundgroups = []
for (const g of groupIDs) { for (const g of groupIDs) {
const found = await getConnectionManager().get().getRepository(UserGroup).find({ id: g }); const found = await getConnectionManager().get().getRepository(UserGroup).find({ id: g });
if (found.length === 0) { if (found.length === 0) {
errors++ errors++
} else { } else {
foundgroups.push(found[0]) foundgroups.push(found[0])
} }
} }
newUser.groups = foundgroups newUser.groups = foundgroups
} }
await validateusergroups() await validateusergroups()
if (errors !== 0) { if (errors !== 0) {
throw new UserGroupNotFoundError(); throw new UserGroupNotFoundError();
} }
} }
newUser.email = this.email newUser.email = this.email
newUser.username = this.username newUser.username = this.username
newUser.firstname = this.firstname newUser.firstname = this.firstname
newUser.middlename = this.middlename newUser.middlename = this.middlename
newUser.lastname = this.lastname newUser.lastname = this.lastname
newUser.uuid = uuid.v4() newUser.uuid = uuid.v4()
newUser.phone = this.phone newUser.phone = this.phone
newUser.password = await argon2.hash(this.password + newUser.uuid); newUser.password = await argon2.hash(this.password + newUser.uuid);
//TODO: ProfilePics //TODO: ProfilePics
return newUser; return newUser;
} }
} }

View File

@ -1,83 +1,83 @@
import { import {
IsEmail, IsEmail,
IsInt, IsInt,
IsNotEmpty, IsNotEmpty,
IsOptional, IsOptional,
IsPhoneNumber, IsPhoneNumber,
IsString IsString
} from "class-validator"; } from "class-validator";
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { config } from '../../config'; import { config } from '../../config';
import { Address } from "./Address"; import { Address } from "./Address";
import { RunnerGroup } from "./RunnerGroup"; import { RunnerGroup } from "./RunnerGroup";
/** /**
* Defines a group's contact. * Defines a group's contact.
*/ */
@Entity() @Entity()
export class GroupContact { export class GroupContact {
/** /**
* Autogenerated unique id (primary key). * Autogenerated unique id (primary key).
*/ */
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
@IsInt() @IsInt()
id: number; id: number;
/** /**
* The contact's first name. * The contact's first name.
*/ */
@Column() @Column()
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
firstname: string; firstname: string;
/** /**
* The contact's middle name. * The contact's middle name.
* Optional * Optional
*/ */
@Column({ nullable: true }) @Column({ nullable: true })
@IsOptional() @IsOptional()
@IsString() @IsString()
middlename?: string; middlename?: string;
/** /**
* The contact's last name. * The contact's last name.
*/ */
@Column() @Column()
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
lastname: string; lastname: string;
/** /**
* The contact's address. * The contact's address.
* Optional * Optional
*/ */
@IsOptional() @IsOptional()
@ManyToOne(() => Address, address => address.participants, { nullable: true }) @ManyToOne(() => Address, address => address.participants, { nullable: true })
address?: Address; address?: Address;
/** /**
* The contact's phone number. * The contact's phone number.
* Optional * Optional
*/ */
@Column({ nullable: true }) @Column({ nullable: true })
@IsOptional() @IsOptional()
@IsPhoneNumber(config.phone_validation_countrycode) @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string; phone?: string;
/** /**
* The contact's email address. * The contact's email address.
* Optional * Optional
*/ */
@Column({ nullable: true }) @Column({ nullable: true })
@IsOptional() @IsOptional()
@IsEmail() @IsEmail()
email?: string; email?: string;
/** /**
* Used to link contacts to groups. * Used to link contacts to groups.
*/ */
@OneToMany(() => RunnerGroup, group => group.contact, { nullable: true }) @OneToMany(() => RunnerGroup, group => group.contact, { nullable: true })
groups: RunnerGroup[]; groups: RunnerGroup[];
} }

View File

@ -1,83 +1,83 @@
import { import {
IsEmail, IsEmail,
IsInt, IsInt,
IsNotEmpty, IsNotEmpty,
IsOptional, IsOptional,
IsPhoneNumber, IsPhoneNumber,
IsString IsString
} from "class-validator"; } from "class-validator";
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn, TableInheritance } from "typeorm"; import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn, TableInheritance } from "typeorm";
import { config } from '../../config'; import { config } from '../../config';
import { Address } from "./Address"; import { Address } from "./Address";
import { Donation } from "./Donation"; import { Donation } from "./Donation";
/** /**
* Defines the participant interface. * Defines the participant interface.
*/ */
@Entity() @Entity()
@TableInheritance({ column: { name: "type", type: "varchar" } }) @TableInheritance({ column: { name: "type", type: "varchar" } })
export abstract class Participant { export abstract class Participant {
/** /**
* Autogenerated unique id (primary key). * Autogenerated unique id (primary key).
*/ */
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
@IsInt() @IsInt()
id: number; id: number;
/** /**
* The participant's first name. * The participant's first name.
*/ */
@Column() @Column()
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
firstname: string; firstname: string;
/** /**
* The participant's middle name. * The participant's middle name.
* Optional * Optional
*/ */
@Column({ nullable: true }) @Column({ nullable: true })
@IsOptional() @IsOptional()
@IsString() @IsString()
middlename?: string; middlename?: string;
/** /**
* The participant's last name. * The participant's last name.
*/ */
@Column() @Column()
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
lastname: string; lastname: string;
/** /**
* The participant's address. * The participant's address.
* Optional * Optional
*/ */
@ManyToOne(() => Address, address => address.participants, { nullable: true }) @ManyToOne(() => Address, address => address.participants, { nullable: true })
address?: Address; address?: Address;
/** /**
* The participant's phone number. * The participant's phone number.
* Optional * Optional
*/ */
@Column({ nullable: true }) @Column({ nullable: true })
@IsOptional() @IsOptional()
@IsPhoneNumber(config.phone_validation_countrycode) @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string; phone?: string;
/** /**
* The participant's email address. * The participant's email address.
* Optional * Optional
*/ */
@Column({ nullable: true }) @Column({ nullable: true })
@IsOptional() @IsOptional()
@IsEmail() @IsEmail()
email?: string; email?: string;
/** /**
* Used to link the participant as the donor of a donation. * Used to link the participant as the donor of a donation.
*/ */
@OneToMany(() => Donation, donation => donation.donor, { nullable: true }) @OneToMany(() => Donation, donation => donation.donor, { nullable: true })
donations: Donation[]; donations: Donation[];
} }

View File

@ -1,9 +1,9 @@
import { IsString } from 'class-validator'; import { IsString } from 'class-validator';
/** /**
* Defines a empty response object * Defines a empty response object
*/ */
export class ResponseEmpty { export class ResponseEmpty {
@IsString() @IsString()
response: string = "nothing here" response: string = "nothing here"
} }

View File

@ -0,0 +1,23 @@
import axios from 'axios';
import { config } from '../config';
const base = "http://localhost:" + config.internal_port
describe('GET /api/openapi.json', () => {
it('is http 200', async () => {
const res = await axios.get(base + '/api/openapi.json');
expect(res.status).toEqual(200);
});
});
describe('GET /', () => {
it('is http 404', async () => {
const res = await axios.get(base + '/', { validateStatus: undefined });
expect(res.status).toEqual(404);
});
});
describe('GET /api/teams', () => {
it('is http 200 && is json', async () => {
const res = await axios.get(base + '/api/teams', { validateStatus: undefined });
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
});

View File

@ -0,0 +1,78 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
describe('GET /api/organisations', () => {
it('basic get should return 200', async () => {
const res = await axios.get(base + '/api/organisations');
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('POST /api/organisations', () => {
it('creating a new org with just a name should return 200', async () => {
const res = await axios.post(base + '/api/organisations', {
"name": "test123"
});
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
it('creating a new org with without a name should return 400', async () => {
const res = await axios.post(base + '/api/organisations', {
"name": null
}, { validateStatus: undefined });
expect(res.status).toEqual(400);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('adding + getting from all orgs', () => {
it('creating a new org with just a name should return 200', async () => {
const res = await axios.post(base + '/api/organisations', {
"name": "test123"
});
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
it('check if org was added', async () => {
const res = await axios.get(base + '/api/organisations');
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
let added_org = res.data[res.data.length - 1]
delete added_org.id
expect(added_org).toEqual({
"name": "test123",
"contact": null,
"address": null,
"teams": []
})
});
});
// ---------------
describe('adding + getting explicitly', () => {
let added_org_id
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
let added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('check if org was added', async () => {
const res2 = await axios.get(base + '/api/organisations/' + added_org_id);
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
let added_org2 = res2.data
added_org_id = added_org2.id;
delete added_org2.id
expect(added_org2).toEqual({
"name": "test123",
"contact": null,
"address": null,
"teams": []
})
});
});

View File

@ -0,0 +1,120 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
// ---------------
describe('adding + deletion (non-existant)', () => {
it('delete', async () => {
const res2 = await axios.delete(base + '/api/organisations/0', { validateStatus: undefined });
expect(res2.status).toEqual(204);
});
});
// ---------------
describe('adding + deletion (successfull)', () => {
let added_org_id
let added_org
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('delete', async () => {
const res2 = await axios.delete(base + '/api/organisations/' + added_org_id);
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
let added_org2 = res2.data
added_org_id = added_org2.id;
delete added_org2.id
expect(added_org2).toEqual({
"name": "test123",
"contact": null,
"address": null,
"teams": []
});
});
it('check if org really was deleted', async () => {
const res3 = await axios.get(base + '/api/organisations/' + added_org_id, { validateStatus: undefined });
expect(res3.status).toEqual(404);
expect(res3.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('adding + deletion with teams still existing (without force)', () => {
let added_org;
let added_org_id;
let added_team;
let added_team_id
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org = res1.data;
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new team with a valid org should return 200', async () => {
const res2 = await axios.post(base + '/api/teams', {
"name": "test123",
"parentGroup": added_org_id
});
added_team = res2.data;
added_team_id = added_team.id;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('delete org - this should fail with a 406', async () => {
const res2 = await axios.delete(base + '/api/organisations/' + added_org_id, { validateStatus: undefined });
expect(res2.status).toEqual(406);
expect(res2.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('adding + deletion with teams still existing (with force)', () => {
let added_org;
let added_org_id;
let added_team;
let added_team_id
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org = res1.data;
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new team with a valid org should return 200', async () => {
const res2 = await axios.post(base + '/api/teams', {
"name": "test123",
"parentGroup": added_org_id
});
added_team = res2.data;
added_team_id = added_team.id;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('delete', async () => {
const res2 = await axios.delete(base + '/api/organisations/' + added_org_id + '?force=true');
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
let added_org2 = res2.data
added_org_id = added_org2.id;
delete added_org2.id;
delete added_org2.teams;
expect(added_org2).toEqual({
"name": "test123",
"contact": null,
"address": null
});
});
it('check if org really was deleted', async () => {
const res3 = await axios.get(base + '/api/organisations/' + added_org_id, { validateStatus: undefined });
expect(res3.status).toEqual(404);
expect(res3.headers['content-type']).toContain("application/json")
});
});

View File

@ -0,0 +1,19 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
describe('GET /api/organisations', () => {
it('basic get should return 200', async () => {
const res = await axios.get(base + '/api/organisations');
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('GET /api/organisations/0', () => {
it('basic get should return 404', async () => {
const res = await axios.get(base + '/api/runners/0', { validateStatus: undefined });
expect(res.status).toEqual(404);
expect(res.headers['content-type']).toContain("application/json")
});
});

View File

@ -0,0 +1,61 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
// ---------------
describe('adding + updating name', () => {
let added_org_id
let added_org
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('update org', async () => {
const res2 = await axios.put(base + '/api/organisations/' + added_org_id, {
"id": added_org_id,
"name": "testlelele",
"contact": null,
"address": null,
});
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
let added_org2 = res2.data
added_org_id = added_org2.id;
delete added_org2.id
expect(added_org2).toEqual({
"name": "testlelele",
"contact": null,
"address": null,
"teams": []
})
});
});
// ---------------
describe('adding + try updating id (should return 406)', () => {
let added_org_id
let added_org
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('update org', async () => {
const res2 = await axios.put(base + '/api/organisations/' + added_org_id, {
"id": added_org_id + 1,
"name": "testlelele",
"contact": null,
"address": null,
}, { validateStatus: undefined });
expect(res2.status).toEqual(406);
expect(res2.headers['content-type']).toContain("application/json")
});
});

View File

@ -0,0 +1,65 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
describe('GET /api/teams', () => {
it('basic get should return 200', async () => {
const res = await axios.get(base + '/api/teams');
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('GET /api/teams/0', () => {
it('basic get should return 404', async () => {
const res = await axios.get(base + '/api/teams/0', { validateStatus: undefined });
expect(res.status).toEqual(404);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('POST /api/teams with errors', () => {
it('creating a new team without a name should return 400', async () => {
const res1 = await axios.post(base + '/api/teams', {
"name": null
}, { validateStatus: undefined });
expect(res1.status).toEqual(400);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new team without a org should return 400', async () => {
const res2 = await axios.post(base + '/api/teams', {
"name": "test_team"
}, { validateStatus: undefined });
expect(res2.status).toEqual(400);
expect(res2.headers['content-type']).toContain("application/json")
});
it('creating a new team without a valid org should return 404', async () => {
const res3 = await axios.post(base + '/api/teams', {
"name": "test_team",
"parentGroup": -1
}, { validateStatus: undefined });
expect(res3.status).toEqual(404);
expect(res3.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('POST /api/teams working', () => {
let added_org_id;
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
let added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new team with a parent org should return 200', async () => {
const res2 = await axios.post(base + '/api/teams', {
"name": "test_team",
"parentGroup": added_org_id
});
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
});

View File

@ -0,0 +1,54 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
// ---------------
describe('adding org', () => {
let added_org;
let added_org_id;
let added_team;
let added_team_id
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org = res1.data;
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new team with a valid org should return 200', async () => {
const res2 = await axios.post(base + '/api/teams', {
"name": "test123",
"parentGroup": added_org_id
});
added_team = res2.data;
added_team_id = added_team.id;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('delete team', async () => {
const res2 = await axios.delete(base + '/api/teams/' + added_team_id);
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
let deleted_team = res2.data
delete deleted_team.id;
delete deleted_team.parentGroup;
expect(deleted_team).toEqual({
"name": "test123",
"contact": null
});
});
it('check if team really was deleted', async () => {
const res3 = await axios.get(base + '/api/teams/' + added_team_id, { validateStatus: undefined });
expect(res3.status).toEqual(404);
expect(res3.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('adding + deletion (non-existant)', () => {
it('delete', async () => {
const res2 = await axios.delete(base + '/api/teams/0', { validateStatus: undefined });
expect(res2.status).toEqual(204);
});
});

View File

@ -0,0 +1,19 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
describe('GET /api/teams', () => {
it('basic get should return 200', async () => {
const res = await axios.get(base + '/api/teams');
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('GET /api/teams/0', () => {
it('basic get should return 404', async () => {
const res = await axios.get(base + '/api/teams/0', { validateStatus: undefined });
expect(res.status).toEqual(404);
expect(res.headers['content-type']).toContain("application/json")
});
});

View File

@ -0,0 +1,119 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
// ---------------
describe('adding + updating name', () => {
let added_org;
let added_org_id;
let added_team;
let added_team_id
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org = res1.data;
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new team with a valid org should return 200', async () => {
const res2 = await axios.post(base + '/api/teams', {
"name": "test123",
"parentGroup": added_org_id
});
added_team = res2.data;
added_team_id = added_team.id;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('update name', async () => {
const res3 = await axios.put(base + '/api/teams/' + added_team_id, {
"id": added_team_id,
"name": "testlelele",
"contact": null,
"parentGroup": added_org
});
expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json")
let updated_team = res3.data;
added_team.name = "testlelele";
expect(updated_team).toEqual(added_team)
});
});
// ---------------
describe('adding + try updating id (should return 406)', () => {
let added_org;
let added_org_id;
let added_team;
let added_team_id
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org = res1.data;
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new team with a valid org should return 200', async () => {
const res2 = await axios.post(base + '/api/teams', {
"name": "test123",
"parentGroup": added_org_id
});
added_team = res2.data;
added_team_id = added_team.id;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('update team', async () => {
added_team.id = added_team.id + 1;
const res3 = await axios.put(base + '/api/teams/' + added_team_id, added_team, { validateStatus: undefined });
expect(res3.status).toEqual(406);
expect(res3.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('add+update parent org (valid)', () => {
let added_org;
let added_org2;
let added_team;
let added_team_id
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org = res1.data;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new team with a valid org should return 200', async () => {
const res2 = await axios.post(base + '/api/teams', {
"name": "test123",
"parentGroup": added_org.id
});
added_team = res2.data;
added_team_id = added_team.id;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('creating a new org with just a name should return 200', async () => {
const res3 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org2 = res3.data;
expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json")
});
it('update team', async () => {
added_team.parentGroup = added_org2;
const res4 = await axios.put(base + '/api/teams/' + added_team_id, added_team, { validateStatus: undefined });
let updated_team = res4.data;
expect(res4.status).toEqual(200);
expect(res4.headers['content-type']).toContain("application/json")
delete added_org2.address;
delete added_org2.contact;
delete added_org2.teams;
expect(updated_team).toEqual(added_team)
});
});

View File

@ -0,0 +1,100 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
describe('GET /api/runners', () => {
it('basic get should return 200', async () => {
const res = await axios.get(base + '/api/runners');
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('GET /api/runners/0', () => {
it('basic get should return 404', async () => {
const res = await axios.get(base + '/api/runners/0', { validateStatus: undefined });
expect(res.status).toEqual(404);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('POST /api/runners with errors', () => {
it('creating a new runner without any parameters should return 400', async () => {
const res1 = await axios.post(base + '/api/runners', null, { validateStatus: undefined });
expect(res1.status).toEqual(400);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new runner without a group should return 400', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"middlename": "middle",
"lastname": "last"
}, { validateStatus: undefined });
expect(res2.status).toEqual(400);
expect(res2.headers['content-type']).toContain("application/json")
});
it('creating a new runner with a invalid valid group should return 404', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"middlename": "middle",
"lastname": "last",
"group": 0
}, { validateStatus: undefined });
expect(res2.status).toEqual(404);
expect(res2.headers['content-type']).toContain("application/json")
});
it('creating a new runner without a invalid phone number should return 400', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"middlename": "middle",
"lastname": "last",
"phone": "123"
}, { validateStatus: undefined });
expect(res2.status).toEqual(400);
expect(res2.headers['content-type']).toContain("application/json")
});
it('creating a new runner without a invalid mail address should return 400', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"middlename": "middle",
"lastname": "last",
"email ": "123"
}, { validateStatus: undefined });
expect(res2.status).toEqual(400);
expect(res2.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('POST /api/runners working', () => {
let added_org_id;
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
let added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new runner with only needed params should return 200', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"lastname": "last",
"group": added_org_id
}, { validateStatus: undefined });
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('creating a new runner with all non-relationship optional params should return 200', async () => {
const res3 = await axios.post(base + '/api/runners', {
"firstname": "first",
"middlename": "middle",
"lastname": "last",
"email": "test@example.com",
"phone": "+4909123123456789",
"group": added_org_id
}, { validateStatus: undefined });
expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json")
});
});

View File

@ -0,0 +1,46 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
describe('adding + deletion (non-existant)', () => {
it('delete', async () => {
const res2 = await axios.delete(base + '/api/runners/0', { validateStatus: undefined });
expect(res2.status).toEqual(204);
});
});
// ---------------
describe('add+delete', () => {
let added_org_id;
let added_runner;
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
let added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new runner with only needed params should return 200', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"lastname": "last",
"group": added_org_id
}, { validateStatus: undefined });
added_runner = res2.data;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('delete runner', async () => {
const res3 = await axios.delete(base + '/api/runners/' + added_runner.id);
expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json")
let deleted_runner = res3.data
expect(deleted_runner).toEqual(added_runner);
});
it('check if team really was deleted', async () => {
const res4 = await axios.get(base + '/api/runners/' + added_runner.id, { validateStatus: undefined });
expect(res4.status).toEqual(404);
expect(res4.headers['content-type']).toContain("application/json")
});
});

View File

@ -0,0 +1,57 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
describe('GET /api/runners', () => {
it('basic get should return 200', async () => {
const res = await axios.get(base + '/api/runners');
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('GET /api/runners/0', () => {
it('basic get should return 404', async () => {
const res = await axios.get(base + '/api/runners/0', { validateStatus: undefined });
expect(res.status).toEqual(404);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('GET /api/runners after adding', () => {
let added_org_id;
let added_runner;
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
let added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new runner with only needed params should return 200', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"lastname": "last",
"group": added_org_id
}, { validateStatus: undefined });
added_runner = res2.data;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('explicit get should return 200', async () => {
const res3 = await axios.get(base + '/api/runners/' + added_runner.id, { validateStatus: undefined });
expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json")
let gotten_runner = res3.data
expect(gotten_runner).toEqual(added_runner);
});
it('get from all runners should return 200', async () => {
const res4 = await axios.get(base + '/api/runners/', { validateStatus: undefined });
expect(res4.status).toEqual(200);
expect(res4.headers['content-type']).toContain("application/json")
let gotten_runners = res4.data
expect(gotten_runners).toContainEqual(added_runner);
});
});

View File

@ -0,0 +1,145 @@
import axios from 'axios';
import { config } from '../../config';
const base = "http://localhost:" + config.internal_port
describe('Update runner name after adding', () => {
let added_org_id;
let added_runner;
let updated_runner;
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
let added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new runner with only needed params should return 200', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"lastname": "last",
"group": added_org_id
}, { validateStatus: undefined });
added_runner = res2.data;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('valid update should return 200', async () => {
let runnercopy = added_runner
runnercopy.firstname = "second"
const res3 = await axios.put(base + '/api/runners/' + added_runner.id, runnercopy, { validateStatus: undefined });
expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json")
updated_runner = res3.data
expect(updated_runner).toEqual(runnercopy);
});
});
// ---------------
describe('Update runner group after adding', () => {
let added_org_id;
let added_org_2;
let added_runner;
let updated_runner;
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
let added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new runner with only needed params should return 200', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"lastname": "last",
"group": added_org_id
}, { validateStatus: undefined });
added_runner = res2.data;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('creating a new org with just a name should return 200', async () => {
const res3 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org_2 = res3.data
delete added_org_2.address;
delete added_org_2.contact;
delete added_org_2.teams;
expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json")
});
it('valid update should return 200', async () => {
added_runner.group = added_org_2;
const res3 = await axios.put(base + '/api/runners/' + added_runner.id, added_runner, { validateStatus: undefined });
expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json")
updated_runner = res3.data
expect(updated_runner).toEqual(added_runner);
});
});
// ---------------
describe('Update runner id after adding(should fail)', () => {
let added_org_id;
let added_runner;
let added_runner_id;
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
let added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new runner with only needed params should return 200', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"lastname": "last",
"group": added_org_id
}, { validateStatus: undefined });
added_runner = res2.data;
added_runner_id = added_runner.id;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('invalid update should return 406', async () => {
added_runner.id++;
const res3 = await axios.put(base + '/api/runners/' + added_runner_id, added_runner, { validateStatus: undefined });
expect(res3.status).toEqual(406);
expect(res3.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('Update runner group with invalid group after adding', () => {
let added_org;
let added_runner;
it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', {
"name": "test123"
});
added_org = res1.data
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
});
it('creating a new runner with only needed params should return 200', async () => {
const res2 = await axios.post(base + '/api/runners', {
"firstname": "first",
"lastname": "last",
"group": added_org.id
}, { validateStatus: undefined });
added_runner = res2.data;
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
});
it('invalid update should return 404', async () => {
added_org.id = 0;
added_runner.group = added_org;
const res3 = await axios.put(base + '/api/runners/' + added_runner.id, added_runner, { validateStatus: undefined });
expect(res3.status).toEqual(404);
expect(res3.headers['content-type']).toContain("application/json")
});
});

104
src/tests/tracks.spec.ts Normal file
View File

@ -0,0 +1,104 @@
import axios from 'axios';
import { config } from '../config';
const base = "http://localhost:" + config.internal_port
describe('GET /api/tracks', () => {
it('basic get should return 200', async () => {
const res = await axios.get(base + '/api/tracks');
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
it('correct distance input should return 200', async () => {
const res = await axios.post(base + '/api/tracks', {
"name": "string",
"distance": 400
});
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('POST /api/tracks', () => {
it('illegal distance input should return 400', async () => {
const res = await axios.post(base + '/api/tracks', {
"name": "string",
"distance": -1
}, { validateStatus: undefined });
expect(res.status).toEqual(400);
expect(res.headers['content-type']).toContain("application/json")
});
it('correct distance input should return 200', async () => {
const res = await axios.post(base + '/api/tracks', {
"name": "string",
"distance": 400
});
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
});
// ---------------
describe('adding + getting tracks', () => {
it('correct distance input should return 200', async () => {
const res = await axios.post(base + '/api/tracks', {
"name": "string",
"distance": 1000
});
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
it('check if track was added', async () => {
const res = await axios.get(base + '/api/tracks');
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
let added_track = res.data[res.data.length - 1]
delete added_track.id
expect(added_track).toEqual({
"name": "string",
"distance": 1000
})
});
});
// ---------------
describe('adding + getting + updating', () => {
let added_track_id
it('correct distance input should return 200', async () => {
const res = await axios.post(base + '/api/tracks', {
"name": "string",
"distance": 1500
});
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});
it('get should return 200', async () => {
const res1 = await axios.get(base + '/api/tracks');
expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json")
let added_track = res1.data[res1.data.length - 1]
added_track_id = added_track.id
delete added_track.id
expect(added_track).toEqual({
"name": "string",
"distance": 1500
})
})
it('get should return 200', async () => {
const res2 = await axios.put(base + '/api/tracks/' + added_track_id, {
"id": added_track_id,
"name": "apitrack",
"distance": 5100
});
expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json")
})
it('get should return 200', async () => {
const res3 = await axios.get(base + '/api/tracks');
expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json")
let added_track2 = res3.data[res3.data.length - 1]
delete added_track2.id
expect(added_track2).toEqual({
"name": "apitrack",
"distance": 5100
})
});
});