forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 2
JCU/Port CitacePRO citation widget from customer/mendelu #1356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kasinhou
wants to merge
3
commits into
customer/jcu
Choose a base branch
from
feature/jcu-citacepro
base: customer/jcu
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
...em-page/simple/field-components/specific-field/citation/item-page-citation.component.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| @if ((citaceProStatus$ | async) && (citaceProURL$ | async); as citaceProURL) { | ||
| <div class="mb-2"> | ||
| <iframe | ||
| class="d-block" | ||
| title="Citace PRO" | ||
| height="150px" | ||
| width="100%" | ||
| style="border: none;" | ||
| [src]="citaceProURL" | ||
| ></iframe> | ||
| </div> | ||
| } | ||
108 changes: 108 additions & 0 deletions
108
...page/simple/field-components/specific-field/citation/item-page-citation.component.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| NO_ERRORS_SCHEMA, | ||
| SecurityContext, | ||
| } from '@angular/core'; | ||
| import { | ||
| ComponentFixture, | ||
| TestBed, | ||
| } from '@angular/core/testing'; | ||
| import { | ||
| By, | ||
| DomSanitizer, | ||
| } from '@angular/platform-browser'; | ||
|
|
||
| import { ConfigurationDataService } from '../../../../../core/data/configuration-data.service'; | ||
| import { ConfigurationProperty } from '../../../../../core/shared/configuration-property.model'; | ||
| import { createSuccessfulRemoteDataObject$ } from '../../../../../shared/remote-data.utils'; | ||
| import { ItemPageCitationFieldComponent } from './item-page-citation.component'; | ||
|
|
||
| const CITACE_PRO_URL = 'https://www.citacepro.com/api/dspace/citace/oai'; | ||
| const CITACE_PRO_UNIVERSITY = 'dspace.jcu.cz'; | ||
|
|
||
| describe('ItemPageCitationFieldComponent', () => { | ||
| let component: ItemPageCitationFieldComponent; | ||
| let fixture: ComponentFixture<ItemPageCitationFieldComponent>; | ||
| const mockHandle = '123456789/3'; | ||
|
|
||
| function mockConfigurationDataService(allowed: string) { | ||
| const valuesByName: { [name: string]: string[] } = { | ||
| 'citace.pro.url': [CITACE_PRO_URL], | ||
| 'citace.pro.university': [CITACE_PRO_UNIVERSITY], | ||
| 'citace.pro.allowed': [allowed], | ||
| }; | ||
| return { | ||
| findByPropertyName: (name: string) => createSuccessfulRemoteDataObject$( | ||
| Object.assign(new ConfigurationProperty(), { name, values: valuesByName[name] ?? [] }), | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| async function init(allowed: string): Promise<void> { | ||
| await TestBed.configureTestingModule({ | ||
| imports: [ItemPageCitationFieldComponent], | ||
| providers: [ | ||
| { provide: ConfigurationDataService, useValue: mockConfigurationDataService(allowed) }, | ||
| ], | ||
| schemas: [NO_ERRORS_SCHEMA], | ||
| }) | ||
| .overrideComponent(ItemPageCitationFieldComponent, { | ||
| set: { changeDetection: ChangeDetectionStrategy.Default }, | ||
| }) | ||
| .compileComponents(); | ||
|
|
||
| fixture = TestBed.createComponent(ItemPageCitationFieldComponent); | ||
| component = fixture.componentInstance; | ||
| component.handle = mockHandle; | ||
| fixture.detectChanges(); | ||
| } | ||
|
|
||
| describe('when citace.pro.allowed is true', () => { | ||
| beforeEach(async () => { | ||
| await init('true'); | ||
| }); | ||
|
|
||
| it('should create', () => { | ||
| expect(component).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('should enable the widget and compose the CitacePRO URL', () => { | ||
| expect(component.citaceProStatus$.getValue()).toBeTrue(); | ||
| const sanitizer = TestBed.inject(DomSanitizer); | ||
| expect(sanitizer.sanitize(SecurityContext.RESOURCE_URL, component.citaceProURL$.getValue())) | ||
| .toBe(`${CITACE_PRO_URL}:${CITACE_PRO_UNIVERSITY}:${mockHandle}`); | ||
| }); | ||
|
|
||
| it('should render the iframe with a title', () => { | ||
| const iframe = fixture.debugElement.query(By.css('iframe')); | ||
| expect(iframe).not.toBeNull(); | ||
| expect(iframe.nativeElement.getAttribute('title')).toBe('Citace PRO'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when citace.pro.allowed is false', () => { | ||
| beforeEach(async () => { | ||
| await init('false'); | ||
| }); | ||
|
|
||
| it('should keep the widget hidden', () => { | ||
| expect(component.citaceProStatus$.getValue()).toBeFalse(); | ||
| expect(fixture.debugElement.query(By.css('iframe'))).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('makeCitaceProURL', () => { | ||
| beforeEach(async () => { | ||
| await init('true'); | ||
| }); | ||
|
|
||
| it('should reject a non-http(s) base URL', () => { | ||
| expect(component.makeCitaceProURL('javascript:alert(1)//', CITACE_PRO_UNIVERSITY)).toBeNull(); | ||
| }); | ||
|
|
||
| it('should reject missing config values', () => { | ||
| expect(component.makeCitaceProURL(undefined, CITACE_PRO_UNIVERSITY)).toBeNull(); | ||
| expect(component.makeCitaceProURL(CITACE_PRO_URL, undefined)).toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
85 changes: 85 additions & 0 deletions
85
...item-page/simple/field-components/specific-field/citation/item-page-citation.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { CommonModule } from '@angular/common'; | ||
| import { | ||
| Component, | ||
| Input, | ||
| OnInit, | ||
| } from '@angular/core'; | ||
| import { | ||
| DomSanitizer, | ||
| SafeResourceUrl, | ||
| } from '@angular/platform-browser'; | ||
| import { | ||
| BehaviorSubject, | ||
| combineLatest, | ||
| of, | ||
| } from 'rxjs'; | ||
| import { | ||
| catchError, | ||
| take, | ||
| } from 'rxjs/operators'; | ||
|
|
||
| import { ConfigurationDataService } from '../../../../../core/data/configuration-data.service'; | ||
| import { getFirstCompletedRemoteData } from '../../../../../core/shared/operators'; | ||
|
|
||
| @Component({ | ||
| selector: 'ds-item-page-citation-field', | ||
| templateUrl: './item-page-citation.component.html', | ||
| imports: [ | ||
| CommonModule, | ||
| ], | ||
| }) | ||
|
Kasinhou marked this conversation as resolved.
|
||
| export class ItemPageCitationFieldComponent implements OnInit { | ||
| @Input() handle: string; | ||
|
|
||
| citaceProStatus$ = new BehaviorSubject<boolean>(false); | ||
| citaceProURL$ = new BehaviorSubject<SafeResourceUrl | null>(null); | ||
|
|
||
| constructor( | ||
| private sanitizer: DomSanitizer, | ||
| private configService: ConfigurationDataService, | ||
| ) {} | ||
|
|
||
|
|
||
| ngOnInit() { | ||
| const citaceProUrl$ = this.configService.findByPropertyName('citace.pro.url').pipe( | ||
| getFirstCompletedRemoteData(), | ||
| catchError(() => of(null)), | ||
| ); | ||
| const universityUsingDspace$ = this.configService.findByPropertyName('citace.pro.university').pipe( | ||
| getFirstCompletedRemoteData(), | ||
| catchError(() => of(null)), | ||
| ); | ||
| const citaceProAllowed$ = this.configService.findByPropertyName('citace.pro.allowed').pipe( | ||
| getFirstCompletedRemoteData(), | ||
| catchError(() => of(null)), | ||
| ); | ||
|
|
||
| combineLatest([citaceProUrl$, universityUsingDspace$, citaceProAllowed$]).pipe( | ||
| take(1), | ||
| ).subscribe(([citaceProUrlData, universityData, citaceProAllowedData]) => { | ||
| const citaceProBaseUrl = citaceProUrlData?.payload?.values?.[0]; | ||
| const universityUsingDspace = universityData?.payload?.values?.[0]; | ||
| this.citaceProURL$.next(this.makeCitaceProURL(citaceProBaseUrl, universityUsingDspace)); | ||
|
|
||
| const citaceProAllowed = citaceProAllowedData?.payload?.values?.[0]; | ||
| this.citaceProStatus$.next(citaceProAllowed === 'true'); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| makeCitaceProURL( | ||
| citaceProBaseUrl: string, | ||
| universityUsingDspace: string, | ||
| ): SafeResourceUrl | null { | ||
| if (!citaceProBaseUrl || !universityUsingDspace || !this.handle) { | ||
| return null; | ||
| } | ||
| // Only http(s) bases may bypass Angular's resource-URL sanitization; | ||
| // anything else (javascript:, data:, ...) must not be trusted as iframe src. | ||
| if (!/^https?:\/\//i.test(citaceProBaseUrl)) { | ||
| return null; | ||
| } | ||
| const url = `${citaceProBaseUrl}:${universityUsingDspace}:${this.handle}`; | ||
| return this.sanitizer.bypassSecurityTrustResourceUrl(url); | ||
|
Kasinhou marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.