Skip to content

Commit

Permalink
Merged in bugfix/SHIBUI-230 (pull request #6)
Browse files Browse the repository at this point in the history
SHIBUI-230: Fixed bug with editor, fixed unit tests

Approved-by: Ryan Mathis <rmathis@unicon.net>
  • Loading branch information
rmathis committed Mar 12, 2018
1 parent 0f1091f commit 9a1b9ec
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Component, ViewChild } from '@angular/core';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
Expand All @@ -12,10 +13,31 @@ import { FinishFormComponent } from './finish-form.component';
import { RouterStub, RouterLinkStubDirective } from '../../../../testing/router.stub';
import { ActivatedRouteStub } from '../../../../testing/activated-route.stub';

import * as stubs from '../../../../testing/provider.stub';
import { InputDefaultsDirective } from '../../directive/input-defaults.directive';
import { I18nTextComponent } from '../i18n-text.component';

@Component({
template: `<finish-form [provider]="provider"></finish-form>`
})
class TestHostComponent {
provider = new EntityDescriptor({
...stubs.provider
});

@ViewChild(FinishFormComponent)
public formUnderTest: FinishFormComponent;

changeProvider(opts: any): void {
this.provider = Object.assign({}, this.provider, opts);
}
}

describe('Finished Form Component', () => {
let fixture: ComponentFixture<FinishFormComponent>;
let instance: FinishFormComponent;
let fixture: ComponentFixture<TestHostComponent>;
let instance: TestHostComponent;
let store: Store<fromProviders.ProviderState>;
let form: FinishFormComponent;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -37,19 +59,40 @@ describe('Finished Form Component', () => {
],
declarations: [
FinishFormComponent,
RouterLinkStubDirective
RouterLinkStubDirective,
I18nTextComponent,
InputDefaultsDirective,
TestHostComponent
],
});
store = TestBed.get(Store);
spyOn(store, 'dispatch').and.callThrough();

fixture = TestBed.createComponent(FinishFormComponent);
fixture = TestBed.createComponent(TestHostComponent);
instance = fixture.componentInstance;
instance.provider = new EntityDescriptor({ entityId: 'foo', serviceProviderName: 'bar' });
form = instance.formUnderTest;
fixture.detectChanges();
});

it('should compile', () => {
expect(fixture).toBeDefined();
});

