commit ab624930dfc23a894d05461bf02faf054f9744db Author: febbweiss Date: Thu Sep 10 14:02:43 2015 +0000 First commit diff --git a/.bowerrc b/.bowerrc new file mode 100644 index 0000000..7f4d43a --- /dev/null +++ b/.bowerrc @@ -0,0 +1,4 @@ +{ + "directory": "public/libs" +} + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7ace1bd --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +node_modules/ +public/libs/* +coverage/* +tmp/ +npm-debug.log diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f334522 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: node_js +node_js: +- '0.12' +env: + global: + secure: G2F3So5W3+ieJxP7+wpWO+MLCrXd7E3TL/qmvRIuASV9SEgJp76Qe5gBuu6QkVkMQaYZqOJTd9MW96Pm2NABqfE2xaojv3za4n/IqeyHpjSi9/Sg3PAABLqQ2/izpBYWcxtiZxtElLhSksGr9qDcj9OnIodogvNiXacaS9YKWwHF2eBO+Z3xQYfjh+jGrY2/gW6TzY+3BVOWoOspUrKNuWT74lxoz+smxVygYtzb5tGINvswj9gpZOCrvDms/E40ksm0mOSFAmNbK6/RasiKncqrd2dB0sgF0/LvoyDYJv6ozpDMkOfG5GVIuoc6DiK3txejlIW6HWyOxzzMItDk5pVIqSAoCAgSzjsti/kvIcy7mfSrZwEAyrqBrzCMs9rxXvNyX1k4etykqwCkA0dpbed2NgNnaUyMlpcObkiodt8xGIzddErLKemCVBhmFi1NxCe6vCOiArsMtqrkJkccDeVILTyKNo/vVCzO3vBLbmvUkbyucJRxD5J9plgleZVtOm6yE4Lm6343zvp7aVBcZZGGmZwf34vS/sJ5mfmxpm7h30SkUB8hHezXPPa6HTlOPeVaatw/QodX1KyNtHB1n4BxOMHjUZcIDHdcRwNS/K4/5Fv+VvFdnSve+AWTiEMC4Ux6SixuNaHtg5syW/JVVVhcRPqyuxSpGI+oupvXAxg= +before_script: + - npm install -g bower + - bower install +after_success: + - 'cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js' + - 'curl "http://technical.viadeo-playground.com/deploy/update?secret_key=$PASSWORD&site=cloudbudget-angularjs"' \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1c89486 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +Cloud Budget AngularJS +Copyright 2015 Febbweiss + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..17a1847 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +Work in progress +[![Build Status][build-image]][build-url] +[![Coverage Status][coverage-image]][coverage-url] +[![License][license-image]][license-url] + +[build-image]: https://travis-ci.org/Febbweiss/CloudBudget-AngularJS.svg?branch=master +[build-url]: https://travis-ci.org/Febbweiss/CloudBudget-ANgularJS +[coverage-image]:https://coveralls.io/repos/Febbweiss/CloudBudget-AngularJS/badge.svg?branch=master&service=github +[coverage-url]: https://coveralls.io/github/Febbweiss/CloudBudget-AngularJS?branch=master +[license-image]: https://img.shields.io/badge/LICENSE-MIT-blue.svg +[license-url]: LICENSE \ No newline at end of file diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..bb9fc12 --- /dev/null +++ b/bower.json @@ -0,0 +1,15 @@ +{ + "name": "cloudbudget-angularjs", + "version": "1.0.0-SNAPSHOT", + "dependencies": { + "angular": "~1.4.1", + "angular-cookies": "~1.4.4", + "angular-route": "~1.4.1", + "animate.css": "~3.3.0", + "bootstrap": "~3.3.5", + "font-awesome": "~4.4.0" + }, + "devDependencies": { + "angular-mocks": "~1.4.4" + } +} diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..f5d86d8 --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,75 @@ +// Karma configuration +// Generated on Mon Aug 24 2015 10:10:09 GMT+0000 (UTC) + +module.exports = function(config) { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: '', + + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ['jasmine', 'mocha', 'chai'], + + + // list of files / patterns to load in the browser + files: [ + 'public/libs/angular/angular.js', + 'public/libs/angular-route/angular-route.js', + 'public/libs/angular-cookies/angular-cookies.js', + 'public/libs/angular-mocks/angular-mocks.js', + 'public/js/**/*.js', + 'public/**/*.controller.js', + 'test/**/*.controller.js' + ], + + + // list of files to exclude + exclude: [ + ], + + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + }, + + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ['progress', 'coverage', 'coveralls'], + + coverageReporter: { + type: 'lcov', // lcov or lcovonly are required for generating lcov.info files + 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: true + }) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..3e5da79 --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "cloudbudget-angularjs", + "version": "1.0.0-SNAPSHOT", + "description": "An AngularJS client for CloudBudget", + "main": "server.js", + "repository": "", + "author": "Febbweiss", + "dependencies": { + "express": "~3.2.4", + "bower": "~1.5.1" + }, + "devDependencies": { + "chai": "^3.2.0", + "coveralls": "^2.11.4", + "istanbul": "^0.3.18", + "jasmine-core": "^2.3.4", + "karma": "^0.13.9", + "karma-chai": "^0.1.0", + "karma-coveralls": "^1.1.2", + "karma-jasmine": "^0.3.6", + "karma-mocha": "^0.2.0", + "karma-phantomjs-launcher": "^0.2.1", + "mocha": "~2.2.5", + "phantomjs": "^1.9.18", + "should": "~7.0.2", + "sinon": "~1.15.4" + }, + "scripts": { + "test-local": "karma start karma.conf.js --browsers PhantomJS", + "test": "./node_modules/karma/bin/karma start karma.conf.js --browsers PhantomJS", + "start": "server.js", + "stop": "pkill -SIGINT CloudBudget-AngularJS" + }, + "license": "MIT" +} diff --git a/public/home/home.controller.js b/public/home/home.controller.js new file mode 100644 index 0000000..44a3300 --- /dev/null +++ b/public/home/home.controller.js @@ -0,0 +1,30 @@ +(function() { + 'use strict'; + + angular + .module('cloudbudget') + .controller('HomeController', HomeController); + + HomeController.$inject = ['$rootScope']; + + function HomeController($rootScope) { + var vm = this; + + vm.firstname = ''; + vm.lastname = ''; + vm.getFullname = getFullname; + vm.user = null; + + (function initController() { + loadUser(); + })(); + + function loadUser() { + vm.user = $rootScope.globals.user; + }; + + function getFullname() { + return vm.firstname + ' ' + vm.lastname; + }; + } +})(); \ No newline at end of file diff --git a/public/home/home.view.html b/public/home/home.view.html new file mode 100644 index 0000000..b4c2ddc --- /dev/null +++ b/public/home/home.view.html @@ -0,0 +1,4 @@ +

