2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-01 09:39:41 -06:00
|
|
|
*/
|
|
|
|
|
|
2022-08-09 15:34:42 -04:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
|
import { setupTest } from 'ember-qunit';
|
|
|
|
|
|
|
|
|
|
module('Unit | Serializer | worker', function (hooks) {
|
|
|
|
|
setupTest(hooks);
|
|
|
|
|
|
2024-07-09 15:10:30 -05:00
|
|
|
test('it serializes normally', function (assert) {
|
2022-08-09 15:34:42 -04:00
|
|
|
const store = this.owner.lookup('service:store');
|
|
|
|
|
const serializer = store.serializerFor('worker');
|
|
|
|
|
const record = store.createRecord('worker', {
|
|
|
|
|
name: 'worker',
|
|
|
|
|
description: 'Description',
|
|
|
|
|
version: 1,
|
|
|
|
|
});
|
|
|
|
|
const snapshot = record._createSnapshot();
|
|
|
|
|
snapshot.adapterOptions = {};
|
|
|
|
|
const serializedRecord = serializer.serialize(snapshot);
|
|
|
|
|
assert.deepEqual(serializedRecord, {
|
|
|
|
|
version: 1,
|
|
|
|
|
description: 'Description',
|
|
|
|
|
name: 'worker',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('it serializes using `adapterOptions.workerGeneratedAuthToken`', function (assert) {
|
2022-08-26 11:50:53 -04:00
|
|
|
const scopeId = 'global';
|
2022-08-09 15:34:42 -04:00
|
|
|
const store = this.owner.lookup('service:store');
|
|
|
|
|
const serializer = store.serializerFor('worker');
|
|
|
|
|
const record = store.createRecord('worker', {
|
|
|
|
|
name: 'worker',
|
|
|
|
|
description: 'Description',
|
|
|
|
|
version: 1,
|
2022-08-26 11:50:53 -04:00
|
|
|
scope: {
|
|
|
|
|
scope_id: scopeId,
|
|
|
|
|
type: 'global',
|
|
|
|
|
},
|
2022-08-09 15:34:42 -04:00
|
|
|
});
|
|
|
|
|
const snapshot = record._createSnapshot();
|
|
|
|
|
snapshot.adapterOptions = {
|
|
|
|
|
workerGeneratedAuthToken: '123-abc',
|
|
|
|
|
};
|
|
|
|
|
const serializedRecord = serializer.serialize(snapshot);
|
|
|
|
|
assert.deepEqual(serializedRecord, {
|
2022-08-26 11:50:53 -04:00
|
|
|
scope_id: scopeId,
|
2022-08-09 15:34:42 -04:00
|
|
|
worker_generated_auth_token: '123-abc',
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-07-09 15:10:30 -05:00
|
|
|
|
|
|
|
|
test('it serializes using `adapterOptions.apiTags`', function (assert) {
|
|
|
|
|
const store = this.owner.lookup('service:store');
|
|
|
|
|
const serializer = store.serializerFor('worker');
|
|
|
|
|
const record = store.createRecord('worker', {
|
|
|
|
|
name: 'worker',
|
|
|
|
|
api_tags: null,
|
|
|
|
|
version: 1,
|
|
|
|
|
});
|
|
|
|
|
const snapshot = record._createSnapshot();
|
|
|
|
|
snapshot.adapterOptions = {
|
|
|
|
|
apiTags: {
|
|
|
|
|
key: ['value'],
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const serializedRecord = serializer.serialize(snapshot);
|
|
|
|
|
assert.deepEqual(serializedRecord, {
|
|
|
|
|
version: 1,
|
|
|
|
|
api_tags: { key: ['value'] },
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-07-12 13:31:42 -05:00
|
|
|
|
|
|
|
|
test('it normalizes missing `api_tags`, `config_tags`, and `canonical_tags`', function (assert) {
|
|
|
|
|
const store = this.owner.lookup('service:store');
|
|
|
|
|
const serializer = store.serializerFor('worker');
|
|
|
|
|
const worker = store.createRecord('worker').constructor;
|
|
|
|
|
const payload = {
|
|
|
|
|
id: 'w_123',
|
|
|
|
|
name: 'hardest worker',
|
|
|
|
|
scope: { id: 'global' },
|
|
|
|
|
version: 1,
|
|
|
|
|
address: 'localhost',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const normalized = serializer.normalizeSingleResponse(
|
|
|
|
|
store,
|
|
|
|
|
worker,
|
|
|
|
|
payload,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert.deepEqual(normalized, {
|
|
|
|
|
data: {
|
|
|
|
|
attributes: {
|
|
|
|
|
address: 'localhost',
|
|
|
|
|
api_tags: {},
|
|
|
|
|
authorized_actions: [],
|
|
|
|
|
canonical_tags: {},
|
|
|
|
|
config_tags: {},
|
|
|
|
|
name: 'hardest worker',
|
|
|
|
|
scope: {
|
|
|
|
|
id: 'global',
|
|
|
|
|
scope_id: 'global',
|
|
|
|
|
},
|
|
|
|
|
version: 1,
|
|
|
|
|
},
|
|
|
|
|
id: 'w_123',
|
|
|
|
|
relationships: {},
|
|
|
|
|
type: 'worker',
|
|
|
|
|
},
|
|
|
|
|
included: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-08-09 15:34:42 -04:00
|
|
|
});
|