authCtrl tests now working, replaced window for $window

This commit is contained in:
Dušan Juretić
2013-10-01 23:20:10 -03:00
parent 43be09a5d5
commit 89b4d2ece0
5 changed files with 43 additions and 41 deletions

View File

@@ -7,15 +7,18 @@ module.exports = function(config) {
basePath: '', basePath: '',
// testing framework to use (jasmine/mocha/qunit/...) // 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 // list of files / patterns to load in the browser
files: [ files: [
'public/bower_components/angular/angular.js', 'public/bower_components/angular/angular.js',
'public/bower_components/angular-mocks/angular-mocks.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',
'public/js/**/*.js', 'public/js/**/*.js',
'test/mock/**/*.js', 'test/mock/**/*.js',
'test/spec/*.js',
'test/spec/**/*.js' 'test/spec/**/*.js'
], ],

View File

@@ -71,6 +71,7 @@
"karma-phantomjs-launcher": "~0.1.0", "karma-phantomjs-launcher": "~0.1.0",
"karma": "~0.10.2", "karma": "~0.10.2",
"karma-ng-html2js-preprocessor": "~0.1.0", "karma-ng-html2js-preprocessor": "~0.1.0",
"karma-chai-plugins": "~0.1.0",
"mocha": "~1.12.1", "mocha": "~1.12.1",
"karma-mocha": "~0.1.0" "karma-mocha": "~0.1.0"
} }

View File

@@ -4,8 +4,8 @@
The authentication controller (login & facebook) The authentication controller (login & facebook)
*/ */
habitrpg.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$location', 'API_URL', habitrpg.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$location', '$window','API_URL',
function($scope, $rootScope, User, $http, $location, API_URL) { function($scope, $rootScope, User, $http, $location, $window, API_URL) {
var runAuth; var runAuth;
var showedFacebookMessage; var showedFacebookMessage;
@@ -25,7 +25,7 @@ habitrpg.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$loca
runAuth = function(id, token) { runAuth = function(id, token) {
User.authenticate(id, token, function(err) { User.authenticate(id, token, function(err) {
window.location.href = '/'; $window.location.href = '/';
//$rootScope.modals.login = false; //$rootScope.modals.login = false;
}); });
}; };
@@ -41,22 +41,22 @@ habitrpg.controller("AuthCtrl", ['$scope', '$rootScope', 'User', '$http', '$loca
runAuth(data.id, data.apiToken); runAuth(data.id, data.apiToken);
}).error(function(data, status, headers, config) { }).error(function(data, status, headers, config) {
if (status === 0) { 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) { } else if (!!data && !!data.err) {
alert(data.err); $window.alert(data.err);
} else { } else {
alert("ERROR: " + status); $window.alert("ERROR: " + status);
} }
}); });
}; };
function errorAlert(data, status, headers, config) { function errorAlert(data, status, headers, config) {
if (status === 0) { 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) { } else if (!!data && !!data.err) {
alert(data.err); $window.alert(data.err);
} else { } else {
alert("ERROR: " + status); $window.alert("ERROR: " + status);
} }
} }

View File

@@ -5,8 +5,8 @@
*/ */
angular.module('userServices', []). angular.module('userServices', []).
factory('User', ['$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, API_URL, STORAGE_USER_ID, STORAGE_SETTINGS_ID) { function($rootScope, $http, $location, $window, API_URL, STORAGE_USER_ID, STORAGE_SETTINGS_ID) {
var authenticated = false, var authenticated = false,
defaultSettings = { defaultSettings = {
auth: { apiId: '', apiToken: ''}, auth: { apiId: '', apiToken: ''},
@@ -150,7 +150,7 @@ angular.module('userServices', []).
*/ */
set: function(k, v) { set: function(k, v) {
var log = { op: 'set', data: {} }; 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; log.data[k] = v;
userServices.log(log); userServices.log(log);
}, },
@@ -158,7 +158,7 @@ angular.module('userServices', []).
setMultiple: function(obj){ setMultiple: function(obj){
var log = { op: 'set', data: {} }; var log = { op: 'set', data: {} };
_.each(obj, function(v,k){ _.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; log.data[k] = v;
}); });
userServices.log(log); userServices.log(log);
@@ -186,16 +186,16 @@ angular.module('userServices', []).
//If user does not have ApiID that forward him to settings. //If user does not have ApiID that forward him to settings.
if (!settings.auth.apiId || !settings.auth.apiToken) { 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(); // 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.err) return alert(search.err);
if (search._id && search.apiToken) { if (search._id && search.apiToken) {
userServices.authenticate(search._id, search.apiToken, function(){ userServices.authenticate(search._id, search.apiToken, function(){
window.location.href='/'; $window.location.href='/';
}); });
} else { } else {
if (window.location.pathname.indexOf('/static') !== 0){ if ($window.location.pathname.indexOf('/static') !== 0){
localStorage.clear(); localStorage.clear();
window.location.href = '/logout'; $window.location.href = '/logout';
} }
} }
} else { } else {

View File

@@ -2,38 +2,36 @@
describe('Auth Controller', function() { describe('Auth Controller', function() {
// do we need this, or has it since been added to Karma proper? beforeEach(module('habitrpg'));
/*beforeEach(function(){
this.addMatchers({
toEqualData: function(expected) {
return angular.equals(this.actual, expected);
}
});
});*/
//beforeEach(module('phonecatServices'));
describe('AuthCtrl', function(){ describe('AuthCtrl', function(){
var scope, ctrl, $httpBackend; var scope, ctrl, user, $httpBackend, $window;
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) { beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_; $httpBackend = _$httpBackend_;
$httpBackend.expectGET('api/v1/users/auth/local').
respond({id: 'abc', apiToken: 'abc'});
scope = $rootScope.$new(); 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() { it('should log in users with correct uname / pass', function() {
// expect(scope.phones).toEqual([]); $httpBackend.expectPOST('/api/v1/user/auth/local').respond({id: 'abc', token: 'abc'});
// $httpBackend.flush(); scope.auth();
// $httpBackend.flush();
// expect(scope.phones).toEqualData( expect(user.authenticate).to.have.been.calledOnce;
// [{name: 'Nexus S'}, {name: 'Motorola DROID'}]); 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;
}); });
}); });