Hi {{vm.user.username}}

+

You're logged in

+

 

+

Logout

\ No newline at end of file diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..03b55df --- /dev/null +++ b/public/index.html @@ -0,0 +1,54 @@ + + + + + + + CloudBudget + + + +
+ +
+
+
+
+
+
+
+
+
+ +
+

+ CloudBudget +

+

+ Febbweiss +

+
+ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/public/js/app.js b/public/js/app.js new file mode 100644 index 0000000..7391501 --- /dev/null +++ b/public/js/app.js @@ -0,0 +1,33 @@ +(function() { + 'use strict'; + + var HOST = 'http://cloudbudget.pavnay.fr/api'; + + angular + .module('cloudbudget', ['ngRoute', 'routes', 'ngCookies']) + .constant('apiRoutes', { + 'host': HOST, + 'port': "80", + 'login': HOST + '/users/login', + 'register': HOST + '/users', + 'unregister': HOST + '/users/' + }) + .run(run); + + run.$inject = ['$rootScope', '$location', '$cookieStore', '$http', '$filter']; + function run( $rootScope, $location, $cookieStore, $http, $filter) { + $rootScope.globals = $cookieStore.get('globals') || {}; + if( $rootScope.globals.user && $rootScope.globals.user.token) { + $http.defaults.headers.common['Authorization'] = 'JWT ' + $rootScope.globals.user.token; + } + + $rootScope.$on('$locationChangeStart', function(event, next, current) { + var restrictedPage = ['/login', '/register'].indexOf($location.path()) === -1; + + var loggedIn = $rootScope.globals.user; + if( restrictedPage && !loggedIn ) { + $location.path('/login'); + } + }); + } +})(); \ No newline at end of file diff --git a/public/js/routes.js b/public/js/routes.js new file mode 100644 index 0000000..cf6debc --- /dev/null +++ b/public/js/routes.js @@ -0,0 +1,34 @@ +(function() { + 'use strict'; + + angular + .module('routes', []) + .config(config); + + config.$inject = ['$routeProvider', '$locationProvider']; + + function config($routeProvider, $locationProvider) { + $routeProvider + .when('/', { + controller: 'HomeController', + templateUrl: 'home/home.view.html', + controllerAs: 'vm' + }) + + .when('/login', { + controller: 'LoginController', + templateUrl: 'login/login.view.html', + controllerAs: 'vm' + }) + + .when('/register', { + controller: 'RegisterController', + templateUrl: 'register/register.view.html', + controllerAs: 'vm' + }) + + .otherwise({redirectTo: '/login'}); + + $locationProvider.html5Mode(true); + }; +})(); \ No newline at end of file diff --git a/public/js/services/authentication.service.js b/public/js/services/authentication.service.js new file mode 100644 index 0000000..4970ea8 --- /dev/null +++ b/public/js/services/authentication.service.js @@ -0,0 +1,57 @@ +(function() { + 'use strict'; + + angular + .module('cloudbudget') + .factory('AuthenticationService', AuthenticationService); + + AuthenticationService.$inject = ['$http', '$cookieStore', '$rootScope', 'apiRoutes']; + + function AuthenticationService($http, $cookieStore, $rootScope, apiRoutes) { + var service = {}; + service.login = login; + service.setCredentials = setCredentials; + service.clearCredentials = clearCredentials; + + return service; + + function login(username, password ) { + return $http.post( apiRoutes.login, {username: username, password: password}) + .then(function(response, status) { + if( status === 200 ) { + return { + success: true, + user: response + }; + } else { + return { + success: false, + message: response.message + }; + } + }, function(response) { + return { + success: false, + message: 'An error occurs' + }; + }); + } + + function setCredentials(user) { + $rootScope.globals = { + user: user, + token: user.token + } + + $http.defaults.headers.common['Authorization'] = 'JWT ' + user.token; + $cookieStore.put('globals', $rootScope.globals); + console.log( $cookieStore.get('globals')); + } + + function clearCredentials() { + $rootScope.globals = {}; + $http.defaults.headers.common.Authorization = 'JWT '; + $cookieStore.remove('globals'); + } + } +})(); \ No newline at end of file diff --git a/public/js/services/flash.service.js b/public/js/services/flash.service.js new file mode 100644 index 0000000..8374a5a --- /dev/null +++ b/public/js/services/flash.service.js @@ -0,0 +1,53 @@ +(function() { + 'use strict'; + + angular + .module('cloudbudget') + .factory('FlashService', FlashService); + + FlashService.$inject = ['$rootScope']; + + function FlashService($rootScope) { + var service = {}; + + service.success = success; + service.error = error; + + initService(); + + return service; + + function initService() { + $rootScope.$on('$locationChangeStart', function() { + clearFlashMessage(); + }); + + function clearFlashMessage() { + var flash = $rootScope.flash; + if( flash ) { + if( !flash.keepAfterLocationChange ) { + delete $rootScope.flash; + } else { + flash.keepAfterLocationChange = false; + } + } + } + } + + function success(message, keepAfterLocationChange) { + $rootScope.flash = { + message: message, + type: 'success', + keepAfterLocationChange: keepAfterLocationChange + }; + } + + function error(message, keepAfterLocationChange) { + $rootScope.flash = { + message: message, + type: 'error', + keepAfterLocationChange: keepAfterLocationChange + }; + } + } +})(); \ No newline at end of file diff --git a/public/js/services/user.service.js b/public/js/services/user.service.js new file mode 100644 index 0000000..0e8e3fe --- /dev/null +++ b/public/js/services/user.service.js @@ -0,0 +1,38 @@ +(function() { + 'use strict'; + + angular + .module('cloudbudget') + .factory('UserService', UserService); + + UserService.$inject = ['$http', 'apiRoutes']; + + function UserService($http, apiRoutes) { + + var service = {}; + service.register = register; + service.unregister = unregister; + + return service; + + function register(user) { + return $http.post( apiRoutes.register, user) + .then(handleSuccess, handleError('Error creating user')); + } + + function unregister(id) { + return $http.delete( apiRoutes.unregister + id) + .then(handleSuccess, handleError('Error deleting user')); + } + + function handleSuccess(response) { + return {success: true, user: response.data}; + } + + function handleError(error) { + return function() { + return {success: false, message: error}; + }; + } + } +})(); \ No newline at end of file diff --git a/public/login/login.controller.js b/public/login/login.controller.js new file mode 100644 index 0000000..10556b5 --- /dev/null +++ b/public/login/login.controller.js @@ -0,0 +1,32 @@ +(function() { + 'use strict'; + + angular + .module('cloudbudget') + .controller('LoginController', LoginController); + + LoginController.$inject = ['$location', 'AuthenticationService', 'FlashService']; + + function LoginController($location, AuthenticationService, FlashService) { + var vm = this; + + vm.login = login; + + (function initController() { + AuthenticationService.clearCredentials(); + })(); + + function login() { + vm.dataLoading = true; + AuthenticationService.login(vm.username, vm.password).then( function(response) { + if( response.success ) { + AuthenticationService.setCredentials(response.user); + $location.path('/'); + } else { + FlashService.error(response.message); + vm.dataLoading = false; + } + }); + } + } +})(); \ No newline at end of file diff --git a/public/login/login.view.html b/public/login/login.view.html new file mode 100644 index 0000000..96b7403 --- /dev/null +++ b/public/login/login.view.html @@ -0,0 +1,21 @@ +
+

