1
0

Added tests

This commit is contained in:
Giuseppe Digilio
2018-05-17 11:05:26 +02:00
parent 240e1ee0af
commit f66dd32658
4 changed files with 161 additions and 43 deletions

View File

@@ -0,0 +1,98 @@
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController, } from '@angular/common/http/testing';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { Router } from '@angular/router';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { AuthInterceptor } from './auth.interceptor';
import { AuthService } from './auth.service';
import { DSpaceRESTv2Service } from '../dspace-rest-v2/dspace-rest-v2.service';
import { RestRequestMethod } from '../data/request.models';
import { RouterStub } from '../../shared/testing/router-stub';
import { TruncatablesState } from '../../shared/truncatable/truncatable.reducer';
import { AuthServiceStub } from '../../shared/testing/auth-service-stub';
describe(`AuthInterceptor`, () => {
let service: DSpaceRESTv2Service;
let httpMock: HttpTestingController;
const authServiceStub = new AuthServiceStub();
const store: Store<TruncatablesState> = jasmine.createSpyObj('store', {
/* tslint:disable:no-empty */
dispatch: {},
/* tslint:enable:no-empty */
select: Observable.of(true)
});
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
DSpaceRESTv2Service,
{provide: AuthService, useValue: authServiceStub},
{provide: Router, useClass: RouterStub},
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
{provide: Store, useValue: store},
],
});
service = TestBed.get(DSpaceRESTv2Service);
httpMock = TestBed.get(HttpTestingController);
});
describe('when has a valid token', () => {
it('should not add an Authorization header when were sending a HTTP request to \'authn\' endpoint', () => {
service.request(RestRequestMethod.Post, 'dspace-spring-rest/api/authn/login', 'password=password&user=user').subscribe((response) => {
expect(response).toBeTruthy();
});
const httpRequest = httpMock.expectOne(`dspace-spring-rest/api/authn/login`);
const token = httpRequest.request.headers.get('authorization');
expect(token).toBeNull();
});
it('should add an Authorization header when were sending a HTTP request to \'authn\' endpoint', () => {
service.request(RestRequestMethod.Post, 'dspace-spring-rest/api/submission/workspaceitems', 'test').subscribe((response) => {
expect(response).toBeTruthy();
});
const httpRequest = httpMock.expectOne(`dspace-spring-rest/api/submission/workspaceitems`);
expect(httpRequest.request.headers.has('authorization'));
const token = httpRequest.request.headers.get('authorization');
expect(token).toBe('Bearer token_test');
});
});
describe('when has an expired token', () => {
beforeEach(() => {
authServiceStub.setTokenAsExpired();
});
afterEach(() => {
authServiceStub.setTokenAsNotExpired();
});
it('should redirect to login', () => {
service.request(RestRequestMethod.Post, 'dspace-spring-rest/api/submission/workspaceitems', 'password=password&user=user').subscribe((response) => {
expect(response).toBeTruthy();
});
service.request(RestRequestMethod.Post, 'dspace-spring-rest/api/submission/workspaceitems', 'password=password&user=user');
httpMock.expectNone('dspace-spring-rest/api/submission/workspaceitems');
});
})
});