Merge branch 'master' into banned-words

This commit is contained in:
Kim Gardner
2017-08-24 17:22:49 +01:00
committed by GitHub
4 changed files with 101 additions and 57 deletions
+47
View File
@@ -0,0 +1,47 @@
const chai = require('chai');
const expect = chai.expect;
const authz = require('../../../middleware/authorization');
describe('middleware.authorization', () => {
describe('#has', () => {
it('allows if no roles are specified', () => {
expect(authz.has({roles: []})).to.be.true;
});
it('allows if the correct roles are met', () => {
expect(authz.has({roles: ['ADMIN']}, 'ADMIN', 'MODERATOR')).to.be.true;
});
it('disallows if the role required is missing', () => {
expect(authz.has({roles: []}, 'ADMIN', 'MODERATOR')).to.be.false;
});
});
describe('#needed', () => {
let needed = (...roles) => {
let middleware = authz.needed(...roles);
return middleware[middleware.length - 1];
};
it('allows if no roles are specified', () => {
needed()({user: {roles: []}}, {}, (err) => {
expect(err).to.be.undefined;
});
});
it('allows if the correct roles are met', () => {
needed()({user: {roles: ['ADMIN']}}, {}, (err) => {
expect(err).to.be.undefined;
});
});
it('disallows if the role required is missing', () => {
needed('ADMIN', 'MODERATOR')({user: {roles: []}}, {}, (err) => {
expect(err).to.not.be.undefined;
});
});
it('disallows if there is no user on the request', () => {
needed('ADMIN', 'MODERATOR')({}, {}, (err) => {
expect(err).to.not.be.undefined;
});
});
});
});
+29 -43
View File
@@ -1,71 +1,57 @@
const passport = require('../../../passport');
const app = require('../../../../../app');
const chai = require('chai');
chai.use(require('chai-as-promised'));
chai.use(require('chai-http'));
const expect = chai.expect;
const UsersService = require('../../../../../services/users');
const SettingsService = require('../../../../../services/settings');
const settings = {id: '1', moderation: 'PRE', wordlist: {banned: ['bad words'], suspect: ['suspect words']}};
// Setup chai.
chai.should();
chai.use(require('chai-http'));
const UsersService = require('../../../../../services/users');
describe('/api/v1/account/username', () => {
let mockUser;
beforeEach(() => SettingsService.init(settings).then(() => {
return UsersService.createLocalUser('ana@gmail.com', '123321123', 'Ana');
})
.then((user) => {
mockUser = user;
}));
beforeEach(async () => {
await SettingsService.init(settings);
mockUser = await UsersService.createLocalUser('ana@gmail.com', '123321123', 'Ana');
});
describe('#put', () => {
it('it should enable a user to edit their username if canEditName is enabled', () => {
return chai.request(app)
it('it should enable a user to edit their username if canEditName is enabled', async () => {
await chai.request(app)
.post(`/api/v1/users/${mockUser.id}/username-enable`)
.set(passport.inject({id: '456', roles: ['ADMIN']}))
.then(() => chai.request(app)
.set(passport.inject({id: '456', roles: ['ADMIN']}));
const res = await chai.request(app)
.put('/api/v1/account/username')
.set(passport.inject({id: mockUser.id, roles: []}))
.send({username: 'MojoJojo'}))
.then((res) => {
expect(res).to.have.status(204);
});
.send({username: 'MojoJojo'});
expect(res).to.have.status(204);
});
it('it should return an error if the wrong user tries to edit a username', (done) => {
chai.request(app)
it('it should return an error if the wrong user tries to edit a username', async () => {
await chai.request(app)
.post(`/api/v1/users/${mockUser.id}/username-enable`)
.set(passport.inject({id: '456', roles: ['ADMIN']}))
.then(() => chai.request(app)
.set(passport.inject({id: '456', roles: ['ADMIN']}));
let res = chai.request(app)
.put('/api/v1/account/username')
.set(passport.inject({id: 'wrongid', roles: []}))
.send({username: 'MojoJojo'}))
.then(() => {
done(new Error('Expected Error'));
})
.catch((err) => {
expect(err).to.be.ok;
done();
});
.send({username: 'MojoJojo'});
return expect(res).to.eventually.be.rejected;
});
it('it should return an error when the user tries to edit their username if canEditName is disabled', (done) => {
chai.request(app)
it('it should return an error when the user tries to edit their username if canEditName is disabled', () => {
let res = chai.request(app)
.put('/api/v1/account/username')
.set(passport.inject({id: mockUser.id, roles: []}))
.send({username: 'MojoJojo'})
.then(() => {
done(new Error('Expected Error'));
})
.catch((err) => {
expect(err).to.be.ok;
done();
});
.send({username: 'MojoJojo'});
return expect(res).to.eventually.be.rejected;
});
});
});