Login

+
{{vm.error}}
+
+
+ + + Username is required +
+
+ + + Password is required +
+
+ + + Register +
+
+
\ No newline at end of file diff --git a/public/register/register.controller.js b/public/register/register.controller.js new file mode 100644 index 0000000..7641bdf --- /dev/null +++ b/public/register/register.controller.js @@ -0,0 +1,31 @@ +(function(){ + 'use strict'; + + angular + .module('cloudbudget') + .controller('RegisterController', RegisterController); + + RegisterController.$inject = ['UserService', '$location', '$rootScope', 'AuthenticationService', 'FlashService']; + + function RegisterController(UserService, $location, $rootScope, AuthenticationService, FlashService) { + var vm = this; + + vm.dataLoading = false; + vm.register = register; + + function register() { + vm.dataLoading = true; + UserService.register(vm.user) + .then(function(response) { + if( response.success ) { + AuthenticationService.setCredentials(response.user); + FlashService.success('Registration successful', true); + $location.path('/'); + } else { + FlashService.error(response.message); + vm.dataLoading = false; + } + }); + } + } +})(); \ No newline at end of file diff --git a/public/register/register.view.html b/public/register/register.view.html new file mode 100644 index 0000000..83223b7 --- /dev/null +++ b/public/register/register.view.html @@ -0,0 +1,21 @@ +
+

