Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
Remove usage of Ember.get()
  • Loading branch information
paulcwatts committed Dec 23, 2020
1 parent ef51b82 commit 98080a6
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 45 deletions.
8 changes: 3 additions & 5 deletions addon-test-support/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getContext } from '@ember/test-helpers';
import { getProperties, set } from '@ember/object';
import { set } from '@ember/object';
import { MockUser } from './utils/ember-cognito';
import { typeOf } from '@ember/utils';
import MockAuth from './utils/-mock-auth';
Expand All @@ -8,8 +8,7 @@ import { CognitoUser as AWSUser, CognitoUserPool } from 'amazon-cognito-identity
export function newUser(username) {
const { owner } = getContext();
const cognito = owner.lookup('service:cognito');
const { poolId = 'us-east-1_TEST', clientId = 'TEST' } =
getProperties(cognito, "poolId", "clientId");
const { poolId = 'us-east-1_TEST', clientId = 'TEST' } = cognito;
const pool = new CognitoUserPool({ UserPoolId: poolId, ClientId: clientId });
return new AWSUser({ Username: username, Pool: pool });
}
Expand All @@ -19,8 +18,7 @@ export function mockCognitoUser(userAttributes) {

const { username } = userAttributes;
const cognito = owner.lookup('service:cognito');
const { poolId = 'us-east-1_TEST', clientId = 'TEST' } =
getProperties(cognito, "poolId", "clientId");
const { poolId = 'us-east-1_TEST', clientId = 'TEST' } = cognito;
const pool = new CognitoUserPool({ UserPoolId: poolId, ClientId: clientId });
const user = new AWSUser({ Username: username, Pool: pool });
const auth = MockAuth.create({ _authenticatedUser: user });
Expand Down
3 changes: 1 addition & 2 deletions tests/acceptance/login-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { setupApplicationTest } from 'ember-qunit';
import { module, test } from 'qunit';
import { get } from '@ember/object';
import { click, currentURL, fillIn, visit } from '@ember/test-helpers';
import { currentSession } from 'ember-simple-auth/test-support';
import { mockCognitoUser } from 'ember-cognito/test-support';
Expand All @@ -25,7 +24,7 @@ module('Acceptance | login', function(hooks) {
await click('.login-form [type=submit]');

let session = await currentSession();
assert.equal(get(session, 'isAuthenticated'), true);
assert.equal(session.isAuthenticated, true);
assert.equal(currentURL(), '/');
});
});
40 changes: 20 additions & 20 deletions tests/unit/authenticators/cognito-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get, set } from '@ember/object';
import { set } from '@ember/object';
import { setupTest } from 'ember-qunit';
import { module, test } from 'qunit';
import config from '../../../config/environment';
Expand All @@ -10,9 +10,9 @@ module('Unit | Authenticator | cognito', function(hooks) {

test('config is set correctly', function(assert) {
let service = this.owner.lookup('authenticator:cognito');
assert.equal(get(service, 'poolId'), 'us-east-1_TEST');
assert.equal(get(service, 'clientId'), 'TEST');
assert.equal(get(service, 'authenticationFlowType'), config.cognito.authenticationFlowType);
assert.equal(service.poolId, 'us-east-1_TEST');
assert.equal(service.clientId, 'TEST');
assert.equal(service.authenticationFlowType, config.cognito.authenticationFlowType);
});

test('restore', async function(assert) {
Expand All @@ -26,9 +26,9 @@ module('Unit | Authenticator | cognito', function(hooks) {
assert.equal(resolvedData.poolId, 'us-east-1_TEST');
assert.equal(resolvedData.clientId, 'TEST');
assert.ok(resolvedData.access_token.startsWith('header.'));
assert.ok(get(service, 'cognito.user'), 'The cognito service user is populated.');
assert.equal(get(service, 'cognito.user.username'), 'testuser', 'The username is set correctly.');
assert.notOk(get(service, 'task'), 'No task was scheduled.');
assert.ok(service.cognito.user, 'The cognito service user is populated.');
assert.equal(service.cognito.user.username, 'testuser', 'The username is set correctly.');
assert.notOk(service.task, 'No task was scheduled.');
});

test('restore no current user', async function(assert) {
Expand All @@ -51,8 +51,8 @@ module('Unit | Authenticator | cognito', function(hooks) {

const data = { poolId: 'us-east-1_TEST', clientId: 'TEST' };
await service.restore(data);
assert.ok(get(service, 'cognito.task') !== undefined, 'Refresh timer was scheduled.');
let taskDuration = get(service, 'cognito._taskDuration');
assert.ok(service.cognito.task !== undefined, 'Refresh timer was scheduled.');
let taskDuration = service.cognito._taskDuration;
assert.ok(taskDuration > (1000 * 1000));
});

Expand All @@ -66,9 +66,9 @@ module('Unit | Authenticator | cognito', function(hooks) {
const data = await service.authenticate({ username: 'testuser', password: 'password' });
assert.equal(data.poolId, 'us-east-1_TEST');
assert.equal(data.clientId, 'TEST');
assert.ok(get(service, 'cognito.user'), 'The cognito service user is populated.');
assert.equal(get(service, 'cognito.user.username'), 'testuser', 'The username is set correctly.');
assert.notOk(get(service, 'cognito.task'), 'Refresh session task not set.');
assert.ok(service.cognito.user, 'The cognito service user is populated.');
assert.equal(service.cognito.user.username, 'testuser', 'The username is set correctly.');
assert.notOk(service.cognito.task, 'Refresh session task not set.');
});

test('authenticateUser, failure', async function(assert) {
Expand Down Expand Up @@ -107,8 +107,8 @@ module('Unit | Authenticator | cognito', function(hooks) {
let data = await service.authenticate({ password: 'newPassword', state });
assert.equal(data.poolId, 'us-east-1_TEST');
assert.equal(data.clientId, 'TEST');
assert.ok(get(service, 'cognito.user'), 'The cognito service user is populated.');
assert.equal(get(service, 'cognito.user.username'), 'testuser', 'The username is set correctly.');
assert.ok(service.cognito.user, 'The cognito service user is populated.');
assert.equal(service.cognito.user.username, 'testuser', 'The username is set correctly.');
});

test('authenticateUser, newPasswordRequired failure', async function(assert) {
Expand Down Expand Up @@ -149,9 +149,9 @@ module('Unit | Authenticator | cognito', function(hooks) {
await mockAuth(MockAuth.create({ _authenticatedUser: user }));

await service.authenticate({ username: 'testuser', password: 'password' });
const task = get(service, 'cognito.task');
const task = service.cognito.task;
assert.notEqual(task, undefined, 'Refresh session task is set.');
let taskDuration = get(service, 'cognito._taskDuration');
let taskDuration = service.cognito._taskDuration;
assert.ok(taskDuration > (1000 * 1000));
});

Expand All @@ -165,10 +165,10 @@ module('Unit | Authenticator | cognito', function(hooks) {
assert.equal(data.poolId, 'us-east-1_TEST');
assert.equal(data.clientId, 'TEST');
assert.ok(data.access_token.startsWith('header.'));
assert.ok(get(service, 'cognito.user'), 'The cognito service user is populated.');
const task = get(service, 'cognito.task');
assert.ok(service.cognito.user, 'The cognito service user is populated.');
const task = service.cognito.task;
assert.notEqual(task, undefined, 'Refresh session task is set.');
let taskDuration = get(service, 'cognito._taskDuration');
let taskDuration = service.cognito._taskDuration;
assert.ok(taskDuration > (1000 * 1000));
});

Expand All @@ -183,6 +183,6 @@ module('Unit | Authenticator | cognito', function(hooks) {
let resolvedData = await service.invalidate(data);
assert.deepEqual(data, resolvedData);
// Cognito user no longer exists on service
assert.equal(get(service, 'cognito.user'), undefined);
assert.equal(service.cognito.user, undefined);
});
});
11 changes: 5 additions & 6 deletions tests/unit/services/cognito-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { get } from '@ember/object';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { CognitoUserAttribute } from 'amazon-cognito-identity-js';
Expand All @@ -12,9 +11,9 @@ module('Unit | Service | cognito', function(hooks) {

test('config is set correctly', function(assert) {
const service = this.owner.lookup('service:cognito');
assert.equal(get(service, 'poolId'), 'us-east-1_TEST');
assert.equal(get(service, 'clientId'), 'TEST');
assert.equal(get(service, 'authenticationFlowType'), config.cognito.authenticationFlowType);
assert.equal(service.poolId, 'us-east-1_TEST');
assert.equal(service.clientId, 'TEST');
assert.equal(service.authenticationFlowType, config.cognito.authenticationFlowType);
});

test('signup works', async function(assert) {
Expand Down Expand Up @@ -76,11 +75,11 @@ module('Unit | Service | cognito', function(hooks) {
const validation = [{ 'foo': 'bar' }];

await service.signUp('testuser', 'password', attrs, validation);
assert.deepEqual(get(auth, 'attributes'), {
assert.deepEqual(auth.attributes, {
email: 'test@email.com',
phone_number: '555-1212'
});
assert.deepEqual(get(auth, 'validationData'), [{ 'foo': 'bar' }]);
assert.deepEqual(auth.validationData, [{ 'foo': 'bar' }]);
});

test('getIdToken auth', async function(assert) {
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/utils/cognito-user-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { get } from '@ember/object';
import { CognitoUserAttribute } from 'amazon-cognito-identity-js';
import { resolve } from 'rsvp';
import { setupTest } from 'ember-qunit';
Expand All @@ -13,7 +12,7 @@ module('Unit | Utility | cognito user', function(hooks) {

test('username', function(assert) {
const user = CognitoUser.create({ user: newUser('testuser') });
assert.equal(get(user, 'username'), 'testuser');
assert.equal(user.username, 'testuser');
});

test('changePassword', async function(assert) {
Expand Down
11 changes: 5 additions & 6 deletions tests/unit/utils/mock-helpers-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { get } from '@ember/object';
import { mockAuth, MockAuth, mockCognitoUser, newUser, unmockCognitoUser } from 'ember-cognito/test-support';

module('Unit | Utility | mock helpers', function(hooks) {
Expand All @@ -15,14 +14,14 @@ module('Unit | Utility | mock helpers', function(hooks) {
]
});
let cognito = this.owner.lookup('service:cognito');
assert.deepEqual(get(cognito, 'user.userAttributes'), [
assert.deepEqual(cognito.user.userAttributes, [
{ name: 'sub', value: 'aaaabbbb-cccc-dddd-eeee-ffffgggghhhh' },
{ name: 'email', value: 'testuser@gmail.com' }
]);

await unmockCognitoUser();
assert.notOk(get(cognito, 'user'));
assert.equal(get(cognito, 'auth._authenticatedUser.username'), 'testuser');
assert.notOk(cognito.user);
assert.equal(cognito.auth._authenticatedUser.username, 'testuser');
});

test('newUser', async function(assert) {
Expand All @@ -33,12 +32,12 @@ module('Unit | Utility | mock helpers', function(hooks) {
test('mockAuth can accept an auth class', async function(assert) {
await mockAuth(MockAuth.extend({ foo: 'bar' }));
const cognito = this.owner.lookup('service:cognito');
assert.equal(get(cognito, 'auth.foo'), 'bar');
assert.equal(cognito.auth.foo, 'bar');
});

test('mockAuth can accept an auth instance', async function(assert) {
await mockAuth(MockAuth.create({ bar: 'baz' }));
const cognito = this.owner.lookup('service:cognito');
assert.equal(get(cognito, 'auth.bar'), 'baz');
assert.equal(cognito.auth.bar, 'baz');
});
});
7 changes: 3 additions & 4 deletions tests/unit/utils/mock-user-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { module, test } from 'qunit';
import { CognitoUserAttribute } from 'amazon-cognito-identity-js';
import { MockUser } from 'ember-cognito/test-support/utils/ember-cognito';
import { get } from '@ember/object';

module('Unit | Utility | mock user', function() {
test('groups', async function(assert) {
Expand All @@ -20,7 +19,7 @@ module('Unit | Utility | mock user', function() {
});

await user.updateAttributes([newAttr]);
assert.deepEqual(get(user, 'userAttributes'), [
assert.deepEqual(user.userAttributes, [
{ name: 'family_name', value: 'Coltrane' }
]);
});
Expand All @@ -39,7 +38,7 @@ module('Unit | Utility | mock user', function() {
});

await user.updateAttributes([newAttr]);
assert.deepEqual(get(user, 'userAttributes'), [
assert.deepEqual(user.userAttributes, [
{ name: 'given_name', value: 'John' },
{ name: 'family_name', value: 'New Trane' }
]);
Expand All @@ -53,7 +52,7 @@ module('Unit | Utility | mock user', function() {
]
});
await user.updateAttributes({ family_name: "New Trane" });
assert.deepEqual(get(user, 'userAttributes'), [
assert.deepEqual(user.userAttributes, [
{ name: 'given_name', value: 'John' },
{ name: 'family_name', value: 'New Trane' }
]);
Expand Down

0 comments on commit 98080a6

Please sign in to comment.