diff --git a/karma.conf.js b/karma.conf.js index 5886046333..0c3f1095bc 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -7,15 +7,18 @@ module.exports = function(config) { basePath: '', // testing framework to use (jasmine/mocha/qunit/...) - frameworks: ['mocha'], + frameworks: ['mocha', 'chai', 'chai-as-promised', 'sinon-chai'], // list of files / patterns to load in the browser files: [ 'public/bower_components/angular/angular.js', 'public/bower_components/angular-mocks/angular-mocks.js', + 'public/bower_components/marked/lib/marked.js', + 'public/bower_components/habitrpg-shared/dist/habitrpg-shared.js', 'public/js/*.js', 'public/js/**/*.js', 'test/mock/**/*.js', + 'test/spec/*.js', 'test/spec/**/*.js' ], diff --git a/package.json b/package.json index 1939c440e2..5943e80c30 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "karma-phantomjs-launcher": "~0.1.0", "karma": "~0.10.2", "karma-ng-html2js-preprocessor": "~0.1.0", + "karma-chai-plugins": "~0.1.0", "mocha": "~1.12.1", "karma-mocha": "~0.1.0" } diff --git a/public/js/controllers/authCtrl.js b/public/js/controllers/authCtrl.js index f83db3397c..f1d4edecb3 100644 --- a/public/js/controllers/authCtrl.js +++ b/public/js/controllers/authCtrl.js @@ -4,8 +4,8 @@ The authentication controller (login & facebook) */ -habitrpg.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$location', 'API_URL', - function($scope, $rootScope, User, $http, $location, API_URL) { +habitrpg.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$location', '$window','API_URL', + function($scope, $rootScope, User, $http, $location, $window, API_URL) { var runAuth; var showedFacebookMessage; @@ -25,7 +25,7 @@ habitrpg.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$loca runAuth = function(id, token) { User.authenticate(id, token, function(err) { - window.location.href = '/'; + $window.location.href = '/'; //$rootScope.modals.login = false; }); }; @@ -41,22 +41,22 @@ habitrpg.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$loca runAuth(data.id, data.apiToken); }).error(function(data, status, headers, config) { if (status === 0) { - alert("Server not currently reachable, try again later"); + $window.alert("Server not currently reachable, try again later"); } else if (!!data && !!data.err) { - alert(data.err); + $window.alert(data.err); } else { - alert("ERROR: " + status); + $window.alert("ERROR: " + status); } }); }; function errorAlert(data, status, headers, config) { if (status === 0) { - alert("Server not currently reachable, try again later"); + $window.alert("Server not currently reachable, try again later"); } else if (!!data && !!data.err) { - alert(data.err); + $window.alert(data.err); } else { - alert("ERROR: " + status); + $window.alert("ERROR: " + status); } } diff --git a/public/js/services/userServices.js b/public/js/services/userServices.js index bc68a2be2a..cb1fe55471 100644 --- a/public/js/services/userServices.js +++ b/public/js/services/userServices.js @@ -5,8 +5,8 @@ */ angular.module('userServices', []). - factory('User', ['$rootScope', '$http', '$location', 'API_URL', 'STORAGE_USER_ID', 'STORAGE_SETTINGS_ID', - function($rootScope, $http, $location, API_URL, STORAGE_USER_ID, STORAGE_SETTINGS_ID) { + factory('User', ['$rootScope', '$http', '$location', '$window', 'API_URL', 'STORAGE_USER_ID', 'STORAGE_SETTINGS_ID', + function($rootScope, $http, $location, $window, API_URL, STORAGE_USER_ID, STORAGE_SETTINGS_ID) { var authenticated = false, defaultSettings = { auth: { apiId: '', apiToken: ''}, @@ -150,7 +150,7 @@ angular.module('userServices', []). */ set: function(k, v) { var log = { op: 'set', data: {} }; - window.habitrpgShared.helpers.dotSet(k, v, this.user); + $window.habitrpgShared.helpers.dotSet(k, v, this.user); log.data[k] = v; userServices.log(log); }, @@ -158,7 +158,7 @@ angular.module('userServices', []). setMultiple: function(obj){ var log = { op: 'set', data: {} }; _.each(obj, function(v,k){ - window.habitrpgShared.helpers.dotSet(k, v, userServices.user); + $window.habitrpgShared.helpers.dotSet(k, v, userServices.user); log.data[k] = v; }); userServices.log(log); @@ -186,16 +186,16 @@ angular.module('userServices', []). //If user does not have ApiID that forward him to settings. if (!settings.auth.apiId || !settings.auth.apiToken) { //var search = $location.search(); // FIXME this should be working, but it's returning an empty object when at a root url /?_id=... - var search = $location.search(window.location.search.substring(1)).$$search; // so we use this fugly hack instead + var search = $location.search($window.location.search.substring(1)).$$search; // so we use this fugly hack instead if (search.err) return alert(search.err); if (search._id && search.apiToken) { userServices.authenticate(search._id, search.apiToken, function(){ - window.location.href='/'; + $window.location.href='/'; }); } else { - if (window.location.pathname.indexOf('/static') !== 0){ + if ($window.location.pathname.indexOf('/static') !== 0){ localStorage.clear(); - window.location.href = '/logout'; + $window.location.href = '/logout'; } } } else { diff --git a/test/spec/authCtrlSpec.js b/test/spec/authCtrlSpec.js index b9f1dddf83..b48d362b32 100644 --- a/test/spec/authCtrlSpec.js +++ b/test/spec/authCtrlSpec.js @@ -2,38 +2,36 @@ describe('Auth Controller', function() { - // do we need this, or has it since been added to Karma proper? - /*beforeEach(function(){ - this.addMatchers({ - toEqualData: function(expected) { - return angular.equals(this.actual, expected); - } - }); - });*/ - - - //beforeEach(module('phonecatServices')); - + beforeEach(module('habitrpg')); describe('AuthCtrl', function(){ - var scope, ctrl, $httpBackend; + var scope, ctrl, user, $httpBackend, $window; beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) { $httpBackend = _$httpBackend_; - $httpBackend.expectGET('api/v1/users/auth/local'). - respond({id: 'abc', apiToken: 'abc'}); - scope = $rootScope.$new(); - ctrl = $controller(AuthCtrl, {$scope: scope}); + scope.loginUsername = 'user' + scope.loginPassword = 'pass' + $window = { location: { href: ""}, alert: sinon.spy() }; + user = { user: {}, authenticate: sinon.spy() }; + + ctrl = $controller('AuthCtrl', {$scope: scope, $window: $window, User: user}); })); - it('should log in users with correct uname / pass', function() { -// expect(scope.phones).toEqual([]); -// $httpBackend.flush(); -// -// expect(scope.phones).toEqualData( -// [{name: 'Nexus S'}, {name: 'Motorola DROID'}]); + $httpBackend.expectPOST('/api/v1/user/auth/local').respond({id: 'abc', token: 'abc'}); + scope.auth(); + $httpBackend.flush(); + expect(user.authenticate).to.have.been.calledOnce; + expect($window.alert).to.not.have.been.called; + }); + + it('should not log in users with incorrect uname / pass', function() { + $httpBackend.expectPOST('/api/v1/user/auth/local').respond(404, ''); + scope.auth(); + $httpBackend.flush(); + expect(user.authenticate).to.not.have.been.called; + expect($window.alert).to.have.been.calledOnce; }); });