mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-19 16:13:06 +00:00
test: add tests for routes
This commit is contained in:
committed by
Rainer Killinger
parent
751693bebc
commit
d3955b3cdd
@@ -25,7 +25,7 @@ import cors from 'cors';
|
||||
import {Express} from 'express';
|
||||
import morgan from 'morgan';
|
||||
import {join} from 'path';
|
||||
import {configFile, isTestEnvironment, mailer, plugins, validator} from './common';
|
||||
import {configFile, DEFAULT_TIMEOUT, isTestEnvironment, mailer, plugins, validator} from './common';
|
||||
import {MailQueue} from './notification/mail-queue';
|
||||
import {bulkAddRouter} from './routes/bulk-add-route';
|
||||
import {bulkDoneRouter} from './routes/bulk-done-route';
|
||||
@@ -53,8 +53,7 @@ export async function configureApp(app: Express) {
|
||||
integrationTestTimeout = setTimeout(() => {
|
||||
process.exit(1);
|
||||
},
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
20000);
|
||||
DEFAULT_TIMEOUT);
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -95,6 +94,7 @@ export async function configureApp(app: Express) {
|
||||
// only accept json as content type for all requests
|
||||
app.use((req, res, next) => {
|
||||
// get the content type
|
||||
// TODO: Always lowercase (see type definition of IncomingHttpHeaders)
|
||||
let contentType = '';
|
||||
// the content type can be string, string[] or undefined
|
||||
if (typeof req.headers['Content-Type'] === 'string') {
|
||||
@@ -189,7 +189,7 @@ export async function configureApp(app: Express) {
|
||||
throw new Error('No implementation for configured database found. Please check your configuration.');
|
||||
}
|
||||
|
||||
Logger.ok('Validated config file sucessfully');
|
||||
Logger.ok('Validated config file successfully');
|
||||
|
||||
// treats /foo and /foo/ as two different routes
|
||||
// see http://expressjs.com/en/api.html#app.set
|
||||
|
||||
@@ -60,3 +60,8 @@ export const plugins = new Map<string, SCPluginMetaData>();
|
||||
*/
|
||||
export const coreVersion: string = JSON.parse((readFileSync(resolve('.', '.', 'package.json'), 'utf-8')).toString())
|
||||
.dependencies['@openstapps/core'];
|
||||
|
||||
/**
|
||||
* The default timeout in milliseconds
|
||||
*/
|
||||
export const DEFAULT_TIMEOUT = 20000;
|
||||
|
||||
@@ -31,6 +31,7 @@ export const bulkAddRouter = createRoute<SCBulkAddRequest, SCBulkAddResponse>(
|
||||
bulkRouteModel,
|
||||
async (request, app, params) => {
|
||||
|
||||
// TODO: DELETE as not used
|
||||
if (typeof params === 'undefined' || typeof params.UID !== 'string') {
|
||||
throw new Error('UID of Bulk was not given, but route with obligatory parameter was called');
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ export const bulkDoneRouter = createRoute<SCBulkDoneRequest, SCBulkDoneResponse>
|
||||
bulkDoneRouteModel,
|
||||
async (_request, app, params) => {
|
||||
|
||||
// TODO: DELETE as not used
|
||||
if (typeof params === 'undefined' || typeof params.UID !== 'string') {
|
||||
throw new Error('UID of Bulk was not given, but route with obligatory parameter was called');
|
||||
}
|
||||
|
||||
@@ -13,33 +13,15 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
// the list provides option to easily implement "isHttpMethod" guard
|
||||
const httpVerbs = ['get', 'post', 'put', 'delete', 'patch', 'options',
|
||||
'head', 'checkout', 'copy', 'lock', 'merge', 'mkactivity', 'mkcol',
|
||||
'move', 'm-search', 'notify', 'purge', 'report', 'search', 'subscribe',
|
||||
'trace', 'unlock','unsubscribe'] as const;
|
||||
/**
|
||||
* Strings that can be used as HTTP verbs (e.g. in requests)
|
||||
* Strings that can be used as HTTP verbs (e.g. in requests): 'get' | 'post' | 'put' | 'delete' etc.
|
||||
*/
|
||||
export type HTTPVerb = 'all' |
|
||||
'get' |
|
||||
'post' |
|
||||
'put' |
|
||||
'delete' |
|
||||
'patch' |
|
||||
'options' |
|
||||
'head' |
|
||||
'checkout' |
|
||||
'copy' |
|
||||
'lock' |
|
||||
'merge' |
|
||||
'mkactivity' |
|
||||
'mkcol' |
|
||||
'move' |
|
||||
'm-search' |
|
||||
'notify' |
|
||||
'purge' |
|
||||
'report' |
|
||||
'search' |
|
||||
'subscribe' |
|
||||
'trace' |
|
||||
'unlock' |
|
||||
'unsubscribe';
|
||||
export type HTTPVerb = typeof httpVerbs[number];
|
||||
|
||||
/**
|
||||
* Provides information if a text (representing a method) is an HTTP verb
|
||||
@@ -47,5 +29,5 @@ export type HTTPVerb = 'all' |
|
||||
* @param method A text (representing a method) to check
|
||||
*/
|
||||
export function isHttpMethod(method: string): method is HTTPVerb {
|
||||
return ['get', 'post', 'put'].indexOf(method) > -1;
|
||||
return (httpVerbs as unknown as string[]).indexOf(method) > -1;
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ export function createRoute<REQUESTTYPE, RETURNTYPE>(
|
||||
// the given type has no index signature so we have to cast to get the IRouteHandler when a HTTP method is given
|
||||
const route = router.route(routeClass.urlFragment);
|
||||
|
||||
// get route parameters (path parameters)
|
||||
// get route parameters (path parameters) TODO: DELETE as not used
|
||||
if (Array.isArray(routeClass.obligatoryParameters) && routeClass.obligatoryParameters.length > 0) {
|
||||
routeClass.obligatoryParameters.forEach((parameterName) => {
|
||||
router.param(parameterName, async (req, _res, next, parameterValue: string) => {
|
||||
@@ -101,7 +101,7 @@ export function createRoute<REQUESTTYPE, RETURNTYPE>(
|
||||
isTestEnvironment,
|
||||
);
|
||||
// The validation error is not caused by faulty user input, but through an error that originates somewhere in
|
||||
// the backend, therefor we use this "stacked" error.
|
||||
// the backend, therefore we use this "stacked" error.
|
||||
const internalServerError = new SCInternalServerErrorResponse(
|
||||
validationError,
|
||||
isTestEnvironment,
|
||||
|
||||
Reference in New Issue
Block a user