Skip to content

Commit

Permalink
Add blueprints for authorizers and authenticators
Browse files Browse the repository at this point in the history
  • Loading branch information
quaertym authored and marcoow committed Feb 2, 2016
1 parent dc47894 commit f1144d9
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 3 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ bower.json
ember-cli-build.js
Brocfile.js
testem.json
node-tests
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ install:
- bower install

script:
- ember try $EMBER_TRY_SCENARIO test
- ember try $EMBER_TRY_SCENARIO test && npm run nodetest

notifications:
email: false
Expand Down
1 change: 1 addition & 0 deletions app/blueprint-tests/authenticator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-simple-auth/blueprint-tests/authenticator';
1 change: 1 addition & 0 deletions app/blueprint-tests/authorizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-simple-auth/blueprint-tests/authorizer';
7 changes: 7 additions & 0 deletions blueprints/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"predef": [
"console"
],
"strict": false,
"node": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= imports %>

export default <%= baseClass %>.extend({<%= body %>
});
49 changes: 49 additions & 0 deletions blueprints/authenticator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var EOL = require('os').EOL;
var isPackageMissing = require('ember-cli-is-package-missing');

module.exports = {
description: 'Generates an Ember Simple Auth authenticator',

availableOptions: [
{ name: 'torii', type: Boolean },
{ name: 'devise', type: Boolean },
{ name: 'oauth2', type: Boolean },
{ name: 'base', type: Boolean }
],

locals: function(options) {
if (options.torii) {
return {
imports: 'import Ember from \'ember\';' + EOL + 'import Torii from \'ember-simple-auth/authenticators/torii\';',
baseClass: 'Torii',
body: EOL + ' torii: Ember.inject.service(\'torii\')'
};
} else if (options.oauth2) {
return {
imports: 'import Ember from \'ember\';' + EOL + 'import OAuth2PasswordGrant from \'ember-simple-auth/authenticators/oauth2-password-grant\';',
baseClass: 'OAuth2PasswordGrant',
body: ''
};
} else if (options.devise) {
return {
imports: 'import Ember from \'ember\';' + EOL + 'import Devise from \'ember-simple-auth/authenticators/devise\';',
baseClass: 'Devise',
body: ''
};
} else {
return {
imports: 'import Ember from \'ember\';' + EOL + 'import Base from \'ember-simple-auth/authenticators/base\';',
baseClass: 'Base',
body: EOL + ' restore(data) {' + EOL + ' },' + EOL + EOL + ' authenticate(/*args*/) {' + EOL + ' },' + EOL + EOL + ' invalidate(data) {' + EOL + ' }'
};
}
},

afterInstall: function(options) {
if (!options.dryRun && options.torii && isPackageMissing(this, 'torii')) {
return this.addPackagesToProject([
{ name: 'torii', target: '~0.6.1' }
]);
}
}
};
4 changes: 4 additions & 0 deletions blueprints/authorizer/files/__root__/authorizers/__name__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= imports %>

export default <%= baseClass %>.extend({<%= body %>
});
33 changes: 33 additions & 0 deletions blueprints/authorizer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var EOL = require('os').EOL;

module.exports = {
description: 'Generates an Ember Simple Auth authorizer',

availableOptions: [
{ name: 'devise', type: Boolean },
{ name: 'oauth2', type: Boolean },
{ name: 'base', type: Boolean }
],

locals: function(options) {
if (options.oauth2) {
return {
imports: 'import OAuth2Bearer from \'ember-simple-auth/authorizers/oauth2-bearer\';',
baseClass: 'OAuth2Bearer',
body: ''
};
} else if (options.devise) {
return {
imports: 'import Devise from \'ember-simple-auth/authorizers/devise\';',
baseClass: 'Devise',
body: ''
};
} else {
return {
imports: 'import Base from \'ember-simple-auth/authorizers/base\';',
baseClass: 'Base',
body: EOL + ' authorize(/*data, block*/) {' + EOL + ' }'
};
}
}
};
76 changes: 76 additions & 0 deletions node-tests/blueprints/authenticator-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

