Merge pull request 'Scanstation "me" endpoint feature/157-scanstation_me' (#158) from feature/157-scanstation_me into dev
continuous-integration/drone/push Build is passing Details

Reviewed-on: #158
This commit is contained in:
Nicolai Ort 2021-03-17 15:52:02 +00:00
commit 13e839902c
3 changed files with 51 additions and 3 deletions

View File

@ -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<Runner>;
private orgRepository: Repository<RunnerOrganization>;
private stationRepository: Repository<ScanStation>;
/**
* 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. <br> 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)

View File

@ -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();
}
}

View File

@ -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);
});
});