diff --git a/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.html b/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.html index a4b81db279..713970f05b 100644 --- a/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.html +++ b/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.html @@ -1,6 +1,6 @@ - + diff --git a/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.spec.ts b/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.spec.ts index 7ffab6aeb4..5e6dcb597e 100644 --- a/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.spec.ts +++ b/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.spec.ts @@ -1,7 +1,6 @@ import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; +import { ActivatedRoute, Router } from '@angular/router'; import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing'; - -import { By } from '@angular/platform-browser'; import { Store, StoreModule } from '@ngrx/store'; import { TranslateModule } from '@ngx-translate/core'; @@ -14,13 +13,21 @@ import { AppState } from '../../../../app.reducer'; import { AuthMethod } from '../../../../core/auth/models/auth.method'; import { AuthMethodType } from '../../../../core/auth/models/auth.method-type'; import { LogInShibbolethComponent } from './log-in-shibboleth.component'; +import { NativeWindowService } from '../../../../core/services/window.service'; +import { RouterStub } from '../../../testing/router-stub'; +import { ActivatedRouteStub } from '../../../testing/active-router-stub'; +import { NativeWindowMockFactory } from '../../../mocks/mock-native-window-ref'; -describe('LogInShibbolethComponent', () => { +fdescribe('LogInShibbolethComponent', () => { let component: LogInShibbolethComponent; let fixture: ComponentFixture; let page: Page; let user: EPerson; + let componentAsAny: any; + let setHrefSpy; + const shibbolethBaseUrl = 'dspace-rest.test/shibboleth?redirectUrl='; + const location = shibbolethBaseUrl + 'http://dspace-angular.test/home'; const authState = { authenticated: false, @@ -44,9 +51,10 @@ describe('LogInShibbolethComponent', () => { ], providers: [ { provide: AuthService, useClass: AuthServiceStub }, - { provide: 'authMethodProvider', - useValue: new AuthMethod(AuthMethodType.Shibboleth, 'dspace.test/shibboleth') - } + { provide: 'authMethodProvider', useValue: new AuthMethod(AuthMethodType.Shibboleth, location) }, + { provide: NativeWindowService, useFactory: NativeWindowMockFactory }, + { provide: Router, useValue: new RouterStub() }, + { provide: ActivatedRoute, useValue: new ActivatedRouteStub() }, ], schemas: [ CUSTOM_ELEMENTS_SCHEMA @@ -68,16 +76,42 @@ describe('LogInShibbolethComponent', () => { // get test component from the fixture component = fixture.componentInstance; + componentAsAny = component; // create page page = new Page(component, fixture); + setHrefSpy = spyOnProperty(componentAsAny._window.nativeWindow.location, 'href', 'set').and.callThrough(); })); - it('should display a link with properly href', () => { + it('should set the properly a new redirectUrl', () => { + const currentUrl = 'http://dspace-angular.test/collections/12345'; + componentAsAny._window.nativeWindow.location.href = currentUrl; + fixture.detectChanges(); - const link = fixture.debugElement.query(By.css('a')); - expect(link.nativeElement.getAttribute('href')).toBe('dspace.test/shibboleth'); + + expect(componentAsAny.injectedAuthMethodModel.location).toBe(location); + expect(componentAsAny._window.nativeWindow.location.href).toBe(currentUrl); + + component.redirectToShibboleth(); + + expect(setHrefSpy).toHaveBeenCalledWith(shibbolethBaseUrl + currentUrl) + + }); + + it('should not set a new redirectUrl', () => { + const currentUrl = 'http://dspace-angular.test/home'; + componentAsAny._window.nativeWindow.location.href = currentUrl; + + fixture.detectChanges(); + + expect(componentAsAny.injectedAuthMethodModel.location).toBe(location); + expect(componentAsAny._window.nativeWindow.location.href).toBe(currentUrl); + + component.redirectToShibboleth(); + + expect(setHrefSpy).toHaveBeenCalledWith(shibbolethBaseUrl + currentUrl) + }); }); diff --git a/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.ts b/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.ts index e07577cddb..6321e6119f 100644 --- a/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.ts +++ b/src/app/shared/log-in/methods/shibboleth/log-in-shibboleth.component.ts @@ -9,6 +9,9 @@ import { AuthMethod } from '../../../../core/auth/models/auth.method'; import { CoreState } from '../../../../core/core.reducers'; import { isAuthenticated, isAuthenticationLoading } from '../../../../core/auth/selectors'; +import { RouteService } from '../../../../core/services/route.service'; +import { NativeWindowRef, NativeWindowService } from '../../../../core/services/window.service'; +import { isNotNull } from '../../../empty.util'; @Component({ selector: 'ds-log-in-shibboleth', @@ -46,10 +49,14 @@ export class LogInShibbolethComponent implements OnInit { /** * @constructor * @param {AuthMethod} injectedAuthMethodModel + * @param {NativeWindowRef} _window + * @param {RouteService} route * @param {Store} store */ constructor( @Inject('authMethodProvider') public injectedAuthMethodModel: AuthMethod, + @Inject(NativeWindowService) protected _window: NativeWindowRef, + private route: RouteService, private store: Store ) { this.authMethod = injectedAuthMethodModel; @@ -63,7 +70,26 @@ export class LogInShibbolethComponent implements OnInit { this.loading = this.store.pipe(select(isAuthenticationLoading)); // set location - this.location = this.injectedAuthMethodModel.location + this.location = decodeURIComponent(this.injectedAuthMethodModel.location); + + } + + redirectToShibboleth() { + let newLocationUrl = this.location; + const currentUrl = this._window.nativeWindow.location.href; + const myRegexp = /\?redirectUrl=(.*)/g; + const match = myRegexp.exec(this.location); + const redirectUrl = (match && match[1]) ? match[1] : null; + + // Check whether the current page is different from the redirect url received from rest + if (isNotNull(redirectUrl) && redirectUrl !== currentUrl) { + // change the redirect url with the current page url + const newRedirectUrl = `?redirectUrl=${currentUrl}`; + newLocationUrl = this.location.replace(/\?redirectUrl=(.*)/g, newRedirectUrl); + } + + // redirect to shibboleth authentication url + this._window.nativeWindow.location.href = newLocationUrl; } } diff --git a/src/app/shared/mocks/mock-native-window-ref.ts b/src/app/shared/mocks/mock-native-window-ref.ts new file mode 100644 index 0000000000..5546bd5ccc --- /dev/null +++ b/src/app/shared/mocks/mock-native-window-ref.ts @@ -0,0 +1,21 @@ +export const MockWindow = { + location: { + _href: '', + set href(url: string) { + this._href = url; + }, + get href() { + return this._href; + } + } +}; + +export class NativeWindowRefMock { + get nativeWindow(): any { + return MockWindow; + } +} + +export function NativeWindowMockFactory() { + return new NativeWindowRefMock(); +}