Skip to content

Commit

Permalink
Merge pull request #65 from logion-network/feature/update-sync
Browse files Browse the repository at this point in the history
Update Sync for pallet logionLoc new functions.
  • Loading branch information
gdethier authored Dec 24, 2021
2 parents 8b14c04 + 0c28de5 commit e138433
Show file tree
Hide file tree
Showing 20 changed files with 103 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/logion/controllers/locrequest.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class LocRequestController extends ApiController {
identityLoc.locType !== 'Identity' ||
identityLoc.status !== 'CLOSED' ||
identityLoc.getVoidInfo() ||
identityLoc.requesterAddress !== undefined) {
identityLoc.requesterAddress) {
throw new Error("UnexpectedRequester: Identity must be an existing Closed, not Void, Logion Identity LOC.")
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/logion/model/locrequest.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ export class LocRequestFactory {
public newLocRequest(params: NewLocRequestParameters): LocRequestAggregateRoot {
const { description } = params;
this.ensureCorrectRequester(description)
this.ensureUserIdentityPresent(description)
const request = new LocRequestAggregateRoot();
request.id = params.id;
request.status = "REQUESTED";
Expand Down Expand Up @@ -737,4 +738,18 @@ export class LocRequestFactory {
}
}
}

private ensureUserIdentityPresent(description: LocRequestDescription) {
if (description.locType === 'Identity' && !description.requesterAddress) {
const userIdentity = description.userIdentity;
if (!userIdentity
|| !userIdentity.firstName
|| !userIdentity.lastName
|| !userIdentity.email
|| !userIdentity.phoneNumber
) {
throw new Error("Logion Identity LOC request must contain first name, last name, email and phone number.")
}
}
}
}
5 changes: 2 additions & 3 deletions src/logion/services/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import jwt, { Algorithm, Jwt, VerifyErrors } from "jsonwebtoken";
import { injectable } from "inversify";
import { Request } from "express";
import { UnauthorizedException } from "dinoloop/modules/builtin/exceptions/exceptions";
import { ALICE, BOB, CHARLY } from "../model/addresses.model";
import moment, { Moment } from "moment";

const ALGORITHM: Algorithm = "HS384";
Expand All @@ -22,7 +21,7 @@ export class LogionUserCheck implements AuthenticatedUser {
this.nodeOwner = nodeOwner;
}

private nodeOwner: string;
private readonly nodeOwner: string;