var EOL = require('os').EOL;
var setupTestHooks = require('ember-cli-blueprint-test-helpers/lib/helpers/setup');
var BlueprintHelpers = require('ember-cli-blueprint-test-helpers/lib/helpers/blueprint-helper');
var generateAndDestroy = BlueprintHelpers.generateAndDestroy;

describe('Acceptance: ember generate and destroy authenticator', function() {
setupTestHooks(this);

it('generates a torii authenticator', function() {
return generateAndDestroy(['authenticator', 'application', '--torii'], {
files: [
{ file: 'app/authenticators/application.js', contains: ['\
import Ember from \'ember\';' + EOL + '\
import Torii from \'ember-simple-auth/authenticators/torii\';' + EOL + '\
' + EOL + '\
export default Torii.extend({' + EOL + '\
torii: Ember.inject.service(\'torii\')' + EOL + '\
});' + EOL + '\
']}
]
});
});

it('generates an OAuth 2.0 authenticator', function() {
return generateAndDestroy(['authenticator', 'application', '--oauth2'], {
files: [
{ file: 'app/authenticators/application.js', contains: ['\
import Ember from \'ember\';' + EOL + '\
import OAuth2PasswordGrant from \'ember-simple-auth/authenticators/oauth2-password-grant\';' + EOL + '\
' + EOL + '\
export default OAuth2PasswordGrant.extend({' + EOL + '\
});' + EOL + '\
']}
]
});
});

it('generates a devise authenticator', function() {
return generateAndDestroy(['authenticator', 'application', '--devise'], {
files: [
{ file: 'app/authenticators/application.js', contains: ['\
import Ember from \'ember\';' + EOL + '\
import Devise from \'ember-simple-auth/authenticators/devise\';' + EOL + '\
' + EOL + '\
export default Devise.extend({' + EOL + '\
});' + EOL + '\
']}
]
});
});

it('generates a generic authenticator', function() {
return generateAndDestroy(['authenticator', 'application'], {
files: [
{ file: 'app/authenticators/application.js', contains: ['\
import Ember from \'ember\';' + EOL + '\
import Base from \'ember-simple-auth/authenticators/base\';' + EOL + '\
' + EOL + '\
export default Base.extend({' + EOL + '\
restore(data) {' + EOL + '\
},' + EOL + '\
' + EOL + '\
authenticate(/*args*/) {' + EOL + '\
},' + EOL + '\
' + EOL + '\
invalidate(data) {' + EOL + '\
}' + EOL + '\
});' + EOL + '\
']}
]
});
});

});
52 changes: 52 additions & 0 deletions node-tests/blueprints/authorizer-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

var EOL = require('os').EOL;
var setupTestHooks = require('ember-cli-blueprint-test-helpers/lib/helpers/setup');
var BlueprintHelpers = require('ember-cli-blueprint-test-helpers/lib/helpers/blueprint-helper');
var generateAndDestroy = BlueprintHelpers.generateAndDestroy;

describe('Acceptance: ember generate and destroy authorizer', function() {
setupTestHooks(this);

it('generates an OAuth 2.0 authorizer', function() {
return generateAndDestroy(['authorizer', 'application', '--oauth2'], {
files: [
{ file: 'app/authorizers/application.js', contains: ['\
import OAuth2Bearer from \'ember-simple-auth/authorizers/oauth2-bearer\';' + EOL + '\
' + EOL + '\
export default OAuth2Bearer.extend({' + EOL + '\
});' + EOL + '\
']}
]
});
});

it('generates a devise authorizer', function() {
return generateAndDestroy(['authorizer', 'application', '--devise'], {
files: [
{ file: 'app/authorizers/application.js', contains: ['\
import Devise from \'ember-simple-auth/authorizers/devise\';' + EOL + '\
' + EOL + '\
export default Devise.extend({' + EOL + '\
});' + EOL + '\
']}
]
});
});

it('generates a generic authorizer', function() {
return generateAndDestroy(['authorizer', 'application'], {
files: [
{ file: 'app/authorizers/application.js', contains: ['\
import Base from \'ember-simple-auth/authorizers/base\';' + EOL + '\
' + EOL + '\
export default Base.extend({' + EOL + '\
authorize(/*data, block*/) {' + EOL + '\
}' + EOL + '\
});' + EOL + '\
']}
]
});
});

});
70 changes: 70 additions & 0 deletions node-tests/nodetest-runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

