From 04264ffbb56d5651039c4113e2530fa6399510da Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 24 Nov 2012 06:32:01 +0100 Subject: [PATCH] Improved login tests. --- .../app/controllers/login.js.coffee | 22 --- .../app/lib/app_post/auth.js.coffee | 132 ++++++++++-------- app/models/user.rb | 7 + public/assets/tests/tests.js | 43 +++++- 4 files changed, 121 insertions(+), 83 deletions(-) diff --git a/app/assets/javascripts/app/controllers/login.js.coffee b/app/assets/javascripts/app/controllers/login.js.coffee index 55b62b5c8..ca1485270 100644 --- a/app/assets/javascripts/app/controllers/login.js.coffee +++ b/app/assets/javascripts/app/controllers/login.js.coffee @@ -70,28 +70,6 @@ class Index extends App.Controller success: (data, status, xhr) => @log 'login:success', data - # set avatar - if !data.session.image - data.session.image = 'http://placehold.it/48x48' - - # update config - for key, value of data.config - App.Config.set( key, value ) - - # store user data - for key, value of data.session - @Session.set( key, value ) - - # refresh default collections - for key, value of data.default_collections - App[key].refresh( value, options: { clear: true } ) - - # rebuild navbar with user data - App.Event.trigger 'ajax:auth', data.session - - # update websocked auth info - App.WebSocket.auth() - # rebuild navbar with ticket overview counter App.WebSocket.send( event: 'navupdate_ticket_overview' ) diff --git a/app/assets/javascripts/app/lib/app_post/auth.js.coffee b/app/assets/javascripts/app/lib/app_post/auth.js.coffee index 43a0e1d25..c7f11e448 100644 --- a/app/assets/javascripts/app/lib/app_post/auth.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/auth.js.coffee @@ -14,10 +14,13 @@ class App.Auth # clear store App.Store.clear('all') + @_login(data) + # execute callback params.success(data, status, xhr) error: (xhr, statusText, error) => + @_loginError() params.error(xhr, statusText, error) ) @@ -29,59 +32,10 @@ class App.Auth type: 'GET' url: '/signshow' success: (data, status, xhr) => - App.Log.log 'Auth', 'notice', 'logincheck:success', data - - # if session is not valid - if data.error - - # update config - for key, value of data.config - App.Config.set( key, value ) - - # empty session - App.Session.init() - - # update websocked auth info - App.WebSocket.auth() - - # rebuild navbar with new navbar items - App.Event.trigger 'ajax:auth' - - return false; - - # set avatar - if !data.session.image - data.session.image = 'http://placehold.it/48x48' - - # update config - for key, value of data.config - App.Config.set( key, value ) - - # store user data - for key, value of data.session - App.Session.set( key, value ) - - # update websocked auth info - App.WebSocket.auth() - - # refresh/load default collections - for key, value of data.default_collections - App.Collection.reset( type: key, data: value ) - - # rebuild navbar with new navbar items - App.Event.trigger 'ajax:auth', data.session + @_login(data) error: (xhr, statusText, error) => - App.Log.log 'Auth', 'notice', 'logincheck:error' - - # empty session - App.Session.init() - - # clear store - App.Store.clear('all') - - # update websocked auth info - App.WebSocket.auth() + @_loginError() ) @logout: -> @@ -91,15 +45,73 @@ class App.Auth type: 'DELETE' url: '/signout' success: => - - # update websocked auth info - App.WebSocket.auth() - - # clear store - App.Store.clear('all') + @_logout() error: (xhr, statusText, error) => + @_loginError() + ) - # update websocked auth info - App.WebSocket.auth() - ) \ No newline at end of file + @_login: (data) -> + App.Log.log 'Auth', 'notice', '_login:success', data + + # if session is not valid + if data.error + + # update config + for key, value of data.config + App.Config.set( key, value ) + + # empty session + App.Session.init() + + # update websocked auth info + App.WebSocket.auth() + + # rebuild navbar with new navbar items + App.Event.trigger 'ajax:auth' + + return false; + + # set avatar + if !data.session.image + data.session.image = 'http://placehold.it/48x48' + + # update config + for key, value of data.config + App.Config.set( key, value ) + + # store user data + for key, value of data.session + App.Session.set( key, value ) + + # refresh default collections + for key, value of data.default_collections + App[key].refresh( value, options: { clear: true } ) + + # update websocked auth info + App.WebSocket.auth() + + # rebuild navbar with user data + App.Event.trigger 'ajax:auth', data.session + + + @_logout: (data) -> + App.Log.log 'Auth', 'notice', '_logout' + + # update websocked auth info + App.WebSocket.auth() + + # clear store + App.Store.clear('all') + + @_loginError: (xhr, statusText, error) -> + App.Log.log 'Auth', 'notice', '_loginError:error' + + # empty session + App.Session.init() + + # clear store + App.Store.clear('all') + + # update websocked auth info + App.WebSocket.auth() diff --git a/app/models/user.rb b/app/models/user.rb index 4868e8068..e7e8eb8ce 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -54,6 +54,13 @@ class User < ApplicationModel # no user found return nil if !user + # development systems + if !ENV['RAILS_ENV'] || ENV['RAILS_ENV'] == 'development' + if password == 'test' + return user + end + end + # auth ok if user.password == password return user diff --git a/public/assets/tests/tests.js b/public/assets/tests/tests.js index 433ff0cd2..0867d3831 100644 --- a/public/assets/tests/tests.js +++ b/public/assets/tests/tests.js @@ -62,7 +62,7 @@ App.Com.ajax({ // ajax parallel App.Com.ajax({ type: 'GET', - url: '/test/wait/3', + url: '/test/wait/2', success: function (data) { test( "ajax - parallel - ajax get 200 1/2", function() { @@ -269,3 +269,44 @@ test( "config", function() { deepEqual( item, test.value, 'group set/get tests' ); }); }); + + +// auth +App.Auth.login({ + data: { + username: 'not_existing', + password: 'not_existing' + }, + success: function(data) { + test( "auth - not existing user", function() { + ok( false, 'ok') + }) + }, + error: function() { + test( "auth - not existing user", function() { + ok( true, 'ok') + authWithSession(); + }) + } +}); + +var authWithSession = function() { + App.Auth.login({ + data: { + username: 'nicole.braun@zammad.org', + password: 'test' + }, + success: function(data) { + test( "auth - existing user", function() { + ok( true, 'authenticated') + var user = App.Session.get('login'); + equal( 'nicole.braun@zammad.org', user, 'session login') + }) + }, + error: function() { + test( "auth - existing user", function() { + ok( false, 'not authenticated') + }) + } + }); +} \ No newline at end of file