Don't use Ember.get in conjunction with dynamic strings in the deployment serializer

This commit is contained in:
Michael Lange
2018-12-12 16:49:52 -08:00
parent 3a07f68d4f
commit c3fdeb3fa6
2 changed files with 132 additions and 2 deletions

View File

@@ -9,8 +9,9 @@ export default ApplicationSerializer.extend({
normalize(typeHash, hash) {
if (hash) {
hash.TaskGroupSummaries = Object.keys(get(hash, 'TaskGroups') || {}).map(key => {
const deploymentStats = get(hash, `TaskGroups.${key}`);
const taskGroups = hash.TaskGroups || {};
hash.TaskGroupSummaries = Object.keys(taskGroups).map(key => {
const deploymentStats = taskGroups[key];
return assign({ Name: key }, deploymentStats);
});

View File

@@ -0,0 +1,129 @@
import { test } from 'ember-qunit';
import DeploymentModel from 'nomad-ui/models/deployment';
import moduleForSerializer from '../../helpers/module-for-serializer';
moduleForSerializer('deployment', 'Unit | Serializer | Deployment', {
needs: [
'adapter:application',
'serializer:deployment',
'service:system',
'service:token',
'transform:fragment-array',
'model:allocation',
'model:job',
'model:task-group-deployment-summary',
],
});
const normalizationTestCases = [
{
name: 'Normal',
in: {
ID: 'test-deployment',
JobID: 'test-job',
Namespace: 'test-namespace',
Status: 'canceled',
TaskGroups: {
taskGroup: {
DesiredCanaries: 2,
},
},
},
out: {
data: {
id: 'test-deployment',
type: 'deployment',
attributes: {
status: 'canceled',
taskGroupSummaries: [
{
name: 'taskGroup',
desiredCanaries: 2,
},
],
},
relationships: {
allocations: {
links: {
related: '/v1/deployment/allocations/test-deployment',
},
},
job: {
data: {
id: '["test-job","test-namespace"]',
type: 'job',
},
},
jobForLatest: {
data: {
id: '["test-job","test-namespace"]',
type: 'job',
},
},
},
},
},
},
{
name: 'Dots in task group names',
in: {
ID: 'test-deployment',
JobID: 'test-job',
Namespace: 'test-namespace',
Status: 'canceled',
TaskGroups: {
'one.two': {
DesiredCanaries: 2,
},
'three.four': {
DesiredCanaries: 3,
},
},
},
out: {
data: {
id: 'test-deployment',
type: 'deployment',
attributes: {
status: 'canceled',
taskGroupSummaries: [
{
name: 'one.two',
desiredCanaries: 2,
},
{
name: 'three.four',
desiredCanaries: 3,
},
],
},
relationships: {
allocations: {
links: {
related: '/v1/deployment/allocations/test-deployment',
},
},
job: {
data: {
id: '["test-job","test-namespace"]',
type: 'job',
},
},
jobForLatest: {
data: {
id: '["test-job","test-namespace"]',
type: 'job',
},
},
},
},
},
},
];
normalizationTestCases.forEach(testCase => {
test(`normalization: ${testCase.name}`, function(assert) {
assert.deepEqual(this.subject().normalize(DeploymentModel, testCase.in), testCase.out);
});
});