mirror of
https://github.com/bpatrik/pigallery2.git
synced 2025-01-26 05:27:35 +02:00
creating some backend tests with mocha
This commit is contained in:
parent
cbcc68dd86
commit
8deff0b605
@ -1,4 +1,4 @@
|
|||||||
///<reference path="jimp.d.ts"/>
|
///<reference path="customtypings/jimp.d.ts"/>
|
||||||
|
|
||||||
|
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
///<reference path="ExtendedRequest.d.ts"/>
|
///<reference path="../customtypings/ExtendedRequest.d.ts"/>
|
||||||
///<reference path="../../typings/main.d.ts"/>
|
///<reference path="../../../typings/main.d.ts"/>
|
||||||
|
|
||||||
import {NextFunction, Request, Response} from "express";
|
import {NextFunction, Request, Response} from "express";
|
||||||
import {Error, ErrorCodes} from "../../common/entities/Error";
|
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
||||||
import {UserRoles} from "../../common/entities/User";
|
import {UserRoles} from "../../../common/entities/User";
|
||||||
import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
|
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
||||||
|
|
||||||
export class AuthenticationMWs {
|
export class AuthenticationMWs {
|
||||||
|
|
||||||
public static authenticate(req:Request, res:Response, next:NextFunction) {
|
public static authenticate(req:Request, res:Response, next:NextFunction) {
|
||||||
if (typeof req.session.user === 'undefined') {
|
if (typeof req.session.user === 'undefined') {
|
||||||
return next(new Error(ErrorCodes.NOT_AUTHENTICATED));
|
return next(new Error(ErrorCodes.NOT_AUTHENTICATED));
|
||||||
}
|
}
|
||||||
//TODO: uncomment
|
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
import {NextFunction, Request, Response} from "express";
|
import {NextFunction, Request, Response} from "express";
|
||||||
import {Error, ErrorCodes} from "../../common/entities/Error";
|
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
||||||
import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
|
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
||||||
import {User} from "../../common/entities/User";
|
import {User} from "../../../common/entities/User";
|
||||||
|
|
||||||
export class UserMWs {
|
export class UserMWs {
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
import {NextFunction, Request, Response} from "express";
|
import {NextFunction, Request, Response} from "express";
|
||||||
import {Error, ErrorCodes} from "../../common/entities/Error";
|
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
||||||
import {UserRoles} from "../../common/entities/User";
|
import {UserRoles} from "../../../common/entities/User";
|
||||||
import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
|
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
||||||
|
|
||||||
export class UserRequestConstrainsMWs {
|
export class UserRequestConstrainsMWs {
|
||||||
|
|
@ -34,6 +34,10 @@ export class ObjectManagerRepository {
|
|||||||
return this._instance;
|
return this._instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static reset() {
|
||||||
|
this._instance = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
getGalleryManager():IGalleryManager {
|
getGalleryManager():IGalleryManager {
|
||||||
return this._galleryManager;
|
return this._galleryManager;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
///<reference path="../../typings/main.d.ts"/>
|
///<reference path="../../typings/main.d.ts"/>
|
||||||
|
|
||||||
import {AuthenticationMWs} from "../middlewares/AuthenticationMWs";
|
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||||
import {UserRoles} from "../../common/entities/User";
|
import {UserRoles} from "../../common/entities/User";
|
||||||
|
|
||||||
export class AdminRouter {
|
export class AdminRouter {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
///<reference path="../../typings/main.d.ts"/>
|
///<reference path="../../typings/main.d.ts"/>
|
||||||
|
|
||||||
import {AuthenticationMWs} from "../middlewares/AuthenticationMWs";
|
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||||
import {GalleryMWs} from "../middlewares/GalleryMWs";
|
import {GalleryMWs} from "../middlewares/GalleryMWs";
|
||||||
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
||||||
import {ThumbnailGeneratorMWs} from "../middlewares/ThumbnailGeneratorMWs";
|
import {ThumbnailGeneratorMWs} from "../middlewares/ThumbnailGeneratorMWs";
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
///<reference path="../../typings/main.d.ts"/>
|
///<reference path="../../typings/main.d.ts"/>
|
||||||
|
|
||||||
import {AuthenticationMWs} from "../middlewares/AuthenticationMWs";
|
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||||
import {UserRoles} from "../../common/entities/User";
|
import {UserRoles} from "../../common/entities/User";
|
||||||
|
|
||||||
export class SharingRouter {
|
export class SharingRouter {
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
///<reference path="../../typings/main.d.ts"/>
|
///<reference path="../../typings/main.d.ts"/>
|
||||||
|
|
||||||
import {UserMWs} from "../middlewares/UserMWs";
|
import {UserMWs} from "../middlewares/user/UserMWs";
|
||||||
import {UserRoles} from "../../common/entities/User";
|
import {UserRoles} from "../../common/entities/User";
|
||||||
import {AuthenticationMWs} from "../middlewares/AuthenticationMWs";
|
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||||
import {UserRequestConstrainsMWs} from "../middlewares/UserRequestConstrainsMWs";
|
import {UserRequestConstrainsMWs} from "../middlewares/user/UserRequestConstrainsMWs";
|
||||||
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
||||||
|
|
||||||
export class UserRouter {
|
export class UserRouter {
|
||||||
|
@ -9,8 +9,8 @@
|
|||||||
"main": "./backend/server.js",
|
"main": "./backend/server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"install": "typings install && tsc -p backend && tsc -p test/backend && tsc -p common && webpack --config ./frontend/webpack.config.js -p",
|
"install": "typings install && tsc -p backend && tsc -p test/backend && tsc -p common && webpack --config ./frontend/webpack.config.js -p",
|
||||||
"pretest": "typings install",
|
"pretest": "typings install && tsc -p test/backend",
|
||||||
"test": "karma start ./karma.conf.js",
|
"test": "karma start ./karma.conf.js && mocha --recursive test/backend/unit",
|
||||||
"start": "node ./backend/server",
|
"start": "node ./backend/server",
|
||||||
"postinstall": "node ./test/backend/initMongo.js"
|
"postinstall": "node ./test/backend/initMongo.js"
|
||||||
},
|
},
|
||||||
@ -54,6 +54,7 @@
|
|||||||
"zone.js": "^0.6.12"
|
"zone.js": "^0.6.12"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"chai": "^3.5.0",
|
||||||
"compression-webpack-plugin": "^0.3.0",
|
"compression-webpack-plugin": "^0.3.0",
|
||||||
"css-loader": "^0.23.1",
|
"css-loader": "^0.23.1",
|
||||||
"es6-promise-loader": "^1.0.1",
|
"es6-promise-loader": "^1.0.1",
|
||||||
@ -74,6 +75,7 @@
|
|||||||
"karma-phantomjs-launcher": "^1.0.0",
|
"karma-phantomjs-launcher": "^1.0.0",
|
||||||
"karma-sourcemap-loader": "^0.3.7",
|
"karma-sourcemap-loader": "^0.3.7",
|
||||||
"karma-webpack": "1.7.0",
|
"karma-webpack": "1.7.0",
|
||||||
|
"mocha": "^2.4.5",
|
||||||
"ng2lint": "0.0.10",
|
"ng2lint": "0.0.10",
|
||||||
"phantomjs-prebuilt": "^2.1.7",
|
"phantomjs-prebuilt": "^2.1.7",
|
||||||
"protractor": "^3.2.2",
|
"protractor": "^3.2.2",
|
||||||
|
1
test/backend/mocha.opts
Normal file
1
test/backend/mocha.opts
Normal file
@ -0,0 +1 @@
|
|||||||
|
--recursive
|
228
test/backend/unit/middlewares/uesr/AuthenticationMWs.ts
Normal file
228
test/backend/unit/middlewares/uesr/AuthenticationMWs.ts
Normal file
@ -0,0 +1,228 @@
|
|||||||
|
import {expect} from "chai";
|
||||||
|
import {AuthenticationMWs} from "../../../../../backend/middlewares/user/AuthenticationMWs";
|
||||||
|
import {Error, ErrorCodes} from "../../../../../common/entities/Error";
|
||||||
|
import {UserRoles} from "../../../../../common/entities/User";
|
||||||
|
import {ObjectManagerRepository} from "../../../../../backend/model/ObjectManagerRepository";
|
||||||
|
import {UserManager} from "../../../../../backend/model/memory/UserManager";
|
||||||
|
|
||||||
|
|
||||||
|
describe('Authentication middleware', () => {
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
ObjectManagerRepository.reset();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('authenticate', () => {
|
||||||
|
it('should call next on authenticated', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
session: {
|
||||||
|
user: "A user"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let next:any = (err) => {
|
||||||
|
expect(err).to.be.undefined;
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
AuthenticationMWs.authenticate(req, null, next);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call next with error on not authenticated', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
session: {}
|
||||||
|
};
|
||||||
|
let res:any = {};
|
||||||
|
let next:any = (err:Error) => {
|
||||||
|
expect(err).not.to.be.undefined;
|
||||||
|
expect(err.code).to.be.eql(ErrorCodes.NOT_AUTHENTICATED);
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
AuthenticationMWs.authenticate(req, null, next);
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('inverseAuthenticate', () => {
|
||||||
|
|
||||||
|
it('should call next with error on authenticated', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
session: {}
|
||||||
|
};
|
||||||
|
let res:any = {};
|
||||||
|
let next:any = (err) => {
|
||||||
|
expect(err).to.be.undefined;
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
AuthenticationMWs.inverseAuthenticate(req, null, next);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should call next error on authenticated', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
session: {
|
||||||
|
user: "A user"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let res:any = {};
|
||||||
|
let next:any = (err:Error) => {
|
||||||
|
expect(err).not.to.be.undefined;
|
||||||
|
expect(err.code).to.be.eql(ErrorCodes.ALREADY_AUTHENTICATED);
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
AuthenticationMWs.inverseAuthenticate(req, null, next);
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('authorise', () => {
|
||||||
|
it('should call next on authorised', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
session: {
|
||||||
|
user: {
|
||||||
|
role: UserRoles.Guest
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let next:any = (err) => {
|
||||||
|
expect(err).to.be.undefined;
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
AuthenticationMWs.authorise(UserRoles.Guest)(req, null, next);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call next with error on not authorised', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
session: {
|
||||||
|
user: {
|
||||||
|
role: UserRoles.Guest
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let next:any = (err:Error) => {
|
||||||
|
expect(err).not.to.be.undefined;
|
||||||
|
expect(err.code).to.be.eql(ErrorCodes.NOT_AUTHORISED);
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
AuthenticationMWs.authorise(UserRoles.Developer)(req, null, next);
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('login', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
ObjectManagerRepository.reset();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('should call next on missing...', () => {
|
||||||
|
it('body', (done) => {
|
||||||
|
let req:any = {};
|
||||||
|
let next:any = (err) => {
|
||||||
|
expect(err).to.be.undefined;
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
AuthenticationMWs.login(req, null, next);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loginCredential', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
body: {}
|
||||||
|
};
|
||||||
|
let next:any = (err) => {
|
||||||
|
expect(err).to.be.undefined;
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
AuthenticationMWs.login(req, null, next);
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('loginCredential content', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
body: {loginCredential: {}}
|
||||||
|
};
|
||||||
|
let next:any = (err) => {
|
||||||
|
expect(err).to.be.undefined;
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
AuthenticationMWs.login(req, null, next);
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
it('should call next with error on not finding user', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
body: {
|
||||||
|
loginCredential: {
|
||||||
|
username: "aa",
|
||||||
|
password: "bb"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let next:any = (err:Error) => {
|
||||||
|
expect(err).not.to.be.undefined;
|
||||||
|
expect(err.code).to.be.eql(ErrorCodes.CREDENTIAL_NOT_FOUND);
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
ObjectManagerRepository.getInstance().setUserManager(<UserManager>{
|
||||||
|
findOne: (filter, cb) => {
|
||||||
|
cb(null, null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
AuthenticationMWs.login(req, null, next);
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call next with user on the session on finding user', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
session: {},
|
||||||
|
body: {
|
||||||
|
loginCredential: {
|
||||||
|
username: "aa",
|
||||||
|
password: "bb"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let next:any = (err:Error) => {
|
||||||
|
expect(err).to.be.undefined;
|
||||||
|
expect(req.session.user).to.be.eql("test user");
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
ObjectManagerRepository.getInstance().setUserManager(<UserManager>{
|
||||||
|
findOne: (filter, cb:any) => {
|
||||||
|
cb(null, "test user");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
AuthenticationMWs.login(req, null, next);
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('logout', () => {
|
||||||
|
it('should call next on logout', (done) => {
|
||||||
|
let req:any = {
|
||||||
|
session: {
|
||||||
|
user: {
|
||||||
|
role: UserRoles.Guest
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let next:any = (err) => {
|
||||||
|
expect(err).to.be.undefined;
|
||||||
|
expect(req.session.user).to.be.undefined;
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
AuthenticationMWs.logout(req, null, next);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
@ -3,6 +3,7 @@
|
|||||||
"version": false,
|
"version": false,
|
||||||
"ambientDependencies": {
|
"ambientDependencies": {
|
||||||
"body-parser": "registry:dt/body-parser#0.0.0+20160317120654",
|
"body-parser": "registry:dt/body-parser#0.0.0+20160317120654",
|
||||||
|
"chai": "registry:dt/chai#3.4.0+20160317120654",
|
||||||
"core-js": "registry:dt/core-js#0.0.0+20160317120654",
|
"core-js": "registry:dt/core-js#0.0.0+20160317120654",
|
||||||
"debug": "github:DefinitelyTyped/DefinitelyTyped/debug/debug.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
"debug": "github:DefinitelyTyped/DefinitelyTyped/debug/debug.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||||
"express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
"express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user