fixed query issues

This commit is contained in:
Wyatt Johnson
2017-11-20 10:32:46 -07:00
parent 1756c7c050
commit debda80db1
6 changed files with 111 additions and 9 deletions
+2
View File
@@ -22,6 +22,7 @@ const TagLink = require('./tag_link');
const Tag = require('./tag');
const UserError = require('./user_error');
const User = require('./user');
const UserState = require('./user_state');
const ValidationUserError = require('./validation_user_error');
const plugins = require('../../services/plugins');
@@ -49,6 +50,7 @@ let resolvers = {
Tag,
UserError,
User,
UserState,
ValidationUserError,
};
+7 -7
View File
@@ -6,7 +6,8 @@ const {
SEARCH_OTHERS_COMMENTS,
UPDATE_USER_ROLES,
SEARCH_COMMENT_METRICS,
LIST_OWN_TOKENS
LIST_OWN_TOKENS,
VIEW_USER_STATUS
} = require('../../perms/constants');
const User = {
@@ -83,12 +84,11 @@ const User = {
}
},
// suspension({id, suspension}, _, {user}) {
// if (user.id !== id && !user.can(VIEW_SUSPENSION_INFO)) {
// return null;
// }
// return suspension;
// }
state(user, args, ctx) {
if (ctx.user && (ctx.user.id === user.id || ctx.user.can(VIEW_USER_STATUS))) {
return user;
}
}
};
// Decorate the User type resolver with a tags field.
+13
View File
@@ -0,0 +1,13 @@
const {
VIEW_USER_STATUS
} = require('../../perms/constants');
const UserState = {
status: (user, args, ctx) => {
if (ctx.user && (ctx.user.id === user.id || ctx.user.can(VIEW_USER_STATUS))) {
return user.status;
}
}
};
module.exports = UserState;
+1 -1
View File
@@ -7,6 +7,6 @@ module.exports = {
SEARCH_COMMENT_METRICS: 'SEARCH_COMMENT_METRICS',
LIST_OWN_TOKENS: 'LIST_OWN_TOKENS',
SEARCH_COMMENT_STATUS_HISTORY: 'SEARCH_COMMENT_STATUS_HISTORY',
VIEW_SUSPENSION_INFO: 'VIEW_SUSPENSION_INFO',
VIEW_PROTECTED_SETTINGS: 'VIEW_PROTECTED_SETTINGS',
VIEW_USER_STATUS: 'VIEW_USER_STATUS'
};
+1 -1
View File
@@ -10,7 +10,7 @@ module.exports = (user, perm) => {
case types.SEARCH_OTHERS_COMMENTS:
case types.SEARCH_COMMENT_METRICS:
case types.SEARCH_COMMENT_STATUS_HISTORY:
case types.VIEW_SUSPENSION_INFO:
case types.VIEW_USER_STATUS:
case types.VIEW_PROTECTED_SETTINGS:
return check(user, ['ADMIN', 'MODERATOR']);
+87
View File
@@ -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;
describe('graph.queries.user', () => {
let user;
beforeEach(async () => {
await SettingsService.init();
user = await UsersService.createLocalUser('usernameA@example.com', 'password', 'usernameA');
});
describe('state', () => {
const meQuery = `
query Me {
me {
state {
status {
username {
status
}
}
}
}
}
`;
it('can query me', async () => {
const ctx = new Context({user});
const {data, errors} = await graphql(schema, meQuery, {}, ctx);
expect(errors).to.be.undefined;
expect(data.me).to.not.be.null;
expect(data.me.state).not.to.be.null;
expect(data.me.state.status.username.status).to.be.equal('SET');
});
const query = `
query User($user_id: ID!) {
user(id: $user_id) {
state {
status {
username {
status
}
}
}
}
}
`;
[
{roles: [], can: false},
{roles: ['STAFF'], can: false},
{roles: ['STAFF', 'MODERATOR'], can: true},
{roles: ['STAFF', 'MODERATOR', 'ADMIN'], can: true},
].forEach(({roles, can}) => {
it(`${can ? 'can' : 'can not'} query with roles = ${roles.length > 0 ? roles : '<none>'}`, async () => {
const actor = new UserModel({roles});
const ctx = new Context({user: actor});
const {data, errors} = await graphql(schema, query, {}, ctx, {
user_id: user.id,
});
expect(errors).to.be.undefined;
if (!can) {
expect(data.user).to.be.null;
} else {
expect(data.user).to.not.be.null;
expect(data.user.state).not.to.be.null;
expect(data.user.state.status.username.status).to.be.equal('SET');
}
});
});
});
});