requireLegalOfficer(): void {
this.require(user => user.legalOfficer, "Reserved to legal officer");
Expand Down Expand Up @@ -161,7 +160,7 @@ export class AuthenticationService {
}

private isLegalOfficer(address: string): boolean {
return address === ALICE || address === BOB || address === CHARLY;
return address === this.nodeOwner;
}

private _unauthorized(error: VerifyErrors): UnauthorizedException<{ error: string }> {
Expand Down
59 changes: 37 additions & 22 deletions src/logion/services/locsynchronization.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { injectable } from 'inversify';
import { LocRequestAggregateRoot, LocRequestRepository } from '../model/locrequest.model';
import { ExtrinsicDataExtractor } from "./extrinsic.data.extractor";
import { decimalToUuid } from '../lib/uuid';
import { JsonArgs } from './call';
import { JsonExtrinsic, toString } from "./types/responses/Extrinsic";
Expand All @@ -22,27 +21,43 @@ export class LocSynchronizer {
logger.info("updateLocRequests() - Skipping extrinsic with error: %s", toString(extrinsic))
return
}
if (extrinsic.method.method === "createLoc") {
const locId = this.extractLocId(extrinsic.args);
await this.mutateLoc(locId, loc => loc.setLocCreatedDate(timestamp));
} else if (extrinsic.method.method === "addMetadata") {
const locId = this.extractLocId(extrinsic.args);
const name = extrinsic.args['item'].name.toUtf8();
await this.mutateLoc(locId, loc => loc.setMetadataItemAddedOn(name, timestamp));
} else if (extrinsic.method.method === "addFile") {
const locId = this.extractLocId(extrinsic.args);
const hash = extrinsic.args['file'].get('hash').toHex();
await this.mutateLoc(locId, loc => loc.setFileAddedOn(hash, timestamp));
} else if (extrinsic.method.method === "addLink") {
const locId = this.extractLocId(extrinsic.args);
const target = decimalToUuid(extrinsic.args['link'].id.toString());
await this.mutateLoc(locId, loc => loc.setLinkAddedOn(target, timestamp));
} else if (extrinsic.method.method === "close") {
const locId = this.extractLocId(extrinsic.args);
await this.mutateLoc(locId, loc => loc.close(timestamp));
} else if (extrinsic.method.method === "makeVoid" || extrinsic.method.method === "makeVoidAndReplace") {
const locId = this.extractLocId(extrinsic.args);
await this.mutateLoc(locId, loc => loc.voidLoc(timestamp));
const locId = this.extractLocId(extrinsic.args);

switch (extrinsic.method.method) {

case "createLogionIdentityLoc":
case "createLogionTransactionLoc":
case "createPolkadotIdentityLoc":
case "createPolkadotTransactionLoc":
await this.mutateLoc(locId, loc => loc.setLocCreatedDate(timestamp));
break;

case "addMetadata":
const name = extrinsic.args['item'].name.toUtf8();
await this.mutateLoc(locId, loc => loc.setMetadataItemAddedOn(name, timestamp));
break;

case "addFile":
const hash = extrinsic.args['file'].get('hash').toHex();
await this.mutateLoc(locId, loc => loc.setFileAddedOn(hash, timestamp));
break;

case "addLink":
const target = decimalToUuid(extrinsic.args['link'].id.toString());
await this.mutateLoc(locId, loc => loc.setLinkAddedOn(target, timestamp));
break;

case "close":
await this.mutateLoc(locId, loc => loc.close(timestamp));
break;

case "makeVoid":
case "makeVoidAndReplace":
await this.mutateLoc(locId, loc => loc.voidLoc(timestamp));
break;

default:
logger.warn("Unexpected method in pallet logionLoc: %s", extrinsic.method.method)
}
}
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/helpers/testapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { JsonResponse } from '../../src/logion/middlewares/json.response';
import { Mock } from "moq.ts";
import { AuthenticationService, LogionUserCheck } from "../../src/logion/services/authentication.service";
import { UnauthorizedException } from "dinoloop/modules/builtin/exceptions/exceptions";
import { ALICE } from "../../src/logion/model/addresses.model";
import { ALICE } from "./addresses";

export function setupApp<T>(
controller: Function & { prototype: T; },
Expand Down
2 changes: 1 addition & 1 deletion test/integration/model/locrequest.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
LocMetadataItem,
LocLink, LocType
} from "../../../src/logion/model/locrequest.model";
import { ALICE, BOB } from "../../../src/logion/model/addresses.model";
import { ALICE, BOB } from "../../helpers/addresses";
import { v4 as uuid } from "uuid";

describe('LocRequestRepository - read accesses', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/model/protectionrequest.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ProtectionRequestKind,
ProtectionRequestStatus,
} from "../../../src/logion/model/protectionrequest.model";
import { ALICE } from "../../../src/logion/model/addresses.model";
import { ALICE } from "../../helpers/addresses";

describe('ProtectionRequestRepositoryTest', () => {

Expand Down
3 changes: 1 addition & 2 deletions test/unit/controllers/authentication.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { setupApp } from "../../helpers/testapp";
import { AuthenticationController } from "../../../src/logion/controllers/authentication.controller";
import request from "supertest";
import { ALICE, BOB } from "../../../src/logion/model/addresses.model";
import { ALICE, BOB } from "../../helpers/addresses";
import { components } from "../../../src/logion/controllers/components";
import { Container } from "inversify";
import { Mock, It } from "moq.ts";
import { AuthenticationService } from "../../../src/logion/services/authentication.service";
import { Log } from "../../../src/logion/util/Log";
import {
SessionRepository,
SessionFactory,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/controllers/locrequest.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFile, writeFile } from 'fs/promises';
import { LocRequestController } from "../../../src/logion/controllers/locrequest.controller";
import { Container } from "inversify";
import request, { Response } from "supertest";
import { ALICE, BOB } from "../../../src/logion/model/addresses.model";
import { ALICE, BOB } from "../../helpers/addresses";
import { Mock, It } from "moq.ts";
import {
LocRequestRepository,
Expand Down
3 changes: 1 addition & 2 deletions test/unit/controllers/protectionrequest.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import {
NewProtectionRequestParameters,
LegalOfficerDecision,
} from '../../../src/logion/model/protectionrequest.model';
import { ALICE, BOB } from '../../../src/logion/model/addresses.model';
import { ALICE } from '../../helpers/addresses';
import { ProtectionRequestController } from '../../../src/logion/controllers/protectionrequest.controller';
import { AuthenticationService } from '../../../src/logion/services/authentication.service';

describe('createProtectionRequest', () => {

Expand Down
2 changes: 1 addition & 1 deletion test/unit/controllers/transaction.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TransactionAggregateRoot,
TransactionDescription
} from "../../../src/logion/model/transaction.model";
import { ALICE } from "../../../src/logion/model/addresses.model";
import { ALICE } from "../../helpers/addresses";

describe('TransactionController', () => {

Expand Down
2 changes: 1 addition & 1 deletion test/unit/jsonwentoken.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jwt, { JwtPayload } from 'jsonwebtoken';
import { ALICE } from "../../src/logion/model/addresses.model";
import { ALICE } from "../helpers/addresses";

const SECRET = "secret-key-that-no-one-could-possibly-know";
const TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MjM2NzQwOTksImV4cCI6MTgyMzY3NDA5OSwibGVnYWxPZmZpY2VyIjp0cnVlLCJpc3MiOiJ3d3cuZXhhbXBsZS5vcmciLCJzdWIiOiI1R3J3dmFFRjV6WGIyNkZ6OXJjUXBEV1M1N0N0RVJIcE5laFhDUGNOb0hHS3V0UVkifQ.yoaRk0oSixyYIztFDS5QCRop-0xAP_xw4UY30uTwVtM";
Expand Down
22 changes: 18 additions & 4 deletions test/unit/model/locrequest.model.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { v4 as uuid } from "uuid";
import { ALICE } from "../../../src/logion/model/addresses.model";
import { ALICE } from "../../helpers/addresses";
import moment, { Moment } from "moment";
import {
LocRequestDescription,
Expand All @@ -12,6 +12,7 @@ import {
VoidInfo,
LocType
} from "../../../src/logion/model/locrequest.model";
import { UserIdentity } from "../../../src/logion/model/useridentity";

describe("LocRequestFactory", () => {

Expand Down Expand Up @@ -85,19 +86,32 @@ describe("LocRequestFactory", () => {

it("creates an open Identity LOC with no requester", () => {
givenRequestId(uuid());
const description = createDescription('Identity');
const userIdentity = {
firstName: "Scott",
lastName: "Tiger",
email: "[email protected]",
phoneNumber: "+789"
};
const description = createDescription('Identity', undefined, undefined, userIdentity);
givenLocDescription(description);
whenCreatingOpenLoc();
});

function createDescription(locType: LocType, requesterAddress?: string, requesterIdentityLoc?: string): LocRequestDescription {
it("fails to create an open Identity LOC with no requester when identity is missing", () => {
givenRequestId(uuid());
const description = createDescription('Identity');
givenLocDescription(description);
expect(() => whenCreatingOpenLoc()).toThrowError();
});

function createDescription(locType: LocType, requesterAddress?: string, requesterIdentityLoc?: string, userIdentity?: UserIdentity): LocRequestDescription {
return {
requesterAddress,
requesterIdentityLoc,
ownerAddress: ALICE,
description: "Mrs ALice, I want to sell my last art work",
createdOn: moment().toISOString(),
userIdentity: undefined,
userIdentity,
locType
};
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/model/protectionrequest.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ProtectionRequestFactory,
ProtectionRequestAggregateRoot,
} from '../../../src/logion/model/protectionrequest.model';
import { BOB } from '../../../src/logion/model/addresses.model';
import { BOB } from '../../helpers/addresses';

describe('ProtectionRequestFactoryTest', () => {

Expand Down
2 changes: 1 addition & 1 deletion test/unit/model/session.model.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SessionFactory, SessionAggregateRoot, NewSessionParameters } from "../../../src/logion/model/session.model";
import { v4 as uuid } from 'uuid';
import { ALICE } from "../../../src/logion/model/addresses.model";
import { ALICE } from "../../helpers/addresses";
import moment from "moment";

describe("SessionFactory", () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/services/authentication.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Mock } from "moq.ts";
import { Request } from "express";
import moment from "moment";
import { UnauthorizedException } from "dinoloop/modules/builtin/exceptions/exceptions";
import { ALICE } from "../../../src/logion/model/addresses.model";
import { ALICE } from "../../helpers/addresses";

const USER_TOKEN = "eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MzEyMTc2MTEsImxlZ2FsT2ZmaWNlciI6ZmFsc2UsImV4cCI6NDc4NDgxNzYxMSwiaXNzIjoiZXhhbXBsZS5vcmciLCJzdWIiOiI1SDRNdkFzb2JmWjZiQkNEeWo1ZHNyV1lMckE4SHJSemFxYTlwNjFVWHR4TWhTQ1kifQ.03P4Ehzp6cweQPct0tf-dBNmkXhFKYfcUYA1xbmPsdpRV9Dn2as1aZdp6neP2iOI"
const USER_TOKEN_WRONG_SIGNATURE = "eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MzEyMTc2MTEsImxlZ2FsT2ZmaWNlciI6ZmFsc2UsImV4cCI6NDc4NDgxNzYxMSwiaXNzIjoiZXhhbXBsZS5vcmciLCJzdWIiOiI1SDRNdkFzb2JmWjZiQkNEeWo1ZHNyV1lMckE4SHJSemFxYTlwNjFVWHR4TWhTQ1kifQ.GTLJB_uMjsdcuWzM3CWL92n1UNI0WYXFUDW7QQ1Vi6k3TQIEvG_WwMuuZ2d9cexY"
Expand Down
23 changes: 17 additions & 6 deletions test/unit/services/locsynchronization.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ describe("LocSynchronizer", () => {
});

it("sets LOC created date", async () => {
givenLocExtrinsic("createLoc", { loc_id: locId });
givenLocRequest();
givenLocRequestExpectsLocCreationDate();
await whenConsumingBlock();
thenLocCreateDateSet();
thenLocIsSaved();

const palletMethods = [
"createLogionIdentityLoc",
"createLogionTransactionLoc",
"createPolkadotIdentityLoc",
"createPolkadotTransactionLoc"
]

for (const palletMethod of palletMethods) {

givenLocExtrinsic(palletMethod, { loc_id: locId });
givenLocRequest();
givenLocRequestExpectsLocCreationDate();
await whenConsumingBlock();
thenLocCreateDateSet();
thenLocIsSaved();
}
});

it("sets metadata item added on", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../../../src/logion/model/protectionrequest.model';
import { JsonExtrinsic } from '../../../src/logion/services/types/responses/Extrinsic';
import { ProtectionSynchronizer } from '../../../src/logion/services/protectionsynchronization.service';
import { ALICE, BOB } from '../../../src/logion/model/addresses.model';
import { ALICE, BOB } from '../../helpers/addresses';

process.env.OWNER = ALICE;

Expand Down
2 changes: 1 addition & 1 deletion test/unit/services/signature.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { It, Mock } from 'moq.ts';
import { BOB } from '../../../src/logion/model/addresses.model';
import { BOB } from '../../helpers/addresses';
import { SignatureService, VerifyFunction, VerifyFunctionParams } from '../../../src/logion/services/signature.service';

describe('SignatureService', () => {
Expand Down

0 comments on commit e138433

Please sign in to comment.