describe('ngOnChanges lifecycle event', () => {
it('should reset the form', () => {
spyOn(form.form, 'reset').and.callThrough();
instance.changeProvider({
serviceEnabled: true
});
fixture.detectChanges();
expect(form.form.reset).toHaveBeenCalled();
});

xit('should reset the form with serviceEnabled = false if no provider', () => {
spyOn(form.form, 'reset').and.callThrough();
delete instance.provider;
fixture.detectChanges();
expect(form.form.reset).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,23 @@ describe('Security (Key) Info Form Component', () => {
expect(form.x509Certificates.length).toBe(0);
});
});

describe('createGroup method', () => {
it('should return a FormGroup with the correct attributes', () => {
let group = form.createGroup();
expect(Object.keys(group.controls)).toEqual(['name', 'type', 'value']);
});

it('should return a FormGroup with the provided attributes', () => {
let group = form.createGroup({
name: 'foo',
type: 'signing',
value: 'bar'
});
let controls = group.controls;
expect(controls.name.value).toEqual('foo');
expect(controls.type.value).toEqual('signing');
expect(controls.value.value).toEqual('bar');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,23 @@ export class KeyInfoFormComponent extends ProviderFormFragmentComponent implemen
return this.form.get('securityInfo.x509Certificates') as FormArray;
}

createGroup(values: Certificate = {name: '', type: 'both', value: ''}): FormGroup {
return this.fb.group({
name: [values.name || '', Validators.required],
type: [values.type || 'both', Validators.required],
value: [values.value || '', Validators.required]
});
}

setCertificates(certs: Certificate[] = []): void {
let fgs = certs.map(ep => this.fb.group(ep)),
let fgs = certs.map(ep => this.createGroup(ep)),
list = this.fb.array(fgs, Validators.minLength(1)),
group = this.form.get('securityInfo') as FormGroup;
group.setControl('x509Certificates', list);
}

addCert(): void {
this.x509Certificates.push(this.fb.group({
name: ['', Validators.required],
type: ['both', Validators.required],
value: ['', Validators.required]
}));
this.x509Certificates.push(this.createGroup());
}

removeCert(index: number): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ViewChild, Component } from '@angular/core';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
Expand All @@ -9,10 +10,32 @@ import { ListValuesService } from '../../service/list-values.service';
import { EntityDescriptor } from '../../model/entity-descriptor';
import { LogoutFormComponent } from './logout-form.component';

import * as stubs from '../../../../testing/provider.stub';
import { InputDefaultsDirective } from '../../directive/input-defaults.directive';
import { I18nTextComponent } from '../i18n-text.component';

@Component({
template: `<logout-form [provider]="provider"></logout-form>`
})
class TestHostComponent {
provider = new EntityDescriptor({
...stubs.provider,
logoutEndpoints: [stubs.logoutEndpoint]
});

@ViewChild(LogoutFormComponent)
public formUnderTest: LogoutFormComponent;

changeProvider(opts: any): void {
this.provider = Object.assign({}, this.provider, opts);
}
}

describe('Logout Endpoints Form Component', () => {
let fixture: ComponentFixture<LogoutFormComponent>;
let instance: LogoutFormComponent;
let fixture: ComponentFixture<TestHostComponent>;
let instance: TestHostComponent;
let store: Store<fromProviders.ProviderState>;
let form: LogoutFormComponent;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -31,19 +54,91 @@ describe('Logout Endpoints Form Component', () => {
NgbPopoverModule
],
declarations: [
LogoutFormComponent
LogoutFormComponent,
TestHostComponent,
InputDefaultsDirective,
I18nTextComponent
],
});
store = TestBed.get(Store);
spyOn(store, 'dispatch').and.callThrough();

fixture = TestBed.createComponent(LogoutFormComponent);
fixture = TestBed.createComponent(TestHostComponent);
instance = fixture.componentInstance;
instance.provider = new EntityDescriptor({ entityId: 'foo', serviceProviderName: 'bar' });
form = instance.formUnderTest;
fixture.detectChanges();
});

it('should compile', () => {
expect(fixture).toBeDefined();
});

describe('ngOnInit method', () => {
it('should remove endpoints if there are none available', () => {
instance.changeProvider({
logoutEndpoints: []
});
fixture.detectChanges();
expect(form.logoutEndpoints.length).toBe(0);
});
});

describe('ngOnChanges method', () => {
it('should add endpoints if provided', () => {
instance.provider = new EntityDescriptor({
...stubs.provider
});
fixture.detectChanges();
expect(form.logoutEndpoints.length).toBe(0);
});
});

describe('setEndpoints method', () => {
it('should add endpoints if provided', () => {
form.setEndpoints();
fixture.detectChanges();
expect(form.form.get('logoutEndpoints')).toBeDefined();
});
});

describe('removeCert method', () => {
it('should remove the endpoint at the given index', () => {
instance.changeProvider({
logoutEndpoints: [stubs.endpoint]
});
fixture.detectChanges();
form.removeEndpoint(0);
fixture.detectChanges();
expect(form.logoutEndpoints.length).toBe(0);
});
});

describe('addCert method', () => {
it('should remove the endpoint at the given index', () => {
instance.changeProvider({
logoutEndpoints: []
});
fixture.detectChanges();
form.addEndpoint();
fixture.detectChanges();
expect(form.logoutEndpoints.length).toBe(1);
});
});

describe('createGroup method', () => {
it('should return a FormGroup with the correct attributes', () => {
let group = form.createGroup();
expect(Object.keys(group.controls)).toEqual(['url', 'bindingType']);
});

it('should return a FormGroup with the provided attributes', () => {
let group = form.createGroup({
url: 'foo',
bindingType: 'bar'
});
let controls = group.controls;
expect(controls.url.value).toEqual('foo');
expect(controls.bindingType.value).toEqual('bar');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,25 @@ export class LogoutFormComponent extends ProviderFormFragmentComponent implement
});
}

createGroup(ep: LogoutEndpoint = { url: '', bindingType: null }): FormGroup {
return this.fb.group({
url: [ep.url || '', Validators.required],
bindingType: [ep.bindingType || null, Validators.required]
});
}

get logoutEndpoints(): FormArray {
return this.form.get('logoutEndpoints') as FormArray;
}

setEndpoints(endpoints: LogoutEndpoint[] = []): void {
let fgs = endpoints.map(ep => this.fb.group(ep)),
list = this.fb.array(fgs);
let fgs = endpoints.map(ep => this.createGroup(ep)),
list = this.fb.array(fgs);
this.form.setControl('logoutEndpoints', list);
}

addEndpoint(): void {
this.logoutEndpoints.push(this.fb.group({
url: ['', Validators.required],
bindingType: [null, Validators.required]
}));
this.logoutEndpoints.push(this.createGroup());
}

removeEndpoint(index: number): void {
Expand Down
8 changes: 7 additions & 1 deletion ui/src/testing/provider.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
Contact,
SsoService,
Certificate,
SecurityInfo
SecurityInfo,
LogoutEndpoint
} from '../app/metadata-provider/model/metadata-provider';

export const draft = {
Expand Down Expand Up @@ -40,3 +41,8 @@ export const secInfo = {
wantAssertionsSigned: true,
x509Certificates: []
} as SecurityInfo;

export const logoutEndpoint = {
url: 'foo',
bindingType: 'bar'
} as LogoutEndpoint;

0 comments on commit 9a1b9ec

Please sign in to comment.