Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from logion-network/feature/logion-prefix
Browse files Browse the repository at this point in the history
Handle parachain prefix
  • Loading branch information
gdethier authored Apr 11, 2024
2 parents 4b887de + 87b732b commit 1d74207
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 84 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logion-directory",
"version": "0.9.3",
"version": "0.9.4-1",
"private": true,
"author": {
"name": "Logion Team",
Expand All @@ -23,8 +23,8 @@
"typeorm": "node ./node_modules/typeorm/cli.js -d ./dist/db-tools/ormconfig.js"
},
"dependencies": {
"@logion/node-api": "^0.28.4",
"@logion/rest-api-core": "^0.4.7",
"@logion/node-api": "^0.29.0-2",
"@logion/rest-api-core": "^0.4.8-6",
"ansi-regex": "^6.0.1",
"body-parser": "^1.20.2",
"bson": "^4.7.0",
Expand Down
12 changes: 7 additions & 5 deletions src/logion/controllers/legalofficer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
LegalOfficerDescription,
LegalOfficerFactory,
} from "../model/legalofficer.model.js";
import { ValidAccountId } from "@logion/node-api";

export function fillInSpec(spec: OpenAPIV3.Document): void {
const tagName = 'Legal Officers';
Expand Down Expand Up @@ -67,7 +68,8 @@ export class LegalOfficerController extends ApiController {
@HttpGet('/:address')
@Async()
async getLegalOfficer(address: string): Promise<LegalOfficerView> {
const legalOfficer = await this.legalOfficerRepository.findByAddress(address);
const account = ValidAccountId.polkadot(address);
const legalOfficer = await this.legalOfficerRepository.findByAccount(account);
if (legalOfficer) {
return this.toView(legalOfficer.getDescription());
} else {
Expand All @@ -79,7 +81,7 @@ export class LegalOfficerController extends ApiController {
const userIdentity = description.userIdentity;
const postalAddress = description.postalAddress;
return {
address: description.address,
address: description.account.address,
userIdentity: {
firstName: userIdentity.firstName,
lastName: userIdentity.lastName,
Expand Down Expand Up @@ -116,14 +118,14 @@ export class LegalOfficerController extends ApiController {
@Async()
async createOrUpdateLegalOfficer(createOrUpdate: CreateOrUpdateLegalOfficerView): Promise<LegalOfficerView> {
const authenticatedUser = await this.authenticationService.authenticatedUser(this.request);
const address = authenticatedUser.address;
const account = authenticatedUser.toValidAccountId();
if (!await authenticatedUser.isLegalOfficer()) {
throw new UnauthorizedException(`${ address } is not a Legal Officer.`)
throw new UnauthorizedException(`${ account.address } is not a Legal Officer.`);
}
const userIdentity = requireDefined(createOrUpdate.userIdentity);
const postalAddress = requireDefined(createOrUpdate.postalAddress);
const description: LegalOfficerDescription = {
address,
account,
userIdentity: {
firstName: userIdentity.firstName || "",
lastName: userIdentity.lastName || "",
Expand Down
13 changes: 8 additions & 5 deletions src/logion/model/legalofficer.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { appDataSource } from "@logion/rest-api-core";
import { Entity, Column, PrimaryColumn, Repository } from "typeorm";
import { injectable } from "inversify";
import { ValidAccountId } from "@logion/node-api";

export const DB_SS58_PREFIX = 42;

@Entity("legal_officer")
export class LegalOfficerAggregateRoot {
Expand Down Expand Up @@ -43,7 +46,7 @@ export class LegalOfficerAggregateRoot {

getDescription(): LegalOfficerDescription {
return {
address: this.address!,
account: ValidAccountId.polkadot(this.address!),
userIdentity: {
firstName: this.firstName || "",
lastName: this.lastName || "",
Expand All @@ -64,7 +67,7 @@ export class LegalOfficerAggregateRoot {
}

export interface LegalOfficerDescription {
readonly address: string;
readonly account: ValidAccountId;
readonly userIdentity: UserIdentity;
readonly postalAddress: PostalAddress;
readonly additionalDetails: string;
Expand All @@ -91,7 +94,7 @@ export class LegalOfficerFactory {

newLegalOfficer(description: LegalOfficerDescription): LegalOfficerAggregateRoot {
const legalOfficer = new LegalOfficerAggregateRoot();
legalOfficer.address = description.address
legalOfficer.address = description.account.getAddress(DB_SS58_PREFIX);

const userIdentity = description.userIdentity;
legalOfficer.firstName = userIdentity.firstName;
Expand Down Expand Up @@ -122,8 +125,8 @@ export class LegalOfficerRepository {

readonly repository: Repository<LegalOfficerAggregateRoot>

public findByAddress(address: string): Promise<LegalOfficerAggregateRoot | null> {
return this.repository.findOneBy({ address });
public findByAccount(address: ValidAccountId): Promise<LegalOfficerAggregateRoot | null> {
return this.repository.findOneBy({ address: address.getAddress(DB_SS58_PREFIX) });
}

public findAll(): Promise<LegalOfficerAggregateRoot []> {
Expand Down
43 changes: 11 additions & 32 deletions test/integration/migration/migration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { TestDb, Log } from "@logion/rest-api-core";
import { MigrationInterface, QueryRunner } from "typeorm";

const { logger } = Log;
const { connect, disconnect, queryRunner, allMigrations } = TestDb;
import { TestDb } from "@logion/rest-api-core";
const { connect, disconnect, queryRunner, runAllMigrations, revertAllMigrations } = TestDb;

describe('Migration', () => {

Expand All @@ -16,50 +13,32 @@ describe('Migration', () => {
await disconnect();
});

async function testMigrationUp(migration: MigrationInterface, runner:QueryRunner) {
logger.info("Migrating UP %s ", migration.name)
await migration.up(runner)
}

async function runAllMigrations(runner:QueryRunner) {
for (const migration of allMigrations()) {
await testMigrationUp(migration, runner);
}
}

it("executes all up()", async () => {

// Given
const runner = queryRunner()
const tablesBefore = await runner.getTables()
const runner = queryRunner();
const tablesBefore = await runner.getTables();

// When
await runAllMigrations(runner)
await runAllMigrations();

// Then
const tablesAfter = await runner.getTables();
expect(tablesAfter.length - tablesBefore.length).toBe(NUM_OF_TABLES)
expect(tablesAfter.length - tablesBefore.length - 1).toBe(NUM_OF_TABLES);
})

async function testMigrationDown(migration: MigrationInterface, runner:QueryRunner) {
logger.info("Migrating DOWN %s ", migration.name)
await migration.down(runner)
}

it("executes all down()", async () => {

// Given
const runner = queryRunner()
await runAllMigrations(runner)
const tablesBefore = await runner.getTables()
await runAllMigrations();
const runner = queryRunner();
const tablesBefore = await runner.getTables();

// When
for (const migration of allMigrations().reverse()) {
await testMigrationDown(migration, runner);
}
await revertAllMigrations();

// Then
const tablesAfter = await runner.getTables();
expect(tablesBefore.length - tablesAfter.length).toBe(NUM_OF_TABLES)
expect(tablesBefore.length - tablesAfter.length).toBe(NUM_OF_TABLES);
})
})
4 changes: 3 additions & 1 deletion test/integration/model/legalofficer.model.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TestDb } from "@logion/rest-api-core";
import { LegalOfficerAggregateRoot, LegalOfficerRepository } from "../../../src/logion/model/legalofficer.model.js";
import { ValidAccountId } from "@logion/node-api";

const { connect, executeScript, disconnect } = TestDb;

Expand All @@ -18,7 +19,8 @@ describe("LegalOfficerRepository", () => {
});

it("findByAddress", async () => {
let result = await repository.findByAddress("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY");
const account = ValidAccountId.polkadot("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY");
let result = await repository.findByAccount(account);
expect(result?.city).toBe("Etterbeek")
})

Expand Down
7 changes: 4 additions & 3 deletions test/testdata.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ValidAccountId } from "@logion/node-api";
import { LegalOfficerDescription } from "../src/logion/model/legalofficer.model.js";

export const LEGAL_OFFICERS: LegalOfficerDescription[] = [
{
address: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
account: ValidAccountId.polkadot("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"),
userIdentity: {
firstName: "Alice",
lastName: "Alice",
Expand All @@ -20,7 +21,7 @@ export const LEGAL_OFFICERS: LegalOfficerDescription[] = [
additionalDetails: "",
},
{
address: "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
account: ValidAccountId.polkadot("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"),
userIdentity: {
firstName: "Bob",
lastName: "Bob",
Expand All @@ -38,7 +39,7 @@ export const LEGAL_OFFICERS: LegalOfficerDescription[] = [
additionalDetails: "",
},
{
address: "5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y",
account: ValidAccountId.polkadot("5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y"),
userIdentity: {
firstName: "Charlie",
lastName: "Charlie",
Expand Down
32 changes: 17 additions & 15 deletions test/unit/controllers/legalofficer.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import {
LegalOfficerAggregateRoot,
LegalOfficerFactory,
LegalOfficerDescription,
DB_SS58_PREFIX,
} from "../../../src/logion/model/legalofficer.model.js";
import { LEGAL_OFFICERS } from "../../testdata.js";
import { ValidAccountId } from "@logion/node-api";

const AUTHENTICATED_ADDRESS = LEGAL_OFFICERS[0].address;
const AUTHENTICATED_ADDRESS = LEGAL_OFFICERS[0].account;
const { setupApp, mockAuthenticationForUserOrLegalOfficer } = TestApp;

describe("LegalOfficerController", () => {
Expand All @@ -32,11 +34,11 @@ describe("LegalOfficerController", () => {
it("should fetch one legal officer", async () => {
const app = setupApp(LegalOfficerController, mockForFetch)
await request(app)
.get("/api/legal-officer/5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
.get("/api/legal-officer/vQx5kESPn8dWyX4KxMCKqUyCaWUwtui1isX6PVNcZh2Ghjitr")
.expect(200)
.expect('Content-Type', /application\/json/)
.then(response => {
expect(response.body.address).toBe("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
expect(response.body.address).toBe("vQx5kESPn8dWyX4KxMCKqUyCaWUwtui1isX6PVNcZh2Ghjitr")
const userIdentity = response.body.userIdentity;
expect(userIdentity.firstName).toBe("Alice")
expect(userIdentity.lastName).toBe("Alice")
Expand All @@ -61,7 +63,7 @@ describe("LegalOfficerController", () => {
.expect(200)
.expect('Content-Type', /application\/json/)
.then(response => {
expect(response.body.address).toBe(AUTHENTICATED_ADDRESS)
expect(response.body.address).toBe(AUTHENTICATED_ADDRESS.address)
const userIdentity = response.body.userIdentity;
expect(userIdentity.firstName).toBe("Alice")
expect(userIdentity.lastName).toBe("Alice")
Expand All @@ -84,7 +86,7 @@ describe("LegalOfficerController", () => {
.put("/api/legal-officer")
.send(payload)
.expect(401)
.expect('Content-Type', /application\/json/)
.expect('Content-Type', /application\/json/);
})
})

Expand All @@ -100,7 +102,7 @@ function mockForFetch(container: Container) {
];
repository.setup(instance => instance.findAll())
.returns(Promise.resolve(legalOfficers));
repository.setup(instance => instance.findByAddress(It.IsAny<string>()))
repository.setup(instance => instance.findByAccount(It.IsAny<string>()))
.returns(Promise.resolve(legalOfficer0));

const factory = new Mock<LegalOfficerFactory>();
Expand All @@ -109,29 +111,29 @@ function mockForFetch(container: Container) {

function mockForCreateOrUpdate(container: Container) {
const repository = new Mock<LegalOfficerRepository>();
container.bind(LegalOfficerRepository).toConstantValue(repository.object())
container.bind(LegalOfficerRepository).toConstantValue(repository.object());
const legalOfficer0 = mockLegalOfficer(repository, 0);
const legalOfficers = [
legalOfficer0,
mockLegalOfficer(repository, 1),
mockLegalOfficer(repository, 2),
];
repository.setup(instance => instance.findAll())
.returns(Promise.resolve(legalOfficers))
.returns(Promise.resolve(legalOfficers));
repository.setup(instance => instance.save(It.IsAny<LegalOfficerAggregateRoot>()))
.returns(Promise.resolve())
.returns(Promise.resolve());

const factory = new Mock<LegalOfficerFactory>();
container.bind(LegalOfficerFactory).toConstantValue(factory.object())
container.bind(LegalOfficerFactory).toConstantValue(factory.object());
factory.setup(instance => instance.newLegalOfficer(It.IsAny<LegalOfficerDescription>()))
.returns(legalOfficer0)
.returns(legalOfficer0);
}

function mockLegalOfficer(repository: Mock<LegalOfficerRepository>, idx:number):LegalOfficerAggregateRoot {
const legalOfficer = new Mock<LegalOfficerAggregateRoot>();
legalOfficer.setup(instance => instance.getDescription()).returns(LEGAL_OFFICERS[idx])
legalOfficer.setup(instance => instance.address).returns(LEGAL_OFFICERS[idx].address)
repository.setup(instance => instance.findByAddress(It.Is<string>(address => address === LEGAL_OFFICERS[idx].address)))
.returns(Promise.resolve(legalOfficer.object()))
legalOfficer.setup(instance => instance.getDescription()).returns(LEGAL_OFFICERS[idx]);
legalOfficer.setup(instance => instance.address).returns(LEGAL_OFFICERS[idx].account.getAddress(DB_SS58_PREFIX));
repository.setup(instance => instance.findByAccount(It.Is<ValidAccountId>(account => account.equals(LEGAL_OFFICERS[idx].account))))
.returns(Promise.resolve(legalOfficer.object()));
return legalOfficer.object();
}
10 changes: 5 additions & 5 deletions test/unit/model/legalofficer.model.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LegalOfficerFactory, LegalOfficerDescription } from "../../../src/logion/model/legalofficer.model.js";
import { LegalOfficerFactory, LegalOfficerDescription, DB_SS58_PREFIX } from "../../../src/logion/model/legalofficer.model.js";
import { LEGAL_OFFICERS } from "../../testdata.js";

describe("LegalOfficerFactory", () => {
Expand All @@ -14,9 +14,9 @@ describe("LegalOfficerFactory", () => {

function testNewLegalOfficer(legalOfficer: LegalOfficerDescription) {
let aggregate = factory.newLegalOfficer(legalOfficer)
expect(aggregate.address).toBe(legalOfficer.address)
expect(aggregate.getDescription().userIdentity).toEqual(legalOfficer.userIdentity)
expect(aggregate.getDescription().postalAddress).toEqual(legalOfficer.postalAddress)
expect(aggregate.getDescription().additionalDetails).toEqual(legalOfficer.additionalDetails)
expect(aggregate.address).toBe(legalOfficer.account.getAddress(DB_SS58_PREFIX));
expect(aggregate.getDescription().userIdentity).toEqual(legalOfficer.userIdentity);
expect(aggregate.getDescription().postalAddress).toEqual(legalOfficer.postalAddress);
expect(aggregate.getDescription().additionalDetails).toEqual(legalOfficer.additionalDetails);
}
})
Loading

0 comments on commit 1d74207

Please sign in to comment.