register.controller.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. };
  82. registerController.register();
  83. $httpBackend.flush();
  84. $timeout.flush();
  85. $location.path().should.be.equal('/');
  86. }));
  87. it('should fail to register on bad parameter', inject(function($controller, $httpBackend, $location) {
  88. shouldPass = false;
  89. $httpBackend.expect('POST', apiRoutes.register)
  90. .respond(400);
  91. var registerController = createController();
  92. registerController.user = {
  93. username: 'test',
  94. password: 'secret'
  95. };
  96. registerController.register();
  97. $httpBackend.flush();
  98. $timeout.flush();
  99. $location.path().should.be.equal('/login');
  100. }));
  101. it('should fail to register on duplicate user', inject(function($controller, $httpBackend, $location) {
  102. shouldPass = false;
  103. $httpBackend.expect('POST', apiRoutes.register)
  104. .respond(409);
  105. var registerController = createController();
  106. registerController.user = {
  107. username: 'test',
  108. password: 'secret'
  109. };
  110. registerController.register();
  111. $httpBackend.flush();
  112. $timeout.flush();
  113. $location.path().should.be.equal('/login');
  114. }));
  115. });
  116. });