diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2017-12-13 16:19:14 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2017-12-13 16:19:14 +0300 |
commit | 83bc5a7f86b4a07dedd9350b2ecba8a4976b4250 (patch) | |
tree | ba21124de1d6a4f0f4b23a77fae7b9d8d28968be | |
parent | ae4fe1c90a276013b3710a22c3aa1cdd6eb16db5 (diff) |
Fix js test execution
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
-rw-r--r-- | .travis.yml | 3 | ||||
-rw-r--r-- | js/challenge.js | 6 | ||||
-rw-r--r-- | js/init_challenge.js | 9 | ||||
-rw-r--r-- | js/init_settings.js (renamed from js/settings.js) | 0 | ||||
-rw-r--r-- | js/tests/spec/settingsviewSpec.js | 136 | ||||
-rw-r--r-- | js/tests/spec/settingsview_spec.js | 138 | ||||
-rw-r--r-- | js/webpack.base.config.js | 4 | ||||
-rw-r--r-- | karma.conf.js | 59 | ||||
-rw-r--r-- | package-lock.json | 80 | ||||
-rw-r--r-- | package.json | 14 |
10 files changed, 273 insertions, 176 deletions
diff --git a/.travis.yml b/.travis.yml index d620397..4c1ff6d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -85,7 +85,8 @@ script: #- php ../../occ app:check-code twofactor_u2f # Run JS tests - - sh -c "if [ '$TEST_JS' = 'TRUE' ]; then ./node_modules/karma/bin/karma start --single-run; fi" + - sh -c "if [ '$TEST_JS' = 'TRUE' ]; then npm run build; fi" + - sh -c "if [ '$TEST_JS' = 'TRUE' ]; then npm run test; fi" - sh -c "if [ '$TEST_JS' = 'TRUE' ]; then cat ./coverage/*/lcov.info | ./node_modules/coveralls/bin/coveralls.js; fi" # Run PHP tests diff --git a/js/challenge.js b/js/challenge.js index 0bae891..f8c1514 100644 --- a/js/challenge.js +++ b/js/challenge.js @@ -3,6 +3,7 @@ define(function (require) { 'use strict'; + var $ = require('jquery'); var u2f = require('u2f-api'); function toggleError(state) { @@ -45,6 +46,7 @@ define(function (require) { u2f.sign(req).then(signCallback); } - $(sign); - + return { + sign: sign + }; }); diff --git a/js/init_challenge.js b/js/init_challenge.js new file mode 100644 index 0000000..91deb4a --- /dev/null +++ b/js/init_challenge.js @@ -0,0 +1,9 @@ +define(function(require) { + 'use strict'; + + var $ = require('jquery'); + + var challenge = require('./challenge'); + + $(challenge.sign); +}); diff --git a/js/settings.js b/js/init_settings.js index 638bc1d..638bc1d 100644 --- a/js/settings.js +++ b/js/init_settings.js diff --git a/js/tests/spec/settingsviewSpec.js b/js/tests/spec/settingsviewSpec.js deleted file mode 100644 index 4537087..0000000 --- a/js/tests/spec/settingsviewSpec.js +++ /dev/null @@ -1,136 +0,0 @@ -/* global expect, Promise, spyOn */ - -describe('Settings view', function () { - - var view; - - beforeEach(function () { - jasmine.Ajax.install(); - view = new OCA.TwoFactorU2F.SettingsView(); - }); - - afterEach(function () { - jasmine.Ajax.uninstall(); - }); - - it('fetches state from the server', function () { - spyOn(OC.Notification, 'showTemporary'); - - view.load(); - - expect(jasmine.Ajax.requests.mostRecent().url).toBe('/apps/twofactor_u2f/settings/state'); - - jasmine.Ajax.requests.mostRecent().respondWith({ - status: 200, - contentType: 'application/json', - responseText: JSON.stringify({ - enabled: false - }) - }); - - expect(OC.Notification.showTemporary).not.toHaveBeenCalled(); - }); - - it('shows a notification if the state cannot be loaded from the server', function (done) { - spyOn(OC.Notification, 'showTemporary'); - - var loading = view.load(); - - expect(jasmine.Ajax.requests.mostRecent().url).toBe('/apps/twofactor_u2f/settings/state'); - - jasmine.Ajax.requests.mostRecent().respondWith({ - status: 500, - contentType: 'application/json' - }); - - loading.then(function () { - expect(OC.Notification.showTemporary).toHaveBeenCalled(); - done(); - }).catch(function (e) { - done.fail(e); - }); - }); - - it('asks for password confirmation when the user enables u2f', function (done) { - spyOn(OC.Notification, 'showTemporary'); - spyOn(view, '_getServerState').and.returnValue(Promise.resolve({ - devices: [] - })); - spyOn(view, '_requirePasswordConfirmation').and.returnValue(Promise.reject({ - message: 'Wrong password' - })); - - view.load().then(function () { - view._onAddU2FDevice().then(function () { - expect(OC.Notification.showTemporary).toHaveBeenCalledWith('Wrong password'); - done(); - }).catch(function (e) { - done.fail(e); - }); - }, function (e) { - done.fail(e); - }); - }); - - it('lets the user register a new device', function (done) { - spyOn(OC.Notification, 'showTemporary'); - spyOn(view, '_getServerState').and.returnValue(Promise.resolve({ - devices: [] - })); - spyOn(view, '_registerU2fDevice').and.returnValue(Promise.resolve({})); - spyOn(view, '_requirePasswordConfirmation').and.returnValue(Promise.resolve()); - jasmine.Ajax.stubRequest('/apps/twofactor_u2f/settings/startregister').andReturn({ - contentType: 'application/json', - responseText: JSON.stringify({ - req: 'reqdata', - sigs: 'sigsdata' - }) - }); - jasmine.Ajax.stubRequest('/apps/twofactor_u2f/settings/finishregister').andReturn({ - contentType: 'application/json', - responseText: JSON.stringify({}) - }); - - view.load().then(function () { - expect(view._getServerState).toHaveBeenCalled(); - return view._onAddU2FDevice().then(function () { - expect(view._registerU2fDevice).toHaveBeenCalled(); - expect(OC.Notification.showTemporary).not.toHaveBeenCalled(); - done(); - }); - }).catch(function (e) { - done.fail(e); - }); - }); - - it('lets the user remove a device', function (done) { - spyOn(OC.Notification, 'showTemporary'); - spyOn(view, '_getServerState').and.returnValue(Promise.resolve({ - devices: [ - { - id: 13, - name: 'Yolokey' - } - ] - })); - spyOn(view, '_requirePasswordConfirmation').and.returnValue(Promise.resolve()); - jasmine.Ajax.stubRequest('/apps/twofactor_u2f/settings/remove').andReturn({ - contentType: 'application/json', - responseText: JSON.stringify({}) - }); - - view.load().then(function () { - expect(view._getServerState).toHaveBeenCalled(); - var fakeEvent = { - target: view.$('.remove-device') - }; - return view._onRemoveDevice(fakeEvent).then(function () { - expect(OC.Notification.showTemporary).not.toHaveBeenCalled(); - done(); - }); - }).catch(function (e) { - done.fail(e); - }); - }); - -}); diff --git a/js/tests/spec/settingsview_spec.js b/js/tests/spec/settingsview_spec.js new file mode 100644 index 0000000..ced685c --- /dev/null +++ b/js/tests/spec/settingsview_spec.js @@ -0,0 +1,138 @@ +/* global expect, Promise, spyOn */ + +define(['settingsview'], function(SettingsView) { + describe('Settings view', function() { + + var view; + + beforeEach(function() { + jasmine.Ajax.install(); + view = new SettingsView(); + }); + + afterEach(function() { + jasmine.Ajax.uninstall(); + }); + + it('fetches state from the server', function() { + spyOn(OC.Notification, 'showTemporary'); + + view.load(); + + expect(jasmine.Ajax.requests.mostRecent().url).toBe('/apps/twofactor_u2f/settings/state'); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'application/json', + responseText: JSON.stringify({ + enabled: false + }) + }); + + expect(OC.Notification.showTemporary).not.toHaveBeenCalled(); + }); + + it('shows a notification if the state cannot be loaded from the server', function(done) { + spyOn(OC.Notification, 'showTemporary'); + + var loading = view.load(); + + expect(jasmine.Ajax.requests.mostRecent().url).toBe('/apps/twofactor_u2f/settings/state'); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 500, + contentType: 'application/json' + }); + + loading.then(function() { + expect(OC.Notification.showTemporary).toHaveBeenCalled(); + done(); + }).catch(function(e) { + done.fail(e); + }); + }); + + it('asks for password confirmation when the user enables u2f', function(done) { + spyOn(OC.Notification, 'showTemporary'); + spyOn(view, '_getServerState').and.returnValue(Promise.resolve({ + devices: [] + })); + spyOn(view, '_requirePasswordConfirmation').and.returnValue(Promise.reject({ + message: 'Wrong password' + })); + + view.load().then(function() { + view._onAddU2FDevice().then(function() { + expect(OC.Notification.showTemporary).toHaveBeenCalledWith('Wrong password'); + done(); + }).catch(function(e) { + done.fail(e); + }); + }, function(e) { + done.fail(e); + }); + }); + + it('lets the user register a new device', function(done) { + spyOn(OC.Notification, 'showTemporary'); + spyOn(view, '_getServerState').and.returnValue(Promise.resolve({ + devices: [] + })); + spyOn(view, '_registerU2fDevice').and.returnValue(Promise.resolve({})); + spyOn(view, '_requirePasswordConfirmation').and.returnValue(Promise.resolve()); + jasmine.Ajax.stubRequest('/apps/twofactor_u2f/settings/startregister').andReturn({ + contentType: 'application/json', + responseText: JSON.stringify({ + req: 'reqdata', + sigs: 'sigsdata' + }) + }); + jasmine.Ajax.stubRequest('/apps/twofactor_u2f/settings/finishregister').andReturn({ + contentType: 'application/json', + responseText: JSON.stringify({}) + }); + + view.load().then(function() { + expect(view._getServerState).toHaveBeenCalled(); + return view._onAddU2FDevice().then(function() { + expect(view._registerU2fDevice).toHaveBeenCalled(); + expect(OC.Notification.showTemporary).not.toHaveBeenCalled(); + done(); + }); + }).catch(function(e) { + done.fail(e); + }); + }); + + it('lets the user remove a device', function(done) { + spyOn(OC.Notification, 'showTemporary'); + spyOn(view, '_getServerState').and.returnValue(Promise.resolve({ + devices: [ + { + id: 13, + name: 'Yolokey' + } + ] + })); + spyOn(view, '_requirePasswordConfirmation').and.returnValue(Promise.resolve()); + jasmine.Ajax.stubRequest('/apps/twofactor_u2f/settings/remove').andReturn({ + contentType: 'application/json', + responseText: JSON.stringify({}) + }); + + view.load().then(function() { + expect(view._getServerState).toHaveBeenCalled(); + var fakeEvent = { + target: view.$('.remove-device') + }; + return view._onRemoveDevice(fakeEvent).then(function() { + expect(OC.Notification.showTemporary).not.toHaveBeenCalled(); + done(); + }); + }).catch(function(e) { + done.fail(e); + }); + }); + + }); +});
\ No newline at end of file diff --git a/js/webpack.base.config.js b/js/webpack.base.config.js index 333494b..99e4a2d 100644 --- a/js/webpack.base.config.js +++ b/js/webpack.base.config.js @@ -3,8 +3,8 @@ const webpack = require('webpack'); module.exports = { entry: { - challenge: './js/challenge.js', - settings: './js/settings.js' + challenge: './js/init_challenge.js', + settings: './js/init_settings.js' }, output: { filename: '[name].js', diff --git a/karma.conf.js b/karma.conf.js index 07839c8..553b08b 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -1,66 +1,77 @@ // Karma configuration -module.exports = function (config) { +var webpackConfig = require('./js/webpack.dev.config.js'); + +webpackConfig.entry = { + challenge: './js/challenge.js', + settings: './js/settingsview.js' +}; +webpackConfig.module.rules.push({ + test: /\.js$/, + exclude: /^init_/ +}); + +module.exports = function(config) { config.set({ // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['jasmine-ajax', 'jasmine'], - // list of files / patterns to load in the browser files: [ - '../../core/vendor/es6-promise/dist/es6-promise.js', - '../../core/vendor/jquery/dist/jquery.js', - '../../core/vendor/underscore/underscore.js', - '../../core/vendor/backbone/backbone.js', - '../../core/vendor/handlebars/handlebars.min.js', - 'js/tests/test-main.js', - 'js/tests/fake-u2f.js', - {pattern: 'js/**/*.js', included: true}, - {pattern: 'js/tests/*.js', included: false} + {pattern: 'node_modules/es6-promise/dist/es6-promise.auto.js', included: true}, + {pattern: 'node_modules/jquery/dist/jquery.js', included: true}, + {pattern: 'node_modules/handlebars/dist/handlebars.js', included: true}, + {pattern: 'node_modules/underscore/underscore.js', included: true}, + {pattern: 'node_modules/backbone/backbone.js', included: true}, + {pattern: 'js/tests/test-main.js', included: true}, + // all files ending in "_test" + {pattern: 'js/tests/spec/*_spec.js', watched: false}, + {pattern: 'js/build/*.js', included: false} ], // list of files to exclude exclude: [ - 'js/vendor/**/*.js', - 'js/challenge.js', - 'js/settings.js' + 'js/webpack.*.js', + 'js/init*.js' ], - // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { - 'js/*.js': ['coverage'] + 'js/**[!vendor]/*[!spec].js': ['coverage', 'sourcemap'], + // add webpack as preprocessor + 'js/tests/*_spec.js': ['webpack', 'sourcemap'], + 'js/tests/**/*_spec.js': ['webpack', 'sourcemap'] + }, + + webpackMiddleware: { + // webpack-dev-middleware configuration + // i. e. + stats: 'errors-only' }, // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter reporters: ['progress', 'coverage'], - coverageReporter: { type: 'lcov', dir: 'coverage/' }, - // web server port port: 9876, - // enable / disable colors in the output (reporters and logs) colors: true, - // level of logging // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG logLevel: config.LOG_INFO, - // enable / disable watching file and executing tests whenever any file changes autoWatch: true, - // start these browsers // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher browsers: ['PhantomJS'], - // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits - singleRun: false + singleRun: false, + webpack: webpackConfig }); }; diff --git a/package-lock.json b/package-lock.json index af5b0a7..0746ef6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -217,6 +217,7 @@ "version": "1.3.3", "resolved": "https://registry.npmjs.org/backbone/-/backbone-1.3.3.tgz", "integrity": "sha1-TMgOp8sWMaxHSInOQPL4vGg7KZk=", + "dev": true, "requires": { "underscore": "1.8.3" } @@ -1162,8 +1163,7 @@ "es6-promise": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.1.1.tgz", - "integrity": "sha512-OaU1hHjgJf+b0NzsxCg7NdIYERD6Hy/PEmFLTjw+b65scuisG3Kt4QoTvJ66BBkPZ581gr0kpoVzKnxniM8nng==", - "dev": true + "integrity": "sha512-OaU1hHjgJf+b0NzsxCg7NdIYERD6Hy/PEmFLTjw+b65scuisG3Kt4QoTvJ66BBkPZ581gr0kpoVzKnxniM8nng==" }, "es6-set": { "version": "0.1.5", @@ -2603,8 +2603,7 @@ "graceful-fs": { "version": "4.1.11", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", - "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=", - "dev": true + "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=" }, "handlebars": { "version": "4.0.11", @@ -3080,6 +3079,12 @@ "integrity": "sha1-vMl5rh+f0FcB5F5S5l06XWPxok4=", "dev": true }, + "jquery": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.2.1.tgz", + "integrity": "sha1-XE2d5lKvbNCncBVKYxu6ErAVx4c=", + "dev": true + }, "js-yaml": { "version": "3.8.2", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.8.2.tgz", @@ -3257,6 +3262,47 @@ } } }, + "karma-sourcemap-loader": { + "version": "0.3.7", + "resolved": "https://registry.npmjs.org/karma-sourcemap-loader/-/karma-sourcemap-loader-0.3.7.tgz", + "integrity": "sha1-kTIsd/jxPUb+0GKwQuEAnUxFBdg=", + "requires": { + "graceful-fs": "4.1.11" + } + }, + "karma-webpack": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/karma-webpack/-/karma-webpack-2.0.6.tgz", + "integrity": "sha512-dcKvtiW00caWrceCKwIvlKwHQu8zI+e3zWZYDLk7kr7nl1lYSp8uP+8fQoBvRCnZiPUGuwU5Psm20NbEIn7KlA==", + "dev": true, + "requires": { + "async": "0.9.2", + "loader-utils": "0.2.17", + "lodash": "3.10.1", + "source-map": "0.5.6", + "webpack-dev-middleware": "1.12.2" + }, + "dependencies": { + "async": { + "version": "0.9.2", + "resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz", + "integrity": "sha1-rqdNXmHB+JlhO/ZL2mbUx48v0X0=", + "dev": true + }, + "loader-utils": { + "version": "0.2.17", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-0.2.17.tgz", + "integrity": "sha1-+G5jdNQyBabmxg6RlvF8Apm/s0g=", + "dev": true, + "requires": { + "big.js": "3.2.0", + "emojis-list": "2.1.0", + "json5": "0.5.1", + "object-assign": "4.1.0" + } + } + } + }, "kew": { "version": "0.7.0", "resolved": "https://registry.npmjs.org/kew/-/kew-0.7.0.tgz", @@ -4911,6 +4957,12 @@ "integrity": "sha1-nnhYNtr0Z0MUWlmEtiaNgoUorGw=", "dev": true }, + "time-stamp": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/time-stamp/-/time-stamp-2.0.0.tgz", + "integrity": "sha1-lcakRTDhW6jW9KPsuMOj+sRto1c=", + "dev": true + }, "timers-browserify": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/timers-browserify/-/timers-browserify-2.0.4.tgz", @@ -5055,7 +5107,8 @@ "underscore": { "version": "1.8.3", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", - "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=" + "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=", + "dev": true }, "unpipe": { "version": "1.0.0", @@ -5368,10 +5421,24 @@ } } }, + "webpack-dev-middleware": { + "version": "1.12.2", + "resolved": "https://registry.npmjs.org/webpack-dev-middleware/-/webpack-dev-middleware-1.12.2.tgz", + "integrity": "sha512-FCrqPy1yy/sN6U/SaEZcHKRXGlqU0DUaEBL45jkUYoB8foVb6wCnbIJ1HKIx+qUFTW+3JpVcCJCxZ8VATL4e+A==", + "dev": true, + "requires": { + "memory-fs": "0.4.1", + "mime": "1.6.0", + "path-is-absolute": "1.0.1", + "range-parser": "1.2.0", + "time-stamp": "2.0.0" + } + }, "webpack-merge": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/webpack-merge/-/webpack-merge-4.1.1.tgz", "integrity": "sha512-geQsZ86YkXOVOjvPC5yv3JSNnL6/X3Kzh935AQ/gJNEYXEfJDQFu/sdFuktS9OW2JcH/SJec8TGfRdrpHshH7A==", + "dev": true, "requires": { "lodash": "4.17.4" }, @@ -5379,7 +5446,8 @@ "lodash": { "version": "4.17.4", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", - "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=" + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", + "dev": true } } }, diff --git a/package.json b/package.json index 563573d..cd4df1c 100644 --- a/package.json +++ b/package.json @@ -7,23 +7,27 @@ "test": "tests" }, "dependencies": { - "backbone": "^1.3.3", + "es6-promise": "^4.1.1", "handlebars": "^4.0.11", - "u2f-api": "^0.2.7", - "underscore": "^1.8.3", - "webpack-merge": "^4.1.1" + "karma-sourcemap-loader": "^0.3.7", + "u2f-api": "^0.2.7" }, "devDependencies": { + "backbone": "^1.3.3", "coveralls": "^2.13.3", "jasmine": "^2.8.0", "jasmine-ajax": "^3.3.1", "jasmine-core": "^2.8.0", + "jquery": "^3.2.1", "karma": "^1.7.1", "karma-coverage": "^1.1.1", "karma-jasmine": "^1.1.1", "karma-jasmine-ajax": "^0.1.13", "karma-phantomjs-launcher": "^1.0.4", - "webpack": "^3.10.0" + "karma-webpack": "^2.0.6", + "underscore": "^1.8.3", + "webpack": "^3.10.0", + "webpack-merge": "^4.1.1" }, "scripts": { "test": "./node_modules/karma/bin/karma start karma.conf.js --single-run", |