diff --git a/src/controllers/RunnerSelfServiceController.ts b/src/controllers/RunnerSelfServiceController.ts index 28f7493..878cf1b 100644 --- a/src/controllers/RunnerSelfServiceController.ts +++ b/src/controllers/RunnerSelfServiceController.ts @@ -1,5 +1,6 @@ +import { Request } from "express"; import * as jwt from "jsonwebtoken"; -import { Body, Get, JsonController, OnUndefined, Param, Post, QueryParam } from 'routing-controllers'; +import { Body, Get, JsonController, OnUndefined, Param, Post, QueryParam, Req, UseBefore } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; import { config } from '../config'; @@ -7,23 +8,27 @@ import { InvalidCredentialsError, JwtNotProvidedError } from '../errors/AuthErro import { MailSendingError } from '../errors/MailErrors'; import { RunnerEmailNeededError, RunnerNotFoundError, RunnerSelfserviceTimeoutError } from '../errors/RunnerErrors'; import { RunnerOrganizationNotFoundError } from '../errors/RunnerOrganizationErrors'; +import { ScanStationNotFoundError } from '../errors/ScanStationErrors'; import { JwtCreator } from '../jwtcreator'; import { Mailer } from '../mailer'; +import ScanAuth from '../middlewares/ScanAuth'; import { CreateSelfServiceCitizenRunner } from '../models/actions/create/CreateSelfServiceCitizenRunner'; import { CreateSelfServiceRunner } from '../models/actions/create/CreateSelfServiceRunner'; import { Runner } from '../models/entities/Runner'; import { RunnerGroup } from '../models/entities/RunnerGroup'; import { RunnerOrganization } from '../models/entities/RunnerOrganization'; +import { ScanStation } from '../models/entities/ScanStation'; import { ResponseEmpty } from '../models/responses/ResponseEmpty'; +import { ResponseScanStation } from '../models/responses/ResponseScanStation'; import { ResponseSelfServiceOrganisation } from '../models/responses/ResponseSelfServiceOrganisation'; import { ResponseSelfServiceRunner } from '../models/responses/ResponseSelfServiceRunner'; import { ResponseSelfServiceScan } from '../models/responses/ResponseSelfServiceScan'; - @JsonController() export class RunnerSelfServiceController { private runnerRepository: Repository; private orgRepository: Repository; + private stationRepository: Repository; /** * Gets the repository of this controller's model/entity. @@ -31,6 +36,7 @@ export class RunnerSelfServiceController { constructor() { this.runnerRepository = getConnectionManager().get().getRepository(Runner); this.orgRepository = getConnectionManager().get().getRepository(RunnerOrganization); + this.stationRepository = getConnectionManager().get().getRepository(ScanStation); } @Get('/runners/me/:jwt') @@ -56,6 +62,18 @@ export class RunnerSelfServiceController { return responseScans; } + @Get('/stations/me') + @UseBefore(ScanAuth) + @ResponseSchema(ResponseScanStation) + @ResponseSchema(ScanStationNotFoundError, { statusCode: 404 }) + @OnUndefined(ScanStationNotFoundError) + @OpenAPI({ description: 'Lists basic information about the station whose token got provided.
This includes it\'s associated track.', security: [{ "ScanApiToken": [] }] }) + async getStationMe(@Req() req: Request) { + let scan = await this.stationRepository.findOne({ id: parseInt(req.headers["station_id"].toString()) }, { relations: ['track'] }) + if (!scan) { throw new ScanStationNotFoundError(); } + return scan.toResponse(); + } + @Post('/runners/forgot') @ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) @OnUndefined(ResponseEmpty) diff --git a/src/middlewares/ScanAuth.ts b/src/middlewares/ScanAuth.ts index 2f39bbf..05793f0 100644 --- a/src/middlewares/ScanAuth.ts +++ b/src/middlewares/ScanAuth.ts @@ -62,7 +62,7 @@ const ScanAuth = async (req: Request, res: Response, next: () => void) => { res.status(401).send("Api token invalid."); return; } - + req.headers["station_id"] = station.id.toString(); next(); } } diff --git a/src/tests/scanstations/scanstations_get.spec.ts b/src/tests/scanstations/scanstations_get.spec.ts index 7280c31..3cb24ad 100644 --- a/src/tests/scanstations/scanstations_get.spec.ts +++ b/src/tests/scanstations/scanstations_get.spec.ts @@ -56,4 +56,34 @@ describe('adding + getting stations', () => { expect(res.status).toEqual(200); expect(res.headers['content-type']).toContain("application/json"); }); +}); +// --------------- +describe('adding + getting via me endpoint', () => { + let added_track; + let added_station; + it('creating a track should return 200', async () => { + const res1 = await axios.post(base + '/api/tracks', { + "name": "test123", + "distance": 123 + }, axios_config); + added_track = res1.data + expect(res1.status).toEqual(200); + expect(res1.headers['content-type']).toContain("application/json") + }); + it('correct description and track input for station creation return 200', async () => { + const res = await axios.post(base + '/api/stations', { + "track": added_track.id, + "description": "I am but a simple test." + }, axios_config); + added_station = res.data; + expect(res.status).toEqual(200); + expect(res.headers['content-type']).toContain("application/json") + }); + it('correct description and track input for station creation return 200', async () => { + const res = await axios.get(base + '/api/stations/me', { headers: { "authorization": "Bearer " + added_station.key } }); + expect(res.status).toEqual(200); + expect(res.headers['content-type']).toContain("application/json") + added_station.key = "Only visible on creation."; + expect(res.data).toEqual(added_station); + }); }); \ No newline at end of file