autentication.service.spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. should.exist(user);
  20. user.username.should.be.equal('test');
  21. user.token.should.be.equal('tok3n');
  22. should.not.exist(data.message);
  23. });
  24. });
  25. it('should fail to login and return message', function() {
  26. httpBackend
  27. .when('POST', 'api/users/login')
  28. .respond(401);
  29. AuthenticationService.login('test', 'password', function(data) {
  30. data.succes.should.be.false;
  31. var message = data.message;
  32. should.exist(message);
  33. message.should.be.equal('Authentication fail');
  34. should.not.exist(data.user);
  35. })
  36. });
  37. });
  38. describe('* setCredentials', function() {
  39. it('should store the given user in scope and store', function() {
  40. AuthenticationService.setCredentials({
  41. username: 'test',
  42. token: 'tok3n'
  43. });
  44. var globals = rootScope.globals;
  45. should.exist(globals);
  46. should.exist(globals.user);
  47. globals.user.username.should.be.equal('test');
  48. globals.user.token.should.be.equal('tok3n');
  49. should.exist(globals.token);
  50. globals.token.should.be.equal('tok3n');
  51. should.exist(cookieStore.get('globals'));
  52. cookieStore.get('globals').should.eql(globals);
  53. });
  54. });
  55. describe('* clearCredentials()', function() {
  56. it('should clean credentials', function() {
  57. AuthenticationService.clearCredentials();
  58. should.exist(rootScope.globals);
  59. rootScope.globals.should.eql({});
  60. should.not.exist(cookieStore.get('globals'))
  61. });
  62. });
  63. });