-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
288 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,68 @@ | ||
import { IocContract } from '@adonisjs/fold' | ||
import { initIocContainer } from '..' | ||
import { AppContext, AppServices } from '../app' | ||
import { Config } from '../config/app' | ||
import { createTestApp, TestContainer } from '../tests/app' | ||
import { createAsset } from '../tests/asset' | ||
import { createContext } from '../tests/context' | ||
import { truncateTables } from '../tests/tableManager' | ||
import { AutoPeeringRoutes } from './routes' | ||
|
||
describe('Auto Peering Routes', (): void => { | ||
let deps: IocContract<AppServices> | ||
let appContainer: TestContainer | ||
let autoPeeringRoutes: AutoPeeringRoutes | ||
|
||
beforeAll(async (): Promise<void> => { | ||
deps = initIocContainer({ ...Config, enableAutoPeering: true }) | ||
appContainer = await createTestApp(deps) | ||
autoPeeringRoutes = await deps.use('autoPeeringRoutes') | ||
}) | ||
|
||
afterEach(async (): Promise<void> => { | ||
await truncateTables(appContainer.knex) | ||
}) | ||
|
||
afterAll(async (): Promise<void> => { | ||
await appContainer.shutdown() | ||
}) | ||
|
||
describe('get', (): void => { | ||
test('returns peering details with assets', async (): Promise<void> => { | ||
const assets = await Promise.all([ | ||
createAsset(deps), | ||
createAsset(deps), | ||
createAsset(deps) | ||
]) | ||
|
||
const ctx = createContext<AppContext>({ | ||
headers: { Accept: 'application/json' }, | ||
url: `/` | ||
}) | ||
|
||
await expect(autoPeeringRoutes.get(ctx)).resolves.toBeUndefined() | ||
expect(ctx.body).toEqual({ | ||
ilpAddress: Config.ilpAddress, | ||
assets: expect.arrayContaining( | ||
assets.map((asset) => ({ | ||
code: asset.code, | ||
scale: asset.scale | ||
})) | ||
) | ||
}) | ||
}) | ||
|
||
test('returns peering details without assets', async (): Promise<void> => { | ||
const ctx = createContext<AppContext>({ | ||
headers: { Accept: 'application/json' }, | ||
url: `/` | ||
}) | ||
|
||
await expect(autoPeeringRoutes.get(ctx)).resolves.toBeUndefined() | ||
expect(ctx.body).toEqual({ | ||
ilpAddress: Config.ilpAddress, | ||
assets: [] | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains 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,35 @@ | ||
import { AppContext } from '../app' | ||
import { BaseService } from '../shared/baseService' | ||
import { AutoPeeringService } from './service' | ||
|
||
export interface ServiceDependencies extends BaseService { | ||
autoPeeringService: AutoPeeringService | ||
} | ||
|
||
export interface AutoPeeringRoutes { | ||
get(ctx: AppContext): Promise<void> | ||
} | ||
|
||
export async function createAutoPeeringRoutes( | ||
deps_: ServiceDependencies | ||
): Promise<AutoPeeringRoutes> { | ||
const deps: ServiceDependencies = { | ||
...deps_, | ||
logger: deps_.logger.child({ | ||
service: 'AutoPeeringRoutes' | ||
}) | ||
} | ||
|
||
return { | ||
get: (ctx: AppContext) => getPeeringDetails(deps, ctx) | ||
} | ||
} | ||
|
||
async function getPeeringDetails( | ||
deps: ServiceDependencies, | ||
ctx: AppContext | ||
): Promise<void> { | ||
const peeringDetails = await deps.autoPeeringService.getPeeringDetails() | ||
|
||
ctx.body = peeringDetails | ||
} |
This file contains 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,55 @@ | ||
import { IocContract } from '@adonisjs/fold' | ||
import { initIocContainer } from '..' | ||
import { AppServices } from '../app' | ||
import { Config } from '../config/app' | ||
import { createTestApp, TestContainer } from '../tests/app' | ||
import { createAsset } from '../tests/asset' | ||
import { truncateTables } from '../tests/tableManager' | ||
import { AutoPeeringService } from './service' | ||
|
||
describe('Auto Peering Service', (): void => { | ||
let deps: IocContract<AppServices> | ||
let appContainer: TestContainer | ||
let autoPeeringService: AutoPeeringService | ||
|
||
beforeAll(async (): Promise<void> => { | ||
deps = initIocContainer({ ...Config, enableAutoPeering: true }) | ||
appContainer = await createTestApp(deps) | ||
autoPeeringService = await deps.use('autoPeeringService') | ||
}) | ||
|
||
afterEach(async (): Promise<void> => { | ||
await truncateTables(appContainer.knex) | ||
}) | ||
|
||
afterAll(async (): Promise<void> => { | ||
await appContainer.shutdown() | ||
}) | ||
|
||
describe('getPeeringDetails', (): void => { | ||
test('returns peering details', async (): Promise<void> => { | ||
const assets = await Promise.all([ | ||
createAsset(deps), | ||
createAsset(deps), | ||
createAsset(deps) | ||
]) | ||
|
||
expect(autoPeeringService.getPeeringDetails()).resolves.toEqual({ | ||
ilpAddress: Config.ilpAddress, | ||
assets: expect.arrayContaining( | ||
assets.map((asset) => ({ | ||
code: asset.code, | ||
scale: asset.scale | ||
})) | ||
) | ||
}) | ||
}) | ||
|
||
test('returns peering details with no assets', async (): Promise<void> => { | ||
expect(autoPeeringService.getPeeringDetails()).resolves.toEqual({ | ||
ilpAddress: Config.ilpAddress, | ||
assets: [] | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains 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,46 @@ | ||
import { AssetService } from '../asset/service' | ||
import { IAppConfig } from '../config/app' | ||
import { BaseService } from '../shared/baseService' | ||
|
||
export interface PeeringDetails { | ||
ilpAddress: string | ||
assets: { code: string; scale: number }[] | ||
} | ||
|
||
export interface AutoPeeringService { | ||
getPeeringDetails(): Promise<PeeringDetails> | ||
} | ||
|
||
export interface ServiceDependencies extends BaseService { | ||
assetService: AssetService | ||
config: IAppConfig | ||
} | ||
|
||
export async function createAutoPeeringService( | ||
deps_: ServiceDependencies | ||
): Promise<AutoPeeringService> { | ||
const deps: ServiceDependencies = { | ||
...deps_, | ||
logger: deps_.logger.child({ | ||
service: 'AutoPeeringService' | ||
}) | ||
} | ||
|
||
return { | ||
getPeeringDetails: () => getPeeringDetails(deps) | ||
} | ||
} | ||
|
||
async function getPeeringDetails( | ||
deps: ServiceDependencies | ||
): Promise<PeeringDetails> { | ||
const assets = await deps.assetService.getAll() | ||
|
||
return { | ||
ilpAddress: deps.config.ilpAddress, | ||
assets: assets.map((asset) => ({ | ||
code: asset.code, | ||
scale: asset.scale | ||
})) | ||
} | ||
} |
This file contains 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 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