Commit 20ead361 authored by Eduardo Sanz García's avatar Eduardo Sanz García
Browse files

test: strengthen the set of tests corrected token

parent 7f6f193b
Pipeline #2745 passed with stages
in 3 minutes and 32 seconds
import {
TestBed,
inject
inject,
// fakeAsync,
// flushMicrotasks
} from '@angular/core/testing';
import {
......@@ -22,7 +24,7 @@ import {
DEFAULT_CONF
} from './auth.config';
describe('AuthService', () => {
describe('AuthService (valid token)', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{
......@@ -45,4 +47,104 @@ describe('AuthService', () => {
it('should be created', inject([AuthService], (service: AuthService) => {
expect(service).toBeTruthy();
}));
it('should be authenticated', inject([AuthService], (service: AuthService) => {
const isAuthenticated = service.isAuthenticated();
isAuthenticated.subscribe(result => expect(result).toBeTruthy());
}));
it('should have credentials', inject([AuthService], (service: AuthService) => {
const credentials = service.credentials();
credentials.subscribe(result => expect(result).toBeTruthy());
}));
it('should have username', inject([AuthService], (service: AuthService) => {
const username = service.username();
username.subscribe(result => expect(result).toEqual('test@ebi.ac.uk'));
}));
it('should have realname', inject([AuthService], (service: AuthService) => {
const realname = service.realname();
realname.subscribe(result => expect(result).toEqual('Ed Munden Gras'));
}));
it('should have token', inject([AuthService], (service: AuthService) => {
const token = service.token();
token.subscribe(result => expect(result).toEqual(VALID_TOKEN));
}));
// It doesn't work because async and timer issues
xit('should have log out', inject([AuthService], (service: AuthService) => {
service.logOut();
const isAuthenticated = service.isAuthenticated();
isAuthenticated.subscribe(result => expect(result).toBeFalsy());
}));
it('should be correct single sing on URL', inject([AuthService], (service: AuthService) => {
expect(service.getSSOURL({
'ttl': '30',
'o': '3'
}))
.toEqual('https://api.aai.ebi.ac.uk/sso?from=http%3A%2F%2Flocalhost%3A9876&ttl=30&o=3');
}));
it('should be correct single sing on URL', inject([AuthService], (service: AuthService) => {
expect(service.getSSOURL({
'ttl': '1441',
'o': '3'
}))
.toEqual('https://api.aai.ebi.ac.uk/sso?from=http%3A%2F%2Flocalhost%3A9876&ttl=1440&o=3');
}));
});
describe('AuthService (expired token)', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{
provide: JWT_OPTIONS,
useValue: {
tokenGetter: () => EXPIRED_TOKEN
}
},
JwtHelperService,
TokenService,
{
provide: AAP_CONFIG,
useValue: DEFAULT_CONF
},
AuthService
]
});
});
it('should be created', inject([AuthService], (service: AuthService) => {
expect(service).toBeTruthy();
}));
it('should not be authenticated', inject([AuthService], (service: AuthService) => {
const isAuthenticated = service.isAuthenticated();
isAuthenticated.subscribe(result => expect(result).toBeFalsy());
}));
it('should not have credentials', inject([AuthService], (service: AuthService) => {
const credentials = service.credentials();
credentials.subscribe(result => expect(result).toBeNull());
}));
it('should not have username', inject([AuthService], (service: AuthService) => {
const username = service.username();
username.subscribe(result => expect(result).toBeNull());
}));
it('should have realname', inject([AuthService], (service: AuthService) => {
const realname = service.realname();
realname.subscribe(result => expect(result).toBeNull());
}));
it('should have token', inject([AuthService], (service: AuthService) => {
const token = service.token();
token.subscribe(result => expect(result).toBeNull());
}));
});
......@@ -15,7 +15,7 @@ import {
EXPIRED_TOKEN
} from 'app/../../testing/tokens';
describe('TokenService', () => {
describe('TokenService (valid token)', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{
......@@ -33,4 +33,80 @@ describe('TokenService', () => {
it('should be created', inject([TokenService], (service: TokenService) => {
expect(service).toBeTruthy();
}));
it('getToken should return valid token', inject([TokenService], (service: TokenService) => {
expect(service.getToken()).toBe(VALID_TOKEN);
}));
it('token should be valid', inject([TokenService], (service: TokenService) => {
expect(service.isTokenValid()).toBeTruthy();
}));
it('token should have correct expired date', inject([TokenService], (service: TokenService) => {
expect(service.getTokenExpirationDate()).toEqual(new Date(1000000000000000));
}));
it('getClaim should return correct value', inject([TokenService], (service: TokenService) => {
expect(service.getClaim('iss', 'Dummy')).toBe('https://tsi.ebi.ac.uk');
}));
it('getClaim should with non-existing claim should return default value "Dummy"', inject([TokenService], (service: TokenService) => {
expect(service.getClaim('issr', 'Dummy')).toEqual('Dummy');
}));
it('getClaim should with non-existing claim should return default value null', inject([TokenService], (service: TokenService) => {
expect(service.getClaim('issr', null)).toEqual(null);
}));
it('getClaim should with non-existing claim should return default value undefined', inject([TokenService], (service: TokenService) => {
expect(service.getClaim('issr', undefined)).toEqual(undefined);
}));
});
describe('TokenService (expired token)', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{
provide: JWT_OPTIONS,
useValue: {
tokenGetter: () => EXPIRED_TOKEN
}
},
JwtHelperService,
TokenService
]
});
});
it('should be created', inject([TokenService], (service: TokenService) => {
expect(service).toBeTruthy();
}));
it('getToken should return expired token', inject([TokenService], (service: TokenService) => {
expect(service.getToken()).toBe(EXPIRED_TOKEN);
}));
it('token should not be valid', inject([TokenService], (service: TokenService) => {
expect(service.isTokenValid()).toBeFalsy();
}));
it('token should have correct expired date', inject([TokenService], (service: TokenService) => {
expect(service.getTokenExpirationDate()).toEqual(new Date(1518083433000));
}));
it('getClaim should return correct value', inject([TokenService], (service: TokenService) => {
expect(service.getClaim('iss', 'Dummy')).toBe('https://tsi.ebi.ac.uk');
}));
it('getClaim should with non-existing claim should return default value "Dummy"', inject([TokenService], (service: TokenService) => {
expect(service.getClaim('issr', 'Dummy')).toEqual('Dummy');
}));
it('getClaim should with non-existing claim should return default value null', inject([TokenService], (service: TokenService) => {
expect(service.getClaim('issr', null)).toEqual(null);
}));
it('getClaim should with non-existing claim should return default value undefined', inject([TokenService], (service: TokenService) => {
expect(service.getClaim('issr', undefined)).toEqual(undefined);
}));
});
/* tslint:disable:max-line-length */
export const EXPIRED_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3RzaS5lYmkuYWMudWsiLCJleHAiOjE1MTgwODM0MzMsImp0aSI6InR6Wmo4Z29xUWVMRVBNakxIMDJwVEEiLCJpYXQiOjE1MTgwODMzNzMsInN1YiI6InVzci03NWY0YjAwMCIsImVtYWlsIjoidGVzdEBlYmkuYWMudWsiLCJuaWNrbmFtZSI6IjZmMzdhMGJlYjdiMTZmMzdhMGJlYjdiMWIiLCJuYW1lIjoiRWQgTXVuZGVuIEdyYXMiLCJkb21haW5zIjpbImFhcC11c2Vycy1kb21haW4iXX0.JrIBLqSmiZixbKfvutQKdx3b_O1SSjssL5mfyVks6Aw';
export const VALID_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3RzaS5lYmkuYWMudWsiLCJleHAiOjEwMDAwMDAwMDAwMDAwMDAwMDAwMCwianRpIjoidHpaajhnb3FRZUxFUE1qTEgwMnBUQSIsImlhdCI6MTUxODA4MzM3Mywic3ViIjoidXNyLTc1ZjRiMDAwIiwiZW1haWwiOiJ0ZXN0QGViaS5hYy51ayIsIm5pY2tuYW1lIjoiNmYzN2EwYmViN2IxNmYzN2EwYmViN2IxYiIsIm5hbWUiOiJFZCBNdW5kZW4gR3JhcyIsImRvbWFpbnMiOlsiYWFwLXVzZXJzLWRvbWFpbiJdfQ.NfvjfYjVQy9BIL2jqFOyHTDXXEVlnf33LVzgccljsc0';
export const VALID_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3RzaS5lYmkuYWMudWsiLCJleHAiOjEwMDAwMDAwMDAwMDAsImp0aSI6InR6Wmo4Z29xUWVMRVBNakxIMDJwVEEiLCJpYXQiOjE1MTgwODMzNzMsInN1YiI6InVzci03NWY0YjAwMCIsImVtYWlsIjoidGVzdEBlYmkuYWMudWsiLCJuaWNrbmFtZSI6IjZmMzdhMGJlYjdiMTZmMzdhMGJlYjdiMWIiLCJuYW1lIjoiRWQgTXVuZGVuIEdyYXMiLCJkb21haW5zIjpbImFhcC11c2Vycy1kb21haW4iXX0.up6dm5r1KB0yunL5vlyHpf8citI1JqMKlhzdg0oEXII';
/* tslint:enable:max-line-length */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment