feat: 🎸 add mocks for group members

This commit is contained in:
Randall Morey committed 2020-09-01 12:03:36 -04:00
1 parent 40a43fd9be
commit 4ea8007c20
5 files changed
+63 -6

No files matched your search

+25
View File
@@ -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
+14 -1
View File
@@ -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 });
}
})
});
+7
View File
@@ -0,0 +1,7 @@
import { Model, hasMany } from 'ember-cli-mirage';
export default Model.extend({
members: hasMany('user')
});
+2 -4
View File
@@ -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');
+15 -1
View File
@@ -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;
}
});