Register

+
{{vm.error}}
+
+
+ + + Username is required +
+
+ + + Password is required +
+
+ + + Cancel +
+
+
\ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 0000000..4ecf2f3 --- /dev/null +++ b/server.js @@ -0,0 +1,16 @@ +var http = require('http'); +var path = require('path'); + +var express = require('express'); + +var router = express(); +var server = http.createServer(router); + +router.use(express.static(path.resolve(__dirname, 'public'))); + +process.title = 'CloudBudget-AngularJS'; + +server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){ + var addr = server.address(); + console.log("CloudBudget-AngularsJS server listening at", addr.address + ":" + addr.port); +}); diff --git a/test/autentication.service.js b/test/autentication.service.js new file mode 100644 index 0000000..c86f57f --- /dev/null +++ b/test/autentication.service.js @@ -0,0 +1,81 @@ +'use strict'; + +describe('AuthenticationService', function() { + var AuthenticationService, httpBackend, rootScope, cookieStore; + + beforeEach(module('cloudbudget')); + beforeEach(inject(function(_AuthenticationService_, $httpBackend, $rootScope, $cookieStore) { + AuthenticationService = _AuthenticationService_; + httpBackend = $httpBackend; + rootScope = $rootScope; + cookieStore = $cookieStore; + })); + + describe('* login()', function() { + + it('should login successfully and return user', function() { + httpBackend + .when('POST', 'api/users/login') + .respond(200, {username: 'test', token: 'tok3n'}); + + AuthenticationService.login('test', 'password', function(data) { + data.success.should.be.true; + + var user = data.user; + should.exist(user); + user.username.should.be.equal('test'); + user.token.should.be.equal('tok3n'); + + should.not.exist(data.message); + }); + }); + + it('should fail to login and return message', function() { + httpBackend + .when('POST', 'api/users/login') + .respond(401); + + AuthenticationService.login('test', 'password', function(data) { + data.succes.should.be.false; + + var message = data.message; + should.exist(message); + message.should.be.equal('Authentication fail'); + + should.not.exist(data.user); + }) + }); + }); + + describe('* setCredentials', function() { + it('should store the given user in scope and store', function() { + AuthenticationService.setCredentials({ + username: 'test', + token: 'tok3n' + }); + + var globals = rootScope.globals; + should.exist(globals); + should.exist(globals.user); + globals.user.username.should.be.equal('test'); + globals.user.token.should.be.equal('tok3n'); + should.exist(globals.token); + globals.token.should.be.equal('tok3n'); + + should.exist(cookieStore.get('globals')); + cookieStore.get('globals').should.eql(globals); + }); + }); + + describe('* clearCredentials()', function() { + it('should clean credentials', function() { + + AuthenticationService.clearCredentials(); + + should.exist(rootScope.globals); + rootScope.globals.should.eql({}); + + should.not.exist(cookieStore.get('globals')) + }); + }); +}); \ No newline at end of file diff --git a/test/home.controller.js b/test/home.controller.js new file mode 100644 index 0000000..fe88744 --- /dev/null +++ b/test/home.controller.js @@ -0,0 +1,23 @@ +describe('HomeController', function() { + + var rootScope; + + beforeEach(module('cloudbudget')); + + beforeEach(inject(function($rootScope) { + rootScope = $rootScope.$new(); + })); + + describe('getFullname()', function() { + it('should handle names correctly', inject(function($controller) { + var scope = {}; + var homeController = $controller('HomeController', { + '$rootScope': rootScope + }); + + homeController.firstname = 'George'; + homeController.lastname = 'Harrison'; + homeController.getFullname().should.be.equal('George Harrison'); + })); + }); +}); \ No newline at end of file diff --git a/test/login.controller.js b/test/login.controller.js new file mode 100644 index 0000000..31cc2f1 --- /dev/null +++ b/test/login.controller.js @@ -0,0 +1,98 @@ +describe('LoginController', function() { + + var scope, + $location, + $timeout, + $rootScope, + AuthenticationServiceMock, + createController, + shouldPass; + + beforeEach(module('cloudbudget')); + + beforeEach(function(){ + module(function($provide) { + $provide.factory('AuthenticationService', ['$q', '$rootScope', function($q, $rootScope) { + function login(username, password ) { + var data; + if(shouldPass){ + data = { + success: true, + user: { + username: 'test', + token: 'tok3n' + } + }; + } else { + data = { + success: false, + message: 'Authentication fail' + }; + } + return $q.when(data); + } + + function setCredentials(user) { + $rootScope.globals = { + user: user, + token: user.token + } + } + + function clearCredentials() { + $rootScope.globals = {}; + } + + return{ + login: login, + setCredentials: setCredentials, + clearCredentials: clearCredentials + }; + }]); + }); + }); + + beforeEach(inject(function ($rootScope, $controller, _$location_, _$timeout_, AuthenticationService) { + $location = _$location_; + scope = $rootScope.$new(); + $timeout = _$timeout_; + AuthenticationServiceMock = AuthenticationService; + + createController = function() { + return $controller('LoginController', { + '$scope': scope, + AuthenticationService: AuthenticationServiceMock + }); + }; + })); + + describe('login()', function() { + it('should log successfully', inject(function($controller, $location) { + shouldPass = true; + + var loginController = createController(); + + loginController.username = 'fail'; + loginController.password = 's3cr3t'; + + loginController.login(); + $timeout.flush(); + + $location.path().should.be.equal('/'); + })); + + it('should fail to log', inject(function($controller, $location) { + shouldPass = false; + + var loginController = $controller('LoginController'); + + loginController.username = 'fail'; + loginController.password = 'secret'; + + loginController.login(); + $timeout.flush(); + + $location.path().should.be.equal('/login'); + })); + }); +}); \ No newline at end of file diff --git a/test/register.controller.js b/test/register.controller.js new file mode 100644 index 0000000..dfdd579 --- /dev/null +++ b/test/register.controller.js @@ -0,0 +1,142 @@ +describe('RegisterController', function() { + + var $location, + $rootScope, + $timeout, + $httpBackend, + UserService, + AuthenticationServiceMock, + FlashService, + createController, + apiRoutes, + shouldPass; + + beforeEach(module('cloudbudget')); + + beforeEach(function() { + module(function($provide) { + $provide.factory('AuthenticationService', ['$q', '$rootScope', function($q, $rootScope) { + function login(username, password ) { + var data; + if(shouldPass){ + data = { + success: true, + user: { + username: 'test', + token: 'tok3n' + } + }; + } else { + data = { + success: false, + message: 'Authentication fail' + }; + } + return $q.when(data); + } + + function setCredentials(user) { + $rootScope.globals = { + user: user, + token: user.token + } + } + + function clearCredentials() { + $rootScope.globals = {}; + } + + return{ + login: login, + setCredentials: setCredentials, + clearCredentials: clearCredentials + }; + }]); + }) + }); + + beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, _$timeout_, AuthenticationService, _UserService_, _FlashService_, _apiRoutes_) { + $location = _$location_; + $httpBackend = $httpBackend; + $rootScope = _$rootScope_.$new(); + $timeout = _$timeout_; + AuthenticationServiceMock = AuthenticationService; + UserService = _UserService_; + FlashService = _FlashService_; + apiRoutes = _apiRoutes_; + + createController = function() { + return $controller('RegisterController', { + '$rootScope': $rootScope, + AuthenticationService: AuthenticationServiceMock, + UserService: _UserService_, + FlashService: _FlashService_ + }); + }; + })); + + describe('register()', function() { + it('should register successfully', inject(function($controller, $httpBackend, $location) { + shouldPass = true; + + $httpBackend.expect('POST', apiRoutes.register) + .respond({ + username: 'test', + token: 'tok3en' + }); + + + var registerController = createController(); + registerController.user = { + username: 'test', + password: 's3cr3t' + }; + + registerController.register(); + $httpBackend.flush(); + $timeout.flush(); + + $location.path().should.be.equal('/'); + })); + + it('should fail to register on bad parameter', inject(function($controller, $httpBackend, $location) { + shouldPass = false; + + $httpBackend.expect('POST', apiRoutes.register) + .respond(400); + + + var registerController = createController(); + registerController.user = { + username: 'test', + password: 'secret' + }; + + registerController.register(); + $httpBackend.flush(); + $timeout.flush(); + + $location.path().should.be.equal('/login'); + })); + + it('should fail to register on duplicate user', inject(function($controller, $httpBackend, $location) { + shouldPass = false; + + $httpBackend.expect('POST', apiRoutes.register) + .respond(409); + + + var registerController = createController(); + registerController.user = { + username: 'test', + password: 'secret' + }; + + registerController.register(); + $httpBackend.flush(); + $timeout.flush(); + + $location.path().should.be.equal('/login'); + })); + }); +}); \ No newline at end of file