open-vault/ui/tests/unit/utils/openapi-to-attrs-test.js
Jordan Reimer 5c2a08de6d
Ember Upgrade to 3.24 (#13443)
* Update browserslist

* Add browserslistrc

* ember-cli-update --to 3.26, fix conflicts

* Run codemodes that start with ember-*

* More codemods - before cp*

* More codemods (curly data-test-*)

* WIP ember-basic-dropdown template errors

* updates ember-basic-dropdown and related deps to fix build issues

* updates basic dropdown instances to new version API

* updates more deps -- ember-template-lint is working again

* runs no-implicit-this codemod

* creates and runs no-quoteless-attributes codemod

* runs angle brackets codemod

* updates lint:hbs globs to only touch hbs files

* removes yield only templates

* creates and runs deprecated args transform

* supresses lint error for invokeAction on LinkTo component

* resolves remaining ambiguous path lint errors

* resolves simple-unless lint errors

* adds warnings for deprecated tagName arg on LinkTo components

* adds warnings for remaining curly component invocation

* updates global template lint rules

* resolves remaining template lint errors

* disables some ember specfic lint rules that target pre octane patterns

* js lint fix run

* resolves remaining js lint errors

* fixes test run

* adds npm-run-all dep

* fixes test attribute issues

* fixes console acceptance tests

* fixes tests

* adds yield only wizard/tutorial-active template

* fixes more tests

* attempts to fix more flaky tests

* removes commented out settled in transit test

* updates deprecations workflow and adds initializer to filter by version

* updates flaky policies acl old test

* updates to flaky transit test

* bumps ember deps down to LTS version

* runs linters after main merge

* fixes client count tests after bad merge conflict fixes

* fixes client count history test

* more updates to lint config

* another round of hbs lint fixes after extending stylistic rule

* updates lint-staged commands

* removes indent eslint rule since it seems to break things

* fixes bad attribute in transform-edit-form template

* test fixes

* fixes enterprise tests

* adds changelog

* removes deprecated ember-concurrency-test-waiters dep and adds @ember/test-waiters

* flaky test fix

Co-authored-by: hashishaw <cshaw@hashicorp.com>
2021-12-16 20:44:29 -07:00

223 lines
5.8 KiB
JavaScript

import { attr } from '@ember-data/model';
import { expandOpenApiProps, combineAttributes, combineFieldGroups } from 'vault/utils/openapi-to-attrs';
import { module, test } from 'qunit';
module('Unit | Util | OpenAPI Data Utilities', function () {
const OPENAPI_RESPONSE_PROPS = {
ttl: {
type: 'string',
format: 'seconds',
description: 'this is a TTL!',
'x-vault-displayAttrs': {
name: 'TTL',
},
},
'awesome-people': {
type: 'array',
items: {
type: 'string',
},
'x-vault-displayAttrs': {
value: 'Grace Hopper,Lady Ada',
},
},
'favorite-ice-cream': {
type: 'string',
enum: ['vanilla', 'chocolate', 'strawberry'],
},
'default-value': {
default: 30,
'x-vault-displayAttrs': {
value: 300,
},
type: 'integer',
},
default: {
'x-vault-displayAttrs': {
value: 30,
},
type: 'integer',
},
'super-secret': {
type: 'string',
'x-vault-displayAttrs': {
sensitive: true,
},
description: 'A really secret thing',
},
};
const EXPANDED_PROPS = {
ttl: {
helpText: 'this is a TTL!',
editType: 'ttl',
label: 'TTL',
fieldGroup: 'default',
},
awesomePeople: {
editType: 'stringArray',
defaultValue: 'Grace Hopper,Lady Ada',
fieldGroup: 'default',
},
favoriteIceCream: {
editType: 'string',
type: 'string',
possibleValues: ['vanilla', 'chocolate', 'strawberry'],
fieldGroup: 'default',
},
defaultValue: {
editType: 'number',
type: 'number',
defaultValue: 300,
fieldGroup: 'default',
},
default: {
editType: 'number',
type: 'number',
defaultValue: 30,
fieldGroup: 'default',
},
superSecret: {
type: 'string',
editType: 'string',
sensitive: true,
helpText: 'A really secret thing',
fieldGroup: 'default',
},
};
const EXISTING_MODEL_ATTRS = [
{
key: 'name',
value: {
isAttribute: true,
name: 'name',
options: {
editType: 'string',
label: 'Role name',
},
},
},
{
key: 'awesomePeople',
value: {
isAttribute: true,
name: 'awesomePeople',
options: {
label: 'People Who Are Awesome',
},
},
},
];
const COMBINED_ATTRS = {
name: attr('string', {
editType: 'string',
type: 'string',
label: 'Role name',
}),
ttl: attr('string', {
editType: 'ttl',
label: 'TTL',
helpText: 'this is a TTL!',
}),
awesomePeople: attr({
label: 'People Who Are Awesome',
editType: 'stringArray',
defaultValue: 'Grace Hopper,Lady Ada',
}),
favoriteIceCream: attr('string', {
type: 'string',
editType: 'string',
possibleValues: ['vanilla', 'chocolate', 'strawberry'],
}),
superSecret: attr('string', {
type: 'string',
editType: 'string',
sensitive: true,
description: 'A really secret thing',
}),
};
const NEW_FIELDS = ['one', 'two', 'three'];
test('it creates objects from OpenAPI schema props', function (assert) {
const generatedProps = expandOpenApiProps(OPENAPI_RESPONSE_PROPS);
for (let propName in EXPANDED_PROPS) {
assert.deepEqual(EXPANDED_PROPS[propName], generatedProps[propName], `correctly expands ${propName}`);
}
});
test('it combines OpenAPI props with existing model attrs', function (assert) {
const combined = combineAttributes(EXISTING_MODEL_ATTRS, EXPANDED_PROPS);
for (let propName in EXISTING_MODEL_ATTRS) {
assert.deepEqual(COMBINED_ATTRS[propName], combined[propName]);
}
});
test('it adds new fields from OpenAPI to fieldGroups except for exclusions', function (assert) {
let modelFieldGroups = [
{ default: ['name', 'awesomePeople'] },
{
Options: ['ttl'],
},
];
const excludedFields = ['two'];
const expectedGroups = [
{ default: ['name', 'awesomePeople', 'one', 'three'] },
{
Options: ['ttl'],
},
];
const newFieldGroups = combineFieldGroups(modelFieldGroups, NEW_FIELDS, excludedFields);
for (let groupName in modelFieldGroups) {
assert.deepEqual(
newFieldGroups[groupName],
expectedGroups[groupName],
'it incorporates all new fields except for those excluded'
);
}
});
test('it adds all new fields from OpenAPI to fieldGroups when excludedFields is empty', function (assert) {
let modelFieldGroups = [
{ default: ['name', 'awesomePeople'] },
{
Options: ['ttl'],
},
];
const excludedFields = [];
const expectedGroups = [
{ default: ['name', 'awesomePeople', 'one', 'two', 'three'] },
{
Options: ['ttl'],
},
];
const nonExcludedFieldGroups = combineFieldGroups(modelFieldGroups, NEW_FIELDS, excludedFields);
for (let groupName in modelFieldGroups) {
assert.deepEqual(
nonExcludedFieldGroups[groupName],
expectedGroups[groupName],
'it incorporates all new fields'
);
}
});
test('it keeps fields the same when there are no brand new fields from OpenAPI', function (assert) {
let modelFieldGroups = [
{ default: ['name', 'awesomePeople', 'two', 'one', 'three'] },
{
Options: ['ttl'],
},
];
const excludedFields = [];
const expectedGroups = [
{ default: ['name', 'awesomePeople', 'two', 'one', 'three'] },
{
Options: ['ttl'],
},
];
const fieldGroups = combineFieldGroups(modelFieldGroups, NEW_FIELDS, excludedFields);
for (let groupName in modelFieldGroups) {
assert.deepEqual(fieldGroups[groupName], expectedGroups[groupName], 'it incorporates all new fields');
}
});
});