Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. Failed: Cannot read property 'userProfile' of undefined
  2.  
  3. import { Injectable } from '@angular/core';
  4. import { Http, Response } from '@angular/http';
  5. import { Observable } from 'rxjs';
  6. import 'rxjs/add/operator/map'
  7. import { Router } from "@angular/router";
  8.  
  9. @Injectable()
  10. export class AuthenticationService {
  11. public token: string;
  12. public authLevel: number;
  13.  
  14. constructor(
  15. private _http: Http,
  16. private _router: Router
  17. ) {
  18. // set token if saved in local storage
  19. const currentUser = JSON.parse(localStorage.getItem('currentUser'));
  20. if(currentUser) {
  21. this.token = currentUser && currentUser.token;
  22. this.authLevel = currentUser.userGroup.authLevel;
  23. } else {
  24. this._router.navigate(['/login']);
  25. }
  26. }
  27.  
  28. login(email: string, password: string): Observable<boolean> {
  29. return this._http.post('/login', { email: email, password: password })
  30. .map((response: Response) => {
  31. // login successful if there's a jwt token in the response
  32. let responseJSONParsed = response.json();
  33. let token = responseJSONParsed.token;
  34. this.authLevel = responseJSONParsed.extras.userProfile.userGroup.authLevel;
  35. if (token) {
  36. // set token property
  37. this.token = token;
  38.  
  39. // store username and jwt token in local storage to keep user logged in between page refreshes
  40. localStorage.setItem('currentUser', JSON.stringify({...responseJSONParsed.extras.userProfile, token: token}));
  41.  
  42. // return true to indicate successful login
  43. return true;
  44. } else {
  45. // return false to indicate failed login
  46. return false;
  47. }
  48. });
  49. }
  50.  
  51. import { HttpModule, Http, BaseRequestOptions, RequestOptions, ResponseOptions, Response } from '@angular/http';
  52. import { Router } from "@angular/router";
  53. import { MockBackend } from '@angular/http/testing';
  54. import { RouterTestingModule } from '@angular/router/testing';
  55. import { async, inject, TestBed} from "@angular/core/testing";
  56. import { AuthenticationService } from "../../services/authentication.service";
  57. import { mockLoginResponse} from "../../mockData/responses/mockLoginResponse";
  58.  
  59. describe('Service: Authentication Service', () => {
  60. let router = {
  61. navigate: jasmine.createSpy('navigate')
  62. };
  63.  
  64. beforeEach(() => {
  65. TestBed.configureTestingModule({
  66. providers: [
  67. AuthenticationService,
  68.  
  69. MockBackend,
  70. BaseRequestOptions,
  71. {
  72. provide: Http,
  73. useFactory: (backend, options) => new Http(backend, options),
  74. deps: [MockBackend, BaseRequestOptions]
  75. },
  76. { provide: Router, useValue: router }
  77. ],
  78. declarations: [
  79.  
  80. ],
  81. imports: [
  82. HttpModule,
  83.  
  84. ],
  85. });
  86. });
  87.  
  88. it('should construct', async(inject([AuthenticationService, MockBackend], (authenticationService, mockBackend) => {
  89. expect(authenticationService).toBeDefined();
  90. })));
  91.  
  92. describe('login method', () => {
  93. const mockResponse = JSON.stringify(mockLoginResponse.successful);
  94.  
  95. it('should return an observable', async(inject([AuthenticationService, MockBackend], (authenticationService, mockBackend) => {
  96. mockBackend.connections.subscribe(conn => {
  97. conn.mockRespond(new Response(new ResponseOptions({ body: JSON.stringify(mockResponse) })));
  98. });
  99.  
  100. const result = authenticationService.login();
  101.  
  102. result.subscribe((response) => {
  103. expect(response).toEqual(1);
  104. });
  105.  
  106. })));
  107. });
  108. });
  109.  
  110. export let mockLoginResponse = {
  111. successful : {
  112. "success": true,
  113. "extras": {
  114. "userProfile": {
  115. "email": "test12",
  116. "username": "test12",
  117. "userToAccount": {
  118. "_id": "5968db040e132d4f53a1019a",
  119. "user": "5968db040e132d4f53a10199",
  120. "userGroup": "596605a46c792e49ecfc936c",
  121. "account": "59676afff29a55b615b9ab1a",
  122. "__v": 0,
  123. "default": true
  124. },
  125. "account": {
  126. "address": "blahblah",
  127. "name": "blahblah",
  128. "_id": "59676afff29a55b615b9ab1a"
  129. },
  130. "userGroup": {
  131. "__v": 1,
  132. "authLevel": 0,
  133. "name": "superadmin",
  134. "_id": "596605a46c792e49ecfc936c"
  135. }
  136. }
  137. },
  138. "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1OTY4ZGIwNDBlMTMyZDRmNTNhMTAxOTkiLCJpc3MiOiJkZWdvdWxkLWxvZ2luLmRldiIsInBlcm1pc3Npb25zIjoiIiwiaWF0IjoxNTAwNDY5MjMwfQ.H7v2wJXhrHvGw4mMi3SjF5seQMQV5Nu7pgrL1wqETzs"
  139. }
  140. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement