-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Submitting the login form results in acquiring a token from the API.
- Loading branch information
1 parent
c9be87c
commit 71b1338
Showing
5 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
frontend/src/main/angular/src/app/auth/services/auth.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,44 @@ | |||
import { TestBed, getTestBed } from '@angular/core/testing'; | |||
import { RouterTestingModule } from '@angular/router/testing'; | |||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | |||
|
|||
import { AuthService } from './auth.service'; | |||
import { TokenService } from './token.service'; | |||
import { Credentials } from '../credentials'; | |||
import { Observable } from 'rxjs'; | |||
|
|||
describe('AuthService', () => { | |||
let injector: TestBed; | |||
let service: AuthService; | |||
let tokenService: TokenService; | |||
|
|||
beforeEach(() => { | |||
TestBed.configureTestingModule({ | |||
providers: [ | |||
AuthService | |||
], | |||
imports: [ | |||
RouterTestingModule, | |||
HttpClientTestingModule | |||
] | |||
}); | |||
injector = getTestBed(); | |||
service = injector.get(AuthService); | |||
tokenService = injector.get(TokenService); | |||
}); | |||
|
|||
it('should be created', () => { | |||
const service: AuthService = TestBed.get(AuthService); | |||
expect(service).toBeTruthy(); | |||
}); | |||
|
|||
it('should have redirectToUrl option set to "cookies" by default', () => { | |||
expect(service.redirectToUrl).toEqual('/cookies'); | |||
}); | |||
|
|||
it('should call token service for ResponseHeaders when logging in a user', () => { | |||
spyOn(tokenService, 'getResponseHeaders').and.returnValue(new Observable<string>()); | |||
service.login(new Credentials('','')); | |||
expect(tokenService.getResponseHeaders).toHaveBeenCalled(); | |||
}); | |||
}); |
33 changes: 33 additions & 0 deletions
33
frontend/src/main/angular/src/app/auth/services/auth.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,33 @@ | |||
import { Injectable } from '@angular/core'; | |||
|
|||
import { TokenService } from './token.service' | |||
import { Credentials } from '../credentials'; | |||
import { HttpResponse } from '@angular/common/http'; | |||
import { Router } from '@angular/router'; | |||
|
|||
@Injectable({ | |||
providedIn: 'root' | |||
}) | |||
export class AuthService { | |||
|
|||
static readonly TOKEN_STORAGE_KEY = 'token'; | |||
redirectToUrl: string = '/cookies'; | |||
|
|||
constructor(private router: Router, private tokenService: TokenService) { } | |||
|
|||
public login(credentials: Credentials): void { | |||
this.tokenService.getResponseHeaders(credentials) | |||
.subscribe((res: HttpResponse<any>) => { | |||
this.saveToken(res.headers.get('authorization')); | |||
this.router.navigate([this.redirectToUrl]); | |||
}); | |||
} | |||
|
|||
private saveToken(token: string){ | |||
localStorage.setItem(AuthService.TOKEN_STORAGE_KEY, token); | |||
} | |||
|
|||
public getToken(): string { | |||
return localStorage.getItem(AuthService.TOKEN_STORAGE_KEY); | |||
} | |||
} |
54 changes: 54 additions & 0 deletions
54
frontend/src/main/angular/src/app/auth/services/token.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,54 @@ | |||
import { TestBed, getTestBed } from '@angular/core/testing'; | |||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | |||
|
|||
import { TokenService } from './token.service'; | |||
import { environment } from '../../../environments/environment'; | |||
import { HttpHeaders, HttpResponse } from '@angular/common/http'; | |||
import { Credentials } from '../credentials'; | |||
|
|||
describe('TokenService', () => { | |||
let injector: TestBed; | |||
let service: TokenService; | |||
let httpMock: HttpTestingController; | |||
let apiUrl = environment.apiUrl; | |||
let HttpResponseMock: HttpResponse<any> = { | |||
body: null, | |||
type: null, | |||
clone:null, | |||
status: 200, | |||
statusText: null, | |||
url: null, | |||
ok: true, | |||
headers: new HttpHeaders({ 'authorization': 'bearer token' }) | |||
}; | |||
|
|||
beforeEach(() => { | |||
TestBed.configureTestingModule({ | |||
providers: [TokenService], | |||
imports: [ HttpClientTestingModule ] | |||
}); | |||
injector = getTestBed(); | |||
service = injector.get(TokenService); | |||
httpMock = injector.get(HttpTestingController); | |||
}); | |||
|
|||
afterEach(() => { | |||
httpMock.verify(); | |||
}); | |||
|
|||
it('should be created', () => { | |||
const service: TokenService = TestBed.get(TokenService); | |||
expect(service).toBeTruthy(); | |||
}); | |||
|
|||
it('should call login endpoint', () => { | |||
service.getResponseHeaders(new Credentials('', '')) | |||
.subscribe((res: HttpResponse<any>) => { | |||
expect(res.headers.get('authorization')).toEqual('bearer token'); | |||
}); | |||
|
|||
const req = httpMock.expectOne(apiUrl + '/login'); | |||
expect(req.request.method).toBe("POST"); | |||
req.flush({}, HttpResponseMock); | |||
}); | |||
}); |
24 changes: 24 additions & 0 deletions
24
frontend/src/main/angular/src/app/auth/services/token.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,24 @@ | |||
import { Injectable } from '@angular/core'; | |||
import { HttpClient, HttpHeaders } from '@angular/common/http'; | |||
|
|||
import { environment } from '../../../environments/environment'; | |||
import { Credentials } from '../credentials'; | |||
|
|||
const httpOptions = { | |||
headers: new HttpHeaders({ 'Content-Type': 'application/json' }), | |||
observe: 'response' as 'response' | |||
}; | |||
const API_URL = environment.apiUrl; | |||
|
|||
@Injectable({ | |||
providedIn: 'root' | |||
}) | |||
export class TokenService { | |||
|
|||
constructor(private http: HttpClient) { } | |||
|
|||
public getResponseHeaders(credentials: Credentials) { | |||
let loginUrl = API_URL + '/login'; | |||
return this.http.post(loginUrl, credentials, httpOptions); | |||
} | |||
} |