Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. /* global xdescribe */
  2. 'use strict';
  3.  
  4. xdescribe('Service: Auth', function () {
  5.  
  6. // load the service's module
  7. beforeEach(module('cmp.auth'));
  8.  
  9. var $httpBackend,
  10. authRequestHandler,
  11. publisherRequestHandler,
  12. createAuth,
  13. credentials,
  14. user,
  15. config;
  16.  
  17. // Initialize the service and a mock scope
  18. beforeEach(inject(function ($injector) {
  19. var Auth;
  20.  
  21. config = $injector.get('cmp.config');
  22. $httpBackend = $injector.get('$httpBackend');
  23. user = {
  24. 'user_hash': 'the-user-hash',
  25. 'first_name': 'first-name',
  26. 'last_name': 'last-name'
  27. };
  28.  
  29. authRequestHandler = $httpBackend.when('POST', config.apiUrl + '/auth/publisher')
  30. .respond({
  31. 'status': {
  32. 'code': 200,
  33. 'error': false,
  34. 'text': 'success'
  35. },
  36. 'body': {
  37. 'user': {
  38. 'user_company_hash': 'user-company-hash',
  39. 'user_hash': 'user-hash',
  40. 'user_first_name': 'user-first-name',
  41. 'user_last_name': 'user-last-name'
  42. }
  43. }
  44. });
  45.  
  46. publisherRequestHandler = $httpBackend.when('GET', config.apiUrl + '/publisher')
  47. .respond({
  48. 'status': {
  49. 'code': 200,
  50. 'error': false,
  51. 'text': 'success'
  52. },
  53. 'body': {
  54. 'publisher_user': user,
  55. 'publisher_company': {}
  56. }
  57. });
  58. credentials = {
  59. email: 'the email',
  60. password: 'the password'
  61. };
  62.  
  63. Auth = $injector.get('Auth');
  64.  
  65. createAuth = function() {
  66. return Auth;
  67. };
  68.  
  69. }));
  70.  
  71.  
  72. it('should fetch authentication token', function() {
  73. $httpBackend.expectPOST(config.apiUrl + '/auth/publisher');
  74. $httpBackend.expectGET(config.apiUrl + '/publisher');
  75. createAuth().login(credentials);
  76. $httpBackend.flush();
  77. });
  78.  
  79. it('should allow publisher login', function () {
  80.  
  81. // current user should not exist
  82. var auth = createAuth();
  83. var thenCalled = false;
  84. var errorCalled = false;
  85. var callbackCalled = false;
  86. var callback = function(){ callbackCalled = true; };
  87.  
  88. expect(auth.getCurrentUser()).toEqual({});
  89.  
  90. // do login
  91. auth.login({
  92. email: 'the email',
  93. password: 'the password'
  94. }, callback)
  95. .then(function() {
  96. thenCalled = true;
  97. })
  98. .catch( function() {
  99. errorCalled = true;
  100. });
  101.  
  102. // trigger backend response
  103. $httpBackend.flush();
  104. // promise then function should have been called
  105. expect(thenCalled).toBe(true);
  106. // promise error function should not have been called
  107. expect(errorCalled).toBe(false);
  108. // callback function should have been called
  109. expect(callbackCalled).toBe(true);
  110.  
  111. // @TODO test Auth cookieStore usage
  112. });
  113.  
  114. afterEach(function() {
  115. $httpBackend.verifyNoOutstandingExpectation();
  116. $httpBackend.verifyNoOutstandingRequest();
  117. });
  118. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement