Skip to content

Commit

Permalink
SHIBUI-1361 Implemented unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Aug 6, 2019
1 parent 0a1240c commit 3d66d91
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</td>
<td *ngIf="i === 0"><translate-i18n key="label.current">Current</translate-i18n> (v{{ history.length - i }})</td>
<td *ngIf="i > 0">v{{ history.length - (i) }}</td>
<td>{{ version.date | date:'medium' }}</td>
<td>{{ version.date | date:DATE_FORMAT }}</td>
<td>{{ version.creator }}</td>
<td>
<button class="btn btn-link" (click)="restore.emit(version)">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, Input, EventEmitter, ChangeDetectionStrategy, Output } from '@angular/core';
import { MetadataHistory } from '../model/history';
import { MetadataVersion } from '../model/version';
import { CONFIG_DATE_FORMAT } from '../configuration.values';

@Component({
selector: 'history-list',
Expand All @@ -15,6 +16,8 @@ export class HistoryListComponent {

selected: MetadataVersion[] = [];

DATE_FORMAT = CONFIG_DATE_FORMAT;

constructor() {}

toggleVersionSelected(version: MetadataVersion): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h2 class="title h4 m-0 flex-grow-1">
<strong [ngStyle]="{'width': width}" translate="label.option">Option</strong>
<strong *ngFor="let date of configuration.dates" [ngStyle]="{'width': width}">
<translate-i18n key="label.value" *ngIf="configuration.dates.length <= 1">Value</translate-i18n>
<span *ngIf="configuration.dates.length > 1">{{ date | date:'medium' }}</span>
<span *ngIf="configuration.dates.length > 1">{{ date | date:DATE_FORMAT }}</span>
</strong>
</div>
<object-property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component, ChangeDetectionStrategy, Input, Output, EventEmitter } from
import { Router, ActivatedRoute } from '@angular/router';
import { MetadataConfiguration } from '../model/metadata-configuration';
import { Metadata } from '../../domain/domain.type';
import { CONFIG_DATE_FORMAT } from '../configuration.values';

@Component({
selector: 'metadata-configuration',
Expand All @@ -18,6 +19,8 @@ export class MetadataConfigurationComponent {

@Output() preview: EventEmitter<any> = new EventEmitter();

DATE_FORMAT = CONFIG_DATE_FORMAT;

constructor(
private router: Router,
private activatedRoute: ActivatedRoute
Expand Down
2 changes: 2 additions & 0 deletions ui/src/app/metadata/configuration/configuration.values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export enum TYPES {
export const DEFINITIONS = {
resolver: MetadataSourceEditor
};

export const CONFIG_DATE_FORMAT = 'MMM dd, yyyy HH:mm:ss';
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ <h2 class="title h4 m-0 ml-2 flex-grow-1">
(onUpdateOrderDown)="updateOrderDown($event)"
(onUpdateOrderUp)="updateOrderUp($event)"
(onRemove)="removeFilter($event)"
(preview)="onPreview($event)"
[filters]="filters$ | async"></filter-configuration-list>
</div>
<button class="btn btn-link" (click)="onScrollTo('header')">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FileBackedHttpMetadataResolver } from './file-backed-http-metadata-resolver';
import { MetadataResolver } from '../../model';

describe('Resolver construct', () => {

Expand Down Expand Up @@ -60,8 +61,8 @@ describe('Resolver construct', () => {
}
],
serviceEnabled: true,
createdDate: 'string (date)',
modifiedDate: 'string (date)',
createdDate: 'December 17, 1995 03:24:00',
modifiedDate: 'December 17, 1995 03:24:00',
relyingPartyOverrides: {
'signAssertion': true,
'dontSignResponse': true,
Expand All @@ -83,11 +84,51 @@ describe('Resolver construct', () => {
'mail'
]
};
const entity = new FileBackedHttpMetadataResolver(config);

it('should populate its own values', () => {
const entity = new FileBackedHttpMetadataResolver(config);
Object.keys(config).forEach(key => {
expect(entity[key]).toEqual(config[key]);
});
});

describe('isDraft method', () => {
it('should return false if no createDate defined', () => {
const entity = new FileBackedHttpMetadataResolver(config);
expect(entity.isDraft()).toBe(false);
});

it('should return false if no createDate defined', () => {
const { createdDate, ...rest } = config;
const entity = new FileBackedHttpMetadataResolver(rest as MetadataResolver);
expect(entity.isDraft()).toBe(true);
});
});

describe('getCreationDate method', () => {
it('should return false if no createDate defined', () => {
const entity = new FileBackedHttpMetadataResolver(config);
expect(entity.getCreationDate()).toBeDefined();
});

it('should return false if no createDate defined', () => {
const { createdDate, ...rest } = config;
const entity = new FileBackedHttpMetadataResolver(rest as MetadataResolver);
expect(entity.getCreationDate()).toBeNull();
});
});

describe('enabled getter', () => {
it('should return the serviceEnabled attribute', () => {
const entity = new FileBackedHttpMetadataResolver(config);
expect(entity.enabled).toBe(config.serviceEnabled);
});
});

describe('serialize method', () => {
it('should return itself', () => {
const entity = new FileBackedHttpMetadataResolver(config);
expect(entity.serialize()).toBe(entity);
});
});
});
67 changes: 67 additions & 0 deletions ui/src/app/metadata/domain/service/draft.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TestBed } from '@angular/core/testing';
import { EntityDraftService } from './draft.service';
import { FileBackedHttpMetadataResolver } from '../entity';
import { MetadataResolver } from '../model';

describe(`EntityDraftService`, () => {
let service: EntityDraftService;
Expand Down Expand Up @@ -36,5 +37,71 @@ describe(`EntityDraftService`, () => {
done();
});
});

it(`should return a 404 error if not found`, (done: DoneFn) => {
let id = null;
service.find(id).subscribe(null, err => {
expect(err).toEqual(404);
done();
});
});
});

describe('exists', () => {
it('should check if the provided id exists in storage', () => {
spyOn(service.storage, 'query').and.returnValue([{id: 'bar'} as MetadataResolver]);
expect(service.exists('foo')).toBe(false);
expect(service.exists('bar')).toBe(true);
});

it('should use the provided attr', () => {
spyOn(service.storage, 'query').and.returnValue([
{ id: 'bar', serviceProviderName: 'foo' } as MetadataResolver
]);
expect(service.exists('foo', 'serviceProviderName')).toBe(true);
expect(service.exists('bar', 'serviceProviderName')).toBe(false);
});
});

describe('save', () => {
it('should add the provider to storage', () => {
const resolver = { id: 'bar' } as MetadataResolver;
spyOn(service.storage, 'add');
service.save(resolver).subscribe();
expect(service.storage.add).toHaveBeenCalledWith(resolver);
});
});

describe('remove', () => {
it('should remove the provider from storage', () => {
const resolver = { id: 'bar' } as MetadataResolver;
spyOn(service.storage, 'removeByAttr');
service.remove(resolver).subscribe();
expect(service.storage.removeByAttr).toHaveBeenCalledWith(resolver.id, 'id');
});
});

describe('update', () => {
it('should remove the provider from storage', () => {
const resolver = { id: 'bar' } as MetadataResolver;
const updates = { id: 'foo', serviceProviderName: 'bar' };
spyOn(service.storage, 'findByAttr').and.returnValue(resolver);
spyOn(service.storage, 'add');
spyOn(service.storage, 'removeByAttr');
service.update(resolver).subscribe();
expect(service.storage.removeByAttr).toHaveBeenCalled();
expect(service.storage.add).toHaveBeenCalled();
expect(service.storage.findByAttr).toHaveBeenCalled();
});

it('should return a 404 if not found', () => {
const resolver = { id: 'bar' } as MetadataResolver;
const updates = { id: 'foo', serviceProviderName: 'bar' };
spyOn(service.storage, 'findByAttr').and.returnValue(null);
service.update(resolver).subscribe(null, (err) => {
expect(err).toBe(404);
expect(service.storage.findByAttr).toHaveBeenCalled();
});
});
});
});
126 changes: 124 additions & 2 deletions ui/src/app/metadata/domain/service/resolver.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,36 @@ describe(`Resolver Service`, () => {
}
)));

xit(`should emit 'true' for 200 Ok`, async(inject([ResolverService, HttpTestingController],
it(`should emit 'true' for 200 Ok`, async(inject([ResolverService, HttpTestingController],
(service: ResolverService, backend: HttpTestingController) => {
service.query().subscribe((next) => {
expect(next).toBeTruthy();
});

backend.expectOne('/api/EntityDescriptors').flush([], { status: 200, statusText: 'Ok' });
backend.expectOne('/api/EntityDescriptors').flush(['foo'], { status: 200, statusText: 'Ok' });
}
)));
});

describe('queryForAdmin', () => {
it(`should send an expected query request`, async(inject([ResolverService, HttpTestingController],
(service: ResolverService, backend: HttpTestingController) => {
service.queryForAdmin().subscribe();

backend.expectOne((req: HttpRequest<any>) => {
return req.url === '/api/EntityDescriptor/disabledNonAdmin'
&& req.method === 'GET';
}, `GET EntityDescriptors collection for an admin`);
}
)));

it(`should emit 'true' for 200 Ok`, async(inject([ResolverService, HttpTestingController],
(service: ResolverService, backend: HttpTestingController) => {
service.queryForAdmin().subscribe((next) => {
expect(next).toBeTruthy();
});

backend.expectOne('/api/EntityDescriptor/disabledNonAdmin').flush(['foo'], { status: 200, statusText: 'Ok' });
}
)));
});
Expand All @@ -55,4 +78,103 @@ describe(`Resolver Service`, () => {
}
)));
});

