import { TestBed } from '@angular/core/testing' ; import { AppTestingModule } from '../testing/app-testing.module' ; import { MockStore , provideMockStore } from '@ngrx/store/testing' ; import { ContentUrlService } from './content-url.service' ; import { getRepositoryStatus } from '@alfresco/aca-shared/store' ; import { ContentApiService } from '@alfresco/aca-shared' ; import { of , throwError } from 'rxjs' ; describe ( 'ContentUrlService' , ( ) => { let contentUrlService ; let contentApiService ; let store ; const fakeNodeId = 'fake-node-id' ; const fakeVersionId = 'fake-version-id' ; const fakeNodeContentUrl = 'https://api.example.com/fake-node-content-url' ; const fakeNodeDirectAccessUrl = 'https://remote.example.com/fake-node-content-url' ; const requestDauMock = { entry : { contentUrl : fakeNodeDirectAccessUrl , attachment : true , expiryTime : new Date ( ) } } ; const requestDauErrorMock = { error : 'fakeError' } ; const overrideSelectorWithEnabledDAU = ( ) => { store . overrideSelector ( getRepositoryStatus , { status : { isDirectAccessUrlEnabled : true } } as any ) ; } ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ AppTestingModule ] , providers : [ provideMockStore ( ) ] } ) ; contentUrlService = TestBed . inject ( ContentUrlService ) ; contentApiService = TestBed . inject ( ContentApiService ) ; store = TestBed . inject ( MockStore ) ; store . overrideSelector ( getRepositoryStatus , { status : { isDirectAccessUrlEnabled : false } } as any ) ; } ) ; describe ( 'Nodes' , ( ) => { let getContentUrlSpy ; let requestNodeDirectAccessUrlSpy ; beforeEach ( ( ) => { getContentUrlSpy = spyOn ( contentApiService , 'getContentUrl' ) . and . returnValue ( fakeNodeContentUrl ) ; } ) ; it ( 'should get the node URL from the content API if DAU is disabled' , async ( ) => { requestNodeDirectAccessUrlSpy = spyOn ( contentApiService , 'requestNodeDirectAccessUrl' ) . and . returnValue ( of ( requestDauMock ) ) ; const url = await contentUrlService . getNodeContentUrl ( fakeNodeId ) . toPromise ( ) ; expect ( url ) . toBe ( fakeNodeContentUrl ) ; expect ( getContentUrlSpy ) . toHaveBeenCalledWith ( fakeNodeId , true ) ; expect ( requestNodeDirectAccessUrlSpy ) . not . toHaveBeenCalled ( ) ; } ) ; it ( 'should get the node URL from remote if DAU is enabled' , async ( ) => { overrideSelectorWithEnabledDAU ( ) ; requestNodeDirectAccessUrlSpy = spyOn ( contentApiService , 'requestNodeDirectAccessUrl' ) . and . returnValue ( of ( requestDauMock ) ) ; const url = await contentUrlService . getNodeContentUrl ( fakeNodeId ) . toPromise ( ) ; expect ( url ) . toBe ( fakeNodeDirectAccessUrl ) ; expect ( getContentUrlSpy ) . not . toHaveBeenCalled ( ) ; expect ( requestNodeDirectAccessUrlSpy ) . toHaveBeenCalledWith ( fakeNodeId ) ; } ) ; it ( 'should fallback to getting the node URL from the content API if an error occurs requesting a DAU' , async ( ) => { overrideSelectorWithEnabledDAU ( ) ; requestNodeDirectAccessUrlSpy = spyOn ( contentApiService , 'requestNodeDirectAccessUrl' ) . and . returnValue ( throwError ( requestDauErrorMock ) ) ; const url = await contentUrlService . getNodeContentUrl ( fakeNodeId ) . toPromise ( ) ; expect ( url ) . toBe ( fakeNodeContentUrl ) ; expect ( getContentUrlSpy ) . toHaveBeenCalledWith ( fakeNodeId , true ) ; expect ( requestNodeDirectAccessUrlSpy ) . toHaveBeenCalledWith ( fakeNodeId ) ; } ) ; } ) ; describe ( 'Versions' , ( ) => { let getVersionContentUrlSpy ; let requestVersionDirectAccessUrlSpy ; beforeEach ( ( ) => { getVersionContentUrlSpy = spyOn ( contentApiService , 'getVersionContentUrl' ) . and . returnValue ( fakeNodeContentUrl ) ; } ) ; it ( 'should get the version URL from the content API if DAU is disabled' , async ( ) => { requestVersionDirectAccessUrlSpy = spyOn ( contentApiService , 'requestVersionDirectAccessUrl' ) . and . returnValue ( of ( requestDauMock ) ) ; const url = await contentUrlService . getVersionContentUrl ( fakeNodeId , fakeVersionId ) . toPromise ( ) ; expect ( url ) . toBe ( fakeNodeContentUrl ) ; expect ( getVersionContentUrlSpy ) . toHaveBeenCalledWith ( fakeNodeId , fakeVersionId , true ) ; expect ( requestVersionDirectAccessUrlSpy ) . not . toHaveBeenCalled ( ) ; } ) ; it ( 'should get the version URL from remote if DAU is enabled' , async ( ) => { overrideSelectorWithEnabledDAU ( ) ; requestVersionDirectAccessUrlSpy = spyOn ( contentApiService , 'requestVersionDirectAccessUrl' ) . and . returnValue ( of ( requestDauMock ) ) ; const url = await contentUrlService . getVersionContentUrl ( fakeNodeId , fakeVersionId ) . toPromise ( ) ; expect ( url ) . toBe ( fakeNodeDirectAccessUrl ) ; expect ( getVersionContentUrlSpy ) . not . toHaveBeenCalled ( ) ; expect ( requestVersionDirectAccessUrlSpy ) . toHaveBeenCalledWith ( fakeNodeId , fakeVersionId ) ; } ) ; it ( 'should fallback to getting the version URL from the content API if an error occurs requesting a DAU' , async ( ) => { overrideSelectorWithEnabledDAU ( ) ; requestVersionDirectAccessUrlSpy = spyOn ( contentApiService , 'requestVersionDirectAccessUrl' ) . and . returnValue ( throwError ( requestDauErrorMock ) ) ; const url = await contentUrlService . getVersionContentUrl ( fakeNodeId , fakeVersionId ) . toPromise ( ) ; expect ( url ) . toBe ( fakeNodeContentUrl ) ; expect ( getVersionContentUrlSpy ) . toHaveBeenCalledWith ( fakeNodeId , fakeVersionId , true ) ; expect ( requestVersionDirectAccessUrlSpy ) . toHaveBeenCalledWith ( fakeNodeId , fakeVersionId ) ; } ) ; } ) ; } ) ;