Files
threshold-ui/addons/api/tests/unit/serializers/credential-library-test.js
T
2024-02-23 10:07:52 -08:00

231 lines
6.9 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import {
TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
} from 'api/models/credential-library';
module('Unit | Serializer | credential library', function (hooks) {
setupTest(hooks);
test('it serializes correctly on create', function (assert) {
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential-library');
const record = store.createRecord('credential-library', {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
name: 'Name',
description: 'Description',
path: '/vault/path',
http_method: 'GET',
version: 1,
credential_type: 'ssh_private_key',
credential_mapping_overrides: { username_attribute: 'user' },
});
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
credential_store_id: null,
name: 'Name',
description: 'Description',
version: 1,
credential_type: 'ssh_private_key',
credential_mapping_overrides: {
username_attribute: 'user',
},
attributes: {
path: '/vault/path',
http_method: 'GET',
http_request_body: null,
},
});
});
test('it serializes correctly on update', function (assert) {
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential-library');
store.push({
data: {
id: '1',
type: 'credential-library',
attributes: {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
name: 'Name',
description: 'Description',
path: '/vault/path',
http_method: 'GET',
version: 1,
credential_type: 'ssh_private_key',
credential_mapping_overrides: {},
},
},
});
const record = store.peekRecord('credential-library', '1');
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
credential_store_id: null,
name: 'Name',
description: 'Description',
credential_type: 'ssh_private_key',
credential_mapping_overrides: {
private_key_attribute: null,
private_key_passphrase_attribute: null,
username_attribute: null,
},
attributes: {
path: '/vault/path',
http_method: 'GET',
http_request_body: null,
},
version: 1,
});
});
test('it serializes empty strings to null', function (assert) {
const store = this.owner.lookup('service:store');
const vaultGenericRecord = store.createRecord('credential-library', {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
http_method: '',
path: null,
credential_type: null,
credential_mapping_overrides: {},
});
const vaultSSHCertificateRecord = store.createRecord('credential-library', {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
http_method: '',
path: null,
});
let serializedVaultGenericRecord = vaultGenericRecord.serialize();
let serializedVaultSSHCertificateRecord =
vaultSSHCertificateRecord.serialize();
assert.deepEqual(serializedVaultGenericRecord, {
attributes: {
path: null,
http_method: null,
http_request_body: null,
},
credential_store_id: null,
description: null,
name: null,
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
credential_type: null,
credential_mapping_overrides: {},
});
assert.deepEqual(serializedVaultSSHCertificateRecord, {
attributes: {
critical_options: null,
extensions: null,
key_bits: null,
key_id: null,
key_type: null,
path: null,
ttl: null,
username: null,
},
credential_store_id: null,
description: null,
name: null,
type: TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
});
});
test('it does not serialize http_request_body unless http_method is set to POST', function (assert) {
const store = this.owner.lookup('service:store');
const record = store.createRecord('credential-library', {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
http_method: 'GET',
http_request_body: 'body',
});
let serializedRecord = record.serialize();
assert.deepEqual(
serializedRecord,
{
attributes: {
http_method: 'GET',
http_request_body: null,
path: null,
},
credential_store_id: null,
description: null,
name: null,
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
credential_type: null,
credential_mapping_overrides: {},
},
'http_request_body attribute is not expected',
);
record.http_method = 'POST';
serializedRecord = record.serialize();
assert.deepEqual(
serializedRecord,
{
attributes: {
http_method: 'POST',
http_request_body: 'body',
path: null,
},
credential_store_id: null,
description: null,
name: null,
type: TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
credential_type: null,
credential_mapping_overrides: {},
},
'http_request_body attribute is expected',
);
});
test('it serializes vault_ssh_certificate correctly', function (assert) {
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('credential-library');
store.push({
data: {
id: '1',
type: 'credential-library',
attributes: {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
version: 1,
name: 'Name',
description: 'Description',
path: '/vault/path',
username: 'user',
key_type: 'rsa',
key_bits: 100,
ttl: '100',
key_id: 'id',
extensions: [{ key: 'key', value: 'value' }],
critical_options: [{ key: 'key', value: 'value' }],
},
},
});
const record = store.peekRecord('credential-library', '1');
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
type: TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
credential_store_id: null,
name: 'Name',
description: 'Description',
attributes: {
path: '/vault/path',
username: 'user',
key_type: 'rsa',
key_bits: 100,
ttl: '100',
key_id: 'id',
extensions: { key: 'value' },
critical_options: { key: 'value' },
},
version: 1,
});
});
});