register.controller.spec.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. describe('RegisterController', function() {
  2. var $location,
  3. $rootScope,
  4. $timeout,
  5. $httpBackend,
  6. UserService,
  7. AuthenticationServiceMock,
  8. FlashService,
  9. createController,
  10. apiRoutes,
  11. shouldPass;
  12. beforeEach(module('cloudbudget'));
  13. beforeEach(function() {
  14. module(function($provide) {
  15. $provide.factory('AuthenticationService', ['$q', '$rootScope', function($q, $rootScope) {
  16. function login(username, password ) {
  17. var data;
  18. if(shouldPass){
  19. data = {
  20. success: true,
  21. user: {
  22. username: 'test',
  23. token: 'tok3n'
  24. }
  25. };
  26. } else {
  27. data = {
  28. success: false,
  29. message: 'Authentication fail'
  30. };
  31. }
  32. return $q.when(data);
  33. }
  34. function setCredentials(user) {
  35. $rootScope.globals = {
  36. user: user,
  37. token: user.token
  38. }
  39. }
  40. function clearCredentials() {
  41. $rootScope.globals = {};
  42. }
  43. return{
  44. login: login,
  45. setCredentials: setCredentials,
  46. clearCredentials: clearCredentials
  47. };
  48. }]);
  49. })
  50. });
  51. beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, _$timeout_, AuthenticationService, _UserService_, _FlashService_, _apiRoutes_) {
  52. $location = _$location_;
  53. $httpBackend = $httpBackend;
  54. $rootScope = _$rootScope_.$new();
  55. $timeout = _$timeout_;
  56. AuthenticationServiceMock = AuthenticationService;
  57. UserService = _UserService_;
  58. FlashService = _FlashService_;
  59. apiRoutes = _apiRoutes_;
  60. createController = function() {
  61. return $controller('RegisterController', {
  62. '$rootScope': $rootScope,
  63. AuthenticationService: AuthenticationServiceMock,
  64. UserService: _UserService_,
  65. FlashService: _FlashService_
  66. });
  67. };
  68. }));
  69. describe('register()', function() {
  70. it('should register successfully', inject(function($controller, $httpBackend, $location) {
  71. shouldPass = true;
  72. $httpBackend.expect('POST', apiRoutes.register)
  73. .respond({
  74. username: 'test',
  75. token: 'tok3en'
  76. });
  77. var registerController = createController();
  78. registerController.user = {
  79. username: 'test',
  80. password: 's3cr3t',
  81. language: 'en'
  82. };
  83. registerController.register();
  84. $httpBackend.flush();
  85. $timeout.flush();
  86. $location.path().should.be.equal('/accounts');
  87. }));
  88. it('should fail to register on bad parameter', inject(function($controller, $httpBackend, $location) {
  89. shouldPass = false;
  90. $httpBackend.expect('POST', apiRoutes.register)
  91. .respond(400);
  92. var registerController = createController();
  93. registerController.user = {
  94. username: 'test',
  95. password: 'secret'
  96. };
  97. registerController.register();
  98. $httpBackend.flush();
  99. $timeout.flush();
  100. $location.path().should.be.equal('/login');
  101. }));
  102. it('should fail to register on duplicate user', inject(function($controller, $httpBackend, $location) {
  103. shouldPass = false;
  104. $httpBackend.expect('POST', apiRoutes.register)
  105. .respond(409);
  106. var registerController = createController();
  107. registerController.user = {
  108. username: 'test',
  109. password: 'secret'
  110. };
  111. registerController.register();
  112. $httpBackend.flush();
  113. $timeout.flush();
  114. $location.path().should.be.equal('/login');
  115. }));
  116. });
  117. });