Merge branch 'master' into cannot-ignore-staff

This commit is contained in:
Kim Gardner
2017-10-11 09:49:06 +01:00
committed by GitHub
55 changed files with 1331 additions and 1069 deletions
@@ -4,10 +4,12 @@ const schema = require('../../../../graph/schema');
const Context = require('../../../../graph/context');
const UserModel = require('../../../../models/user');
const SettingsService = require('../../../../services/settings');
const isEqual = require('lodash/isEqual');
const {expect} = require('chai');
describe('graph.mutations.updateSettings', () => {
beforeEach(async () => {
await SettingsService.init();
});
@@ -71,4 +73,86 @@ describe('graph.mutations.updateSettings', () => {
});
});
});
describe('nested objects', () => {
const user = new UserModel({roles: ['ADMIN']});
const ctx = new Context({user});
it('should handle nested objects', async () => {
const initSettings = {
wordlist: {
banned: ['fuck'],
suspect: ['idiot', 'nazis'],
},
domains: {
whitelist: ['localhost:3000'],
},
};
let res = await graphql(schema, QUERY, {}, ctx, {
settings: initSettings,
});
if (res.errors) {
console.error(res.errors);
}
if (res.data.updateSettings && res.data.updateSettings.errors) {
console.error(res.data.updateSettings.errors);
}
expect(res.errors).to.be.empty;
expect(res.data.updateSettings).to.be.null;
let retrievedSettings = await SettingsService.retrieve();
Object.keys(initSettings).forEach((key) => {
Object.keys(initSettings[key]).forEach((nestedKey) => {
expect(retrievedSettings).to.have.property(key);
expect(retrievedSettings[key]).to.have.property(nestedKey);
expect(isEqual(retrievedSettings[key][nestedKey], initSettings[key][nestedKey])).to.be.true;
});
});
const change = {
wordlist: {
suspect: ['idiot'],
},
domains: {
whitelist: ['coralproject.org'],
},
};
const changedSettings = Object.assign({}, initSettings, {
wordlist: {
suspect: change.wordlist.suspect,
},
domains: {
whitelist: change.domains.whitelist,
}
});
res = await graphql(schema, QUERY, {}, ctx, {
settings: change,
});
if (res.errors) {
console.error(res.errors);
}
if (res.data.updateSettings && res.data.updateSettings.errors) {
console.error(res.data.updateSettings.errors);
}
expect(res.errors).to.be.empty;
expect(res.data.updateSettings).to.be.null;
retrievedSettings = await SettingsService.retrieve();
Object.keys(changedSettings).forEach((key) => {
Object.keys(changedSettings[key]).forEach((nestedKey) => {
expect(retrievedSettings).to.have.property(key);
expect(retrievedSettings[key]).to.have.property(nestedKey);
expect(isEqual(retrievedSettings[key][nestedKey], changedSettings[key][nestedKey])).to.be.true;
});
});
});
});
});
@@ -1,82 +0,0 @@
const {graphql} = require('graphql');
const schema = require('../../../../graph/schema');
const Context = require('../../../../graph/context');
const UserModel = require('../../../../models/user');
const SettingsService = require('../../../../services/settings');
const {expect} = require('chai');
describe('graph.mutations.updateWordlist', () => {
beforeEach(async () => {
await SettingsService.init();
});
const QUERY = `
mutation UpdateWordlist($wordlist: UpdateWordlistInput!) {
updateWordlist(input: $wordlist) {
errors {
translation_key
}
}
}
`;
describe('context with different user roles', () => {
[
{error: 'NOT_AUTHORIZED'},
{error: 'NOT_AUTHORIZED', roles: []},
{roles: ['ADMIN']},
{roles: ['ADMIN', 'MODERATOR']},
{roles: ['MODERATOR']},
].forEach(({roles, error}) => {
it(roles && roles.length > 0 ? roles.join(', ') : '<None>', async () => {
let user;
if (roles != null) {
user = new UserModel({roles});
}
const ctx = new Context({user});
const wordlist = {
banned: [
'happy',
],
suspect: [
'sad',
],
};
const res = await graphql(schema, QUERY, {}, ctx, {
wordlist,
});
if (res.errors) {
console.error(res.errors);
}
expect(res.errors).to.be.empty;
if (error) {
expect(res.data.updateWordlist.errors).to.not.be.empty;
expect(res.data.updateWordlist.errors[0]).to.have.property('translation_key', error);
const {wordlist: retrievedWordlist} = await SettingsService.retrieve();
expect(retrievedWordlist).to.have.property('banned');
expect(retrievedWordlist.banned).to.have.members([]);
expect(retrievedWordlist).to.have.property('suspect');
expect(retrievedWordlist.suspect).to.have.members([]);
} else {
if (res.data.updateWordlist && res.data.updateWordlist.errors) {
console.error(res.data.updateWordlist.errors);
}
expect(res.data.updateWordlist).to.be.null;
const {wordlist: retrievedWordlist} = await SettingsService.retrieve();
expect(retrievedWordlist).to.have.property('banned');
expect(retrievedWordlist.banned).to.have.members(wordlist.banned);
expect(retrievedWordlist).to.have.property('suspect');
expect(retrievedWordlist.suspect).to.have.members(wordlist.suspect);
}
});
});
});
});