mirror of
https://github.com/xuthus83/pigallery2.git
synced 2025-01-14 14:43:17 +08: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";
|
||||
|
@ -1,18 +1,17 @@
|
||||
///<reference path="ExtendedRequest.d.ts"/>
|
||||
///<reference path="../../typings/main.d.ts"/>
|
||||
///<reference path="../customtypings/ExtendedRequest.d.ts"/>
|
||||
///<reference path="../../../typings/main.d.ts"/>
|
||||
|
||||
import {NextFunction, Request, Response} from "express";
|
||||
import {Error, ErrorCodes} from "../../common/entities/Error";
|
||||
import {UserRoles} from "../../common/entities/User";
|
||||
import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
|
||||
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
||||
import {UserRoles} from "../../../common/entities/User";
|
||||
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
||||
|
||||
export class AuthenticationMWs {
|
||||
|
||||
public static authenticate(req:Request, res:Response, next:NextFunction) {
|
||||
if (typeof req.session.user === 'undefined') {
|
||||
return next(new Error(ErrorCodes.NOT_AUTHENTICATED));
|
||||
}
|
||||
//TODO: uncomment
|
||||
}
|
||||
return next();
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import {NextFunction, Request, Response} from "express";
|
||||
import {Error, ErrorCodes} from "../../common/entities/Error";
|
||||
import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
|
||||
import {User} from "../../common/entities/User";
|
||||
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
||||
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
||||
import {User} from "../../../common/entities/User";
|
||||
|
||||
export class UserMWs {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import {NextFunction, Request, Response} from "express";
|
||||
import {Error, ErrorCodes} from "../../common/entities/Error";
|
||||
import {UserRoles} from "../../common/entities/User";
|
||||
import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
|
||||
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
||||
import {UserRoles} from "../../../common/entities/User";
|
||||
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
||||
|
||||
export class UserRequestConstrainsMWs {
|
||||
|
@ -34,6 +34,10 @@ export class ObjectManagerRepository {
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
public static reset() {
|
||||
this._instance = null;
|
||||
}
|
||||
|
||||
|
||||
getGalleryManager():IGalleryManager {
|
||||
return this._galleryManager;
|
||||
|
@ -1,6 +1,6 @@
|
||||
///<reference path="../../typings/main.d.ts"/>
|
||||
|
||||
import {AuthenticationMWs} from "../middlewares/AuthenticationMWs";
|
||||
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||
import {UserRoles} from "../../common/entities/User";
|
||||
|
||||
export class AdminRouter {
|
||||
|
@ -1,6 +1,6 @@
|
||||
///<reference path="../../typings/main.d.ts"/>
|
||||
|
||||
import {AuthenticationMWs} from "../middlewares/AuthenticationMWs";
|
||||
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||
import {GalleryMWs} from "../middlewares/GalleryMWs";
|
||||
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
||||
import {ThumbnailGeneratorMWs} from "../middlewares/ThumbnailGeneratorMWs";
|
||||
|
@ -1,6 +1,6 @@
|
||||
///<reference path="../../typings/main.d.ts"/>
|
||||
|
||||
import {AuthenticationMWs} from "../middlewares/AuthenticationMWs";
|
||||
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||
import {UserRoles} from "../../common/entities/User";
|
||||
|
||||
export class SharingRouter {
|
||||
|
@ -1,9 +1,9 @@
|
||||
///<reference path="../../typings/main.d.ts"/>
|
||||
|
||||
import {UserMWs} from "../middlewares/UserMWs";
|
||||
import {UserMWs} from "../middlewares/user/UserMWs";
|
||||
import {UserRoles} from "../../common/entities/User";
|
||||
import {AuthenticationMWs} from "../middlewares/AuthenticationMWs";
|
||||
import {UserRequestConstrainsMWs} from "../middlewares/UserRequestConstrainsMWs";
|
||||
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||
import {UserRequestConstrainsMWs} from "../middlewares/user/UserRequestConstrainsMWs";
|
||||
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
||||
|
||||
export class UserRouter {
|
||||
|
@ -9,8 +9,8 @@
|
||||
"main": "./backend/server.js",
|
||||
"scripts": {
|
||||
"install": "typings install && tsc -p backend && tsc -p test/backend && tsc -p common && webpack --config ./frontend/webpack.config.js -p",
|
||||
"pretest": "typings install",
|
||||
"test": "karma start ./karma.conf.js",
|
||||
"pretest": "typings install && tsc -p test/backend",
|
||||
"test": "karma start ./karma.conf.js && mocha --recursive test/backend/unit",
|
||||
"start": "node ./backend/server",
|
||||
"postinstall": "node ./test/backend/initMongo.js"
|
||||
},
|
||||
@ -54,6 +54,7 @@
|
||||
"zone.js": "^0.6.12"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^3.5.0",
|
||||
"compression-webpack-plugin": "^0.3.0",
|
||||
"css-loader": "^0.23.1",
|
||||
"es6-promise-loader": "^1.0.1",
|
||||
@ -74,6 +75,7 @@
|
||||
"karma-phantomjs-launcher": "^1.0.0",
|
||||
"karma-sourcemap-loader": "^0.3.7",
|
||||
"karma-webpack": "1.7.0",
|
||||
"mocha": "^2.4.5",
|
||||
"ng2lint": "0.0.10",
|
||||
"phantomjs-prebuilt": "^2.1.7",
|
||||
"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,
|
||||
"ambientDependencies": {
|
||||
"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",
|
||||
"debug": "github:DefinitelyTyped/DefinitelyTyped/debug/debug.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||
"express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#0d622d857f97d44ea7dcad2b3edec1f23c48fe9e",
|
||||
|
Loading…
x
Reference in New Issue
Block a user