login.controller.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. describe('LoginController', function() {
  2. var scope,
  3. $location,
  4. $timeout,
  5. $rootScope,
  6. AuthenticationServiceMock,
  7. createController,
  8. shouldPass;
  9. beforeEach(module('cloudbudget'));
  10. beforeEach(function(){
  11. module(function($provide) {
  12. $provide.factory('AuthenticationService', ['$q', '$rootScope', function($q, $rootScope) {
  13. function login(username, password ) {
  14. var data;
  15. if(shouldPass){
  16. data = {
  17. success: true,
  18. user: {
  19. username: 'test',
  20. token: 'tok3n'
  21. }
  22. };
  23. } else {
  24. data = {
  25. success: false,
  26. message: 'Authentication fail'
  27. };
  28. }
  29. return $q.when(data);
  30. }
  31. function setCredentials(user) {
  32. $rootScope.globals = {
  33. user: user,
  34. token: user.token
  35. }
  36. }
  37. function clearCredentials() {
  38. $rootScope.globals = {};
  39. }
  40. return{
  41. login: login,
  42. setCredentials: setCredentials,
  43. clearCredentials: clearCredentials
  44. };
  45. }]);
  46. });
  47. });
  48. beforeEach(inject(function ($rootScope, $controller, _$location_, _$timeout_, AuthenticationService) {
  49. $location = _$location_;
  50. scope = $rootScope.$new();
  51. $timeout = _$timeout_;
  52. AuthenticationServiceMock = AuthenticationService;
  53. createController = function() {
  54. return $controller('LoginController', {
  55. '$scope': scope,
  56. AuthenticationService: AuthenticationServiceMock
  57. });
  58. };
  59. }));
  60. describe('login()', function() {
  61. it('should log successfully', inject(function($controller, $location) {
  62. shouldPass = true;
  63. var loginController = createController();
  64. loginController.username = 'fail';
  65. loginController.password = 's3cr3t';
  66. loginController.login();
  67. $timeout.flush();
  68. $location.path().should.be.equal('/');
  69. }));
  70. it('should fail to log', inject(function($controller, $location) {
  71. shouldPass = false;
  72. var loginController = $controller('LoginController');
  73. loginController.username = 'fail';
  74. loginController.password = 'secret';
  75. loginController.login();
  76. $timeout.flush();
  77. $location.path().should.be.equal('/login');
  78. }));
  79. });
  80. });