var glob = require('glob');
var Mocha = require('mocha');
var Promise = require('ember-cli/lib/ext/promise');
var rimraf = require('rimraf');
var mochaOnlyDetector = require('mocha-only-detector');

if (process.env.EOLNEWLINE) {
require('os').EOL = '\n';
}

rimraf.sync('.node_modules-tmp');
rimraf.sync('.bower_components-tmp');

var root = 'node-tests/{blueprints,acceptance,unit}';
var _checkOnlyInTests = Promise.denodeify(mochaOnlyDetector.checkFolder.bind(null, root + '/**/*{-test}.js'));
var optionOrFile = process.argv[2];
var mocha = new Mocha({
timeout: 5000,
reporter: 'spec'
});
var testFiles = glob.sync(root + '/**/*-test.js');

if (optionOrFile === 'all') {
addFiles(mocha, testFiles);
addFiles(mocha, 'node-tests/**/*-test.js');
addFiles(mocha, '/**/*-test-slow.js');
} else if (process.argv.length > 2) {
addFiles(mocha, process.argv.slice(2));
} else {
addFiles(mocha, testFiles);
}

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

function checkOnlyInTests() {
console.log('Verifing `.only` in tests');
return _checkOnlyInTests().then(function() {
console.log('No `.only` found');
});
}

function runMocha() {
mocha.run(function(failures) {
process.on('exit', function() {
process.exit(failures);
});
});
}

function ciVerificationStep() {
if (process.env.CI === 'true') {
return checkOnlyInTests();
} else {
return Promise.resolve();
}
}

ciVerificationStep()
.then(function() {
runMocha();
})
.catch(function(error) {
console.error(error);
process.exit(1);
});
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"scripts": {
"build": "ember build",
"start": "ember server",
"test": "ember try:testall"
"test": "ember try:testall",
"nodetest": "node node-tests/nodetest-runner.js"
},
"repository": "https://github.com/simplabs/ember-simple-auth",
"engines": {
Expand All @@ -26,21 +27,24 @@
"ember-cli": "1.13.13",
"ember-cli-app-version": "^1.0.0",
"ember-cli-base64": "~0.0.1",
"ember-cli-blueprint-test-helpers": "0.7.0",
"ember-cli-content-security-policy": "0.4.0",
"ember-cli-dependency-checker": "^1.1.0",
"ember-cli-htmlbars": "^1.0.1",
"ember-cli-htmlbars-inline-precompile": "^0.3.1",
"ember-cli-ic-ajax": "0.2.4",
"ember-cli-inject-live-reload": "^1.3.1",
"ember-cli-internal-test-helpers": "^0.5.0",
"ember-cli-mocha": "~0.9.7",
"ember-cli-is-package-missing": "^1.0.0",
"ember-cli-pretender": "~0.5.0",
"ember-cli-release": "0.2.8",
"ember-cli-sri": "^1.2.0",
"ember-cli-uglify": "^1.2.0",
"ember-data": "~2.2.0",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-disable-proxy-controllers": "^1.0.1",
"ember-export-application-global": "^1.0.4",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-sinon": "~0.3.0",
"ember-suave": "~1.2.2",
"ember-try": "~0.0.8",
Expand All @@ -50,7 +54,10 @@
"handlebars": "~3.0.3",
"lodash": "^3.10.1",
"marked": "^0.3.5",
"mocha": "^2.4.5",
"mocha-only-detector": "0.0.2",
"morgan": "^1.5.2",
"rimraf": "^2.3.2",
"sinon-chai": "~2.8.0",
"torii": "~0.6.1"
},
Expand Down

0 comments on commit f1144d9

Please sign in to comment.