四好公路
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

217 lines
7.1 KiB

/**
* Created by rain on 2015/11/20.
*/
'use strict';
const path = require('path');
const scaffold = require('../');
const config = require('./config');
const app = scaffold(config);
const should = require('should');
const assert = require('assert');
const client = require('supertest');
const co = require('co');
describe('auth url test by real http request', function () {
let models = app.fs.dc.models;
before(function () {
// runs before all tests in this block
});
after(function () {
// runs after all tests in this block
});
beforeEach(function (done) {
co(function*() {
try {
app.router.get('/xxx', co.wrap(function *(ctx, next) {
ctx.status = 200;
ctx.body = 'xxx';
}));
app.router.post('/yyy', co.wrap(function * (ctx, next) {
ctx.status = 200;
ctx.body = 'yyy';
}));
app.router.post('/zzz', co.wrap(function * (ctx, next) {
ctx.status = 200;
ctx.body = 'zzz';
}));
app.router.post('/aaa', co.wrap(function * (ctx, next) {
ctx.status = 200;
ctx.body = 'aaa';
}));
app.use(app.router.routes);
yield models.User.sync({force: true});
yield models.Role.sync({force: true});
yield models.UserRole.sync({force: true});
yield models.Url.sync({force: true});
yield models.UrlRole.sync({force: true});
let user = yield models.User.create({username: 'abc', password: '123', enable: true, creator: 1});
let role = yield models.Role.create({name: 'Test', desc: ''});
yield models.UserRole.create({userId: user.id, roleId: role.id});
let url = yield models.Url.create({path: "/xxx", desc: "test", auth: true, operation: "Get"});
yield models.UrlRole.create({roleId: role.id, urlId: url.id});
url = yield models.Url.create({path: "/yyy", desc: "test", auth: true, operation: "Post"});
yield models.UrlRole.create({roleId: role.id, urlId: url.id});
yield models.Url.create({path: "/zzz", desc: "test", auth: false, operation: "Post"});
yield models.Url.create({path: "/aaa", desc: "test", auth: true, operation: "Post"});
//ignore
yield models.Url.create({path: "/login", desc: "test", auth: false, operation: "Get"});
yield models.Role.create({name: 'Test1', desc: ''});
role = yield models.Role.create({name: 'Test2', desc: ''});
models.UrlRole.create({roleId: role.id, urlId: url.id});
done();
} catch (e) {
done(e);
}
});
});
afterEach(function () {
// runs after each test in this block
app.server.close();
});
it('not authed test', function (done) {
//this test will be block by auth kernel mv.
client(app.listen())
.get('/xxx')
.expect(302, done);
});
it('after login, user post yyy', function (done) {
co(function*() {
try {
let cli = client.agent(app.listen());
cli.post('/auth/login')
.send({username: 'abc', password: '123'})
.expect(200, function () {
cli.post('/yyy')
.expect(200, done)
});
} catch (err) {
done(err);
}
});
});
it('after login, user post zzz(exclude)', function (done) {
co(function*() {
try {
let cli = client.agent(app.listen());
cli.post('/auth/login')
.send({username: 'abc', password: '123'})
.expect(200, function () {
cli.post('/zzz')
.expect(200, done)
});
} catch (err) {
done(err);
}
});
});
it('after login, user post aaa', function (done) {
co(function*() {
try {
let cli = client.agent(app.listen());
cli.post('/auth/login')
.send({username: 'abc', password: '123'})
.expect(200, function () {
cli.post('/aaa')
.expect(400, done)
});
} catch (err) {
done(err);
}
});
});
it('after login, user get xxx', function (done) {
co(function*() {
try {
let cli = client.agent(app.listen());
cli.post('/auth/login')
.send({username: 'abc', password: '123'})
.expect(200, function () {
cli.get('/xxx')
.expect(200, done)
});
} catch (err) {
done(err);
}
});
});
it('after login, delete role has been referred by user', function (done) {
co(function*() {
try {
let cli = client.agent(app.listen());
cli.post('/auth/login')
.send({username: 'abc', password: '123'})
.expect(200, function () {
cli.del('/role/1')
.expect(400, done);
});
} catch (err) {
done(err);
}
});
});
it('after login, delete role which only associated with url resource', function (done) {
co(function*() {
try {
let cli = client.agent(app.listen());
cli.post('/auth/login')
.send({username: 'abc', password: '123'})
.expect(200, function () {
cli.del('/role/3')
.expect(400, done);
});
} catch (err) {
done(err);
}
});
});
it('after login, delete role which is isolate', function (done) {
co(function*() {
try {
let cli = client.agent(app.listen());
cli.post('/auth/login')
.send({username: 'abc', password: '123'})
.expect(200, function () {
cli.del('/role/2')
.expect(200, done);
});
} catch (err) {
done(err);
}
});
});
it('in valid path', function (done) {
done();
});
it('not exist path', function (done) {
done();
});
it('excluded path', function (done) {
done();
});
it('valid path with auth', function (done) {
done();
});
it('valid path without auth', function (done) {
done();
});
});