feat: 🎸 add mocks for group members
This commit is contained in:
5 files changed
+63
-6
No files matched your search
@@ -110,6 +110,31 @@ export default function() {
|
||||
this.get('/scopes/:scope_id/groups/:id');
|
||||
this.patch('/scopes/:scope_id/groups/:id');
|
||||
this.del('/scopes/:scope_id/groups/:id');
|
||||
this.post('/scopes/:scope_id/groups/:idMethod', function ({ groups }, { params: { idMethod } }) {
|
||||
const attrs = this.normalizedRequestAttrs();
|
||||
const id = idMethod.split(':')[0];
|
||||
const method = idMethod.split(':')[1];
|
||||
const group = groups.find(id);
|
||||
const updatedAttrs = {
|
||||
version: attrs.version,
|
||||
memberIds: group.memberIds
|
||||
};
|
||||
// If adding members, push them into the array
|
||||
if (method === 'add-members') {
|
||||
attrs.memberIds.forEach(id => {
|
||||
if (!updatedAttrs.memberIds.includes(id)) {
|
||||
updatedAttrs.memberIds.push(id);
|
||||
}
|
||||
});
|
||||
}
|
||||
// If deleting members, filter them out of the array
|
||||
if (method === 'remove-members') {
|
||||
updatedAttrs.memberIds = updatedAttrs.memberIds.filter(id => {
|
||||
return !attrs.memberIds.includes(id);
|
||||
});
|
||||
}
|
||||
return group.update(updatedAttrs);
|
||||
});
|
||||
|
||||
// Other resources
|
||||
// host-catalog
|
||||
|
||||
@@ -1,5 +1,18 @@
|
||||
import factory from '../generated/factories/group';
|
||||
import { trait } from 'ember-cli-mirage';
|
||||
|
||||
export default factory.extend({
|
||||
id: (i) => `group-${i}`
|
||||
id: (i) => `group-${i}`,
|
||||
|
||||
/**
|
||||
* Generates some users for the same scope and assigns them to the group
|
||||
* as members.
|
||||
*/
|
||||
withMembers: trait({
|
||||
afterCreate(group, server) {
|
||||
const scope = group.scope;
|
||||
const members = server.createList('user', 5, { scope });
|
||||
group.update({ members });
|
||||
}
|
||||
})
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Model, hasMany } from 'ember-cli-mirage';
|
||||
|
||||
export default Model.extend({
|
||||
|
||||
members: hasMany('user')
|
||||
|
||||
});
|
||||
@@ -21,10 +21,8 @@ export default function(server) {
|
||||
// same items for request at all scopes.
|
||||
server.createList('auth-method', 3, { scope });
|
||||
|
||||
// User
|
||||
server.createList('user', 5, { scope });
|
||||
// Group
|
||||
server.createList('group', 5, { scope });
|
||||
// Groups and Users
|
||||
server.createList('group', 5, { scope }, 'withMembers');
|
||||
// Role
|
||||
server.createList('role', 5, { scope }, 'withPrincipals');
|
||||
|
||||
|
||||
@@ -1,5 +1,19 @@
|
||||
import ApplicationSerializer from './application';
|
||||
import { underscore } from '@ember/string';
|
||||
|
||||
export default ApplicationSerializer.extend({
|
||||
modelName: 'group'
|
||||
modelName: 'group',
|
||||
|
||||
keyForRelationshipIds(relationshipName) {
|
||||
return `${this._container.inflector.singularize(
|
||||
underscore(relationshipName)
|
||||
)}_ids`;
|
||||
},
|
||||
|
||||
_hashForModel(model) {
|
||||
const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments);
|
||||
json.member_ids = model.memberIds;
|
||||
return json;
|
||||
}
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user