Skip to content

Commit

Permalink
* minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrn committed Oct 16, 2020
1 parent b9e6931 commit c11bafe
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import { Subject } from 'rxjs';
import { Node } from '@alfresco/js-api';
import { AlfrescoApiService } from '@alfresco/adf-core';

export interface AttachFileWidgetDialogComponentData {
title: string;
Expand All @@ -29,5 +28,6 @@ export interface AttachFileWidgetDialogComponentData {
isSelectionValid?: (entry: Node) => boolean;
showFilesInResult?: boolean;
loginOnly?: boolean;
login?: Subject<AlfrescoApiService>;
accountIdentifier?: string;
registerExternalHost?: Function;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ describe('AttachFileWidgetDialogComponent', () => {
let siteService: SitesService;
let nodeService: NodesApiService;
let documentListService: DocumentListService;
let apiService: AlfrescoApiService;
let matDialogRef: MatDialogRef<AttachFileWidgetDialogComponent>;

let isLogged = false;
const fakeSite = new SiteEntry({ entry: { id: 'fake-site', guid: 'fake-site', title: 'fake-site', visibility: 'visible' } });
Expand All @@ -70,6 +72,8 @@ describe('AttachFileWidgetDialogComponent', () => {
siteService = fixture.debugElement.injector.get(SitesService);
nodeService = fixture.debugElement.injector.get(NodesApiService);
documentListService = fixture.debugElement.injector.get(DocumentListService);
matDialogRef = fixture.debugElement.injector.get(MatDialogRef);
apiService = fixture.debugElement.injector.get(AlfrescoApiService);

spyOn(documentListService, 'getFolderNode').and.returnValue(of(<NodeEntry> { entry: { path: { elements: [] } } }));
spyOn(documentListService, 'getFolder').and.returnValue(throwError('No results for test'));
Expand Down Expand Up @@ -170,4 +174,39 @@ describe('AttachFileWidgetDialogComponent', () => {
expect(titleElement.nativeElement.innerText).toBe('ATTACH-FILE.ACTIONS.CHOOSE_ITEM');
});
});

describe('login only', () => {
beforeEach(async(() => {
spyOn(authService, 'login').and.returnValue(of({ type: 'type', ticket: 'ticket'}));
spyOn(matDialogRef, 'close').and.callThrough();
fixture.detectChanges();
widget.data.loginOnly = true;
widget.data.registerExternalHost = () => {};
isLogged = false;
}));

it('should close the dialog once user loggedIn', () => {
fixture.detectChanges();
isLogged = true;
const loginButton: HTMLButtonElement = element.querySelector('button[data-automation-id="attach-file-dialog-actions-login"]');
const usernameInput: HTMLInputElement = element.querySelector('#username');
const passwordInput: HTMLInputElement = element.querySelector('#password');
usernameInput.value = 'fake-user';
passwordInput.value = 'fake-user';
usernameInput.dispatchEvent(new Event('input'));
passwordInput.dispatchEvent(new Event('input'));
loginButton.click();
authService.onLogin.next('logged In');
fixture.detectChanges();
expect(matDialogRef.close).toHaveBeenCalled();
});

it('should close the dialog immediately if user already loggedIn', () => {
isLogged = true;
fixture.detectChanges();
spyOn(apiService, 'getInstance').and.returnValue({ isLoggedIn: () => true });
widget.updateExternalHost();
expect(matDialogRef.close).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,21 @@ export class AttachFileWidgetDialogComponent {
this.action = data.actionName ? data.actionName.toUpperCase() : 'CHOOSE';
this.buttonActionName = `ATTACH-FILE.ACTIONS.${this.action}`;
this.updateTitle('DROPDOWN.MY_FILES_OPTION');
this.toggleExternalHostLoginDialog();
this.updateExternalHost();
}

private toggleExternalHostLoginDialog() {
updateExternalHost() {
this.authenticationService.onLogin.subscribe(() => this.registerAndClose());
if (this.externalApiService.getInstance().isLoggedIn()) {
this.closeDialog();
this.registerAndClose();
}
this.authenticationService.onLogin.subscribe(() => this.closeDialog());
}

private closeDialog() {
private registerAndClose() {
this.data.registerExternalHost(this.data.accountIdentifier, this.externalApiService);
if (this.data.loginOnly) {
this.data.login.next(this.externalApiService);
this.matDialogRef.close(this.externalApiService);
this.data.selected.complete();
this.matDialogRef.close();
}
}

Expand All @@ -80,7 +81,6 @@ export class AttachFileWidgetDialogComponent {
}

close() {
this.data.login.complete();
this.data.selected.complete();
}

Expand All @@ -93,7 +93,6 @@ export class AttachFileWidgetDialogComponent {
}

onClick() {
this.data.login.complete();
this.data.selected.next(this.chosenNode);
this.data.selected.complete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { MatDialog } from '@angular/material/dialog';
import { EventEmitter, Injectable, Output } from '@angular/core';
import { AlfrescoApiService, TranslationService } from '@alfresco/adf-core';
import { Observable, of, race, Subject } from 'rxjs';
import { Observable, of, Subject } from 'rxjs';
import { AttachFileWidgetDialogComponentData } from './attach-file-widget-dialog-component.interface';
import { AlfrescoEndpointRepresentation, Node } from '@alfresco/js-api';
import { AttachFileWidgetDialogComponent } from './attach-file-widget-dialog.component';
Expand All @@ -29,7 +29,7 @@ import { switchMap } from 'rxjs/operators';
})
// tslint:disable-next-line: directive-class-suffix
export class AttachFileWidgetDialogService {
private endpointAuthServices: { [key: string]: AlfrescoApiService } = {};
private externalApis: { [key: string]: AlfrescoApiService } = {};

/** Emitted when an error occurs. */
@Output()
Expand All @@ -46,15 +46,15 @@ export class AttachFileWidgetDialogService {
* @returns Information about the chosen file(s)
*/
openLogin(repository: AlfrescoEndpointRepresentation, currentFolderId = '-my-'): Observable<Node[]> {
const { ecmHost, login, selected } = this.constructPayload(repository);
const { title, ecmHost, selected, registerExternalHost } = this.constructPayload(repository);
const data: AttachFileWidgetDialogComponentData = {
title : this.getLoginTitleTranslation(ecmHost),
title,
selected,
ecmHost,
currentFolderId,
isSelectionValid: (entry: Node) => entry.isFile,
showFilesInResult: true,
login: login
registerExternalHost
};

this.openLoginDialog(data, 'adf-attach-file-widget-dialog', '630px');
Expand All @@ -66,40 +66,42 @@ export class AttachFileWidgetDialogService {
}

private showExternalHostLoginDialog(repository: AlfrescoEndpointRepresentation): Observable<AlfrescoApiService> {
const { login, selected, ecmHost } = this.constructPayload(repository);
const data = {
title : this.getLoginTitleTranslation(ecmHost),
selected,
ecmHost,
login,
...this.constructPayload(repository),
loginOnly: true
};
return race(this.dialog.open(AttachFileWidgetDialogComponent, { data, panelClass: 'adf-attach-file-widget-dialog', width: '630px' }).afterClosed(), data.login);
return this.dialog.open(AttachFileWidgetDialogComponent, { data, panelClass: 'adf-attach-file-widget-dialog', width: '630px' })
.afterClosed();
}

downloadURL(repository: AlfrescoEndpointRepresentation, sourceId: string): Observable<string> {
const accountIdentifier = 'alfresco-' + repository.id + '-' + repository.name;
if (this.endpointAuthServices[accountIdentifier]?.getInstance()?.isLoggedIn()) {
return of(this.endpointAuthServices[accountIdentifier].contentApi.getContentUrl(sourceId));
const { accountIdentifier } = this.constructPayload(repository);

if (this.externalApis[accountIdentifier]?.getInstance()?.isLoggedIn()) {
return of(this.externalApis[accountIdentifier].contentApi.getContentUrl(sourceId));
}
return this.showExternalHostLoginDialog(repository).pipe(switchMap((apiService) => {
this.endpointAuthServices[accountIdentifier] = apiService;
return of(this.endpointAuthServices[accountIdentifier].getInstance().content.getContentUrl(sourceId));
}));

return this.showExternalHostLoginDialog(repository).pipe(
switchMap(() => of(this.externalApis[accountIdentifier].getInstance().content.getContentUrl(sourceId)))
);
}

private constructPayload(repository: AlfrescoEndpointRepresentation) {
const accountIdentifier = 'alfresco-' + repository.id + '-' + repository.name;
const ecmHost = repository.repositoryUrl.replace('/alfresco', '');
const login = new Subject<AlfrescoApiService>();
const selected = new Subject<Node[]>();
selected.subscribe({
complete: this.close.bind(this)
});
login.subscribe({
next: (externalApiService) => this.endpointAuthServices[accountIdentifier] = externalApiService
});
return { ecmHost, login, selected };
const title = this.getLoginTitleTranslation(ecmHost);
const registerExternalHost = this.addService.bind(this);
return { ecmHost, accountIdentifier, selected, title, registerExternalHost };
}

addService(accountIdentifier: string, apiService: AlfrescoApiService) {
if (!this.externalApis[accountIdentifier]) {
this.externalApis[accountIdentifier] = apiService;
}
}

/** Closes the currently open dialog. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { of } from 'rxjs';
import { Node } from '@alfresco/js-api';
import { ProcessTestingModule } from '../testing/process.testing.module';
import { TranslateModule } from '@ngx-translate/core';
import { AttachFileWidgetDialogService } from './attach-file-widget-dialog.service';

const fakeRepositoryListAnswer = [
{
Expand All @@ -50,7 +51,16 @@ const fakeRepositoryListAnswer = [
'metaDataAllowed': true,
'name': 'GOKUSHARE',
'repositoryUrl': 'http://localhost:0000/GOKUSHARE'
}];
},
{
'authorized': true,
'serviceId': 'alfresco-2000-external',
'metaDataAllowed': true,
'name': 'external',
'repositoryUrl': 'http://externalhost.com/alfresco',
'id': 2000
}
];

const onlyLocalParams = {
fileSource: {
Expand Down Expand Up @@ -82,6 +92,16 @@ const definedSourceParams = {
}
};

const externalDefinedSourceParams = {
fileSource: {
serviceId: 'external-sources',
name: 'external',
selectedFolder: {
accountId: 'external-account-id'
}
}
};

const fakeMinimalNode: Node = <Node> {
id: 'fake',
name: 'fake-name',
Expand Down Expand Up @@ -130,6 +150,7 @@ describe('AttachFileWidgetComponent', () => {
let processContentService: ProcessContentService;
let downloadService: DownloadService;
let formService: FormService;
let attachFileWidgetDialogService: AttachFileWidgetDialogService;

setupTestBed({
imports: [
Expand All @@ -148,6 +169,7 @@ describe('AttachFileWidgetComponent', () => {
processContentService = TestBed.inject(ProcessContentService);
downloadService = TestBed.inject(DownloadService);
formService = TestBed.inject(FormService);
attachFileWidgetDialogService = TestBed.inject(AttachFileWidgetDialogService);
}));

afterEach(() => {
Expand Down Expand Up @@ -532,4 +554,23 @@ describe('AttachFileWidgetComponent', () => {
expect(showOption.disabled).toBeTruthy();
});
});

it('should be able to upload files when a defined folder from external content service', async(() => {
widget.field = new FormFieldModel(new FormModel(), { type: FormFieldTypes.UPLOAD, value: [] });
widget.field.id = 'attach-external-file-attach';
widget.field.params = <FormFieldMetadata> externalDefinedSourceParams;
spyOn(activitiContentService, 'getAlfrescoRepositories').and.returnValue(of(fakeRepositoryListAnswer));
spyOn(activitiContentService, 'applyAlfrescoNode').and.returnValue(of(fakePngAnswer));
spyOn(attachFileWidgetDialogService, 'openLogin').and.returnValue(of([fakeMinimalNode]));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
const attachButton: HTMLButtonElement = element.querySelector('#attach-external-file-attach');
attachButton.click();
fixture.detectChanges();
fixture.debugElement.query(By.css('#attach-external')).nativeElement.click();
fixture.detectChanges();
expect(element.querySelector('#file-1155-icon')).not.toBeNull();
});
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ export class AttachFileWidgetComponent extends UploadWidgetComponent implements
const sourceHost = this.findSource(file.source);
if (this.isExternalHost(sourceHost)) {
this.attachDialogService.downloadURL(sourceHost, file.sourceId).subscribe((nodeUrl) => {
console.log(nodeUrl);
this.downloadService.downloadUrl(nodeUrl, file.name);
});
} else {
Expand All @@ -217,14 +216,13 @@ export class AttachFileWidgetComponent extends UploadWidgetComponent implements
}

openSelectDialog(repository: AlfrescoEndpointRepresentation) {
const accountIdentifier = 'alfresco-' + repository.id + '-' + repository.name;
if (this.isExternalHost(repository)) {
this.uploadFileFromExternalCS(repository);
} else {
this.contentDialog.openFileBrowseDialogByDefaultLocation().subscribe(
(selections: Node[]) => {
this.tempFilesList.push(...selections);
this.uploadFileFromCS(selections, accountIdentifier);
this.uploadFileFromCS(selections, `alfresco-${repository.id}-${repository.name}`);
});
}
}
Expand All @@ -236,20 +234,15 @@ export class AttachFileWidgetComponent extends UploadWidgetComponent implements
}

private findSource(sourceIdentifier: string): AlfrescoEndpointRepresentation {
return this.repositoryList.find(repository => {
const accountIdentifier = 'alfresco-' + repository.id + '-' + repository.name;
return sourceIdentifier === accountIdentifier;
});
return this.repositoryList.find(repository => sourceIdentifier === `alfresco-${repository.id}-${repository.name}`);
}

private uploadFileFromExternalCS(repository: AlfrescoEndpointRepresentation, currentFolderId?: string) {
const accountIdentifier = 'alfresco-' + repository.id + '-' + repository.name;

this.attachDialogService.openLogin(repository, currentFolderId).subscribe(
(selections: any[]) => {
selections.forEach((node) => node.isExternal = true);
this.tempFilesList.push(...selections);
this.uploadFileFromCS(selections, accountIdentifier);
this.uploadFileFromCS(selections, `alfresco-${repository.id}-${repository.name}`);
});
}

Expand Down

0 comments on commit c11bafe

Please sign in to comment.