Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize tests #2013

Merged
merged 8 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion addon/mixins/application-route-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,12 @@ export default Mixin.create({
if (this.get('_isFastBoot')) {
this.transitionTo(Configuration.rootURL);
} else {
window.location.replace(Configuration.rootURL);
this._refresh();
}
}
},

_refresh() {
window.location.replace(Configuration.rootURL);
}
});
3 changes: 3 additions & 0 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module.exports = function(defaults) {
sourcemaps: {
enabled: true,
extensions: ['js']
},
'ember-cli-babel': {
includePolyfill: true
}
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"ember-fastboot-addon-tests": "^0.5.0",
"ember-fetch": "^6.7.2",
"ember-load-initializers": "^2.0.0",
"ember-mocha": "^0.15.1",
"ember-resolver": "^6.0.0",
"ember-sinon": "~3.1.0",
"ember-source": "~3.14.0",
Expand Down
4 changes: 4 additions & 0 deletions tests/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ module.exports = {
'simplabs/configs/ember-mocha',
'simplabs/plugins/mocha',
],
parserOptions: {
ecmaVersion: 2017,
sourceType: 'module'
},
};
65 changes: 26 additions & 39 deletions tests/acceptance/authentication-test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { tryInvoke } from '@ember/utils';
import {
currentURL,
setupContext,
setupApplicationContext,
teardownApplicationContext,
teardownContext,
visit
} from '@ember/test-helpers';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import {
describe,
it,
beforeEach,
afterEach
} from 'mocha';
import { setupApplicationTest } from 'ember-mocha';
import { expect } from 'chai';
import Pretender from 'pretender';
import {
Expand All @@ -24,17 +20,11 @@ import {
import config from '../../config/environment';

describe('Acceptance: Authentication', function() {
let context;
setupApplicationTest();
let server;

beforeEach(function() {
context = {};
return setupContext(context).then(() => setupApplicationContext(context));
});

afterEach(function() {
tryInvoke(server, 'shutdown');
return teardownApplicationContext(context).then(() => teardownContext(context));
});

describe('the protected route', function() {
Expand All @@ -43,26 +33,25 @@ describe('Acceptance: Authentication', function() {
return;
}

it('cannot be visited when the session is not authenticated', function() {
return invalidateSession()
.then(() => visit('/protected'))
.then(() => {
expect(currentURL()).to.eq('/login');
});
it('cannot be visited when the session is not authenticated', async function() {
await invalidateSession();
await visit('/protected');

expect(currentURL()).to.eq('/login');
});

it('can be visited when the session is authenticated', function() {
it('can be visited when the session is authenticated', async function() {
server = new Pretender(function() {
this.get(`${config.apiHost}/posts`, () => [200, { 'Content-Type': 'application/json' }, '{"data":[]}']);
});
return authenticateSession({ userId: 1, otherData: 'some-data' })
.then(() => visit('/protected'))
.then(() => {
let session = currentSession();
expect(currentURL()).to.eq('/protected');
expect(session.get('data.authenticated.userId')).to.eql(1);
expect(session.get('data.authenticated.otherData')).to.eql('some-data');
});

await authenticateSession({ userId: 1, otherData: 'some-data' });
await visit('/protected');

let session = currentSession();
expect(currentURL()).to.eq('/protected');
expect(session.get('data.authenticated.userId')).to.eql(1);
expect(session.get('data.authenticated.otherData')).to.eql('some-data');
});
});

Expand All @@ -72,20 +61,18 @@ describe('Acceptance: Authentication', function() {
return;
}

it('can be visited when the session is not authenticated', function() {
return invalidateSession()
.then(() => visit('/login'))
.then(() => {
expect(currentURL()).to.eq('/login');
});
it('can be visited when the session is not authenticated', async function() {
await invalidateSession();
await visit('/login');

expect(currentURL()).to.eq('/login');
});

it('cannot be visited when the session is authenticated', function() {
return authenticateSession()
.then(() => visit('/login'))
.then(() => {
expect(currentURL()).to.eq('/');
});
it('cannot be visited when the session is authenticated', async function() {
await authenticateSession();
await visit('/login');

expect(currentURL()).to.eq('/');
});
});
});
12 changes: 0 additions & 12 deletions tests/helpers/create-with-container.js

This file was deleted.

30 changes: 18 additions & 12 deletions tests/unit/authenticators/base-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,35 @@ describe('BaseAuthenticator', () => {
});

describe('#restore', function() {
it('returns a rejecting promise', function(done) {
authenticator.restore().catch(() => {
it('returns a rejecting promise', async function() {
try {
await authenticator.restore();
expect(false).to.be.true;
} catch (_error) {
expect(true).to.be.true;
done();
});
}
});
});

describe('#authenticate', function() {
it('returns a rejecting promise', function(done) {
authenticator.authenticate().catch(() => {
it('returns a rejecting promise', async function() {
try {
await authenticator.authenticate();
expect(false).to.be.true;
} catch (_error) {
expect(true).to.be.true;
done();
});
}
});
});

describe('#invalidate', function() {
it('returns a resolving promise', function(done) {
authenticator.invalidate().then(() => {
it('returns a resolving promise', async function() {
try {
await authenticator.invalidate();
expect(true).to.be.true;
done();
});
} catch (_error) {
expect(false).to.be.true;
}
});
});
});
104 changes: 55 additions & 49 deletions tests/unit/authenticators/devise-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ describe('DeviseAuthenticator', () => {

describe('#restore', function() {
describe('when the data contains a token and email', function() {
it('resolves with the correct data', function(done) {
authenticator.restore({ token: 'secret token!', email: '[email protected]' }).then((content) => {
expect(content).to.eql({ token: 'secret token!', email: '[email protected]' });
done();
});
it('resolves with the correct data', async function() {
let content = await authenticator.restore({ token: 'secret token!', email: '[email protected]' });

expect(content).to.eql({ token: 'secret token!', email: '[email protected]' });
});
});

Expand All @@ -37,10 +36,10 @@ describe('DeviseAuthenticator', () => {
authenticator = Devise.extend({ tokenAttributeName: 'employee.token', identificationAttributeName: 'employee.email' }).create();
});

it('resolves with the correct data', function() {
return authenticator.restore({ employee: { token: 'secret token!', email: '[email protected]' } }).then((content) => {
expect(content).to.eql({ employee: { token: 'secret token!', email: '[email protected]' } });
});
it('resolves with the correct data', async function() {
let content = await authenticator.restore({ employee: { token: 'secret token!', email: '[email protected]' } });

expect(content).to.eql({ employee: { token: 'secret token!', email: '[email protected]' } });
});
});
});
Expand All @@ -50,46 +49,49 @@ describe('DeviseAuthenticator', () => {
server.post('/users/sign_in', () => [201, { 'Content-Type': 'application/json' }, '{ "token": "secret token!", "email": "[email protected]" }']);
});

it('sends an AJAX request to the sign in endpoint', function() {
return authenticator.authenticate('identification', 'password').then(() => {
let [request] = server.handledRequests;
it('sends an AJAX request to the sign in endpoint', async function() {
await authenticator.authenticate('identification', 'password');
let [request] = server.handledRequests;

expect(request.url).to.eql('/users/sign_in');
expect(request.method).to.eql('POST');
expect(JSON.parse(request.requestBody)).to.eql({ user: { email: 'identification', password: 'password' } });
expect(request.requestHeaders['content-type']).to.eql('application/json');
expect(request.requestHeaders.accept).to.eql('application/json');
});
expect(request.url).to.eql('/users/sign_in');
expect(request.method).to.eql('POST');
expect(JSON.parse(request.requestBody)).to.eql({ user: { email: 'identification', password: 'password' } });
expect(request.requestHeaders['content-type']).to.eql('application/json');
expect(request.requestHeaders.accept).to.eql('application/json');
});

describe('when the authentication request is successful', function() {
beforeEach(function() {
server.post('/users/sign_in', () => [201, { 'Content-Type': 'application/json' }, '{ "token": "secret token!", "email": "[email protected]" }']);
});

it('resolves with the correct data', function(done) {
authenticator.authenticate('[email protected]', 'password').then((data) => {
expect(true).to.be.true;
expect(data).to.eql({ token: 'secret token!', email: '[email protected]' });
done();
});
it('resolves with the correct data', async function() {
let data = await authenticator.authenticate('[email protected]', 'password');

expect(data).to.eql({ token: 'secret token!', email: '[email protected]' });
});

describe('when the server returns incomplete data', function() {
it('fails when token is missing', function() {
it('fails when token is missing', async function() {
server.post('/users/sign_in', () => [201, { 'Content-Type': 'application/json' }, '{ "email": "[email protected]" }']);

return authenticator.authenticate('[email protected]', 'password').catch((error) => {
try {
await authenticator.authenticate('[email protected]', 'password');
marcoow marked this conversation as resolved.
Show resolved Hide resolved
expect(false).to.be.true;
} catch (error) {
expect(error).to.eql('Check that server response includes token and email');
});
}
});

it('fails when identification is missing', function() {
it('fails when identification is missing', async function() {
server.post('/users/sign_in', () => [201, { 'Content-Type': 'application/json' }, '{ "token": "secret token!" }']);

return authenticator.authenticate('[email protected]', 'password').catch((error) => {
try {
await authenticator.authenticate('[email protected]', 'password');
expect(false).to.be.true;
} catch (error) {
expect(error).to.eql('Check that server response includes token and email');
});
}
});
});
});
Expand All @@ -99,14 +101,17 @@ describe('DeviseAuthenticator', () => {
server.post('/users/sign_in', () => [400, { 'Content-Type': 'application/json', 'X-Custom-Context': 'foobar' }, '{ "error": "invalid_grant" }']);
});

it('rejects with the response', function() {
return authenticator.authenticate('username', 'password').catch((response) => {
it('rejects with the response', async function() {
try {
await authenticator.authenticate('username', 'password');
expect(false).to.be.true;
} catch (response) {
expect(response.ok).to.be.false;
});
}
});
});

it('can customize the ajax request', function() {
it('can customize the ajax request', async function() {
server.put('/login', () => [201, { 'Content-Type': 'application/json' }, '{ "token": "secret token!", "email": "[email protected]" }']);

authenticator = Devise.extend({
Expand All @@ -115,32 +120,33 @@ describe('DeviseAuthenticator', () => {
}
}).create();

return authenticator.authenticate('identification', 'password').then(() => {
let [request] = server.handledRequests;
await authenticator.authenticate('identification', 'password');

expect(request.url).to.eql('/login');
expect(request.method).to.eql('PUT');
});
let [request] = server.handledRequests;

expect(request.url).to.eql('/login');
expect(request.method).to.eql('PUT');
});

it('can handle a resp with the namespace of the resource name', function(done) {
it('can handle a resp with the namespace of the resource name', async function() {
server.post('/users/sign_in', () => [201, { 'Content-Type': 'application/json' }, '{ "user": { "token": "secret token!", "email": "[email protected]" } }']);

authenticator.authenticate('[email protected]', 'password').then((data) => {
expect(true).to.be.true;
expect(data).to.eql({ token: 'secret token!', email: '[email protected]' });
done();
});
let data = await authenticator.authenticate('[email protected]', 'password');

expect(true).to.be.true;
expect(data).to.eql({ token: 'secret token!', email: '[email protected]' });
});

});

describe('#invalidate', function() {
it('returns a resolving promise', function(done) {
authenticator.invalidate().then(() => {
it('returns a resolving promise', async function() {
try {
await authenticator.invalidate();
expect(true).to.be.true;
done();
});
} catch (_error) {
expect(false).to.be.true;
}
});
});
});
Loading