Skip to content
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

ACS-8770: fix api service bugs and remove workarounds #10212

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,24 @@
* limitations under the License.
*/

import { AdfHttpClient } from '@alfresco/adf-core/api';
import { StorageService, AppConfigService } from '@alfresco/adf-core';
import { AlfrescoApi, AlfrescoApiConfig } from '@alfresco/js-api';
import { Injectable } from '@angular/core';
import { AlfrescoApiService } from '../services/alfresco-api.service';

/** @deprecated use `AlfrescoApiService` instead */
@Injectable()
export class AlfrescoApiNoAuthService extends AlfrescoApiService {
constructor(
storage: StorageService,
appConfig: AppConfigService,
private readonly adfHttpClient: AdfHttpClient
) {
super(appConfig, storage);
constructor() {
super();
}

override createInstance(config: AlfrescoApiConfig) {
return new AlfrescoApi({
...config,
oauthInit: false
}, this.adfHttpClient);
return new AlfrescoApi(
{
...config,
oauthInit: false
},
this.adfHttpClient
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { EventEmitter } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { AppConfigModule, AppConfigService, AppConfigServiceMock } from '@alfresco/adf-core';
import { AppConfigService, AppConfigServiceMock } from '@alfresco/adf-core';
import { UploadService } from './upload.service';
import { RepositoryInfo } from '@alfresco/js-api';
import { BehaviorSubject } from 'rxjs';
Expand All @@ -26,6 +26,7 @@ import { FileModel, FileUploadStatus } from '../../common/models/file.model';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AlfrescoApiService } from '../../services';
import { AlfrescoApiServiceMock } from '../../mock';
import { AdfHttpClient } from '@alfresco/adf-core/api';
DenysVuika marked this conversation as resolved.
Show resolved Hide resolved

declare let jasmine: any;

Expand All @@ -38,11 +39,13 @@ describe('UploadService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [AppConfigModule, HttpClientTestingModule],
imports: [HttpClientTestingModule],
providers: [
UploadService,
{ provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock },
{ provide: AppConfigService, useClass: AppConfigServiceMock },
// TODO: remove this as soon as unit test not using jasmine.Ajax
{ provide: AdfHttpClient, useValue: null },
{
provide: DiscoveryApiService,
useValue: {
Expand Down
3 changes: 0 additions & 3 deletions lib/content-services/src/lib/content.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ import { AlfrescoViewerComponent } from './viewer';
import { ContentTypeDialogComponent } from './content-type';
import { MaterialModule } from './material.module';
import { AlfrescoIconComponent } from './alfresco-icon/alfresco-icon.component';
import { AlfrescoApiService } from './services/alfresco-api.service';
import { AlfrescoApiNoAuthService } from './api-factories/alfresco-api-no-auth.service';
import { AlfrescoApiLoaderService, createAlfrescoApiInstance } from './api-factories/alfresco-api-v2-loader.service';

@NgModule({
Expand Down Expand Up @@ -114,7 +112,6 @@ export class ContentModule {
providers: [
provideTranslations('adf-content-services', 'assets/adf-content-services'),
ContentAuthLoaderService,
{ provide: AlfrescoApiService, useClass: AlfrescoApiNoAuthService },
{
provide: APP_INITIALIZER,
useFactory: versionCompatibilityFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import { DocumentListService } from './document-list.service';
import { fakeAsync, TestBed } from '@angular/core/testing';
import { ContentTestingModule } from '../../testing/content.testing.module';
import { AdfHttpClient } from '@alfresco/adf-core/api';

declare let jasmine: any;

Expand Down Expand Up @@ -68,7 +69,11 @@ describe('DocumentListService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ContentTestingModule]
imports: [ContentTestingModule],
providers: [
// TODO: remove this as soon as unit test not using jasmine.Ajax
{ provide: AdfHttpClient, useValue: null }
]
});
service = TestBed.inject(DocumentListService);
jasmine.Ajax.install();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@

import { Injectable } from '@angular/core';
import { AlfrescoApiService } from '../services/alfresco-api.service';
import { AppConfigService, StorageService } from '@alfresco/adf-core';

@Injectable()
export class AlfrescoApiServiceMock extends AlfrescoApiService {
constructor() {
super();

constructor(protected appConfig: AppConfigService,
protected storageService: StorageService) {
super(appConfig, storageService);
if (!this.alfrescoApi) {
this.initAlfrescoApi();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { HttpClientTestingModule } from '@angular/common/http/testing';
import { EMPTY, of } from 'rxjs';
import { AlfrescoApiService } from '../../services';
import { AlfrescoApiServiceMock } from '../../mock';
import { AdfHttpClient } from '@alfresco/adf-core/api';

declare let jasmine: any;

Expand All @@ -34,7 +35,9 @@ describe('NodeCommentsService', () => {
imports: [HttpClientTestingModule],
providers: [
{ provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock },
{ provide: RedirectAuthService, useValue: { onLogin: EMPTY, onTokenReceived: of() } }
{ provide: RedirectAuthService, useValue: { onLogin: EMPTY, onTokenReceived: of() } },
// TODO: remove this as soon as unit test not using jasmine.Ajax
{ provide: AdfHttpClient, useValue: null }
]
});
service = TestBed.inject(NodeCommentsService);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*!
* @license
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { TestBed } from '@angular/core/testing';
import { AlfrescoApiService } from './alfresco-api.service';
import { AppConfigService, StorageService } from '@alfresco/adf-core';
import { HttpClientTestingModule } from '@angular/common/http/testing';

describe('AlfrescoApiService', () => {
let service: AlfrescoApiService;

beforeEach(() => {
const appConfigSpy = jasmine.createSpyObj('AppConfigService', ['get']);
const storageSpy = jasmine.createSpyObj('StorageService', ['']);

TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [AlfrescoApiService, { provide: AppConfigService, useValue: appConfigSpy }, { provide: StorageService, useValue: storageSpy }]
});

service = TestBed.inject(AlfrescoApiService);

service['lastConfig'] = {
hostBpm: 'http://localhost:8080',
contextRootBpm: 'activiti-app'
} as any;
});

it('should return true for excluded error URL', () => {
const currentFullPath = 'http://localhost:8080/activiti-app/api/enterprise/system/properties';
expect(service.isExcludedErrorListener(currentFullPath)).toBeTrue();
});

it('should return false for non-excluded error URL', () => {
const currentFullPath = 'http://localhost:8080/activiti-app/api/enterprise/other';
expect(service.isExcludedErrorListener(currentFullPath)).toBeFalse();
});
});
33 changes: 21 additions & 12 deletions lib/content-services/src/lib/services/alfresco-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
* limitations under the License.
*/

import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
import { inject, Injectable, InjectionToken } from '@angular/core';
import { AlfrescoApi, AlfrescoApiConfig } from '@alfresco/js-api';
import { ReplaySubject } from 'rxjs';
import { AlfrescoApiFactory } from './alfresco-api.interface';
import { AppConfigService, AppConfigValues, OauthConfigModel, OpenidConfiguration, StorageService } from '@alfresco/adf-core';
import { AdfHttpClient } from '@alfresco/adf-core/api';

export const ALFRESCO_API_FACTORY = new InjectionToken('ALFRESCO_API_FACTORY');

@Injectable({
providedIn: 'root'
})
export class AlfrescoApiService {
protected appConfig = inject(AppConfigService);
protected storageService = inject(StorageService);
protected alfrescoApiFactory = inject<AlfrescoApiFactory>(ALFRESCO_API_FACTORY, { optional: true });
protected adfHttpClient = inject(AdfHttpClient, { optional: true });

alfrescoApiInitialized: ReplaySubject<boolean> = new ReplaySubject(1);

protected alfrescoApi: AlfrescoApi;
Expand All @@ -36,20 +42,12 @@ export class AlfrescoApiService {

idpConfig: OpenidConfiguration;

private excludedErrorUrl: string[] = ['api/enterprise/system/properties'];
private readonly excludedErrorUrl = ['/api/enterprise/system/properties'];

getInstance(): AlfrescoApi {
return this.alfrescoApi;
}

constructor(
protected appConfig: AppConfigService,
protected storageService: StorageService,
@Optional()
@Inject(ALFRESCO_API_FACTORY)
private alfrescoApiFactory?: AlfrescoApiFactory
) {}

async load(config: AlfrescoApiConfig): Promise<void> {
this.currentAppConfig = config;

Expand Down Expand Up @@ -122,15 +120,26 @@ export class AlfrescoApiService {
if (this.alfrescoApiFactory) {
return this.alfrescoApiFactory.createAlfrescoApi(config);
}
return new AlfrescoApi(config);

return new AlfrescoApi(
{
...config,
oauthInit: false
},
this.adfHttpClient
);
DenysVuika marked this conversation as resolved.
Show resolved Hide resolved
}

isDifferentConfig(lastConfig: AlfrescoApiConfig, newConfig: AlfrescoApiConfig) {
return JSON.stringify(lastConfig) !== JSON.stringify(newConfig);
}

private formatExcludedPath(path: string): string {
return path.replace(this.lastConfig.hostBpm + '/' + this.lastConfig.contextRootBpm, '');
}

isExcludedErrorListener(currentFullPath: string): boolean {
const formattedPath = currentFullPath.replace(this.lastConfig.hostBpm + '/' + this.lastConfig.contextRootBpm, '');
const formattedPath = this.formatExcludedPath(currentFullPath);
return this.excludedErrorUrl.includes(formattedPath);
}
}
1 change: 1 addition & 0 deletions lib/core/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
export * from './lib/types';
export * from './lib/adf-http-client.service';
export * from './lib/interfaces';
export * from './lib/tokens';
Loading
Loading