Files
threshold-ui/addons/api/tests/unit/serializers/role-test.js
T
Carlos Alcaide Corvo b51c8203ee chore: 🤖 Upgrade eslint-plugin-qunit (#2011)
* chore: 🤖 Upgrade eslint-plugin-qunit

* Delete unnecessary assert.expect from tests
2023-12-08 12:56:00 -08:00

109 lines
3.0 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Serializer | role', function (hooks) {
setupTest(hooks);
test('it serializes roles normally, without grants', function (assert) {
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'],
version: 1,
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
name: 'User',
description: 'Description',
version: 1,
grant_scope_id: null,
});
});
test('it serializes only grant strings when `adapterOptions.grantStrings` is set', function (assert) {
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'],
version: 1,
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {
grantStrings: ['foo', 'bar'],
};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
grant_strings: ['foo', 'bar'],
version: 1,
});
});
test('it normalizes records with array fields', function (assert) {
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',
grant_strings: ['*', '*'],
};
const normalized = serializer.normalizeSingleResponse(
store,
roleModelClass,
payload,
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'role',
attributes: {
authorized_actions: [],
name: 'Role 1',
grant_strings: ['*', '*'],
principals: [],
},
relationships: {},
},
});
});
test('it normalizes missing principals to empty array', function (assert) {
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',
scope: { id: 'o_123' },
};
const normalized = serializer.normalizeSingleResponse(store, role, payload);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'role',
attributes: {
authorized_actions: [],
name: 'Role 1',
scope: { id: 'o_123', scope_id: 'o_123' },
principals: [],
grant_strings: [],
},
relationships: {},
},
});
});
});