|
| 1 | +import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; |
| 2 | +import { ActivatedRoute, Router } from '@angular/router'; |
| 3 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
| 4 | + |
| 5 | +import { provideMockStore } from '@ngrx/store/testing'; |
| 6 | +import { Store, StoreModule } from '@ngrx/store'; |
| 7 | +import { TranslateModule } from '@ngx-translate/core'; |
| 8 | + |
| 9 | +import { EPerson } from '../../../../core/eperson/models/eperson.model'; |
| 10 | +import { EPersonMock } from '../../../testing/eperson.mock'; |
| 11 | +import { authReducer } from '../../../../core/auth/auth.reducer'; |
| 12 | +import { AuthService } from '../../../../core/auth/auth.service'; |
| 13 | +import { AuthServiceStub } from '../../../testing/auth-service.stub'; |
| 14 | +import { storeModuleConfig } from '../../../../app.reducer'; |
| 15 | +import { AuthMethod } from '../../../../core/auth/models/auth.method'; |
| 16 | +import { AuthMethodType } from '../../../../core/auth/models/auth.method-type'; |
| 17 | +import { LogInOidcComponent } from './log-in-oidc.component'; |
| 18 | +import { NativeWindowService } from '../../../../core/services/window.service'; |
| 19 | +import { RouterStub } from '../../../testing/router.stub'; |
| 20 | +import { ActivatedRouteStub } from '../../../testing/active-router.stub'; |
| 21 | +import { NativeWindowMockFactory } from '../../../mocks/mock-native-window-ref'; |
| 22 | +import { HardRedirectService } from '../../../../core/services/hard-redirect.service'; |
| 23 | + |
| 24 | + |
| 25 | +describe('LogInOidcComponent', () => { |
| 26 | + |
| 27 | + let component: LogInOidcComponent; |
| 28 | + let fixture: ComponentFixture<LogInOidcComponent>; |
| 29 | + let page: Page; |
| 30 | + let user: EPerson; |
| 31 | + let componentAsAny: any; |
| 32 | + let setHrefSpy; |
| 33 | + let oidcBaseUrl; |
| 34 | + let location; |
| 35 | + let initialState: any; |
| 36 | + let hardRedirectService: HardRedirectService; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + user = EPersonMock; |
| 40 | + oidcBaseUrl = 'dspace-rest.test/oidc?redirectUrl='; |
| 41 | + location = oidcBaseUrl + 'http://dspace-angular.test/home'; |
| 42 | + |
| 43 | + hardRedirectService = jasmine.createSpyObj('hardRedirectService', { |
| 44 | + getCurrentRoute: {}, |
| 45 | + redirect: {} |
| 46 | + }); |
| 47 | + |
| 48 | + initialState = { |
| 49 | + core: { |
| 50 | + auth: { |
| 51 | + authenticated: false, |
| 52 | + loaded: false, |
| 53 | + blocking: false, |
| 54 | + loading: false, |
| 55 | + authMethods: [] |
| 56 | + } |
| 57 | + } |
| 58 | + }; |
| 59 | + }); |
| 60 | + |
| 61 | + beforeEach(waitForAsync(() => { |
| 62 | + // refine the test module by declaring the test component |
| 63 | + TestBed.configureTestingModule({ |
| 64 | + imports: [ |
| 65 | + StoreModule.forRoot({ auth: authReducer }, storeModuleConfig), |
| 66 | + TranslateModule.forRoot() |
| 67 | + ], |
| 68 | + declarations: [ |
| 69 | + LogInOidcComponent |
| 70 | + ], |
| 71 | + providers: [ |
| 72 | + { provide: AuthService, useClass: AuthServiceStub }, |
| 73 | + { provide: 'authMethodProvider', useValue: new AuthMethod(AuthMethodType.Oidc, location) }, |
| 74 | + { provide: 'isStandalonePage', useValue: true }, |
| 75 | + { provide: NativeWindowService, useFactory: NativeWindowMockFactory }, |
| 76 | + { provide: Router, useValue: new RouterStub() }, |
| 77 | + { provide: ActivatedRoute, useValue: new ActivatedRouteStub() }, |
| 78 | + { provide: HardRedirectService, useValue: hardRedirectService }, |
| 79 | + provideMockStore({ initialState }), |
| 80 | + ], |
| 81 | + schemas: [ |
| 82 | + CUSTOM_ELEMENTS_SCHEMA |
| 83 | + ] |
| 84 | + }) |
| 85 | + .compileComponents(); |
| 86 | + |
| 87 | + })); |
| 88 | + |
| 89 | + beforeEach(() => { |
| 90 | + // create component and test fixture |
| 91 | + fixture = TestBed.createComponent(LogInOidcComponent); |
| 92 | + |
| 93 | + // get test component from the fixture |
| 94 | + component = fixture.componentInstance; |
| 95 | + componentAsAny = component; |
| 96 | + |
| 97 | + // create page |
| 98 | + page = new Page(component, fixture); |
| 99 | + setHrefSpy = spyOnProperty(componentAsAny._window.nativeWindow.location, 'href', 'set').and.callThrough(); |
| 100 | + |
| 101 | + }); |
| 102 | + |
| 103 | + it('should set the properly a new redirectUrl', () => { |
| 104 | + const currentUrl = 'http://dspace-angular.test/collections/12345'; |
| 105 | + componentAsAny._window.nativeWindow.location.href = currentUrl; |
| 106 | + |
| 107 | + fixture.detectChanges(); |
| 108 | + |
| 109 | + expect(componentAsAny.injectedAuthMethodModel.location).toBe(location); |
| 110 | + expect(componentAsAny._window.nativeWindow.location.href).toBe(currentUrl); |
| 111 | + |
| 112 | + component.redirectToOidc(); |
| 113 | + |
| 114 | + expect(setHrefSpy).toHaveBeenCalledWith(currentUrl); |
| 115 | + |
| 116 | + }); |
| 117 | + |
| 118 | + it('should not set a new redirectUrl', () => { |
| 119 | + const currentUrl = 'http://dspace-angular.test/home'; |
| 120 | + componentAsAny._window.nativeWindow.location.href = currentUrl; |
| 121 | + |
| 122 | + fixture.detectChanges(); |
| 123 | + |
| 124 | + expect(componentAsAny.injectedAuthMethodModel.location).toBe(location); |
| 125 | + expect(componentAsAny._window.nativeWindow.location.href).toBe(currentUrl); |
| 126 | + |
| 127 | + component.redirectToOidc(); |
| 128 | + |
| 129 | + expect(setHrefSpy).toHaveBeenCalledWith(currentUrl); |
| 130 | + |
| 131 | + }); |
| 132 | + |
| 133 | +}); |
| 134 | + |
| 135 | +/** |
| 136 | + * I represent the DOM elements and attach spies. |
| 137 | + * |
| 138 | + * @class Page |
| 139 | + */ |
| 140 | +class Page { |
| 141 | + |
| 142 | + public emailInput: HTMLInputElement; |
| 143 | + public navigateSpy: jasmine.Spy; |
| 144 | + public passwordInput: HTMLInputElement; |
| 145 | + |
| 146 | + constructor(private component: LogInOidcComponent, private fixture: ComponentFixture<LogInOidcComponent>) { |
| 147 | + // use injector to get services |
| 148 | + const injector = fixture.debugElement.injector; |
| 149 | + const store = injector.get(Store); |
| 150 | + |
| 151 | + // add spies |
| 152 | + this.navigateSpy = spyOn(store, 'dispatch'); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments