autentication.service.spec.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. describe('AuthenticationService', function() {
  3. var AuthenticationService, httpBackend, rootScope, cookieStore;
  4. beforeEach(module('cloudbudget'));
  5. beforeEach(inject(function(_AuthenticationService_, $httpBackend, $rootScope, $cookieStore) {
  6. AuthenticationService = _AuthenticationService_;
  7. httpBackend = $httpBackend;
  8. rootScope = $rootScope;
  9. cookieStore = $cookieStore;
  10. }));
  11. describe('* login()', function() {
  12. it('should login successfully and return user', function() {
  13. httpBackend
  14. .when('POST', 'api/users/login')
  15. .respond(200, {username: 'test', token: 'tok3n'});
  16. AuthenticationService.login('test', 'password', function(data) {
  17. data.success.should.be.true;
  18. var user = data.user;
  19. httpBackend.flush();
  20. should.exist(user);
  21. user.username.should.be.equal('test');
  22. user.token.should.be.equal('tok3n');
  23. should.not.exist(data.message);
  24. });
  25. });
  26. it('should fail to login and return message', function() {
  27. httpBackend
  28. .when('POST', 'api/users/login')
  29. .respond(401, {message: 'Authentication failed'});
  30. AuthenticationService.login('test', 'password', function(data) {
  31. data.succes.should.be.false;
  32. var message = data.message;
  33. httpBackend.flush();
  34. should.exist(message);
  35. message.should.be.equal('Authentication fail');
  36. should.not.exist(data.user);
  37. })
  38. });
  39. });
  40. describe('* setCredentials', function() {
  41. it('should store the given user in scope and store', function() {
  42. AuthenticationService.setCredentials({
  43. username: 'test',
  44. token: 'tok3n'
  45. });
  46. var globals = rootScope.globals;
  47. should.exist(globals);
  48. should.exist(globals.user);
  49. globals.user.username.should.be.equal('test');
  50. globals.user.token.should.be.equal('tok3n');
  51. should.exist(globals.token);
  52. globals.token.should.be.equal('tok3n');
  53. should.exist(cookieStore.get('globals'));
  54. cookieStore.get('globals').should.eql(globals);
  55. });
  56. });
  57. describe('* clearCredentials()', function() {
  58. it('should clean credentials', function() {
  59. AuthenticationService.clearCredentials();
  60. should.exist(rootScope.globals);
  61. rootScope.globals.should.eql({});
  62. should.not.exist(cookieStore.get('globals'))
  63. });
  64. });
  65. });