Files
threshold-ui/addons/api/tests/unit/serializers/role-test.js
T

104 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-08-07 15:42:40 -04:00
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
2020-09-16 14:29:02 -04:00
module('Unit | Serializer | role', function (hooks) {
2020-08-07 15:42:40 -04:00
setupTest(hooks);
test('it serializes roles normally, without grants', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('role');
const record = store.createRecord('role', {
name: 'User',
description: 'Description',
grant_strings: ['foo', 'bar'],
2020-09-16 14:29:02 -04:00
version: 1,
2020-08-07 15:42:40 -04:00
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
name: 'User',
description: 'Description',
2020-09-16 14:29:02 -04:00
version: 1,
2020-11-02 17:40:37 -05:00
grant_scope_id: null
2020-08-07 15:42:40 -04:00
});
});
test('it serializes only grant strings when `adapterOptions.grantStrings` is set', function (assert) {
2020-08-07 15:42:40 -04:00
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('role');
const record = store.createRecord('role', {
name: 'User',
description: 'Description',
grant_strings: ['grant1', 'grant2'],
2020-09-16 14:29:02 -04:00
version: 1,
2020-08-07 15:42:40 -04:00
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {
grantStrings: ['foo', 'bar'],
2020-08-07 15:42:40 -04:00
};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
2020-08-11 12:00:09 -04:00
grant_strings: ['foo', 'bar'],
2020-09-16 14:29:02 -04:00
version: 1,
});
});
test('it normalizes records with array fields', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('role');
const roleModelClass = store.createRecord('role').constructor;
const payload = {
id: '1',
name: 'Role 1',
2020-09-16 14:29:02 -04:00
grant_strings: ['*', '*'],
};
const normalized = serializer.normalizeSingleResponse(
store,
roleModelClass,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'role',
attributes: {
name: 'Role 1',
grant_strings: ['*', '*'],
2020-09-16 14:29:02 -04:00
principals: [],
},
relationships: {},
},
});
});
test('it normalizes missing principals to empty array', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('role');
const role = store.createRecord('role').constructor;
const payload = {
id: '1',
name: 'Role 1',
2020-09-16 14:29:02 -04:00
scope: { id: 'o_123' },
};
2020-09-16 14:29:02 -04:00
const normalized = serializer.normalizeSingleResponse(store, role, payload);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'role',
attributes: {
name: 'Role 1',
scope: { scope_id: 'o_123' },
2020-09-16 14:29:02 -04:00
principals: [],
},
relationships: {},
},
2020-08-07 15:42:40 -04:00
});
});
});