From 021097ad44b529bc07cf4096dfa9893408050e27 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Mon, 14 Feb 2022 14:57:34 -0500 Subject: [PATCH 1/2] fix: append ?plugin_name to dynamic host catalog create requests --- addons/api/addon/adapters/host-catalog.js | 28 +++++++++++++++++++ addons/api/app/adapters/host-catalog.js | 1 + addons/api/mirage/config.js | 14 +++++++++- .../tests/unit/adapters/host-catalog-test.js | 12 ++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 addons/api/addon/adapters/host-catalog.js create mode 100644 addons/api/app/adapters/host-catalog.js create mode 100644 addons/api/tests/unit/adapters/host-catalog-test.js diff --git a/addons/api/addon/adapters/host-catalog.js b/addons/api/addon/adapters/host-catalog.js new file mode 100644 index 000000000..52d30fe6f --- /dev/null +++ b/addons/api/addon/adapters/host-catalog.js @@ -0,0 +1,28 @@ +import ApplicationAdapter from './application'; +import { serializeIntoHash } from '@ember-data/adapter/-private'; + +export default class HostCatalogAdapter extends ApplicationAdapter { + /** + Called by the store when a newly created record is + saved via the `save` method on a model record instance. + + The `createRecord` method serializes the record and makes an Ajax (HTTP POST) request + to a URL computed by `buildURL`. + + See `serialize` for information on how to customize the serialized form + of a record. + + @method createRecord + @param {Store} store + @param {Model} type + @param {Snapshot} snapshot + @return {Promise} promise + */ + createRecord(store, type, snapshot) { + let url = this.buildURL(type.modelName, null, snapshot, 'createRecord'); + const pluginName = snapshot.attr('plugin')?.name; + if (pluginName) url = `${url}?plugin_name=${pluginName}`; + const data = serializeIntoHash(store, type, snapshot); + return this.ajax(url, 'POST', { data }); + } +} diff --git a/addons/api/app/adapters/host-catalog.js b/addons/api/app/adapters/host-catalog.js new file mode 100644 index 000000000..00197a624 --- /dev/null +++ b/addons/api/app/adapters/host-catalog.js @@ -0,0 +1 @@ +export { default } from 'api/adapters/host-catalog'; diff --git a/addons/api/mirage/config.js b/addons/api/mirage/config.js index 759eb63c3..bb969a04c 100644 --- a/addons/api/mirage/config.js +++ b/addons/api/mirage/config.js @@ -270,7 +270,19 @@ export default function () { return hostCatalogs.where({ scopeId }); } ); - this.post('/host-catalogs'); + this.post( + '/host-catalogs', + function ({ hostCatalogs }, { queryParams: { plugin_name } }) { + const attrs = this.normalizedRequestAttrs(); + if (plugin_name) { + attrs.type = 'plugin'; + attrs.plugin = { + name: plugin_name, + }; + } + return hostCatalogs.create(attrs); + } + ); this.get('/host-catalogs/:id'); this.patch('/host-catalogs/:id'); this.del('/host-catalogs/:id'); diff --git a/addons/api/tests/unit/adapters/host-catalog-test.js b/addons/api/tests/unit/adapters/host-catalog-test.js new file mode 100644 index 000000000..5b1fee96f --- /dev/null +++ b/addons/api/tests/unit/adapters/host-catalog-test.js @@ -0,0 +1,12 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Adapter | host catalog', function (hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function (assert) { + let adapter = this.owner.lookup('adapter:host-catalog'); + assert.ok(adapter); + }); +}); From e0989fffe3b22f2292ceba3790bfa91a25bf5a39 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Mon, 14 Feb 2022 17:10:54 -0500 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20missing=20adap?= =?UTF-8?q?ter=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/adapters/host-catalog.js | 39 +++++++--------- .../tests/unit/adapters/host-catalog-test.js | 44 +++++++++++++++++-- 2 files changed, 57 insertions(+), 26 deletions(-) diff --git a/addons/api/addon/adapters/host-catalog.js b/addons/api/addon/adapters/host-catalog.js index 52d30fe6f..3f1fab706 100644 --- a/addons/api/addon/adapters/host-catalog.js +++ b/addons/api/addon/adapters/host-catalog.js @@ -1,28 +1,23 @@ import ApplicationAdapter from './application'; -import { serializeIntoHash } from '@ember-data/adapter/-private'; export default class HostCatalogAdapter extends ApplicationAdapter { /** - Called by the store when a newly created record is - saved via the `save` method on a model record instance. - - The `createRecord` method serializes the record and makes an Ajax (HTTP POST) request - to a URL computed by `buildURL`. - - See `serialize` for information on how to customize the serialized form - of a record. - - @method createRecord - @param {Store} store - @param {Model} type - @param {Snapshot} snapshot - @return {Promise} promise - */ - createRecord(store, type, snapshot) { - let url = this.buildURL(type.modelName, null, snapshot, 'createRecord'); - const pluginName = snapshot.attr('plugin')?.name; - if (pluginName) url = `${url}?plugin_name=${pluginName}`; - const data = serializeIntoHash(store, type, snapshot); - return this.ajax(url, 'POST', { data }); + * Overrides the default `buildURL` such that on _create_, URLs for records of + * type `plugin` get an extra query parameter `?plugin_name`. All other + * request types behave as normal. + * + * @override + * @param {string} modelName + * @param {string} id + * @return {string} url + * @return {string} requestType + */ + buildURL(modelName, id, snapshot, requestType) { + let url = super.buildURL(...arguments); + if (requestType === 'createRecord') { + const pluginName = snapshot.attr('plugin')?.name; + if (pluginName) url = `${url}?plugin_name=${pluginName}`; + } + return url; } } diff --git a/addons/api/tests/unit/adapters/host-catalog-test.js b/addons/api/tests/unit/adapters/host-catalog-test.js index 5b1fee96f..2df45a117 100644 --- a/addons/api/tests/unit/adapters/host-catalog-test.js +++ b/addons/api/tests/unit/adapters/host-catalog-test.js @@ -4,9 +4,45 @@ import { setupTest } from 'ember-qunit'; module('Unit | Adapter | host catalog', function (hooks) { setupTest(hooks); - // Replace this with your real tests. - test('it exists', function (assert) { - let adapter = this.owner.lookup('adapter:host-catalog'); - assert.ok(adapter); + test('it generates correct createRecord URLs for static host catalogs', function (assert) { + assert.expect(1); + const scopeID = 'o_1'; + const mockSnapshot = { + adapterOptions: { + scopeID, + }, + attr() { + return null; + }, + }; + const adapter = this.owner.lookup('adapter:host-catalog'); + const createRecordURL = adapter.buildURL( + 'host-catalog', + null, + mockSnapshot, + 'createRecord' + ); + assert.equal(createRecordURL, '/v1/host-catalogs'); + }); + + test('it generates correct createRecord URLs for dynamic host catalogs with extra query parameter ?plugin_name', function (assert) { + assert.expect(1); + const scopeID = 'o_1'; + const mockSnapshot = { + adapterOptions: { + scopeID, + }, + attr() { + return { name: 'aws' }; + }, + }; + const adapter = this.owner.lookup('adapter:host-catalog'); + const createRecordURL = adapter.buildURL( + 'host-catalog', + null, + mockSnapshot, + 'createRecord' + ); + assert.equal(createRecordURL, '/v1/host-catalogs?plugin_name=aws'); }); });