describe('update', () => {
let id = 'foo',
serviceProviderName = 'Test Provider',
createdBy = 'admin';

it(`should send an expected PUT request`, async(inject([ResolverService, HttpTestingController],
(service: ResolverService, backend: HttpTestingController) => {
service.update({id, serviceProviderName, createdBy}).subscribe();

backend.expectOne((req: HttpRequest<any>) => {
return req.url === `/api/EntityDescriptor/${id}`
&& req.method === 'PUT';
}, `PUT EntityDescriptor by id`);
}
)));
});

describe('create', () => {
let id = 'foo',
serviceProviderName = 'Test Provider',
createdBy = 'admin';

it(`should send an expected POST request`, async(inject([ResolverService, HttpTestingController],
(service: ResolverService, backend: HttpTestingController) => {
service.save({ id, serviceProviderName, createdBy }).subscribe();

backend.expectOne((req: HttpRequest<any>) => {
return req.url === `/api/EntityDescriptor`
&& req.method === 'POST';
}, `POST new EntityDescriptor`);
}
)));
});

describe('remove', () => {
let id = 'foo',
serviceProviderName = 'Test Provider',
createdBy = 'admin';

it(`should send an expected PUT request`, async(inject([ResolverService, HttpTestingController],
(service: ResolverService, backend: HttpTestingController) => {
service.remove({ id, serviceProviderName, createdBy }).subscribe();

backend.expectOne((req: HttpRequest<any>) => {
return req.url === `/api/EntityDescriptor/${id}`
&& req.method === 'DELETE';
}, `DELETE an EntityDescriptor`);
}
)));
});

describe('preview', () => {
let id = 'foo';

it(`should send an expected GET request`, async(inject([ResolverService, HttpTestingController],
(service: ResolverService, backend: HttpTestingController) => {
service.preview(id).subscribe();

backend.expectOne((req: HttpRequest<any>) => {
return req.url === `/api/EntityDescriptor/${id}`
&& req.method === 'GET'
&& req.headers.get('Accept') === 'application/xml'
&& req.responseType === 'text';
}, `GET an EntityDescriptor (xml)`);
}
)));
});

describe('upload', () => {
it(`should send an expected POST request`, async(inject([ResolverService, HttpTestingController],
(service: ResolverService, backend: HttpTestingController) => {
const name = 'foo', xml = '<foo></foo>';
service.upload(name, xml).subscribe();

backend.expectOne((req: HttpRequest<any>) => {
return req.url === `/api/EntityDescriptor`
&& req.method === 'POST'
&& req.headers.get('Content-Type') === 'application/xml';
}, `POST new EntityDescriptor`);
}
)));
});

describe('createFromUrl', () => {
it(`should send an expected POST request`, async(inject([ResolverService, HttpTestingController],
(service: ResolverService, backend: HttpTestingController) => {
const name = 'foo', url = 'http://goo.gle';
service.createFromUrl(name, url).subscribe();

backend.expectOne((req: HttpRequest<any>) => {
return req.url === `/api/EntityDescriptor`
&& req.method === 'POST'
&& req.headers.get('Content-Type') === 'application/x-www-form-urlencoded'
&& req.body === `metadataUrl=${url}`;
}, `POST new EntityDescriptor`);
}
)));
});
});
Loading

0 comments on commit 3d66d91

Please sign in to comment.