From 0e924449d60c994c29b95abba675309e31c04383 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 18:45:47 +0100 Subject: [PATCH 01/15] Cleanup: Renamed the creation folder to the more fitting "actions" ref #11 #13 --- src/controllers/AuthController.ts | 6 ++--- src/controllers/RunnerController.ts | 2 +- .../RunnerOrganisationController.ts | 2 +- src/controllers/RunnerTeamController.ts | 2 +- src/controllers/TrackController.ts | 2 +- src/controllers/UserController.ts | 2 +- src/controllers/UserGroupController.ts | 2 +- .../{creation => actions}/CreateAddress.ts | 0 .../{creation => actions}/CreateAuth.ts | 0 .../CreateGroupContact.ts | 0 .../CreateParticipant.ts | 0 .../{creation => actions}/CreateRunner.ts | 0 .../CreateRunnerGroup.ts | 0 .../CreateRunnerOrganisation.ts | 0 .../{creation => actions}/CreateRunnerTeam.ts | 0 .../{creation => actions}/CreateTrack.ts | 0 .../{creation => actions}/CreateUser.ts | 0 .../{creation => actions}/CreateUserGroup.ts | 0 .../{creation => actions}/HandleLogout.ts | 0 .../{creation => actions}/RefreshAuth.ts | 0 src/tests.ts | 23 +++++++++++++++++++ 21 files changed, 32 insertions(+), 9 deletions(-) rename src/models/{creation => actions}/CreateAddress.ts (100%) rename src/models/{creation => actions}/CreateAuth.ts (100%) rename src/models/{creation => actions}/CreateGroupContact.ts (100%) rename src/models/{creation => actions}/CreateParticipant.ts (100%) rename src/models/{creation => actions}/CreateRunner.ts (100%) rename src/models/{creation => actions}/CreateRunnerGroup.ts (100%) rename src/models/{creation => actions}/CreateRunnerOrganisation.ts (100%) rename src/models/{creation => actions}/CreateRunnerTeam.ts (100%) rename src/models/{creation => actions}/CreateTrack.ts (100%) rename src/models/{creation => actions}/CreateUser.ts (100%) rename src/models/{creation => actions}/CreateUserGroup.ts (100%) rename src/models/{creation => actions}/HandleLogout.ts (100%) rename src/models/{creation => actions}/RefreshAuth.ts (100%) create mode 100644 src/tests.ts diff --git a/src/controllers/AuthController.ts b/src/controllers/AuthController.ts index 4e5ced1..d5320a3 100644 --- a/src/controllers/AuthController.ts +++ b/src/controllers/AuthController.ts @@ -2,9 +2,9 @@ import { Body, JsonController, Post } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { IllegalJWTError, InvalidCredentialsError, JwtNotProvidedError, PasswordNeededError, RefreshTokenCountInvalidError, UsernameOrEmailNeededError } from '../errors/AuthError'; import { UserNotFoundError } from '../errors/UserErrors'; -import { CreateAuth } from '../models/creation/CreateAuth'; -import { HandleLogout } from '../models/creation/HandleLogout'; -import { RefreshAuth } from '../models/creation/RefreshAuth'; +import { CreateAuth } from '../models/actions/CreateAuth'; +import { HandleLogout } from '../models/actions/HandleLogout'; +import { RefreshAuth } from '../models/actions/RefreshAuth'; import { Auth } from '../models/responses/Auth'; import { Logout } from '../models/responses/Logout'; diff --git a/src/controllers/RunnerController.ts b/src/controllers/RunnerController.ts index 6508f67..8293f36 100644 --- a/src/controllers/RunnerController.ts +++ b/src/controllers/RunnerController.ts @@ -3,7 +3,7 @@ import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors'; -import { CreateRunner } from '../models/creation/CreateRunner'; +import { CreateRunner } from '../models/actions/CreateRunner'; import { Runner } from '../models/entities/Runner'; import { ResponseRunner } from '../models/responses/ResponseRunner'; diff --git a/src/controllers/RunnerOrganisationController.ts b/src/controllers/RunnerOrganisationController.ts index 8cfef6a..1a598e5 100644 --- a/src/controllers/RunnerOrganisationController.ts +++ b/src/controllers/RunnerOrganisationController.ts @@ -3,7 +3,7 @@ import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; -import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganisation'; +import { CreateRunnerOrganisation } from '../models/actions/CreateRunnerOrganisation'; import { RunnerOrganisation } from '../models/entities/RunnerOrganisation'; import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation'; import { RunnerController } from './RunnerController'; diff --git a/src/controllers/RunnerTeamController.ts b/src/controllers/RunnerTeamController.ts index 1b7627d..7b0a841 100644 --- a/src/controllers/RunnerTeamController.ts +++ b/src/controllers/RunnerTeamController.ts @@ -3,7 +3,7 @@ import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNotFoundError } from '../errors/RunnerTeamErrors'; -import { CreateRunnerTeam } from '../models/creation/CreateRunnerTeam'; +import { CreateRunnerTeam } from '../models/actions/CreateRunnerTeam'; import { RunnerTeam } from '../models/entities/RunnerTeam'; import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam'; import { RunnerController } from './RunnerController'; diff --git a/src/controllers/TrackController.ts b/src/controllers/TrackController.ts index 02370fc..149b837 100644 --- a/src/controllers/TrackController.ts +++ b/src/controllers/TrackController.ts @@ -3,7 +3,7 @@ import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { TrackIdsNotMatchingError, TrackNotFoundError } from "../errors/TrackErrors"; -import { CreateTrack } from '../models/creation/CreateTrack'; +import { CreateTrack } from '../models/actions/CreateTrack'; import { Track } from '../models/entities/Track'; import { ResponseTrack } from '../models/responses/ResponseTrack'; diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 01e743e..8c2f2ea 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -3,7 +3,7 @@ import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { UserGroupNotFoundError, UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors'; -import { CreateUser } from '../models/creation/CreateUser'; +import { CreateUser } from '../models/actions/CreateUser'; import { User } from '../models/entities/User'; diff --git a/src/controllers/UserGroupController.ts b/src/controllers/UserGroupController.ts index c937f36..70e3c5a 100644 --- a/src/controllers/UserGroupController.ts +++ b/src/controllers/UserGroupController.ts @@ -3,7 +3,7 @@ import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors'; -import { CreateUserGroup } from '../models/creation/CreateUserGroup'; +import { CreateUserGroup } from '../models/actions/CreateUserGroup'; import { UserGroup } from '../models/entities/UserGroup'; diff --git a/src/models/creation/CreateAddress.ts b/src/models/actions/CreateAddress.ts similarity index 100% rename from src/models/creation/CreateAddress.ts rename to src/models/actions/CreateAddress.ts diff --git a/src/models/creation/CreateAuth.ts b/src/models/actions/CreateAuth.ts similarity index 100% rename from src/models/creation/CreateAuth.ts rename to src/models/actions/CreateAuth.ts diff --git a/src/models/creation/CreateGroupContact.ts b/src/models/actions/CreateGroupContact.ts similarity index 100% rename from src/models/creation/CreateGroupContact.ts rename to src/models/actions/CreateGroupContact.ts diff --git a/src/models/creation/CreateParticipant.ts b/src/models/actions/CreateParticipant.ts similarity index 100% rename from src/models/creation/CreateParticipant.ts rename to src/models/actions/CreateParticipant.ts diff --git a/src/models/creation/CreateRunner.ts b/src/models/actions/CreateRunner.ts similarity index 100% rename from src/models/creation/CreateRunner.ts rename to src/models/actions/CreateRunner.ts diff --git a/src/models/creation/CreateRunnerGroup.ts b/src/models/actions/CreateRunnerGroup.ts similarity index 100% rename from src/models/creation/CreateRunnerGroup.ts rename to src/models/actions/CreateRunnerGroup.ts diff --git a/src/models/creation/CreateRunnerOrganisation.ts b/src/models/actions/CreateRunnerOrganisation.ts similarity index 100% rename from src/models/creation/CreateRunnerOrganisation.ts rename to src/models/actions/CreateRunnerOrganisation.ts diff --git a/src/models/creation/CreateRunnerTeam.ts b/src/models/actions/CreateRunnerTeam.ts similarity index 100% rename from src/models/creation/CreateRunnerTeam.ts rename to src/models/actions/CreateRunnerTeam.ts diff --git a/src/models/creation/CreateTrack.ts b/src/models/actions/CreateTrack.ts similarity index 100% rename from src/models/creation/CreateTrack.ts rename to src/models/actions/CreateTrack.ts diff --git a/src/models/creation/CreateUser.ts b/src/models/actions/CreateUser.ts similarity index 100% rename from src/models/creation/CreateUser.ts rename to src/models/actions/CreateUser.ts diff --git a/src/models/creation/CreateUserGroup.ts b/src/models/actions/CreateUserGroup.ts similarity index 100% rename from src/models/creation/CreateUserGroup.ts rename to src/models/actions/CreateUserGroup.ts diff --git a/src/models/creation/HandleLogout.ts b/src/models/actions/HandleLogout.ts similarity index 100% rename from src/models/creation/HandleLogout.ts rename to src/models/actions/HandleLogout.ts diff --git a/src/models/creation/RefreshAuth.ts b/src/models/actions/RefreshAuth.ts similarity index 100% rename from src/models/creation/RefreshAuth.ts rename to src/models/actions/RefreshAuth.ts diff --git a/src/tests.ts b/src/tests.ts new file mode 100644 index 0000000..bf43e1e --- /dev/null +++ b/src/tests.ts @@ -0,0 +1,23 @@ +import * as dotenvSafe from "dotenv-safe"; +import "reflect-metadata"; +import { createExpressServer } from "routing-controllers"; +import authchecker from "./authchecker"; +import loaders from "./loaders/index"; +import { ErrorHandler } from './middlewares/ErrorHandler'; + +dotenvSafe.config(); +const PORT = process.env.APP_PORT || 4010; + +const app = createExpressServer({ + authorizationChecker: authchecker, + middlewares: [ErrorHandler], + development: process.env.NODE_ENV === "production", + cors: true, + routePrefix: "/api", + controllers: [__dirname + "/controllers/*.ts"], +}); + +async function main() { + await loaders(app); +} +main(); From 61e7ae4f86e0453c0efa15f151271d674ea0a44d Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 18:49:13 +0100 Subject: [PATCH 02/15] Cleanup: Renamed Responses to represent their response nature ref #11 #13 #14 --- src/controllers/AuthController.ts | 4 ++-- src/models/actions/CreateAuth.ts | 2 +- src/models/actions/HandleLogout.ts | 2 +- src/models/actions/RefreshAuth.ts | 2 +- src/models/responses/{Auth.ts => ResponseAuth.ts} | 0 src/models/responses/{Logout.ts => ResponseLogout.ts} | 0 6 files changed, 5 insertions(+), 5 deletions(-) rename src/models/responses/{Auth.ts => ResponseAuth.ts} (100%) rename src/models/responses/{Logout.ts => ResponseLogout.ts} (100%) diff --git a/src/controllers/AuthController.ts b/src/controllers/AuthController.ts index d5320a3..435f3ca 100644 --- a/src/controllers/AuthController.ts +++ b/src/controllers/AuthController.ts @@ -5,8 +5,8 @@ import { UserNotFoundError } from '../errors/UserErrors'; import { CreateAuth } from '../models/actions/CreateAuth'; import { HandleLogout } from '../models/actions/HandleLogout'; import { RefreshAuth } from '../models/actions/RefreshAuth'; -import { Auth } from '../models/responses/Auth'; -import { Logout } from '../models/responses/Logout'; +import { Auth } from '../models/responses/ResponseAuth'; +import { Logout } from '../models/responses/ResponseLogout'; @JsonController('/auth') export class AuthController { diff --git a/src/models/actions/CreateAuth.ts b/src/models/actions/CreateAuth.ts index 140f9ef..bf8584c 100644 --- a/src/models/actions/CreateAuth.ts +++ b/src/models/actions/CreateAuth.ts @@ -5,7 +5,7 @@ import { getConnectionManager } from 'typeorm'; import { InvalidCredentialsError, PasswordNeededError, UserNotFoundError } from '../../errors/AuthError'; import { UsernameOrEmailNeededError } from '../../errors/UserErrors'; import { User } from '../entities/User'; -import { Auth } from '../responses/Auth'; +import { Auth } from '../responses/ResponseAuth'; export class CreateAuth { @IsOptional() diff --git a/src/models/actions/HandleLogout.ts b/src/models/actions/HandleLogout.ts index c50edd0..7b68d91 100644 --- a/src/models/actions/HandleLogout.ts +++ b/src/models/actions/HandleLogout.ts @@ -3,7 +3,7 @@ import * as jsonwebtoken from 'jsonwebtoken'; import { getConnectionManager } from 'typeorm'; import { IllegalJWTError, JwtNotProvidedError, RefreshTokenCountInvalidError, UserNotFoundError } from '../../errors/AuthError'; import { User } from '../entities/User'; -import { Logout } from '../responses/Logout'; +import { Logout } from '../responses/ResponseLogout'; export class HandleLogout { @IsString() diff --git a/src/models/actions/RefreshAuth.ts b/src/models/actions/RefreshAuth.ts index 66e27f8..6e61a75 100644 --- a/src/models/actions/RefreshAuth.ts +++ b/src/models/actions/RefreshAuth.ts @@ -3,7 +3,7 @@ import * as jsonwebtoken from 'jsonwebtoken'; import { getConnectionManager } from 'typeorm'; import { IllegalJWTError, JwtNotProvidedError, RefreshTokenCountInvalidError, UserNotFoundError } from '../../errors/AuthError'; import { User } from '../entities/User'; -import { Auth } from '../responses/Auth'; +import { Auth } from '../responses/ResponseAuth'; export class RefreshAuth { @IsString() diff --git a/src/models/responses/Auth.ts b/src/models/responses/ResponseAuth.ts similarity index 100% rename from src/models/responses/Auth.ts rename to src/models/responses/ResponseAuth.ts diff --git a/src/models/responses/Logout.ts b/src/models/responses/ResponseLogout.ts similarity index 100% rename from src/models/responses/Logout.ts rename to src/models/responses/ResponseLogout.ts From 21ad622c10712ae36fb0c5e1519fc3ecba6341ae Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 18:49:59 +0100 Subject: [PATCH 03/15] Removed console logs ref #11 --- src/controllers/AuthController.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/controllers/AuthController.ts b/src/controllers/AuthController.ts index 435f3ca..00380cb 100644 --- a/src/controllers/AuthController.ts +++ b/src/controllers/AuthController.ts @@ -25,7 +25,6 @@ export class AuthController { let auth; try { auth = await createAuth.toAuth(); - console.log(auth); } catch (error) { return error; } @@ -44,7 +43,6 @@ export class AuthController { let logout; try { logout = await handleLogout.logout() - console.log(logout); } catch (error) { return error; } @@ -62,7 +60,6 @@ export class AuthController { let auth; try { auth = await refreshAuth.toAuth(); - console.log(auth); } catch (error) { return error; } From 1ae466a6f4618ca41beaffb64a673a5a66ca4901 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 19:01:30 +0100 Subject: [PATCH 04/15] Error cleanup #11 #13 #14 --- src/controllers/RunnerController.ts | 4 ++-- src/controllers/UserController.ts | 3 ++- src/errors/AddressErrors.ts | 6 ++++++ src/errors/AuthError.ts | 20 ++++++++++---------- src/errors/GroupContactErrors.ts | 6 ++++++ src/errors/RunnerErrors.ts | 20 +++----------------- src/errors/RunnerGroupErrors.ts | 14 ++++++++++++++ src/errors/RunnerOrganisationErrors.ts | 3 +++ src/errors/UserErrors.ts | 10 ---------- src/models/actions/CreateRunner.ts | 2 +- src/models/actions/CreateUser.ts | 3 ++- 11 files changed, 49 insertions(+), 42 deletions(-) create mode 100644 src/errors/RunnerGroupErrors.ts diff --git a/src/controllers/RunnerController.ts b/src/controllers/RunnerController.ts index 8293f36..acee2eb 100644 --- a/src/controllers/RunnerController.ts +++ b/src/controllers/RunnerController.ts @@ -2,7 +2,8 @@ import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, Query import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; -import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors'; +import { RunnerGroupNeededError, RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors'; +import { RunnerGroupNotFoundError } from '../errors/RunnerGroupErrors'; import { CreateRunner } from '../models/actions/CreateRunner'; import { Runner } from '../models/entities/Runner'; import { ResponseRunner } from '../models/responses/ResponseRunner'; @@ -43,7 +44,6 @@ export class RunnerController { @Post() @ResponseSchema(ResponseRunner) - @ResponseSchema(RunnerOnlyOneGroupAllowedError) @ResponseSchema(RunnerGroupNeededError) @ResponseSchema(RunnerGroupNotFoundError) @OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' }) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 8c2f2ea..f833f54 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -2,7 +2,8 @@ import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; -import { UserGroupNotFoundError, UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors'; +import { UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors'; +import { UserGroupNotFoundError } from '../errors/UserGroupErrors'; import { CreateUser } from '../models/actions/CreateUser'; import { User } from '../models/entities/User'; diff --git a/src/errors/AddressErrors.ts b/src/errors/AddressErrors.ts index c7ae8af..18cf624 100644 --- a/src/errors/AddressErrors.ts +++ b/src/errors/AddressErrors.ts @@ -1,6 +1,9 @@ import { IsString } from 'class-validator'; import { NotAcceptableError, NotFoundError } from 'routing-controllers'; +/** + * Error to throw, when to provided address doesn't belong to the accepted types. + */ export class AddressWrongTypeError extends NotAcceptableError { @IsString() name = "AddressWrongTypeError" @@ -9,6 +12,9 @@ export class AddressWrongTypeError extends NotAcceptableError { message = "The address must be an existing adress's id. \n You provided a object of another type." } +/** + * Error to throw, when a non-existant address get's loaded. + */ export class AddressNotFoundError extends NotFoundError { @IsString() name = "AddressNotFoundError" diff --git a/src/errors/AuthError.ts b/src/errors/AuthError.ts index 2c2ce5b..ebd5c00 100644 --- a/src/errors/AuthError.ts +++ b/src/errors/AuthError.ts @@ -2,7 +2,7 @@ import { IsString } from 'class-validator'; import { ForbiddenError, NotAcceptableError, NotFoundError, UnauthorizedError } from 'routing-controllers'; /** - * Error to throw when a jwt is expired + * Error to throw when a jwt is expired. */ export class ExpiredJWTError extends UnauthorizedError { @IsString() @@ -13,7 +13,7 @@ export class ExpiredJWTError extends UnauthorizedError { } /** - * Error to throw when a jwt could not be parsed + * Error to throw when a jwt could not be parsed. */ export class IllegalJWTError extends UnauthorizedError { @IsString() @@ -24,7 +24,7 @@ export class IllegalJWTError extends UnauthorizedError { } /** - * Error to throw when user is nonexistant or refreshtoken is invalid + * Error to throw when user is nonexistant or refreshtoken is invalid. */ export class UserNonexistantOrRefreshtokenInvalidError extends UnauthorizedError { @IsString() @@ -35,7 +35,7 @@ export class UserNonexistantOrRefreshtokenInvalidError extends UnauthorizedError } /** - * Error to throw when provided credentials are invalid + * Error to throw when provided credentials are invalid. */ export class InvalidCredentialsError extends UnauthorizedError { @IsString() @@ -46,7 +46,7 @@ export class InvalidCredentialsError extends UnauthorizedError { } /** - * Error to throw when a jwt does not have permission for this route/ action + * Error to throw when a jwt does not have permission for this route/action. */ export class NoPermissionError extends ForbiddenError { @IsString() @@ -57,7 +57,7 @@ export class NoPermissionError extends ForbiddenError { } /** - * Error to thow when no username and no email is set + * Error to throw when no username and no email is set. */ export class UsernameOrEmailNeededError extends NotAcceptableError { @IsString() @@ -68,7 +68,7 @@ export class UsernameOrEmailNeededError extends NotAcceptableError { } /** - * Error to thow when no password is provided + * Error to throw when no password is provided. */ export class PasswordNeededError extends NotAcceptableError { @IsString() @@ -79,7 +79,7 @@ export class PasswordNeededError extends NotAcceptableError { } /** - * Error to thow when no user could be found for provided credential + * Error to throw when no user could be found mating the provided credential. */ export class UserNotFoundError extends NotFoundError { @IsString() @@ -90,7 +90,7 @@ export class UserNotFoundError extends NotFoundError { } /** - * Error to thow when no jwt token was provided + * Error to throw when no jwt token was provided (but one had to be). */ export class JwtNotProvidedError extends NotAcceptableError { @IsString() @@ -101,7 +101,7 @@ export class JwtNotProvidedError extends NotAcceptableError { } /** - * Error to thow when user was not found or refresh token count was invalid + * Error to throw when user was not found or refresh token count was invalid. */ export class UserNotFoundOrRefreshTokenCountInvalidError extends NotAcceptableError { @IsString() diff --git a/src/errors/GroupContactErrors.ts b/src/errors/GroupContactErrors.ts index c5bf9c7..7dc19e8 100644 --- a/src/errors/GroupContactErrors.ts +++ b/src/errors/GroupContactErrors.ts @@ -1,6 +1,9 @@ import { IsString } from 'class-validator'; import { NotAcceptableError, NotFoundError } from 'routing-controllers'; +/** + * Error to throw, when a provided groupContact doesn't belong to the accepted types. + */ export class GroupContactWrongTypeError extends NotAcceptableError { @IsString() name = "GroupContactWrongTypeError" @@ -9,6 +12,9 @@ export class GroupContactWrongTypeError extends NotAcceptableError { message = "The groupContact must be an existing groupContact's id. \n You provided a object of another type." } +/** + * Error to throw, when a non-existant groupContact get's loaded. + */ export class GroupContactNotFoundError extends NotFoundError { @IsString() name = "GroupContactNotFoundError" diff --git a/src/errors/RunnerErrors.ts b/src/errors/RunnerErrors.ts index 8250d17..06dc78f 100644 --- a/src/errors/RunnerErrors.ts +++ b/src/errors/RunnerErrors.ts @@ -26,27 +26,13 @@ export class RunnerIdsNotMatchingError extends NotAcceptableError { message = "The id's don't match!! \n And if you wanted to change a runner's id: This isn't allowed" } -export class RunnerOnlyOneGroupAllowedError extends NotAcceptableError { - @IsString() - name = "RunnerOnlyOneGroupAllowedError" - - @IsString() - message = "Runner's can only be part of one group (team or organisiation)! \n You provided an id for both." -} - +/** + * Error to throw when a runner is missing his group association. + */ export class RunnerGroupNeededError extends NotAcceptableError { @IsString() name = "RunnerGroupNeededError" @IsString() message = "Runner's need to be part of one group (team or organisiation)! \n You provided neither." -} - - -export class RunnerGroupNotFoundError extends NotFoundError { - @IsString() - name = "RunnerGroupNotFoundError" - - @IsString() - message = "The group you provided couldn't be located in the system. \n Please check your request." } \ No newline at end of file diff --git a/src/errors/RunnerGroupErrors.ts b/src/errors/RunnerGroupErrors.ts new file mode 100644 index 0000000..ed9624c --- /dev/null +++ b/src/errors/RunnerGroupErrors.ts @@ -0,0 +1,14 @@ +import { IsString } from 'class-validator'; +import { NotFoundError } from 'routing-controllers'; + +/** + * Error to throw when a runner group couldn't be found. + * Implemented this ways to work with the json-schema conversion for openapi. + */ +export class RunnerGroupNotFoundError extends NotFoundError { + @IsString() + name = "RunnerGroupNotFoundError" + + @IsString() + message = "RunnerGroup not found!" +} \ No newline at end of file diff --git a/src/errors/RunnerOrganisationErrors.ts b/src/errors/RunnerOrganisationErrors.ts index 65736b1..040befb 100644 --- a/src/errors/RunnerOrganisationErrors.ts +++ b/src/errors/RunnerOrganisationErrors.ts @@ -50,6 +50,9 @@ export class RunnerOrganisationHasTeamsError extends NotAcceptableError { message = "This organisation still has teams associated with it. \n If you want to delete this organisation with all it's runners and teams ass `?force` to your query." } +/** + * Error to throw, when a provided runnerOrganisation doesn't belong to the accepted types. + */ export class RunnerOrganisationWrongTypeError extends NotAcceptableError { @IsString() name = "RunnerOrganisationWrongTypeError" diff --git a/src/errors/UserErrors.ts b/src/errors/UserErrors.ts index 4c9e98b..cfceff1 100644 --- a/src/errors/UserErrors.ts +++ b/src/errors/UserErrors.ts @@ -1,16 +1,6 @@ import { IsString } from 'class-validator'; import { NotAcceptableError, NotFoundError } from 'routing-controllers'; -/** - * Error to throw when a usergroup couldn't be found. - */ -export class UserGroupNotFoundError extends NotFoundError { - @IsString() - name = "UserGroupNotFoundError" - - @IsString() - message = "User Group not found!" -} /** * Error to throw when no username or email is set diff --git a/src/models/actions/CreateRunner.ts b/src/models/actions/CreateRunner.ts index 4e5f3e9..f3611ce 100644 --- a/src/models/actions/CreateRunner.ts +++ b/src/models/actions/CreateRunner.ts @@ -1,6 +1,6 @@ import { IsInt } from 'class-validator'; import { getConnectionManager } from 'typeorm'; -import { RunnerGroupNotFoundError } from '../../errors/RunnerErrors'; +import { RunnerGroupNotFoundError } from '../../errors/RunnerGroupErrors'; import { RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors'; import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors'; import { Runner } from '../entities/Runner'; diff --git a/src/models/actions/CreateUser.ts b/src/models/actions/CreateUser.ts index fb7841a..ff6771a 100644 --- a/src/models/actions/CreateUser.ts +++ b/src/models/actions/CreateUser.ts @@ -2,7 +2,8 @@ import * as argon2 from "argon2"; import { IsEmail, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import * as uuid from 'uuid'; -import { UserGroupNotFoundError, UsernameOrEmailNeededError } from '../../errors/UserErrors'; +import { UsernameOrEmailNeededError } from '../../errors/UserErrors'; +import { UserGroupNotFoundError } from '../../errors/UserGroupErrors'; import { User } from '../entities/User'; import { UserGroup } from '../entities/UserGroup'; From 33b3bcb8c2eaec3dd9d92a2f92d1e561920b97a9 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 19:01:30 +0100 Subject: [PATCH 05/15] Error cleanup #11 #13 #14 --- src/controllers/RunnerController.ts | 4 ++-- src/controllers/UserController.ts | 3 ++- src/errors/AddressErrors.ts | 6 ++++++ src/errors/AuthError.ts | 22 +++++++++++----------- src/errors/GroupContactErrors.ts | 6 ++++++ src/errors/RunnerErrors.ts | 20 +++----------------- src/errors/RunnerGroupErrors.ts | 14 ++++++++++++++ src/errors/RunnerOrganisationErrors.ts | 3 +++ src/errors/UserErrors.ts | 10 ---------- src/models/actions/CreateRunner.ts | 2 +- src/models/actions/CreateUser.ts | 3 ++- 11 files changed, 50 insertions(+), 43 deletions(-) create mode 100644 src/errors/RunnerGroupErrors.ts diff --git a/src/controllers/RunnerController.ts b/src/controllers/RunnerController.ts index 8293f36..acee2eb 100644 --- a/src/controllers/RunnerController.ts +++ b/src/controllers/RunnerController.ts @@ -2,7 +2,8 @@ import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, Query import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; -import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors'; +import { RunnerGroupNeededError, RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors'; +import { RunnerGroupNotFoundError } from '../errors/RunnerGroupErrors'; import { CreateRunner } from '../models/actions/CreateRunner'; import { Runner } from '../models/entities/Runner'; import { ResponseRunner } from '../models/responses/ResponseRunner'; @@ -43,7 +44,6 @@ export class RunnerController { @Post() @ResponseSchema(ResponseRunner) - @ResponseSchema(RunnerOnlyOneGroupAllowedError) @ResponseSchema(RunnerGroupNeededError) @ResponseSchema(RunnerGroupNotFoundError) @OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' }) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 8c2f2ea..f833f54 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -2,7 +2,8 @@ import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; -import { UserGroupNotFoundError, UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors'; +import { UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors'; +import { UserGroupNotFoundError } from '../errors/UserGroupErrors'; import { CreateUser } from '../models/actions/CreateUser'; import { User } from '../models/entities/User'; diff --git a/src/errors/AddressErrors.ts b/src/errors/AddressErrors.ts index c7ae8af..18cf624 100644 --- a/src/errors/AddressErrors.ts +++ b/src/errors/AddressErrors.ts @@ -1,6 +1,9 @@ import { IsString } from 'class-validator'; import { NotAcceptableError, NotFoundError } from 'routing-controllers'; +/** + * Error to throw, when to provided address doesn't belong to the accepted types. + */ export class AddressWrongTypeError extends NotAcceptableError { @IsString() name = "AddressWrongTypeError" @@ -9,6 +12,9 @@ export class AddressWrongTypeError extends NotAcceptableError { message = "The address must be an existing adress's id. \n You provided a object of another type." } +/** + * Error to throw, when a non-existant address get's loaded. + */ export class AddressNotFoundError extends NotFoundError { @IsString() name = "AddressNotFoundError" diff --git a/src/errors/AuthError.ts b/src/errors/AuthError.ts index 2c2ce5b..7687371 100644 --- a/src/errors/AuthError.ts +++ b/src/errors/AuthError.ts @@ -2,7 +2,7 @@ import { IsString } from 'class-validator'; import { ForbiddenError, NotAcceptableError, NotFoundError, UnauthorizedError } from 'routing-controllers'; /** - * Error to throw when a jwt is expired + * Error to throw when a jwt is expired. */ export class ExpiredJWTError extends UnauthorizedError { @IsString() @@ -13,7 +13,7 @@ export class ExpiredJWTError extends UnauthorizedError { } /** - * Error to throw when a jwt could not be parsed + * Error to throw when a jwt could not be parsed. */ export class IllegalJWTError extends UnauthorizedError { @IsString() @@ -24,7 +24,7 @@ export class IllegalJWTError extends UnauthorizedError { } /** - * Error to throw when user is nonexistant or refreshtoken is invalid + * Error to throw when user is nonexistant or refreshtoken is invalid. */ export class UserNonexistantOrRefreshtokenInvalidError extends UnauthorizedError { @IsString() @@ -35,7 +35,7 @@ export class UserNonexistantOrRefreshtokenInvalidError extends UnauthorizedError } /** - * Error to throw when provided credentials are invalid + * Error to throw when provided credentials are invalid. */ export class InvalidCredentialsError extends UnauthorizedError { @IsString() @@ -46,7 +46,7 @@ export class InvalidCredentialsError extends UnauthorizedError { } /** - * Error to throw when a jwt does not have permission for this route/ action + * Error to throw when a jwt does not have permission for this route/action. */ export class NoPermissionError extends ForbiddenError { @IsString() @@ -57,7 +57,7 @@ export class NoPermissionError extends ForbiddenError { } /** - * Error to thow when no username and no email is set + * Error to throw when no username and no email is set. */ export class UsernameOrEmailNeededError extends NotAcceptableError { @IsString() @@ -68,7 +68,7 @@ export class UsernameOrEmailNeededError extends NotAcceptableError { } /** - * Error to thow when no password is provided + * Error to throw when no password is provided. */ export class PasswordNeededError extends NotAcceptableError { @IsString() @@ -79,7 +79,7 @@ export class PasswordNeededError extends NotAcceptableError { } /** - * Error to thow when no user could be found for provided credential + * Error to throw when no user could be found mating the provided credential. */ export class UserNotFoundError extends NotFoundError { @IsString() @@ -90,7 +90,7 @@ export class UserNotFoundError extends NotFoundError { } /** - * Error to thow when no jwt token was provided + * Error to throw when no jwt token was provided (but one had to be). */ export class JwtNotProvidedError extends NotAcceptableError { @IsString() @@ -101,7 +101,7 @@ export class JwtNotProvidedError extends NotAcceptableError { } /** - * Error to thow when user was not found or refresh token count was invalid + * Error to throw when user was not found or refresh token count was invalid. */ export class UserNotFoundOrRefreshTokenCountInvalidError extends NotAcceptableError { @IsString() @@ -112,7 +112,7 @@ export class UserNotFoundOrRefreshTokenCountInvalidError extends NotAcceptableEr } /** - * Error to thow when refresh token count was invalid + * Error to throw when refresh token count was invalid */ export class RefreshTokenCountInvalidError extends NotAcceptableError { @IsString() diff --git a/src/errors/GroupContactErrors.ts b/src/errors/GroupContactErrors.ts index c5bf9c7..7dc19e8 100644 --- a/src/errors/GroupContactErrors.ts +++ b/src/errors/GroupContactErrors.ts @@ -1,6 +1,9 @@ import { IsString } from 'class-validator'; import { NotAcceptableError, NotFoundError } from 'routing-controllers'; +/** + * Error to throw, when a provided groupContact doesn't belong to the accepted types. + */ export class GroupContactWrongTypeError extends NotAcceptableError { @IsString() name = "GroupContactWrongTypeError" @@ -9,6 +12,9 @@ export class GroupContactWrongTypeError extends NotAcceptableError { message = "The groupContact must be an existing groupContact's id. \n You provided a object of another type." } +/** + * Error to throw, when a non-existant groupContact get's loaded. + */ export class GroupContactNotFoundError extends NotFoundError { @IsString() name = "GroupContactNotFoundError" diff --git a/src/errors/RunnerErrors.ts b/src/errors/RunnerErrors.ts index 8250d17..06dc78f 100644 --- a/src/errors/RunnerErrors.ts +++ b/src/errors/RunnerErrors.ts @@ -26,27 +26,13 @@ export class RunnerIdsNotMatchingError extends NotAcceptableError { message = "The id's don't match!! \n And if you wanted to change a runner's id: This isn't allowed" } -export class RunnerOnlyOneGroupAllowedError extends NotAcceptableError { - @IsString() - name = "RunnerOnlyOneGroupAllowedError" - - @IsString() - message = "Runner's can only be part of one group (team or organisiation)! \n You provided an id for both." -} - +/** + * Error to throw when a runner is missing his group association. + */ export class RunnerGroupNeededError extends NotAcceptableError { @IsString() name = "RunnerGroupNeededError" @IsString() message = "Runner's need to be part of one group (team or organisiation)! \n You provided neither." -} - - -export class RunnerGroupNotFoundError extends NotFoundError { - @IsString() - name = "RunnerGroupNotFoundError" - - @IsString() - message = "The group you provided couldn't be located in the system. \n Please check your request." } \ No newline at end of file diff --git a/src/errors/RunnerGroupErrors.ts b/src/errors/RunnerGroupErrors.ts new file mode 100644 index 0000000..ed9624c --- /dev/null +++ b/src/errors/RunnerGroupErrors.ts @@ -0,0 +1,14 @@ +import { IsString } from 'class-validator'; +import { NotFoundError } from 'routing-controllers'; + +/** + * Error to throw when a runner group couldn't be found. + * Implemented this ways to work with the json-schema conversion for openapi. + */ +export class RunnerGroupNotFoundError extends NotFoundError { + @IsString() + name = "RunnerGroupNotFoundError" + + @IsString() + message = "RunnerGroup not found!" +} \ No newline at end of file diff --git a/src/errors/RunnerOrganisationErrors.ts b/src/errors/RunnerOrganisationErrors.ts index 65736b1..040befb 100644 --- a/src/errors/RunnerOrganisationErrors.ts +++ b/src/errors/RunnerOrganisationErrors.ts @@ -50,6 +50,9 @@ export class RunnerOrganisationHasTeamsError extends NotAcceptableError { message = "This organisation still has teams associated with it. \n If you want to delete this organisation with all it's runners and teams ass `?force` to your query." } +/** + * Error to throw, when a provided runnerOrganisation doesn't belong to the accepted types. + */ export class RunnerOrganisationWrongTypeError extends NotAcceptableError { @IsString() name = "RunnerOrganisationWrongTypeError" diff --git a/src/errors/UserErrors.ts b/src/errors/UserErrors.ts index 4c9e98b..cfceff1 100644 --- a/src/errors/UserErrors.ts +++ b/src/errors/UserErrors.ts @@ -1,16 +1,6 @@ import { IsString } from 'class-validator'; import { NotAcceptableError, NotFoundError } from 'routing-controllers'; -/** - * Error to throw when a usergroup couldn't be found. - */ -export class UserGroupNotFoundError extends NotFoundError { - @IsString() - name = "UserGroupNotFoundError" - - @IsString() - message = "User Group not found!" -} /** * Error to throw when no username or email is set diff --git a/src/models/actions/CreateRunner.ts b/src/models/actions/CreateRunner.ts index 4e5f3e9..f3611ce 100644 --- a/src/models/actions/CreateRunner.ts +++ b/src/models/actions/CreateRunner.ts @@ -1,6 +1,6 @@ import { IsInt } from 'class-validator'; import { getConnectionManager } from 'typeorm'; -import { RunnerGroupNotFoundError } from '../../errors/RunnerErrors'; +import { RunnerGroupNotFoundError } from '../../errors/RunnerGroupErrors'; import { RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors'; import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors'; import { Runner } from '../entities/Runner'; diff --git a/src/models/actions/CreateUser.ts b/src/models/actions/CreateUser.ts index fb7841a..ff6771a 100644 --- a/src/models/actions/CreateUser.ts +++ b/src/models/actions/CreateUser.ts @@ -2,7 +2,8 @@ import * as argon2 from "argon2"; import { IsEmail, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import * as uuid from 'uuid'; -import { UserGroupNotFoundError, UsernameOrEmailNeededError } from '../../errors/UserErrors'; +import { UsernameOrEmailNeededError } from '../../errors/UserErrors'; +import { UserGroupNotFoundError } from '../../errors/UserGroupErrors'; import { User } from '../entities/User'; import { UserGroup } from '../entities/UserGroup'; From f58a715c45ae5e8ea95041816d78991498461b94 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 19:09:08 +0100 Subject: [PATCH 06/15] Cleaned up the loaders ref #11 --- src/loaders/database.ts | 3 +++ src/loaders/express.ts | 6 ++++-- src/loaders/index.ts | 7 +++++-- src/loaders/openapi.ts | 7 +++++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/loaders/database.ts b/src/loaders/database.ts index b8e9607..ee331ae 100644 --- a/src/loaders/database.ts +++ b/src/loaders/database.ts @@ -1,5 +1,8 @@ import { createConnection } from "typeorm"; +/** + * Loader for the database that creates the database connection and initializes the database tabels. + */ export default async () => { const connection = await createConnection(); connection.synchronize(); diff --git a/src/loaders/express.ts b/src/loaders/express.ts index 787497a..32a28cd 100644 --- a/src/loaders/express.ts +++ b/src/loaders/express.ts @@ -1,7 +1,9 @@ import { Application } from "express"; -import bodyParser from 'body-parser'; -import cors from 'cors'; +/** + * Loader for express related configurations. + * Currently only enables the proxy trust. + */ export default async (app: Application) => { app.enable('trust proxy'); return app; diff --git a/src/loaders/index.ts b/src/loaders/index.ts index 704ea47..802a732 100644 --- a/src/loaders/index.ts +++ b/src/loaders/index.ts @@ -1,8 +1,11 @@ +import { Application } from "express"; +import databaseLoader from "./database"; import expressLoader from "./express"; import openapiLoader from "./openapi"; -import databaseLoader from "./database"; -import { Application } from "express"; +/** + * Index Loader that executes the other loaders in the right order. + */ export default async (app: Application) => { await databaseLoader(); await openapiLoader(app); diff --git a/src/loaders/openapi.ts b/src/loaders/openapi.ts index fe0c77a..9c270ed 100644 --- a/src/loaders/openapi.ts +++ b/src/loaders/openapi.ts @@ -4,11 +4,16 @@ import { getMetadataArgsStorage } from "routing-controllers"; import { routingControllersToSpec } from "routing-controllers-openapi"; import * as swaggerUiExpress from "swagger-ui-express"; +/** + * Loader for everything openapi related - from creating the schema to serving it via a static route. + */ export default async (app: Application) => { const storage = getMetadataArgsStorage(); const schemas = validationMetadatasToSchemas({ refPointerPrefix: "#/components/schemas/", }); + + //Spec creation based on the previously created schemas const spec = routingControllersToSpec( storage, { @@ -32,6 +37,8 @@ export default async (app: Application) => { }, } ); + + //Options for swaggerUiExpress const options = { explorer: true, }; From 1fb09e577c539d8261afe4cc4a2cfae88b8f866b Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 19:14:04 +0100 Subject: [PATCH 07/15] Cleaned up up the middlewares ref #11 --- src/middlewares/ErrorHandler.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/middlewares/ErrorHandler.ts b/src/middlewares/ErrorHandler.ts index 7f45073..40c1ff6 100644 --- a/src/middlewares/ErrorHandler.ts +++ b/src/middlewares/ErrorHandler.ts @@ -1,20 +1,14 @@ -import { - Middleware, - ExpressErrorMiddlewareInterface -} from "routing-controllers"; +import { ExpressErrorMiddlewareInterface, Middleware } from "routing-controllers"; +/** + * Our Error handling middlware that returns our custom httperrors to the user + */ @Middleware({ type: "after" }) export class ErrorHandler implements ExpressErrorMiddlewareInterface { - public error( - error: any, - request: any, - response: any, - next: (err: any) => any - ) { + public error(error: any, request: any, response: any, next: (err: any) => any) { if (response.headersSent) { return; } - response.json(error); } } From ad6c9e72111f91f432b66bc67001ef8ceea499dc Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 19:15:56 +0100 Subject: [PATCH 08/15] Removed garbage file --- src/tests.ts | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 src/tests.ts diff --git a/src/tests.ts b/src/tests.ts deleted file mode 100644 index bf43e1e..0000000 --- a/src/tests.ts +++ /dev/null @@ -1,23 +0,0 @@ -import * as dotenvSafe from "dotenv-safe"; -import "reflect-metadata"; -import { createExpressServer } from "routing-controllers"; -import authchecker from "./authchecker"; -import loaders from "./loaders/index"; -import { ErrorHandler } from './middlewares/ErrorHandler'; - -dotenvSafe.config(); -const PORT = process.env.APP_PORT || 4010; - -const app = createExpressServer({ - authorizationChecker: authchecker, - middlewares: [ErrorHandler], - development: process.env.NODE_ENV === "production", - cors: true, - routePrefix: "/api", - controllers: [__dirname + "/controllers/*.ts"], -}); - -async function main() { - await loaders(app); -} -main(); From 5103e8a6e5db8a3484eae5710f8f4f7712cb8b14 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 20:01:06 +0100 Subject: [PATCH 09/15] Updated folders in the readme --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 077f888..f974523 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,11 @@ docker-compose up --build ## File Structure -- src/models/\* - database models (typeorm entities) +- src/models/entities\* - database models (typeorm entities) +- src/models/actions\* - actions models +- src/models/responses\* - response models - src/controllers/\* - routing-controllers - src/loaders/\* - loaders for the different init steps of the api server -- src/routes/\* - express routes for everything we don't do via routing-controllers (shouldn't be much) - src/middlewares/\* - express middlewares (mainly auth r/n) - src/errors/* - our custom (http) errors +- src/routes/\* - express routes for everything we don't do via routing-controllers (depreciated) \ No newline at end of file From 4df63a8cc0c1346dda6758bf5aa20f939dc64ade Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 9 Dec 2020 16:10:56 +0100 Subject: [PATCH 10/15] Fixxed missing plural ref #13 --- src/controllers/RunnerOrganisationController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/RunnerOrganisationController.ts b/src/controllers/RunnerOrganisationController.ts index 8cfef6a..3302913 100644 --- a/src/controllers/RunnerOrganisationController.ts +++ b/src/controllers/RunnerOrganisationController.ts @@ -10,7 +10,7 @@ import { RunnerController } from './RunnerController'; import { RunnerTeamController } from './RunnerTeamController'; -@JsonController('/organisation') +@JsonController('/organisations') //@Authorized('RUNNERS:read') export class RunnerOrganisationController { private runnerOrganisationRepository: Repository; From e4cb8eba1d7a4e7623ebf38d84227752c84d1ead Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 9 Dec 2020 17:48:24 +0100 Subject: [PATCH 11/15] Removed relations resolution that broke the update --- src/controllers/RunnerOrganisationController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/RunnerOrganisationController.ts b/src/controllers/RunnerOrganisationController.ts index 1a80809..3ad839e 100644 --- a/src/controllers/RunnerOrganisationController.ts +++ b/src/controllers/RunnerOrganisationController.ts @@ -66,7 +66,7 @@ export class RunnerOrganisationController { @ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 }) @OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." }) async put(@Param('id') id: number, @EntityFromBody() runnerOrganisation: RunnerOrganisation) { - let oldRunnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] }); + let oldRunnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }); if (!oldRunnerOrganisation) { throw new RunnerOrganisationNotFoundError(); From 204e2352a9a580a5579fad98ffcfb8cdb4683eab Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 9 Dec 2020 18:11:23 +0100 Subject: [PATCH 12/15] Fix for getting one ref #13 --- src/controllers/RunnerController.ts | 4 +++- src/controllers/RunnerOrganisationController.ts | 4 +++- src/controllers/RunnerTeamController.ts | 4 +++- src/controllers/TrackController.ts | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/controllers/RunnerController.ts b/src/controllers/RunnerController.ts index acee2eb..9d7a847 100644 --- a/src/controllers/RunnerController.ts +++ b/src/controllers/RunnerController.ts @@ -39,7 +39,9 @@ export class RunnerController { @OnUndefined(RunnerNotFoundError) @OpenAPI({ description: 'Returns a runner of a specified id (if it exists)' }) async getOne(@Param('id') id: number) { - return new ResponseRunner(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(); } + return new ResponseRunner(runner); } @Post() diff --git a/src/controllers/RunnerOrganisationController.ts b/src/controllers/RunnerOrganisationController.ts index 3ad839e..5636fa6 100644 --- a/src/controllers/RunnerOrganisationController.ts +++ b/src/controllers/RunnerOrganisationController.ts @@ -41,7 +41,9 @@ export class RunnerOrganisationController { @OnUndefined(RunnerOrganisationNotFoundError) @OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' }) async getOne(@Param('id') id: number) { - return new ResponseRunnerOrganisation(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(); } + return new ResponseRunnerOrganisation(runnerOrg); } @Post() diff --git a/src/controllers/RunnerTeamController.ts b/src/controllers/RunnerTeamController.ts index 7b0a841..148f16d 100644 --- a/src/controllers/RunnerTeamController.ts +++ b/src/controllers/RunnerTeamController.ts @@ -40,7 +40,9 @@ export class RunnerTeamController { @OnUndefined(RunnerTeamNotFoundError) @OpenAPI({ description: 'Returns a runnerTeam of a specified id (if it exists)' }) async getOne(@Param('id') id: number) { - return new ResponseRunnerTeam(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(); } + return new ResponseRunnerTeam(runnerTeam); } @Post() diff --git a/src/controllers/TrackController.ts b/src/controllers/TrackController.ts index 149b837..bf5fb71 100644 --- a/src/controllers/TrackController.ts +++ b/src/controllers/TrackController.ts @@ -37,7 +37,9 @@ export class TrackController { @OnUndefined(TrackNotFoundError) @OpenAPI({ description: "Returns a track of a specified id (if it exists)" }) async getOne(@Param('id') id: number) { - return new ResponseTrack(await this.trackRepository.findOne({ id: id })); + let track = await this.trackRepository.findOne({ id: id }); + if (!track) { throw new TrackNotFoundError(); } + return new ResponseTrack(track); } @Post() From 2d603a1467eca50263a0ad2eea427e59da88d263 Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Wed, 9 Dec 2020 18:45:39 +0100 Subject: [PATCH 13/15] resolve groups + permissions ref #12 --- src/models/actions/CreateAuth.ts | 2 +- src/models/actions/RefreshAuth.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/actions/CreateAuth.ts b/src/models/actions/CreateAuth.ts index 2832e19..d36c17f 100644 --- a/src/models/actions/CreateAuth.ts +++ b/src/models/actions/CreateAuth.ts @@ -28,7 +28,7 @@ export class CreateAuth { if (!this.password) { throw new PasswordNeededError() } - const found_users = await getConnectionManager().get().getRepository(User).find({ where: [{ username: this.username }, { email: this.email }] }); + const found_users = await getConnectionManager().get().getRepository(User).find({ relations: ['groups', 'permissions'], where: [{ username: this.username }, { email: this.email }] }); if (found_users.length === 0) { throw new UserNotFoundError() } else { diff --git a/src/models/actions/RefreshAuth.ts b/src/models/actions/RefreshAuth.ts index 55c124f..77a2728 100644 --- a/src/models/actions/RefreshAuth.ts +++ b/src/models/actions/RefreshAuth.ts @@ -21,7 +21,7 @@ export class RefreshAuth { } catch (error) { throw new IllegalJWTError() } - const found_user = await getConnectionManager().get().getRepository(User).findOne({ id: decoded["userid"] }); + const found_user = await getConnectionManager().get().getRepository(User).findOne({ id: decoded["userid"] }, { relations: ['groups', 'permissions'] }); if (!found_user) { throw new UserNotFoundError() } From 02236caa413c3a374c62a30d9cfe0e77e1cb4c91 Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Wed, 9 Dec 2020 18:46:09 +0100 Subject: [PATCH 14/15] send empty array for user permissions if null ref #12 --- src/models/actions/CreateAuth.ts | 1 + src/models/actions/RefreshAuth.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/src/models/actions/CreateAuth.ts b/src/models/actions/CreateAuth.ts index d36c17f..b31bee6 100644 --- a/src/models/actions/CreateAuth.ts +++ b/src/models/actions/CreateAuth.ts @@ -35,6 +35,7 @@ export class CreateAuth { const found_user = found_users[0] if (await argon2.verify(found_user.password, this.password + found_user.uuid)) { const timestamp_accesstoken_expiry = Math.floor(Date.now() / 1000) + 5 * 60 + found_user.permissions = found_user.permissions || [] delete found_user.password; newAuth.access_token = jsonwebtoken.sign({ userdetails: found_user, diff --git a/src/models/actions/RefreshAuth.ts b/src/models/actions/RefreshAuth.ts index 77a2728..dacca59 100644 --- a/src/models/actions/RefreshAuth.ts +++ b/src/models/actions/RefreshAuth.ts @@ -28,6 +28,7 @@ export class RefreshAuth { if (found_user.refreshTokenCount !== decoded["refreshtokencount"]) { throw new RefreshTokenCountInvalidError() } + found_user.permissions = found_user.permissions || [] delete found_user.password; const timestamp_accesstoken_expiry = Math.floor(Date.now() / 1000) + 5 * 60 delete found_user.password; From af1ad482d4dd9cf65cc2895896c19f0ecf9d879d Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 9 Dec 2020 18:53:36 +0100 Subject: [PATCH 15/15] Now throwing errors to the next instance ref #13 --- src/models/actions/CreateRunnerTeam.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/models/actions/CreateRunnerTeam.ts b/src/models/actions/CreateRunnerTeam.ts index 98429cb..21a6af9 100644 --- a/src/models/actions/CreateRunnerTeam.ts +++ b/src/models/actions/CreateRunnerTeam.ts @@ -35,7 +35,12 @@ export class CreateRunnerTeam extends CreateRunnerGroup { let newRunnerTeam: RunnerTeam = new RunnerTeam(); newRunnerTeam.name = this.name; - newRunnerTeam.parentGroup = await this.getParent(); + try { + newRunnerTeam.parentGroup = await this.getParent(); + } catch (error) { + throw error; + } + newRunnerTeam.contact = await this.getContact() return newRunnerTeam;