From cf5a6257402b664fc056f90eb0217262c22493be Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Mon, 14 Sep 2020 10:45:56 -0400 Subject: [PATCH 01/11] feat: Generate account model in api addon --- addons/api/addon/generated/models/account.js | 46 ++++++++++++++++++++ addons/api/addon/models/account.js | 3 ++ addons/api/app/models/account.js | 1 + addons/api/tests/unit/models/account-test.js | 13 ++++++ 4 files changed, 63 insertions(+) create mode 100644 addons/api/addon/generated/models/account.js create mode 100644 addons/api/addon/models/account.js create mode 100644 addons/api/app/models/account.js create mode 100644 addons/api/tests/unit/models/account-test.js diff --git a/addons/api/addon/generated/models/account.js b/addons/api/addon/generated/models/account.js new file mode 100644 index 000000000..1abfe3e3f --- /dev/null +++ b/addons/api/addon/generated/models/account.js @@ -0,0 +1,46 @@ +import BaseModel from '../../models/base'; +import { attr } from '@ember-data/model'; + +/** + * Account contains all fields related to an account resource + */ +export default class GeneratedAccountModel extends BaseModel { + + // =attributes + + @attr('string', { + description: 'The type of the resource, to help differentiate schemas' + }) type; + + @attr('string', { + description: 'The owning auth method ID.', + }) auth_method_id; + + @attr('string', { + description: 'Optional name for identification purposes', + }) + name; + + @attr('string', { + description: 'Optional user-set description for identification purposes', + }) + description; + + @attr('date', { + description: 'The time this resource was created\nOutput only.', + readOnly: true, + }) + created_time; + + @attr('date', { + description: 'The time this resource was last updated\nOutput only.', + readOnly: true, + }) + updated_time; + + @attr('number', { + description: 'Current version number of this resource.', + }) + version; + +} diff --git a/addons/api/addon/models/account.js b/addons/api/addon/models/account.js new file mode 100644 index 000000000..0e61e3aa5 --- /dev/null +++ b/addons/api/addon/models/account.js @@ -0,0 +1,3 @@ +import GeneratedAccountModel from '../generated/models/account'; + +export default class AccountModel extends GeneratedAccountModel {} diff --git a/addons/api/app/models/account.js b/addons/api/app/models/account.js new file mode 100644 index 000000000..83b737fe4 --- /dev/null +++ b/addons/api/app/models/account.js @@ -0,0 +1 @@ +export { default } from 'api/models/account'; diff --git a/addons/api/tests/unit/models/account-test.js b/addons/api/tests/unit/models/account-test.js new file mode 100644 index 000000000..8dcfecdde --- /dev/null +++ b/addons/api/tests/unit/models/account-test.js @@ -0,0 +1,13 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Model | account', function(hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function(assert) { + let store = this.owner.lookup('service:store'); + let model = store.createRecord('account', {}); + assert.ok(model); + }); +}); From 879989a127b7e9e84f89716b186061a8400e2a9c Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Mon, 14 Sep 2020 11:26:54 -0400 Subject: [PATCH 02/11] feat: Mock accounts in mirage --- ui/core/mirage/factories/account.js | 3 +++ ui/core/mirage/factories/auth-method.js | 16 +++++++++++++++- ui/core/mirage/generated/factories/account.js | 16 ++++++++++++++++ ui/core/mirage/models/account.js | 8 ++++++++ ui/core/mirage/scenarios/default.js | 2 +- ui/core/mirage/serializers/account.js | 5 +++++ 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 ui/core/mirage/factories/account.js create mode 100644 ui/core/mirage/generated/factories/account.js create mode 100644 ui/core/mirage/models/account.js create mode 100644 ui/core/mirage/serializers/account.js diff --git a/ui/core/mirage/factories/account.js b/ui/core/mirage/factories/account.js new file mode 100644 index 000000000..5908d0a21 --- /dev/null +++ b/ui/core/mirage/factories/account.js @@ -0,0 +1,3 @@ +import factory from '../generated/factories/account'; + +export default factory.extend(); diff --git a/ui/core/mirage/factories/auth-method.js b/ui/core/mirage/factories/auth-method.js index aa15a1103..8c75d95df 100644 --- a/ui/core/mirage/factories/auth-method.js +++ b/ui/core/mirage/factories/auth-method.js @@ -1,3 +1,17 @@ import factory from '../generated/factories/auth-method'; +import { trait } from 'ember-cli-mirage'; -export default factory.extend(); +export default factory.extend({ + id: (i) => `auth-method-id-${i}` + + /** + * Adds accounts. + */ + withAccounts: trait({ + afterCreate(authMethod, server) { + const { scope } = authMethod; + server.createList('account', 5, { scope }); + } + }) + +}); diff --git a/ui/core/mirage/generated/factories/account.js b/ui/core/mirage/generated/factories/account.js new file mode 100644 index 000000000..a53c6aefe --- /dev/null +++ b/ui/core/mirage/generated/factories/account.js @@ -0,0 +1,16 @@ +import { Factory } from 'ember-cli-mirage'; +import { random, date } from 'faker'; + +/** + * GeneratedAccountFactory + */ +export default Factory.extend({ + + type: 'password', + name: () => random.words(), + description: () => random.words(), + created_time: () => date.recent(), + updated_time: () => date.recent(), + version: () => random.number(), + +}); diff --git a/ui/core/mirage/models/account.js b/ui/core/mirage/models/account.js new file mode 100644 index 000000000..e6d3e303c --- /dev/null +++ b/ui/core/mirage/models/account.js @@ -0,0 +1,8 @@ +import { Model, belongsTo } from 'ember-cli-mirage'; + +export default Model.extend({ + + scope: belongsTo(), + authMethod: belongsTo() + +}); diff --git a/ui/core/mirage/scenarios/default.js b/ui/core/mirage/scenarios/default.js index 1524a7647..9492f92e3 100644 --- a/ui/core/mirage/scenarios/default.js +++ b/ui/core/mirage/scenarios/default.js @@ -15,7 +15,7 @@ export default function(server) { // Auth server.createList('auth-method', 3, { scope: globalScope }); - server.createList('auth-method', 3, { scope: orgScope }); + server.createList('auth-method', 3, { scope: orgScope }, 'withAccounts'); // Groups and Users server.createList('group', 5, { scope: orgScope }, 'withMembers'); diff --git a/ui/core/mirage/serializers/account.js b/ui/core/mirage/serializers/account.js new file mode 100644 index 000000000..3a5f8ae35 --- /dev/null +++ b/ui/core/mirage/serializers/account.js @@ -0,0 +1,5 @@ +import ApplicationSerializer from './application'; + +export default ApplicationSerializer.extend({ + modelName: 'account' +}); From a3060e089dd05fff3a05120771bbe2ba9ea67b85 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Mon, 14 Sep 2020 11:45:48 -0400 Subject: [PATCH 03/11] feat: Add account attributes to account model --- addons/api/addon/models/account.js | 11 ++++++++- .../models/fragment-account-attributes.js | 13 +++++++++++ .../addon/models/fragment-host-attributes.js | 1 + .../app/models/fragment-account-attributes.js | 1 + addons/api/tests/unit/models/account-test.js | 11 +++++++++ ui/core/mirage/config.js | 23 +++++++++++++++++++ 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 addons/api/addon/models/fragment-account-attributes.js create mode 100644 addons/api/app/models/fragment-account-attributes.js diff --git a/addons/api/addon/models/account.js b/addons/api/addon/models/account.js index 0e61e3aa5..76321d86d 100644 --- a/addons/api/addon/models/account.js +++ b/addons/api/addon/models/account.js @@ -1,3 +1,12 @@ import GeneratedAccountModel from '../generated/models/account'; +import { fragment } from 'ember-data-model-fragments/attributes'; -export default class AccountModel extends GeneratedAccountModel {} +export default class AccountModel extends GeneratedAccountModel { + + /** + * Attributes of this resource, if any, represented as a JSON fragment. + * @type {FragmentAccountAttributesModel} + */ + @fragment('fragment-account-attributes', { defaultValue: {} }) attributes; + +} diff --git a/addons/api/addon/models/fragment-account-attributes.js b/addons/api/addon/models/fragment-account-attributes.js new file mode 100644 index 000000000..ee2281f18 --- /dev/null +++ b/addons/api/addon/models/fragment-account-attributes.js @@ -0,0 +1,13 @@ +import Fragment from 'ember-data-model-fragments/fragment'; +import { attr } from '@ember-data/model'; + +export default class FragmentAccountAttributesModel extends Fragment { + + @attr('string', { + description: 'The account login name' + }) login_name; + + @attr('string', { + description: 'The account password' + }) password; +} diff --git a/addons/api/addon/models/fragment-host-attributes.js b/addons/api/addon/models/fragment-host-attributes.js index 114da4bca..851b404b2 100644 --- a/addons/api/addon/models/fragment-host-attributes.js +++ b/addons/api/addon/models/fragment-host-attributes.js @@ -6,4 +6,5 @@ export default class FragmentHostAttributesModel extends Fragment { @attr('string', { description: 'The address (DNS or IP name) used to reach the host' }) address; + } diff --git a/addons/api/app/models/fragment-account-attributes.js b/addons/api/app/models/fragment-account-attributes.js new file mode 100644 index 000000000..8d0d8f53f --- /dev/null +++ b/addons/api/app/models/fragment-account-attributes.js @@ -0,0 +1 @@ +export { default } from 'api/models/fragment-account-attributes'; diff --git a/addons/api/tests/unit/models/account-test.js b/addons/api/tests/unit/models/account-test.js index 8dcfecdde..4237ae6b6 100644 --- a/addons/api/tests/unit/models/account-test.js +++ b/addons/api/tests/unit/models/account-test.js @@ -10,4 +10,15 @@ module('Unit | Model | account', function(hooks) { let model = store.createRecord('account', {}); assert.ok(model); }); + + test('it contains attributes', function(assert) { + let store = this.owner.lookup('service:store'); + let model = store.createRecord('account', { + type: 'password', + attributes: { + login_name: 'loginname' + } + }); + assert.equal(model.attributes.login_name, 'loginname'); + }); }); diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index 3b9e24cce..225a896ba 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -49,6 +49,29 @@ export default function() { this.patch('/auth-methods/:id'); this.del('/auth-methods/:id'); + // Auth Method Accounts + this.get('/accounts', ({ accounts }, { queryParams: { scope_id: scopeId } }) => { + return accounts.where({ scopeId }); + }); + this.post('/accounts'); + this.get('/accounts/:id'); + this.patch('/accounts/:id'); + this.del('/accounts/:id'); + this.post('/accounts/:idMethod', function ({ accounts }, { params: { idMethod } }) { + const attrs = this.normalizedRequestAttrs(); + const id = idMethod.split(':')[0]; + const method = idMethod.split(':')[1]; + const account = accounts.find(id); + const updatedAttrs = { + version: attrs.version + }; + // Set new password + if (method === 'set-password') { + updatedAttrs.password = accounts.attributes.password; + } + return account.update(updatedAttrs); + }); + // Authenticate/deauthenticate routes this.post('/auth-methods/:id_method', authHandler); this.post('/scopes/:id_method', deauthHandler); From 85aedacde4197353624242ee7c39e1507f482ba1 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Mon, 14 Sep 2020 12:00:21 -0400 Subject: [PATCH 04/11] fix: Fix missing comma lint issue --- ui/core/mirage/factories/auth-method.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/core/mirage/factories/auth-method.js b/ui/core/mirage/factories/auth-method.js index 8c75d95df..25cca40c1 100644 --- a/ui/core/mirage/factories/auth-method.js +++ b/ui/core/mirage/factories/auth-method.js @@ -2,7 +2,7 @@ import factory from '../generated/factories/auth-method'; import { trait } from 'ember-cli-mirage'; export default factory.extend({ - id: (i) => `auth-method-id-${i}` + id: (i) => `auth-method-id-${i}`, /** * Adds accounts. From 252dbaa75b1d0f9fc28a4e3191f7d7620665978a Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Mon, 14 Sep 2020 15:50:29 -0400 Subject: [PATCH 05/11] feat: Add account associations in mirage --- ui/core/mirage/config.js | 4 ++-- ui/core/mirage/factories/auth-method.js | 4 ++-- ui/core/mirage/generated/factories/account.js | 1 + ui/core/mirage/models/auth-method.js | 5 +++-- ui/core/mirage/serializers/account.js | 8 +++++++- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index 225a896ba..62cb242b7 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -50,8 +50,8 @@ export default function() { this.del('/auth-methods/:id'); // Auth Method Accounts - this.get('/accounts', ({ accounts }, { queryParams: { scope_id: scopeId } }) => { - return accounts.where({ scopeId }); + this.get('/accounts', ({ accounts }, { queryParams: { auth_method_id: authMethodId } }) => { + return accounts.where({ authMethodId }); }); this.post('/accounts'); this.get('/accounts/:id'); diff --git a/ui/core/mirage/factories/auth-method.js b/ui/core/mirage/factories/auth-method.js index 25cca40c1..80254614a 100644 --- a/ui/core/mirage/factories/auth-method.js +++ b/ui/core/mirage/factories/auth-method.js @@ -5,12 +5,12 @@ export default factory.extend({ id: (i) => `auth-method-id-${i}`, /** - * Adds accounts. + * Adds accounts to auth method. */ withAccounts: trait({ afterCreate(authMethod, server) { const { scope } = authMethod; - server.createList('account', 5, { scope }); + server.createList('account', 5, { scope, authMethod }); } }) diff --git a/ui/core/mirage/generated/factories/account.js b/ui/core/mirage/generated/factories/account.js index a53c6aefe..a66184e1f 100644 --- a/ui/core/mirage/generated/factories/account.js +++ b/ui/core/mirage/generated/factories/account.js @@ -12,5 +12,6 @@ export default Factory.extend({ created_time: () => date.recent(), updated_time: () => date.recent(), version: () => random.number(), + attributes: () => ({ login_name: random.words() }), }); diff --git a/ui/core/mirage/models/auth-method.js b/ui/core/mirage/models/auth-method.js index 08b1f5ad3..878b8bd8a 100644 --- a/ui/core/mirage/models/auth-method.js +++ b/ui/core/mirage/models/auth-method.js @@ -1,7 +1,8 @@ -import { Model, belongsTo } from 'ember-cli-mirage'; +import { Model, belongsTo, hasMany} from 'ember-cli-mirage'; export default Model.extend({ - scope: belongsTo() + scope: belongsTo(), + accounts: hasMany('account') }); diff --git a/ui/core/mirage/serializers/account.js b/ui/core/mirage/serializers/account.js index 3a5f8ae35..7c6186756 100644 --- a/ui/core/mirage/serializers/account.js +++ b/ui/core/mirage/serializers/account.js @@ -1,5 +1,11 @@ import ApplicationSerializer from './application'; export default ApplicationSerializer.extend({ - modelName: 'account' + modelName: 'account', + + _hashForModel(model) { + const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments); + json.auth_method_id = model.authMethodId; + return json; + } }); From dc68dfad85ee9bdc4ac18a93b43d52b385b53397 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Tue, 15 Sep 2020 11:00:00 -0400 Subject: [PATCH 06/11] test: Add missing account serializer test Delete account password attribute in mirage to mimic API exactly --- addons/api/tests/unit/models/account-test.js | 11 ------ .../tests/unit/serializers/account-test.js | 38 +++++++++++++++++++ ui/core/mirage/serializers/account.js | 1 + 3 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 addons/api/tests/unit/serializers/account-test.js diff --git a/addons/api/tests/unit/models/account-test.js b/addons/api/tests/unit/models/account-test.js index 4237ae6b6..dcd3fe8bb 100644 --- a/addons/api/tests/unit/models/account-test.js +++ b/addons/api/tests/unit/models/account-test.js @@ -4,21 +4,10 @@ import { setupTest } from 'ember-qunit'; module('Unit | Model | account', function(hooks) { setupTest(hooks); - // Replace this with your real tests. test('it exists', function(assert) { let store = this.owner.lookup('service:store'); let model = store.createRecord('account', {}); assert.ok(model); }); - test('it contains attributes', function(assert) { - let store = this.owner.lookup('service:store'); - let model = store.createRecord('account', { - type: 'password', - attributes: { - login_name: 'loginname' - } - }); - assert.equal(model.attributes.login_name, 'loginname'); - }); }); diff --git a/addons/api/tests/unit/serializers/account-test.js b/addons/api/tests/unit/serializers/account-test.js new file mode 100644 index 000000000..9ab5b521c --- /dev/null +++ b/addons/api/tests/unit/serializers/account-test.js @@ -0,0 +1,38 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Serializer | account', function(hooks) { + setupTest(hooks); + + test('it serializes account', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('account'); + const record = store.createRecord('account', { + type: 'password', + name: 'Name', + description: 'Description', + attributes: { + login_name: 'Login Name', + password: 'Password', + }, + auth_method_id: '1', + version: 1 + }); + const snapshot = record._createSnapshot(); + snapshot.adapterOptions = {}; + const serializedRecord = serializer.serialize(snapshot); + assert.deepEqual(serializedRecord, { + type: 'password', + name: 'Name', + description: 'Description', + attributes: { + login_name: 'Login Name', + password: 'Password', + }, + auth_method_id: '1', + version: 1 + }); + }); + +}); diff --git a/ui/core/mirage/serializers/account.js b/ui/core/mirage/serializers/account.js index 7c6186756..85d38851b 100644 --- a/ui/core/mirage/serializers/account.js +++ b/ui/core/mirage/serializers/account.js @@ -6,6 +6,7 @@ export default ApplicationSerializer.extend({ _hashForModel(model) { const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments); json.auth_method_id = model.authMethodId; + delete json.attributes?.password; return json; } }); From 5643db1eabe61a5738c25c35db156bd0af59fcc2 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Tue, 15 Sep 2020 12:25:13 -0400 Subject: [PATCH 07/11] fix: Mirage set-password should use attributes for update --- ui/core/mirage/config.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index 62cb242b7..f3a7cda5f 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -63,11 +63,12 @@ export default function() { const method = idMethod.split(':')[1]; const account = accounts.find(id); const updatedAttrs = { - version: attrs.version + version: attrs.version, + attributes: {} }; // Set new password if (method === 'set-password') { - updatedAttrs.password = accounts.attributes.password; + updatedAttrs.attributes.password = attrs.password; } return account.update(updatedAttrs); }); From d9353bdc3b3164961702f8646da6a40ef47a6793 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Tue, 15 Sep 2020 16:51:07 -0400 Subject: [PATCH 08/11] fix: Remove password attribute from account model --- addons/api/addon/models/account.js | 25 +++++++ .../models/fragment-account-attributes.js | 3 - addons/api/addon/serializers/account.js | 39 +++++++++++ addons/api/app/serializers/account.js | 1 + .../tests/unit/serializers/account-test.js | 67 +++++++++++++++++-- 5 files changed, 126 insertions(+), 9 deletions(-) create mode 100644 addons/api/addon/serializers/account.js create mode 100644 addons/api/app/serializers/account.js diff --git a/addons/api/addon/models/account.js b/addons/api/addon/models/account.js index 76321d86d..4c16a34a4 100644 --- a/addons/api/addon/models/account.js +++ b/addons/api/addon/models/account.js @@ -9,4 +9,29 @@ export default class AccountModel extends GeneratedAccountModel { */ @fragment('fragment-account-attributes', { defaultValue: {} }) attributes; + // =methods + + /** + * Save account password via the `set-password` method. + * See serializer and adapter for more information. + * @param {string} password + * @param {object} options + * @param {object} options.adapterOptions + * @return {Promise} + */ + savePassword(password, options={ adapterOptions: {} }) { + const defaultAdapterOptions = { + method: 'set-password', + serializeWithNewPassword: true, + password + }; + return this.save({ + ...options, + adapterOptions: { + ...defaultAdapterOptions, + ...options.adapterOptions + } + }); + } + } diff --git a/addons/api/addon/models/fragment-account-attributes.js b/addons/api/addon/models/fragment-account-attributes.js index ee2281f18..76ee82487 100644 --- a/addons/api/addon/models/fragment-account-attributes.js +++ b/addons/api/addon/models/fragment-account-attributes.js @@ -7,7 +7,4 @@ export default class FragmentAccountAttributesModel extends Fragment { description: 'The account login name' }) login_name; - @attr('string', { - description: 'The account password' - }) password; } diff --git a/addons/api/addon/serializers/account.js b/addons/api/addon/serializers/account.js new file mode 100644 index 000000000..acad9cb7e --- /dev/null +++ b/addons/api/addon/serializers/account.js @@ -0,0 +1,39 @@ +import ApplicationSerializer from './application'; +import { get } from '@ember/object'; + +export default class AccountSerializer extends ApplicationSerializer { + + // =methods + + /** + * If `adapterOptions.serializeWithNewPassword` is true, the serialization should + * include **password** and the version. + * If `adapterOptions.password` is set, the serialization should + * include **password** in it's `attributes`. + * @override + * @param {Snapshot} snapshot + * @return {object} + */ + serialize(snapshot) { + const password = get(snapshot, 'adapterOptions.password'); + const serializeWithNewPassword = get(snapshot, 'adapterOptions.serializeWithNewPassword'); + let serialized = super.serialize(...arguments); + if (password) serialized.attributes.password = password; + if (serializeWithNewPassword) serialized = this.serializeWithNewPassword(snapshot, password) + return serialized; + } + + /** + * Returns a payload containing only new password. + * @param {Snapshot} snapshot + * @param {string} password + * @return {object} + */ + serializeWithNewPassword(snapshot, password) { + return { + version: snapshot.attr('version'), + password, + }; + } + +} diff --git a/addons/api/app/serializers/account.js b/addons/api/app/serializers/account.js new file mode 100644 index 000000000..af9b2cc1c --- /dev/null +++ b/addons/api/app/serializers/account.js @@ -0,0 +1 @@ +export { default } from 'api/serializers/account'; diff --git a/addons/api/tests/unit/serializers/account-test.js b/addons/api/tests/unit/serializers/account-test.js index 9ab5b521c..7a523d9dd 100644 --- a/addons/api/tests/unit/serializers/account-test.js +++ b/addons/api/tests/unit/serializers/account-test.js @@ -4,19 +4,18 @@ import { setupTest } from 'ember-qunit'; module('Unit | Serializer | account', function(hooks) { setupTest(hooks); - test('it serializes account', function (assert) { + test('it serializes normally', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('account'); const record = store.createRecord('account', { type: 'password', name: 'Name', + auth_method_id: '1', description: 'Description', attributes: { login_name: 'Login Name', - password: 'Password', }, - auth_method_id: '1', version: 1 }); const snapshot = record._createSnapshot(); @@ -25,14 +24,70 @@ module('Unit | Serializer | account', function(hooks) { assert.deepEqual(serializedRecord, { type: 'password', name: 'Name', + auth_method_id: '1', description: 'Description', attributes: { - login_name: 'Login Name', - password: 'Password', + login_name: 'Login Name' }, - auth_method_id: '1', version: 1 }); }); + test('it serializes password attribute when `adapterOptions.password` is true', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('account'); + const record = store.createRecord('account', { + type: 'password', + name: 'Name', + auth_method_id: '1', + description: 'Description', + attributes: { + login_name: 'Login Name', + }, + version: 1 + }); + const snapshot = record._createSnapshot(); + snapshot.adapterOptions = { + password: 'Password' + }; + const serializedRecord = serializer.serialize(snapshot); + assert.deepEqual(serializedRecord, { + type: 'password', + name: 'Name', + auth_method_id: '1', + description: 'Description', + attributes: { + login_name: 'Login Name', + password: 'Password' + }, + version: 1 + }); + }); + + test('it serializes only password when `adapterOptions.serializeWithNewPassword` is true', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('account'); + const record = store.createRecord('account', { + type: 'password', + name: 'Name', + description: 'Description', + attributes: { + login_name: 'Login Name' + }, + auth_method_id: '1', + version: 1 + }); + const snapshot = record._createSnapshot(); + snapshot.adapterOptions = { + serializeWithNewPassword: true, + password: 'Password' + }; + const serializedRecord = serializer.serialize(snapshot); + assert.deepEqual(serializedRecord, { + password: 'Password', + version: 1 + }); + }); }); From b6c91ede402e93baf09cdebd1130746cc6085c7a Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Tue, 15 Sep 2020 17:02:36 -0400 Subject: [PATCH 09/11] test: Remove inverse relation in auth-method --- ui/core/mirage/config.js | 2 +- ui/core/mirage/models/auth-method.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ui/core/mirage/config.js b/ui/core/mirage/config.js index f3a7cda5f..7f2ea5ba0 100644 --- a/ui/core/mirage/config.js +++ b/ui/core/mirage/config.js @@ -64,7 +64,7 @@ export default function() { const account = accounts.find(id); const updatedAttrs = { version: attrs.version, - attributes: {} + attributes: account.attributes }; // Set new password if (method === 'set-password') { diff --git a/ui/core/mirage/models/auth-method.js b/ui/core/mirage/models/auth-method.js index 878b8bd8a..3aebcb472 100644 --- a/ui/core/mirage/models/auth-method.js +++ b/ui/core/mirage/models/auth-method.js @@ -2,7 +2,6 @@ import { Model, belongsTo, hasMany} from 'ember-cli-mirage'; export default Model.extend({ - scope: belongsTo(), - accounts: hasMany('account') + scope: belongsTo() }); From 6549e094c1703210507e6cea19dbcbb76e1a1bc4 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Tue, 15 Sep 2020 17:06:33 -0400 Subject: [PATCH 10/11] fix: Remove unused 'hasMany' import in auth-method mirage --- ui/core/mirage/models/auth-method.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/core/mirage/models/auth-method.js b/ui/core/mirage/models/auth-method.js index 3aebcb472..08b1f5ad3 100644 --- a/ui/core/mirage/models/auth-method.js +++ b/ui/core/mirage/models/auth-method.js @@ -1,4 +1,4 @@ -import { Model, belongsTo, hasMany} from 'ember-cli-mirage'; +import { Model, belongsTo } from 'ember-cli-mirage'; export default Model.extend({ From 72e5ad601c114d34497e7df862a1fb648ef4b0e8 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Tue, 15 Sep 2020 18:11:23 -0400 Subject: [PATCH 11/11] refactor: Remove serializeWithNewPassword adapteroptions --- addons/api/addon/models/account.js | 3 +-- addons/api/addon/serializers/account.js | 16 ++++++++-------- .../api/tests/unit/serializers/account-test.js | 8 ++++++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/addons/api/addon/models/account.js b/addons/api/addon/models/account.js index 4c16a34a4..4c52347f5 100644 --- a/addons/api/addon/models/account.js +++ b/addons/api/addon/models/account.js @@ -19,10 +19,9 @@ export default class AccountModel extends GeneratedAccountModel { * @param {object} options.adapterOptions * @return {Promise} */ - savePassword(password, options={ adapterOptions: {} }) { + setPassword(password, options={ adapterOptions: {} }) { const defaultAdapterOptions = { method: 'set-password', - serializeWithNewPassword: true, password }; return this.save({ diff --git a/addons/api/addon/serializers/account.js b/addons/api/addon/serializers/account.js index acad9cb7e..c014c2361 100644 --- a/addons/api/addon/serializers/account.js +++ b/addons/api/addon/serializers/account.js @@ -1,13 +1,12 @@ import ApplicationSerializer from './application'; -import { get } from '@ember/object'; export default class AccountSerializer extends ApplicationSerializer { // =methods /** - * If `adapterOptions.serializeWithNewPassword` is true, the serialization should - * include **password** and the version. + * If `adapterOptions.method` is `set-password`, the serialization should + * include only **password** and the version. * If `adapterOptions.password` is set, the serialization should * include **password** in it's `attributes`. * @override @@ -15,11 +14,12 @@ export default class AccountSerializer extends ApplicationSerializer { * @return {object} */ serialize(snapshot) { - const password = get(snapshot, 'adapterOptions.password'); - const serializeWithNewPassword = get(snapshot, 'adapterOptions.serializeWithNewPassword'); + const password = snapshot?.adapterOptions?.password; let serialized = super.serialize(...arguments); - if (password) serialized.attributes.password = password; - if (serializeWithNewPassword) serialized = this.serializeWithNewPassword(snapshot, password) + if (password && snapshot?.record?.isNew) serialized.attributes.password = password; + if (snapshot?.adapterOptions?.method === 'set-password') { + serialized = this.serializeForSetPassword(snapshot, password); + } return serialized; } @@ -29,7 +29,7 @@ export default class AccountSerializer extends ApplicationSerializer { * @param {string} password * @return {object} */ - serializeWithNewPassword(snapshot, password) { + serializeForSetPassword(snapshot, password) { return { version: snapshot.attr('version'), password, diff --git a/addons/api/tests/unit/serializers/account-test.js b/addons/api/tests/unit/serializers/account-test.js index 7a523d9dd..85f9b655f 100644 --- a/addons/api/tests/unit/serializers/account-test.js +++ b/addons/api/tests/unit/serializers/account-test.js @@ -65,7 +65,11 @@ module('Unit | Serializer | account', function(hooks) { }); }); - test('it serializes only password when `adapterOptions.serializeWithNewPassword` is true', function (assert) { + test('it does not serialize password for non-new records', function (assert) { + assert.expect(0); + }); + + test('it serializes only password when `adapterOptions.method` is set to `set-password`', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store'); const serializer = store.serializerFor('account'); @@ -81,7 +85,7 @@ module('Unit | Serializer | account', function(hooks) { }); const snapshot = record._createSnapshot(); snapshot.adapterOptions = { - serializeWithNewPassword: true, + method: 'set-password', password: 'Password' }; const serializedRecord = serializer.serialize(snapshot);