chore: remove calling payments server for subscriptions if using third party api hosts (#2398)

This commit is contained in:
Karol Sójko
2023-08-09 13:16:19 +02:00
committed by GitHub
parent e05d8c9e76
commit 90dcb33a44
24 changed files with 233 additions and 89 deletions

View File

@@ -0,0 +1,60 @@
import { Result } from '@standardnotes/domain-core'
import { GetHost } from '../..'
import { IsApplicationUsingThirdPartyHost } from './IsApplicationUsingThirdPartyHost'
describe('IsApplicationUsingThirdPartyHost', () => {
let getHostUseCase: GetHost
const createUseCase = () => new IsApplicationUsingThirdPartyHost(getHostUseCase)
beforeEach(() => {
getHostUseCase = {} as jest.Mocked<GetHost>
getHostUseCase.execute = jest.fn().mockReturnValue(Result.ok('https://api.standardnotes.com'))
})
it('returns true if host is localhost', () => {
getHostUseCase.execute = jest.fn().mockReturnValue(Result.ok('http://localhost:3000'))
const useCase = createUseCase()
const result = useCase.execute()
expect(result.getValue()).toBe(true)
})
it('returns false if host is api.standardnotes.com', () => {
getHostUseCase.execute = jest.fn().mockReturnValue(Result.ok('https://api.standardnotes.com'))
const useCase = createUseCase()
const result = useCase.execute()
expect(result.getValue()).toBe(false)
})
it('returns false if host is sync.standardnotes.org', () => {
getHostUseCase.execute = jest.fn().mockReturnValue(Result.ok('https://sync.standardnotes.org'))
const useCase = createUseCase()
const result = useCase.execute()
expect(result.getValue()).toBe(false)
})
it('returns false if host is files.standardnotes.com', () => {
getHostUseCase.execute = jest.fn().mockReturnValue(Result.ok('https://files.standardnotes.com'))
const useCase = createUseCase()
const result = useCase.execute()
expect(result.getValue()).toBe(false)
})
it('returns true if host is not first party', () => {
getHostUseCase.execute = jest.fn().mockReturnValue(Result.ok('https://example.com'))
const useCase = createUseCase()
const result = useCase.execute()
expect(result.getValue()).toBe(true)
})
})

View File

@@ -0,0 +1,31 @@
import { Result, SyncUseCaseInterface } from '@standardnotes/domain-core'
import { GetHost } from './GetHost'
export class IsApplicationUsingThirdPartyHost implements SyncUseCaseInterface<boolean> {
private readonly APPLICATION_DEFAULT_HOSTS = ['api.standardnotes.com', 'sync.standardnotes.org']
private readonly FILES_DEFAULT_HOSTS = ['files.standardnotes.com']
constructor(private getHostUseCase: GetHost) {}
execute(): Result<boolean> {
const result = this.getHostUseCase.execute()
if (result.isFailed()) {
return Result.fail(result.getError())
}
const host = result.getValue()
return Result.ok(!this.isUrlFirstParty(host))
}
private isUrlFirstParty(url: string): boolean {
try {
const { host } = new URL(url)
return this.APPLICATION_DEFAULT_HOSTS.includes(host) || this.FILES_DEFAULT_HOSTS.includes(host)
} catch (error) {
return false
}
}
}