Skip to content

Commit

Permalink
[Fastboot] Integration testing for fastboot (#1045)
Browse files Browse the repository at this point in the history
* Add session register back

* Create a fastboot test app and create basic tests against fastboot.
  • Loading branch information
josemarluedke authored and marcoow committed Sep 7, 2016
1 parent 6a341d6 commit 2bd16a7
Show file tree
Hide file tree
Showing 29 changed files with 346 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ install:
script:
# Usually, it's ok to finish the test scenario without reverting
# to the addon's original dependency state, skipping "cleanup".
- ember try:one $EMBER_TRY_SCENARIO test --skip-cleanup
- ember try:one $EMBER_TRY_SCENARIO test --skip-cleanup && npm run fastboot-nodetest

notifications:
email: false
Expand Down
1 change: 1 addition & 0 deletions addon/initializers/setup-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export default function setupSession(registry) {
registry.register(store, Ephemeral);
}

inject(registry, store, 'cookies', 'service:cookies');
inject(registry, 'session:main', 'store', store);
}
9 changes: 9 additions & 0 deletions addon/session-stores/adaptive.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export default Base.extend({
*/
localStorageKey: 'ember_simple_auth-session',

/**
The cookie store injected by initializers/setup-session.
This is necessary for the adaptive session store because it and only it
creates an instance of another store, bypassing the Ember registry.
Because the created store does not come from the registry, it has no
container and you cannot call getOwner within it.
*/
cookies: null,

/**
The domain to use for the cookie if `localStorage` is not available, e.g.,
"example.com", ".example.com" (which includes all subdomains) or
Expand Down
34 changes: 34 additions & 0 deletions node-tests/fastboot-runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* global require, process */
var glob = require('glob');
var Mocha = require('mocha');

var root = 'node-tests/fastboot';

var mocha = new Mocha({
timeout: 5000,
reporter: 'spec'
});

var testFiles = glob.sync(root + '/**/*-test.js');

addFiles(mocha, testFiles);

try {
runMocha();
} catch (error) {
console.error(error);
process.exit(1);
}

function addFiles(mocha, files) {
files = (typeof files === 'string') ? glob.sync(root + files) : files;
files.forEach(mocha.addFile.bind(mocha));
}

function runMocha() {
mocha.run(function(failures) {
process.on('exit', function() {
process.exit(failures);
});
});
}
72 changes: 72 additions & 0 deletions node-tests/fastboot/basic-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* global describe, before, after, it */
/* jshint node:true */
var chai = require('chai');
var expect = chai.expect;
var RSVP = require('rsvp');
var AddonTestApp = require('ember-cli-addon-tests').AddonTestApp;
var request = require('request');
var request = RSVP.denodeify(request);

describe('Basic tests', function() {
this.timeout(600000);

var app;
var url = 'http://localhost:49741';

before(function() {
app = new AddonTestApp();
return app.create('fastboot-app', { fixturesPath: 'node-tests/fixtures/' })
.then(addDependencies)
.then(function() {
return app.startServer({
command: 'fastboot',
additionalArguments: ['--host 0.0.0.0']
});
});
});

after(function() {
return app.stopServer();
});

it('redirects to login page on a protected route', function() {
return request({ url: url + '/protected' })
.then(function(response) {

expect(response.headers['set-cookie']).to.deep.equal(['ember_simple_auth-session=%7B%22authenticated%22%3A%7B%7D%7D; path=/'])
expect(response.req.path).to.equal('/login');
});
});

it('authenticates and create the session when accessing authenticate route', function() {
return request({ url: url + '/authenticate', jar: true })
.then(function(response) {

expect(response.headers['set-cookie']).to.deep.equal(['ember_simple_auth-session=%7B%22authenticated%22%3A%7B%22authenticator%22%3A%22authenticator%3Aauth%22%2C%22account_id%22%3A%22123%22%7D%7D; path=/'])
expect(response.req.path).to.equal('/');
});
});

it('user can access protected page after authentication', function() {
return request({ url: url + '/protected', jar: true })
.then(function(response) {
expect(response.req.path).to.equal('/protected');
expect(response.body).to.contain('This is a protected page only visible to authenticated users!');
});
});

it('invalidate removes session cookies', function() {
return request({ url: url + '/invalidate', jar: true })
.then(function(response) {
expect(response.req.path).to.equal('/invalidate');
expect(response.headers['set-cookie']).to.deep.equal(['ember_simple_auth-session=%7B%22authenticated%22%3A%7B%7D%7D; path=/'])
});
});
});

function addDependencies(app) {
app.editPackageJSON(function(pkg) {
pkg['devDependencies']['ember-cli-fastboot'] = "^1.0.0-beta.8";
});
return app.run('npm', 'install');
}
20 changes: 20 additions & 0 deletions node-tests/fixtures/fastboot-app/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Ember from 'ember';
import Resolver from './resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

const { Application } = Ember;

let App;

Ember.MODEL_FACTORY_INJECTIONS = true;

App = Application.extend({
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
Resolver
});

loadInitializers(App, config.modulePrefix);

export default App;
25 changes: 25 additions & 0 deletions node-tests/fixtures/fastboot-app/app/authenticators/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Ember from 'ember';
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';

const { RSVP: { resolve, reject }, inject: { service } } = Ember;

export default BaseAuthenticator.extend({
session: service(),

authenticate() {
return resolve({ account_id: '123' });
},

invalidate() {
return resolve();
},

restore(data) {
if (this.get('session.shouldError')) {
return reject();
} else {
return resolve(data);
}
}

});
Empty file.
Empty file.
11 changes: 11 additions & 0 deletions node-tests/fixtures/fastboot-app/app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Ember from 'ember';

const { Controller } = Ember;

export default Controller.extend({
actions: {
transitionToLoginRoute() {
this.transitionToRoute('login');
}
}
});
25 changes: 25 additions & 0 deletions node-tests/fixtures/fastboot-app/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Dummy</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">

{{content-for "head"}}

<link rel="stylesheet" href="assets/vendor.css">
<link rel="stylesheet" href="assets/dummy.css">

{{content-for "head-footer"}}
</head>
<body>
{{content-for "body"}}

<script src="assets/vendor.js"></script>
<script src="assets/dummy.js"></script>

{{content-for "body-footer"}}
</body>
</html>
3 changes: 3 additions & 0 deletions node-tests/fixtures/fastboot-app/app/resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Resolver from 'ember-resolver';

export default Resolver;
17 changes: 17 additions & 0 deletions node-tests/fixtures/fastboot-app/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Ember from 'ember';
import config from './config/environment';

const { Router: EmberRouter } = Ember;

const Router = EmberRouter.extend({
location: config.locationType
});

Router.map(function() {
this.route('login');
this.route('protected');
this.route('authenticate');
this.route('invalidate');
});

export default Router;
Empty file.
8 changes: 8 additions & 0 deletions node-tests/fixtures/fastboot-app/app/routes/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

const { Route } = Ember;

export default Route.extend(ApplicationRouteMixin, {

});
11 changes: 11 additions & 0 deletions node-tests/fixtures/fastboot-app/app/routes/authenticate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Ember from 'ember';

const { Route, inject: { service } } = Ember;

export default Route.extend({
session: service('session'),

model() {
this.get('session').authenticate('authenticator:auth');
}
});
12 changes: 12 additions & 0 deletions node-tests/fixtures/fastboot-app/app/routes/invalidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

const { Route, inject: { service } } = Ember;

export default Route.extend(AuthenticatedRouteMixin, {
session: service(),

model() {
this.get('session').invalidate();
}
});
6 changes: 6 additions & 0 deletions node-tests/fixtures/fastboot-app/app/routes/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Ember from 'ember';
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';

const { Route } = Ember;

export default Route.extend(UnauthenticatedRouteMixin);
7 changes: 7 additions & 0 deletions node-tests/fixtures/fastboot-app/app/routes/protected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

const { Route } = Ember;

export default Route.extend(AuthenticatedRouteMixin, {
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Cookie from 'ember-simple-auth/session-stores/cookie';

export default Cookie.extend();
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="container">
{{outlet}}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{outlet}}
Empty file.
1 change: 1 addition & 0 deletions node-tests/fixtures/fastboot-app/app/templates/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Ember Simple Auth example app</h1>
4 changes: 4 additions & 0 deletions node-tests/fixtures/fastboot-app/app/templates/login.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="page-header">
<h1>Login</h1>
</div>
{{login-form}}
6 changes: 6 additions & 0 deletions node-tests/fixtures/fastboot-app/app/templates/protected.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="page-header">
<h1>Protected Page</h1>
</div>
<div class="alert alert-warning">
This is a protected page only visible to authenticated users!
</div>
50 changes: 50 additions & 0 deletions node-tests/fixtures/fastboot-app/config/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* jshint node: true */

module.exports = function(environment) {
var ENV = {
modulePrefix: 'fastboot-app',
environment: environment,
rootURL: '/',
locationType: 'auto',
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
// e.g. 'with-controller': true
}
},

APP: {
// Here you can pass flags/options to your application instance
// when it is created
},

fastboot: {
hostWhitelist:[/^localhost:\d+$/]
},
};

if (environment === 'development') {
// ENV.APP.LOG_RESOLVER = true;
// ENV.APP.LOG_ACTIVE_GENERATION = true;
// ENV.APP.LOG_TRANSITIONS = true;
// ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
// ENV.APP.LOG_VIEW_LOOKUPS = true;
}

if (environment === 'test') {
// Testem prefers this...
ENV.locationType = 'none';

// keep test console output quieter
ENV.APP.LOG_ACTIVE_GENERATION = false;
ENV.APP.LOG_VIEW_LOOKUPS = false;

ENV.APP.rootElement = '#ember-testing';
}

if (environment === 'production') {

}

return ENV;
};
Loading

0 comments on commit 2bd16a7

Please sign in to comment.