Skip to content

Commit

Permalink
refactor(core-api): accept address, publicKey, delegate name as busin…
Browse files Browse the repository at this point in the history
…ess id (#3310)
  • Loading branch information
dated authored and faustbrian committed Dec 5, 2019
1 parent b23a335 commit 7f07ff0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 38 deletions.
66 changes: 43 additions & 23 deletions __tests__/integration/core-api/handlers/businesses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,47 @@ import { Database, State } from "@arkecosystem/core-interfaces";
import { setUp, tearDown } from "../__support__/setup";
import { utils } from "../utils";

const username = "username";
const address = "AG8kwwk4TsYfA2HdwaWBVAJQBj6VhdcpMo";
const publicKey = "0377f81a18d25d77b100cb17e829a72259f08334d064f6c887298917a04df8f647";

beforeAll(async () => await setUp());
afterAll(async () => await tearDown());

describe("API 2.0 - Businesses", () => {
let walletManager: State.IWalletManager;

const bridgechainAsset = {
name: "arkecosystem1",
seedNodes: ["74.125.224.71", "74.125.224.72", "64.233.173.193", "2001:4860:4860::8888", "2001:4860:4860::8844"],
genesisHash: "127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935",
bridgechainRepository: "http://www.repository.com/myorg/myrepo",
ports: { "@arkecosystem/core-api": 4003 },
};

const businessAttribute = {
businessAsset: {
name: "bizzz",
website: "biz.com",
bridgechains: {
"127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935": {
bridgechainAsset: {
name: "arkecosystem1",
seedNodes: [
"74.125.224.71",
"74.125.224.72",
"64.233.173.193",
"2001:4860:4860::8888",
"2001:4860:4860::8844",
],
genesisHash: "127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935",
bridgechainRepository: "http://www.repository.com/myorg/myrepo",
ports: { "@arkecosystem/core-api": 4003 },
},
bridgechainAsset,
},
},
},
};

const validIdentifiers = {
username,
address,
publicKey,
};

beforeAll(() => {
walletManager = app.resolvePlugin<Database.IDatabaseService>("database").walletManager;

const wallet = walletManager.findByPublicKey(publicKey);
wallet.setAttribute("delegate.username", username);
wallet.setAttribute("business", businessAttribute);
wallet.setAttribute("business.bridgechains", businessAttribute.businessAsset.bridgechains);

Expand All @@ -60,21 +66,35 @@ describe("API 2.0 - Businesses", () => {
});

describe("GET /businesses/:id", () => {
it("should GET business by id (wallet publicKey)", async () => {
const response = await utils.request("GET", `businesses/${publicKey}`);
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeObject();
expect(response.data.data.attributes.business).toEqual(businessAttribute);
it("should GET a business by the given valid identifier", async () => {
for (const identifier of Object.values(validIdentifiers)) {
const response = await utils.request("GET", `businesses/${identifier}`);
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeObject();

expect(response.data.data.attributes.business).toEqual(businessAttribute);
}
});

it("should fail to GET a business by an unknown identifier", async () => {
utils.expectError(await utils.request("GET", "businesses/i_do_not_exist"), 404);
});
});

describe("GET /businesses/:id/bridgechains", () => {
it("should GET business bridgechains", async () => {
const response = await utils.request("GET", `businesses/${publicKey}/bridgechains`);
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
expect(response.data.data).toHaveLength(1);
// TODO check bridgechainId is correct (after bridgechainId = genesisHash PR is merged)
for (const identifier of Object.values(validIdentifiers)) {
const response = await utils.request("GET", `businesses/${identifier}/bridgechains`);
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
expect(response.data.data).toHaveLength(1);

expect(response.data.data[0].genesisHash).toEqual(bridgechainAsset.genesisHash);
}
});

it("should fail to GET business bridgechains by an unknown identifier", async () => {
utils.expectError(await utils.request("GET", "businesses/i_do_not_exist/bridgechains"), 404);
});
});

Expand Down
14 changes: 6 additions & 8 deletions packages/core-api/src/handlers/businesses/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,24 @@ const index = async request => {
};

const show = async request => {
const business = databaseService.walletManager.findByPublicKey(request.params.id);
const wallet = databaseService.wallets.findById(Database.SearchScope.Wallets, request.params.id);

if (!business) {
if (!wallet || !wallet.hasAttribute("business")) {
return Boom.notFound("Business not found");
}

return respondWithResource(business, "business");
return respondWithResource(wallet, "business");
};

const bridgechains = async request => {
const business = databaseService.wallets.search(Database.SearchScope.Businesses, {
publicKey: request.params.id,
});
const wallet = databaseService.wallets.findById(Database.SearchScope.Wallets, request.params.id);

if (!business) {
if (!wallet || !wallet.hasAttribute("business")) {
return Boom.notFound("Business not found");
}

const bridgechains = databaseService.wallets.search(Database.SearchScope.Bridgechains, {
publicKey: request.params.id,
publicKey: wallet.publicKey,
...request.query,
...paginate(request),
});
Expand Down
10 changes: 3 additions & 7 deletions packages/core-api/src/handlers/businesses/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from "@hapi/joi";
import { orderBy, pagination } from "../shared/schemas";
import { orderBy, pagination, walletId } from "../shared/schemas";

export const index: object = {
query: {
Expand All @@ -16,17 +16,13 @@ export const index: object = {

export const show: object = {
params: {
id: Joi.string()
.hex()
.length(66),
id: walletId,
},
};

export const bridgechains: object = {
params: {
id: Joi.string()
.hex()
.length(66),
id: walletId,
},
query: {
...pagination,
Expand Down

0 comments on commit 7f07ff0

Please sign in to comment.