Skip to content

Commit 1eeece9

Browse files
refactor: 💡 move url building logic to core + add unit test
✅ Closes: https://hashicorp.atlassian.net/browse/ICU-17231
1 parent 9930878 commit 1eeece9

File tree

7 files changed

+91
-12
lines changed

7 files changed

+91
-12
lines changed

‎addons/auth/addon/authenticators/base.js‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import SimpleAuthBaseAuthenticator from 'ember-simple-auth/authenticators/base';
77
import { resolve, reject } from 'rsvp';
88
import { waitForPromise } from '@ember/test-waiters';
9-
import { service } from '@ember/service';
109

1110
/**
1211
* Encapsulates common authenticator functionality.
@@ -20,10 +19,6 @@ import { service } from '@ember/service';
2019
* All other responses should resolve the session restoration successfully.
2120
*/
2221
export default class BaseAuthenticator extends SimpleAuthBaseAuthenticator {
23-
// =services
24-
25-
@service store;
26-
2722
// =unimplemented methods
2823

2924
/**
@@ -56,6 +51,14 @@ export default class BaseAuthenticator extends SimpleAuthBaseAuthenticator {
5651
*/
5752
buildTokenValidationEndpointURL(/* tokenID */) {}
5853

54+
/**
55+
* Generates an account URL used to retrieve authenticated account.
56+
* @override
57+
* @param {string} accountID
58+
* @return {string}
59+
*/
60+
buildAccountEndpointURL(/* accountID */) {}
61+
5962
// =methods
6063

6164
/**
@@ -116,15 +119,12 @@ export default class BaseAuthenticator extends SimpleAuthBaseAuthenticator {
116119
data.isOrg = data?.scope?.type === 'org';
117120

118121
try {
119-
const adapter = this.store.adapterFor('application');
120-
const findAccountRecordURL = adapter.buildURL(
121-
'account',
122+
const accountFindRecordURL = this.buildAccountEndpointURL(
122123
data?.account_id,
123-
{},
124-
'findRecord',
125124
);
125+
126126
const response = await waitForPromise(
127-
fetch(findAccountRecordURL, {
127+
fetch(accountFindRecordURL, {
128128
method: 'get',
129129
headers: { Authorization: `Bearer ${data.attributes.token}` },
130130
}),

‎addons/auth/tests/dummy/app/authenticators/base.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ export default class ApplicationBaseAuthenticator extends BaseAuthenticator {
1313
buildDeauthEndpointURL() {
1414
return '/deauthenticate';
1515
}
16+
17+
buildAccountEndpointURL(id) {
18+
return `/account/${id}`;
19+
}
1620
}

‎addons/auth/tests/unit/authenticators/base-test.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,34 @@ module('Unit | Authenticator | base', function (hooks) {
8888
assert.ok(true, 'restoration failed');
8989
}
9090
});
91+
92+
test('it normalizes authenticated data correctly', async function (assert) {
93+
assert.expect(6);
94+
const account_id = 'account_123';
95+
const token = 'token123';
96+
const scope = { id: 'global', type: 'global' };
97+
const mockData = {
98+
scope,
99+
id: 'auth_123',
100+
attributes: { account_id, token },
101+
};
102+
const account = { id: account_id, attributes: { email: '[email protected]' } };
103+
const authenticator = this.owner.lookup('authenticator:base');
104+
105+
server.get(authenticator.buildAccountEndpointURL(account_id), () => {
106+
assert.ok(true, 'account data was requested');
107+
return [200, account, JSON.stringify(account)];
108+
});
109+
110+
const normalizedData = await authenticator.normalizeData(mockData);
111+
112+
assert.true(normalizedData.isGlobal);
113+
assert.false(normalizedData.isOrg);
114+
assert.strictEqual(normalizedData.username, account.attributes.email);
115+
assert.strictEqual(
116+
normalizedData.account_id,
117+
mockData.attributes.account_id,
118+
);
119+
assert.strictEqual(normalizedData.token, mockData.attributes.token);
120+
});
91121
});

‎addons/core/addon/authenticators/oidc.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ export default class OIDCAuthenticator extends BaseOIDCAuthenticator {
5656
return adapter.buildURL('auth-token', tokenID, {}, 'findRecord');
5757
}
5858

59+
/**
60+
* Generates an account URL used to retrieve authenticated account.
61+
* @override
62+
* @param {string} accountID
63+
* @return {string}
64+
*/
65+
buildAccountEndpointURL(accountID) {
66+
const adapter = this.store.adapterFor('application');
67+
return adapter.buildURL('account', accountID, {}, 'findRecord');
68+
}
69+
5970
/**
6071
* Intercepts the authenticate response, if any, and assigns the returned
6172
* token to all future requests via `addTokenToAuthorization`.

‎addons/core/addon/authenticators/password.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ export default class PasswordAuthenticator extends BasePasswordAuthenticator {
5858
return adapter.buildURL('auth-token', tokenID, {}, 'findRecord');
5959
}
6060

61+
/**
62+
* Generates an account URL used to retrieve authenticated account.
63+
* @override
64+
* @param {string} accountID
65+
* @return {string}
66+
*/
67+
buildAccountEndpointURL(accountID) {
68+
const adapter = this.store.adapterFor('application');
69+
return adapter.buildURL('account', accountID, {}, 'findRecord');
70+
}
71+
6172
/**
6273
* Intercepts the authenticate response, if any, and assigns the returned
6374
* token to all future requests via `addTokenToAuthorization`.

‎ui/admin/app/styles/app.scss‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,3 +1022,18 @@
10221022
.dropdown-checkbox-list {
10231023
margin-bottom: 1rem;
10241024
}
1025+
1026+
// User menu dropdown in global side nav
1027+
.user-menu-dropdown {
1028+
max-width: 125px;
1029+
1030+
&__toggle-button {
1031+
width: 100%;
1032+
1033+
.hds-dropdown-toggle-button__text {
1034+
white-space: nowrap;
1035+
overflow: hidden;
1036+
text-overflow: ellipsis;
1037+
}
1038+
}
1039+
}

‎ui/admin/app/templates/application.hbs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,24 @@
6060

6161
<Hds::Dropdown
6262
@enableCollisionDetection={{true}}
63+
class='user-menu-dropdown'
6364
data-test-side-nav-user-dropdown
6465
as |dd|
6566
>
66-
<dd.ToggleIcon @icon='user' @text={{t 'titles.user-menu'}} />
6767
{{#if this.session.data.authenticated.username}}
68+
<dd.ToggleButton
69+
@icon='user'
70+
@text={{this.session.data.authenticated.username}}
71+
{{hds-tooltip this.session.data.authenticated.username}}
72+
class='user-menu-dropdown__toggle-button'
73+
/>
6874
<dd.Title @text={{t 'titles.authenticated'}} />
6975
<dd.Description
7076
@text={{this.session.data.authenticated.username}}
7177
/>
7278
<dd.Separator />
79+
{{else}}
80+
<dd.ToggleIcon @icon='user' @text={{t 'titles.user-menu'}} />
7381
{{/if}}
7482

7583
<dd.Interactive @route='account.change-password'>{{t

0 commit comments

Comments
 (0)