initial pass at status support

This commit is contained in:
Wyatt Johnson
2017-11-02 17:16:57 -06:00
parent 1f3722edc1
commit 76a255fb7b
49 changed files with 1112 additions and 1598 deletions
+1 -1
View File
@@ -48,7 +48,7 @@ describe('graph.mutations.addTag', () => {
Object.entries({
'anonymous': undefined,
'regular commenter': new UserModel({}),
'banned moderator': new UserModel({roles: ['MODERATOR'], status: 'BANNED'})
'banned moderator': new UserModel({roles: ['MODERATOR'], banned: true})
}).forEach(([ userDescription, user ]) => {
it(userDescription, async () => {
const context = new Context({user});
+55 -61
View File
@@ -71,27 +71,28 @@ describe('graph.mutations.createComment', () => {
beforeEach(() => AssetModel.create({id: '123'}));
[
{user: new UserModel({status: 'ACTIVE'}), error: null},
{user: new UserModel({status: 'BANNED'}), error: 'NOT_AUTHORIZED'},
{user: new UserModel({status: 'PENDING'}), error: null},
{user: new UserModel({status: 'APPROVED'}), error: null}
{user: new UserModel({}), error: null},
{user: new UserModel({banned: true}), error: 'NOT_AUTHORIZED'},
{user: new UserModel({suspended: new Date((new Date()).getTime() - (10 * 86400000))}), error: null},
{user: new UserModel({suspended: new Date((new Date()).getTime() + (10 * 86400000))}), error: 'NOT_AUTHORIZED'},
].forEach(({user, error}) => {
describe(`user.status=${user.status}`, () => {
it(error ? 'does not create the comment' : 'creates the comment', () => {
describe(`user.banned=${user.banned} user.suspended=${user.suspended}`, () => {
it(error ? 'does not create the comment' : 'creates the comment', async () => {
const context = new Context({user});
const {data, errors} = await graphql(schema, query, {}, context);
return graphql(schema, query, {}, context)
.then(({data, errors}) => {
expect(errors).to.be.undefined;
if (error) {
expect(data.createComment).to.have.property('comment').null;
expect(data.createComment).to.have.property('errors').not.null;
expect(data.createComment.errors[0]).to.have.property('translation_key', error);
} else {
expect(data.createComment).to.have.property('comment').not.null;
expect(data.createComment).to.have.property('errors').null;
}
});
expect(errors).to.be.undefined;
if (error) {
expect(data.createComment).to.have.property('comment').null;
expect(data.createComment).to.have.property('errors').not.null;
expect(data.createComment.errors[0]).to.have.property('translation_key', error);
} else {
if (data.createComment.errors && data.createComment.errors.length > 0) {
console.error(data.createComment.errors);
}
expect(data.createComment).to.have.property('errors').null;
expect(data.createComment).to.have.property('comment').not.null;
}
});
});
});
@@ -109,7 +110,7 @@ describe('graph.mutations.createComment', () => {
beforeEach(() => asset.save());
it(error ? 'does not create the comment' : 'creates the comment', () => {
const context = new Context({user: new UserModel({status: 'ACTIVE'})});
const context = new Context({user: new UserModel({})});
return graphql(schema, query, {}, context)
.then(({data, errors}) => {
@@ -142,7 +143,7 @@ describe('graph.mutations.createComment', () => {
beforeEach(() => AssetModel.create({id: '123', settings: {moderation}}));
it(`creates comment with status=${status}`, () => {
const context = new Context({user: new UserModel({status: 'ACTIVE'})});
const context = new Context({user: new UserModel()});
return graphql(schema, query, {}, context)
.then(({data, errors}) => {
@@ -172,33 +173,30 @@ describe('graph.mutations.createComment', () => {
].forEach(({message, body, status, flagged}) => {
describe(message, () => {
it(`should create a comment with status=${status} and it ${flagged ? 'should' : 'should not'} be flagged`, () => {
const context = new Context({user: new UserModel({status: 'ACTIVE'})});
it(`should create a comment with status=${status} and it ${flagged ? 'should' : 'should not'} be flagged`, async () => {
const context = new Context({user: new UserModel({})});
return graphql(schema, query, {}, context, {
const {data, errors} = await graphql(schema, query, {}, context, {
input: {
asset_id: '123',
body
}
})
.then(({data, errors}) => {
expect(errors).to.be.undefined;
expect(data.createComment).to.have.property('comment').not.null;
expect(data.createComment.comment).to.have.property('status', status);
expect(data.createComment).to.have.property('errors').null;
});
return ActionModel.find({
item_id: data.createComment.comment.id,
action_type: 'FLAG'
});
})
.then((actions) => {
if (flagged) {
expect(actions).to.have.length(1);
} else {
expect(actions).to.have.length(0);
}
});
expect(errors).to.be.undefined;
expect(data.createComment).to.have.property('comment').not.null;
expect(data.createComment.comment).to.have.property('status', status);
expect(data.createComment).to.have.property('errors').null;
const actions = await ActionModel.find({
item_id: data.createComment.comment.id,
action_type: 'FLAG'
});
if (flagged) {
expect(actions).to.have.length(1);
} else {
expect(actions).to.have.length(0);
}
});
});
@@ -217,30 +215,26 @@ describe('graph.mutations.createComment', () => {
].forEach(({roles, tag}) => {
describe(`user.roles=${JSON.stringify(roles)}`, () => {
it(`creates comment ${tag ? `with tag=${tag}` : 'without tags'}`, () => {
it(`creates comment ${tag ? `with tag=${tag}` : 'without tags'}`, async () => {
const context = new Context({user: new UserModel({roles})});
return graphql(schema, query, {}, context)
.then(({data, errors}) => {
if (errors) {
console.error(errors);
}
expect(errors).to.be.undefined;
expect(data.createComment).to.have.property('comment').not.null;
expect(data.createComment).to.have.property('errors').null;
const {data, errors} = await graphql(schema, query, {}, context);
return CommentsService.findById(data.createComment.comment.id);
})
.then(({tags}) => {
if (tag) {
expect(tags).to.have.length(1);
expect(tags[0].tag.name).to.have.equal(tag);
} else {
expect(tags).length(0);
}
});
if (errors) {
console.error(errors);
}
expect(errors).to.be.undefined;
expect(data.createComment).to.have.property('comment').not.null;
expect(data.createComment).to.have.property('errors').null;
const {tags} = await CommentsService.findById(data.createComment.comment.id);
if (tag) {
expect(tags).to.have.length(1);
expect(tags[0].tag.name).to.have.equal(tag);
} else {
expect(tags).length(0);
}
});
});
});
+1 -1
View File
@@ -66,7 +66,7 @@ describe('graph.mutations.removeTag', () => {
Object.entries({
'anonymous': undefined,
'regular commenter': new UserModel({}),
'banned moderator': new UserModel({roles: ['MODERATOR'], status: 'BANNED'})
'banned moderator': new UserModel({roles: ['MODERATOR'], banned: true})
}).forEach(([userDescription, user]) => {
it(userDescription, async function () {
const context = new Context({user});
@@ -0,0 +1,104 @@
const {graphql} = require('graphql');
const schema = require('../../../../graph/schema');
const Context = require('../../../../graph/context');
const SettingsService = require('../../../../services/settings');
const UserModel = require('../../../../models/user');
const UsersService = require('../../../../services/users');
const {expect} = require('chai');
describe('graph.mutations.setUserBanStatus', () => {
let user;
beforeEach(async () => {
await SettingsService.init();
user = await UsersService.createLocalUser('usernameA@example.com', 'password', 'usernameA');
});
const setUserBanStatusMutation = `
mutation SetUserBanStatus($user_id: ID!, $status: Boolean!) {
setUserBanStatus(input: {
id: $user_id,
status: $status
}) {
errors {
translation_key
}
}
}
`;
[
{self: true, error: 'NOT_AUTHORIZED', roles: null},
{self: true, error: 'NOT_AUTHORIZED', roles: ['STAFF']},
{self: true, error: 'NOT_AUTHORIZED', roles: []},
{error: 'NOT_AUTHORIZED', roles: null},
{error: 'NOT_AUTHORIZED', roles: ['STAFF']},
{error: 'NOT_AUTHORIZED', roles: []},
{error: false, roles: ['MODERATOR']},
{error: false, roles: ['ADMIN']},
{error: false, roles: ['ADMIN', 'MODERATOR']},
].forEach(({self, error, roles}) => {
it(`${error ? 'can not' : 'can'} ban ${self ? 'themself' : 'another user'} as a user with roles ${roles && roles.length ? roles : JSON.stringify(roles)}`, async () => {
const actor = new UserModel({roles});
// If we're testing self assign, set the id of the actor to the user
// we're acting on.
if (self) {
actor.id = user.id;
}
const ctx = new Context({user: actor});
const {data, errors} = await graphql(schema, setUserBanStatusMutation, {}, ctx, {
user_id: user.id,
status: true
});
if (errors && errors.length > 0) {
console.error(errors);
}
expect(errors).to.be.undefined;
if (error) {
expect(data.setUserBanStatus).to.have.property('errors').not.null;
expect(data.setUserBanStatus.errors[0]).to.have.property('translation_key', error);
} else {
expect(data.setUserBanStatus).to.be.null;
user = await UserModel.findOne({id: user.id});
expect(user.status.banned.status).to.be.true;
expect(user.status.banned.history).to.have.length(1);
expect(user.status.banned.history[0]).to.have.property('status', true);
expect(user.status.banned.history[0]).to.have.property('assigned_by', actor.id);
expect(user.status.banned.history[0]).to.have.property('created_at').not.null;
expect(user.banned).to.be.true;
const res = await graphql(schema, setUserBanStatusMutation, {}, ctx, {
user_id: user.id,
status: false
});
if (res.errors && res.errors.length > 0) {
console.error(res.errors);
}
expect(res.errors).to.be.undefined;
expect(res.data.setUserBanStatus).to.be.null;
user = await UserModel.findOne({id: user.id});
expect(user.status.banned.status).to.be.false;
expect(user.status.banned.history).to.have.length(2);
expect(user.status.banned.history[0]).to.have.property('status').to.be.true;
expect(user.status.banned.history[0]).to.have.property('assigned_by', actor.id);
expect(user.status.banned.history[0]).to.have.property('created_at').not.null;
expect(user.status.banned.history[1]).to.have.property('status').to.be.false;
expect(user.status.banned.history[1]).to.have.property('assigned_by', actor.id);
expect(user.status.banned.history[1]).to.have.property('created_at').not.null;
expect(user.banned).to.be.false;
}
});
});
});
@@ -0,0 +1,115 @@
const {graphql} = require('graphql');
const timekeeper = require('timekeeper');
const schema = require('../../../../graph/schema');
const Context = require('../../../../graph/context');
const SettingsService = require('../../../../services/settings');
const UserModel = require('../../../../models/user');
const UsersService = require('../../../../services/users');
const chai = require('chai');
chai.use(require('chai-datetime'));
const {expect} = chai;
describe('graph.mutations.setUserSuspensionStatus', () => {
let user;
beforeEach(async () => {
await SettingsService.init();
user = await UsersService.createLocalUser('usernameA@example.com', 'password', 'usernameA');
});
const setUserSuspensionStatusMutation = `
mutation SetUserUsernameStatus($user_id: ID!, $until: Date) {
setUserSuspensionStatus(input: {
id: $user_id,
until: $until
}) {
errors {
translation_key
}
}
}
`;
[
{self: true, error: 'NOT_AUTHORIZED', roles: null},
{self: true, error: 'NOT_AUTHORIZED', roles: ['STAFF']},
{self: true, error: 'NOT_AUTHORIZED', roles: []},
{error: 'NOT_AUTHORIZED', roles: null},
{error: 'NOT_AUTHORIZED', roles: ['STAFF']},
{error: 'NOT_AUTHORIZED', roles: []},
{error: false, roles: ['MODERATOR']},
{error: false, roles: ['ADMIN']},
{error: false, roles: ['ADMIN', 'MODERATOR']},
].forEach(({self, error, roles}) => {
it(`${error ? 'can not' : 'can'} suspend ${self ? 'themself' : 'another user'} as a user with roles ${roles && roles.length ? roles : JSON.stringify(roles)}`, async () => {
const actor = new UserModel({roles});
// If we're testing self assign, set the id of the actor to the user
// we're acting on.
if (self) {
actor.id = user.id;
}
const ctx = new Context({user: actor});
const now = new Date();
const oneHourFromNow = new Date(new Date(now).setHours(now.getHours() + 1));
const {data, errors} = await graphql(schema, setUserSuspensionStatusMutation, {}, ctx, {
user_id: user.id,
until: oneHourFromNow
});
if (errors && errors.length > 0) {
console.error(errors);
}
expect(errors).to.be.undefined;
if (error) {
expect(data.setUserSuspensionStatus).to.have.property('errors').not.null;
expect(data.setUserSuspensionStatus.errors[0]).to.have.property('translation_key', error);
} else {
expect(data.setUserSuspensionStatus).to.be.null;
user = await UserModel.findOne({id: user.id});
// Mongoose messes with the date, check within a 2 second window.
expect(user.status.suspension.until).to.be.withinTime(new Date(oneHourFromNow.getTime() - 1000), new Date(oneHourFromNow.getTime() + 1000));
expect(user.status.suspension.history).to.have.length(1);
expect(user.status.suspension.history[0]).to.have.property('until').to.be.withinTime(new Date(oneHourFromNow.getTime() - 1000), new Date(oneHourFromNow.getTime() + 1000));
expect(user.status.suspension.history[0]).to.have.property('assigned_by', actor.id);
expect(user.status.suspension.history[0]).to.have.property('created_at').not.null;
expect(user.suspended).to.be.true;
timekeeper.travel(new Date(oneHourFromNow.getTime() + 10000));
expect(user.suspended).to.be.false;
timekeeper.reset();
const res = await graphql(schema, setUserSuspensionStatusMutation, {}, ctx, {
user_id: user.id,
until: null
});
if (res.errors && res.errors.length > 0) {
console.error(res.errors);
}
expect(res.errors).to.be.undefined;
expect(res.data.setUserSuspensionStatus).to.be.null;
user = await UserModel.findOne({id: user.id});
// Mongoose messes with the date, check within a 2 second window.
expect(user.status.suspension.until).to.be.null;
expect(user.status.suspension.history).to.have.length(2);
expect(user.status.suspension.history[0]).to.have.property('until').to.be.withinTime(new Date(oneHourFromNow.getTime() - 1000), new Date(oneHourFromNow.getTime() + 1000));
expect(user.status.suspension.history[0]).to.have.property('assigned_by', actor.id);
expect(user.status.suspension.history[0]).to.have.property('created_at').not.null;
expect(user.status.suspension.history[1]).to.have.property('until').to.be.null;
expect(user.status.suspension.history[1]).to.have.property('assigned_by', actor.id);
expect(user.status.suspension.history[1]).to.have.property('created_at').not.null;
expect(user.suspended).to.be.false;
}
});
});
});
@@ -0,0 +1,87 @@
const {graphql} = require('graphql');
const schema = require('../../../../graph/schema');
const Context = require('../../../../graph/context');
const SettingsService = require('../../../../services/settings');
const UserModel = require('../../../../models/user');
const UsersService = require('../../../../services/users');
const chai = require('chai');
chai.use(require('chai-datetime'));
const {expect} = chai;
[
{status: 'APPROVED', name: 'approve', mutation: 'approveUsername'},
{status: 'REJECTED', name: 'reject', mutation: 'rejectUsername'}
].forEach(({status, name, mutation}) => {
describe(`graph.mutations.${mutation}`, () => {
let user;
beforeEach(async () => {
await SettingsService.init();
user = await UsersService.createLocalUser('usernameA@example.com', 'password', 'usernameA');
});
const setUserUsernameStatusMutation = `
mutation SetUserUsernameStatus($user_id: ID!) {
${mutation}(id: $user_id) {
errors {
translation_key
}
}
}
`;
[
{self: true, error: 'NOT_AUTHORIZED', roles: null},
{self: true, error: 'NOT_AUTHORIZED', roles: ['STAFF']},
{self: true, error: 'NOT_AUTHORIZED', roles: []},
{error: 'NOT_AUTHORIZED', roles: null},
{error: 'NOT_AUTHORIZED', roles: ['STAFF']},
{error: 'NOT_AUTHORIZED', roles: []},
{error: false, roles: ['MODERATOR']},
{error: false, roles: ['ADMIN']},
{error: false, roles: ['ADMIN', 'MODERATOR']},
].forEach(({self, error, roles}) => {
it(`${error ? 'can not' : 'can'} ${name} a username with the user roles ${roles && roles.length ? roles : JSON.stringify(roles)}${self ? ' on themself' : ''}`, async () => {
const actor = new UserModel({roles});
// If we're testing self assign, set the id of the actor to the user
// we're acting on.
if (self) {
actor.id = user.id;
}
const ctx = new Context({user: actor});
const {data, errors} = await graphql(schema, setUserUsernameStatusMutation, {}, ctx, {
user_id: user.id,
});
if (errors && errors.length > 0) {
console.error(errors);
}
expect(errors).to.be.undefined;
if (error) {
expect(data[mutation]).to.have.property('errors').not.null;
expect(data[mutation].errors[0]).to.have.property('translation_key', error);
} else {
expect(data[mutation]).to.be.null;
user = await UserModel.findOne({id: user.id});
expect(user.status.username.status).to.equal(status);
expect(user.status.username.history).to.have.length(2);
expect(user.status.username.history[0]).to.have.property('status', 'SET');
expect(user.status.username.history[0]).to.have.property('assigned_by').is.null;
expect(user.status.username.history[0]).to.have.property('created_at').not.null;
expect(user.status.username.history[1]).to.have.property('status', status);
expect(user.status.username.history[1]).to.have.property('assigned_by', actor.id);
expect(user.status.username.history[1]).to.have.property('created_at').not.null;
expect(user.status.username.history[1].created_at).afterTime(user.status.username.history[0].created_at);
}
});
});
});
});
@@ -22,8 +22,7 @@ describe('graph.mutations.updateAssetSettings', () => {
translation_key
}
}
}
`;
}`;
describe('context with different user roles', () => {
@@ -21,8 +21,7 @@ describe('graph.mutations.updateSettings', () => {
translation_key
}
}
}
`;
}`;
describe('context with different user roles', () => {
-57
View File
@@ -1,57 +0,0 @@
const passport = require('../../../passport');
const app = require('../../../../../app');
const UsersService = require('../../../../../services/users');
const SettingsService = require('../../../../../services/settings');
const settings = {id: '1', moderation: 'PRE', wordlist: {banned: ['bad words'], suspect: ['suspect words']}};
const chai = require('chai');
chai.should();
chai.use(require('chai-http'));
const expect = chai.expect;
describe('/api/v1/account/username', () => {
let mockUser;
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', async () => {
await chai.request(app)
.post(`/api/v1/users/${mockUser.id}/username-enable`)
.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'});
expect(res).to.have.status(204);
});
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']}));
let res = chai.request(app)
.put('/api/v1/account/username')
.set(passport.inject({id: 'wrongid', roles: []}))
.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', () => {
let res = chai.request(app)
.put('/api/v1/account/username')
.set(passport.inject({id: mockUser.id, roles: []}))
.send({username: 'MojoJojo'});
return expect(res).to.eventually.be.rejected;
});
});
});
-49
View File
@@ -48,52 +48,3 @@ describe('/api/v1/users/:user_id/email/confirm', () => {
});
});
});
describe('/api/v1/users/:user_id/actions', () => {
let mockUser;
beforeEach(() => SettingsService.init(settings).then(() => {
return UsersService.createLocalUser('ana@gmail.com', '123321123', 'Ana');
})
.then((user) => {
mockUser = user;
}));
describe('#post', () => {
it('it should update actions', () => {
return chai.request(app)
.post(`/api/v1/users/${mockUser.id}/actions`)
.set(passport.inject({id: '456', roles: ['ADMIN']}))
.send({'action_type': 'FLAG', metadata: {reason: 'Bio is too awesome.'}})
.then((res) => {
expect(res).to.have.status(201);
expect(res).to.have.body;
expect(res.body).to.have.property('action_type', 'FLAG');
expect(res.body).to.have.property('item_id', mockUser.id);
});
});
});
});
describe('/api/v1/users/:user_id/username-enable', () => {
let mockUser;
beforeEach(() => SettingsService.init(settings).then(() => {
return UsersService.createLocalUser('ana@gmail.com', '123321123', 'Ana');
})
.then((user) => {
mockUser = user;
}));
describe('#post', () => {
it('it should enable a user to edit their username', () => {
return chai.request(app)
.post(`/api/v1/users/${mockUser.id}/username-enable`)
.set(passport.inject({id: '456', roles: ['ADMIN']}))
.then((res) => {
expect(res).to.have.status(204);
});
});
});
});
@@ -1,27 +1,27 @@
const expect = require('chai').expect;
const Domainlist = require('../../../services/domainlist');
const DomainList = require('../../../services/domain_list');
const SettingsService = require('../../../services/settings');
describe('services.Domainlist', () => {
describe('services.DomainList', () => {
const domainlists = {
const domainLists = {
whitelist: [
'nytimes.com',
'wapo.com'
]
};
let domainlist = new Domainlist();
let domainList = new DomainList();
const settings = {id: '1', moderation: 'PRE', domainlist: {whitelist: ['nytimes.com', 'wapo.com']}};
beforeEach(() => SettingsService.init(settings));
describe('#init', () => {
before(() => domainlist.upsert(domainlists));
before(() => domainList.upsert(domainLists));
it('has entries', () => {
expect(domainlist.lists.whitelist).to.not.be.empty;
expect(domainList.lists.whitelist).to.not.be.empty;
});
});
@@ -92,21 +92,21 @@ describe('services.Domainlist', () => {
['google.Ca:80', 'google.ca'],
['google.Ca:443', 'google.ca'],
].forEach(([domain, hostname]) => {
expect(Domainlist.parseURL(domain), `domain ${domain} should be parsed as ${hostname}`).to.equal(hostname);
expect(DomainList.parseURL(domain), `domain ${domain} should be parsed as ${hostname}`).to.equal(hostname);
});
});
});
describe('#match', () => {
const whiteList = Domainlist.parseList(domainlists['whitelist']);
const whiteList = DomainList.parseList(domainLists['whitelist']);
it('does match on an included domain', () => {
[
'http://wapo.com',
'nytimes.com'
].forEach((domain) => {
expect(domainlist.match(whiteList, domain)).to.be.true;
expect(domainList.match(whiteList, domain)).to.be.true;
});
});
@@ -116,7 +116,7 @@ describe('services.Domainlist', () => {
'www.badsite.com',
'otherexample.com'
].forEach((domain) => {
expect(domainlist.match(whiteList, domain)).to.be.false;
expect(domainList.match(whiteList, domain)).to.be.false;
});
});
});
-22
View File
@@ -1,22 +0,0 @@
describe('services.scraper', () => {
describe('#create', () => {
it('should create a new kue job');
});
describe('#scrape', () => {
it('should scrape complete information');
it('should scrape what it can');
});
describe('#update', () => {
it('should update the database record entries from the meta');
});
describe('#process', () => {
it('should start the processor to scrape assets');
});
describe('#shutdown', () => {
it('should shutdown the job processor');
});
});
+53 -116
View File
@@ -151,21 +151,6 @@ describe('services.UsersService', () => {
});
describe('#setStatus', () => {
it('should set the status to active', () => {
return UsersService
.setStatus(mockUsers[0].id, 'ACTIVE')
.then(() => UsersService.findById(mockUsers[0].id))
.then((user) => {
expect(user).to.have.property('status', 'ACTIVE');
})
.then(() => {
expect(MailerService.sendSimple).to.not.have.been.called;
});
});
});
describe('#ignoreUser', () => {
it('should add user id to ignoredUsers set', async () => {
const user = mockUsers[0];
@@ -194,54 +179,6 @@ describe('services.UsersService', () => {
});
});
describe('#ban', () => {
it('should set the status to banned', () => {
return UsersService
.setStatus(mockUsers[0].id, 'BANNED')
.then(() => UsersService.findById(mockUsers[0].id))
.then((user) => {
expect(user).to.have.property('status', 'BANNED');
})
.then(() => {
expect(MailerService.sendSimple).to.have.been.calledWithMatch({
template: 'banned',
to: mockUsers[0].profiles[0].id
});
});
});
it('should still disable and ban the user if there is no comment', () => {
return UsersService
.setStatus(mockUsers[0].id, 'BANNED')
.then(() => UsersService.findById(mockUsers[0].id))
.then((user) => {
expect(user).to.have.property('status', 'BANNED');
});
});
});
describe('#unban', () => {
it('should set the status to active', () => {
return UsersService
.setStatus(mockUsers[0].id, 'ACTIVE')
.then(() => UsersService.findById(mockUsers[0].id))
.then((user) => {
expect(user).to.have.property('status', 'ACTIVE');
});
});
});
describe('#toggleNameEdit', () => {
it('should toggle the canEditName field', () => {
return UsersService
.toggleNameEdit(mockUsers[0].id, true)
.then(() => UsersService.findById(mockUsers[0].id))
.then((user) => {
expect(user).to.have.property('canEditName', true);
});
});
});
describe('#search', () => {
it('should return all the results without a value', async () => {
expect(await UsersService.search()).to.have.length(3);
@@ -286,63 +223,63 @@ describe('services.UsersService', () => {
});
});
describe('#editName', () => {
it('should let the user edit their username if the proper toggle is set', () => {
return UsersService
.toggleNameEdit(mockUsers[0].id, true)
.then(() => UsersService.editName(mockUsers[0].id, 'Jojo'))
.then(() => UsersService.findById(mockUsers[0].id))
.then((user) => {
expect(user).to.have.property('username', 'Jojo');
expect(user).to.have.property('canEditName', false);
});
});
// describe('#editName', () => {
// it('should let the user edit their username if the proper toggle is set', () => {
// return UsersService
// .toggleNameEdit(mockUsers[0].id, true)
// .then(() => UsersService.editName(mockUsers[0].id, 'Jojo'))
// .then(() => UsersService.findById(mockUsers[0].id))
// .then((user) => {
// expect(user).to.have.property('username', 'Jojo');
// expect(user).to.have.property('canEditName', false);
// });
// });
it('should let the user submit the same username if user is not banned (create username)', () => {
return UsersService
.toggleNameEdit(mockUsers[0].id, true)
.then(() => UsersService.editName(mockUsers[0].id, mockUsers[0].username))
.then(() => UsersService.findById(mockUsers[0].id))
.then((user) => {
expect(user).to.have.property('username', mockUsers[0].username);
expect(user).to.have.property('canEditName', false);
});
});
// it('should let the user submit the same username if user is not banned (create username)', () => {
// return UsersService
// .toggleNameEdit(mockUsers[0].id, true)
// .then(() => UsersService.editName(mockUsers[0].id, mockUsers[0].username))
// .then(() => UsersService.findById(mockUsers[0].id))
// .then((user) => {
// expect(user).to.have.property('username', mockUsers[0].username);
// expect(user).to.have.property('canEditName', false);
// });
// });
it('should return error when a banned user submits the same username (rejected username)', () => {
return UsersService
.toggleNameEdit(mockUsers[0].id, true)
.then(() => UsersService.setStatus(mockUsers[0].id, 'BANNED'))
.then(() => UsersService.editName(mockUsers[0].id, mockUsers[0].username))
.then(() => UsersService.findById(mockUsers[0].id))
.then(() => {
throw new Error('Error expected');
})
.catch((err) => {
expect(err.status).to.equal(400);
expect(err.translation_key).to.equal('SAME_USERNAME_PROVIDED');
});
});
// it('should return error when a banned user submits the same username (rejected username)', () => {
// return UsersService
// .toggleNameEdit(mockUsers[0].id, true)
// .then(() => UsersService.setStatus(mockUsers[0].id, 'BANNED'))
// .then(() => UsersService.editName(mockUsers[0].id, mockUsers[0].username))
// .then(() => UsersService.findById(mockUsers[0].id))
// .then(() => {
// throw new Error('Error expected');
// })
// .catch((err) => {
// expect(err.status).to.equal(400);
// expect(err.translation_key).to.equal('SAME_USERNAME_PROVIDED');
// });
// });
it('should return an error if canEditName is false', async () => {
return expect(UsersService.editName(mockUsers[0].id, 'Jojo')).to.eventually.be.rejected;
});
// it('should return an error if canEditName is false', async () => {
// return expect(UsersService.editName(mockUsers[0].id, 'Jojo')).to.eventually.be.rejected;
// });
it('should return an error if the username is already taken', async () => {
await UsersService.toggleNameEdit(mockUsers[0].id, true);
return expect(UsersService.editName(mockUsers[0].id, 'Marvel')).to.eventually.be.rejected;
});
// it('should return an error if the username is already taken', async () => {
// await UsersService.toggleNameEdit(mockUsers[0].id, true);
// return expect(UsersService.editName(mockUsers[0].id, 'Marvel')).to.eventually.be.rejected;
// });
it('should not allow non-alphanumeric characters in usernames', () => {
return UsersService
.isValidUsername('hi🖕')
.then(() => {
expect(false).to.be.true;
})
.catch((err) => {
expect(err).to.be.ok;
});
});
});
// it('should not allow non-alphanumeric characters in usernames', () => {
// return UsersService
// .isValidUsername('hi🖕')
// .then(() => {
// expect(false).to.be.true;
// })
// .catch((err) => {
// expect(err).to.be.ok;
// });
// });
// });
});