Skip to content

Commit

Permalink
fixed e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aramikm committed Sep 20, 2024
1 parent 8774531 commit 811a738
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 29 deletions.
2 changes: 2 additions & 0 deletions apps/account-api/test/e2e-setup.mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { KeyringPair } from '@polkadot/keyring/types';
import { AccountId } from '@polkadot/types/interfaces';
import { cryptoWaitReady, decodeAddress } from '@polkadot/util-crypto';
import log from 'loglevel';
import { u8aToHex } from '@polkadot/util';

export const FREQUENCY_URL = process.env.FREQUENCY_URL || 'ws://0.0.0.0:9944';
export const BASE_SEED_PHRASE = process.env.SEED_PHRASE || '//Alice';
Expand Down Expand Up @@ -74,6 +75,7 @@ export async function generateSignedAddKeyPayload(user: ChainUser, newKeys: Keyr
const addKeyData = ExtrinsicHelper.api.registry.createType('PalletMsaAddKeyData', payload);
const ownerProof = signPayloadSr25519(user.keypair, addKeyData);
const newKeyProof = signPayloadSr25519(newKeys, addKeyData);
payload.newPublicKey = u8aToHex(payload.newPublicKey);

return { payload, ownerProof, newKeyProof };
}
Expand Down
4 changes: 2 additions & 2 deletions apps/account-api/test/handles.controller.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('Handles Controller', () => {

await request(HTTP_SERVER)
.post('/v1/handles')
.send({ accountId, payload, proof })
.send({ accountId, payload, proof: proof.Sr25519 })
.expect(200)
.expect((req) => req.text === 'Handle created successfully');
});
Expand All @@ -121,7 +121,7 @@ describe('Handles Controller', () => {
const accountId = user.keypair.address;
await request(HTTP_SERVER)
.post('/v1/handles/change')
.send({ accountId, payload, proof })
.send({ accountId, payload, proof: proof.Sr25519 })
.expect(200)
.expect((res) => res.text === 'Handle created successfully');
});
Expand Down
6 changes: 1 addition & 5 deletions apps/account-api/test/keys.controller.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ describe('Keys Controller', () => {
},
};

await request(HTTP_SERVER)
.post('/v1/keys/add')
.send(keysRequest)
.expect(200)
.expect((req) => req.text === 'Successfully added key.');
await request(HTTP_SERVER).post('/v1/keys/add').send(keysRequest).expect(200);
});
});

Expand Down
8 changes: 3 additions & 5 deletions libs/types/src/dtos/account/accounts.request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HexString } from '@polkadot/util/types';
import { RetireMsaPayloadResponseDto } from './accounts.response.dto';
import { TransactionType } from '#types/enums/account-enums';
import { IsAccountIdOrAddress } from '#utils/decorators/is-account-id-address.decorator';
import { IsHexValue } from '#utils/decorators';
import { IsSignature } from '#utils/decorators/is-signature.decorator';

export class RetireMsaRequestDto extends RetireMsaPayloadResponseDto {
@ApiProperty({
Expand All @@ -13,10 +13,8 @@ export class RetireMsaRequestDto extends RetireMsaPayloadResponseDto {
example:
'0x065d733ca151c9e65b78f2ba77348224d31647e6913c44ad2765c6e8ba06f834dc21d8182447d01c30f84a41d90a8f2e58001d825c6f0d61b0afe89f984eec85',
})
@IsHexValue({
minLength: 128,
maxLength: 128,
message: 'signature should be a 64 bytes value in hex format!',
@IsSignature({
message: 'signature should be a 64 (or 65 if it is MultiSignature type) bytes value in hex format!',
})
signature: HexString;

Expand Down
5 changes: 4 additions & 1 deletion libs/types/src/dtos/account/graphs.request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IsSchemaId } from '#utils/decorators/is-schema-id.decorator';
import { Type } from 'class-transformer';
import { IsAccountIdOrAddress } from '#utils/decorators/is-account-id-address.decorator';
import { IsMsaId } from '#utils/decorators/is-msa-id.decorator';
import { IsSignature } from '#utils/decorators/is-signature.decorator';

// eslint-disable-next-line no-shadow
export enum ItemActionType {
Expand Down Expand Up @@ -103,7 +104,9 @@ export class AddNewPublicKeyAgreementRequestDto {
example:
'0x065d733ca151c9e65b78f2ba77348224d31647e6913c44ad2765c6e8ba06f834dc21d8182447d01c30f84a41d90a8f2e58001d825c6f0d61b0afe89f984eec85',
})
@IsHexValue({ minLength: 128, maxLength: 128, message: 'Proof should be a 64 bytes value in hex format!' })
@IsSignature({
message: 'Proof should be a 64 (or 65 if it is MultiSignature type) bytes value in hex format!',
})
proof: HexString;
}

Expand Down
5 changes: 4 additions & 1 deletion libs/types/src/dtos/account/handles.request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IsIntValue } from '#utils/decorators/is-int-value.decorator';
import { IsAccountIdOrAddress } from '#utils/decorators/is-account-id-address.decorator';
import { Type } from 'class-transformer';
import { IsHexValue } from '#utils/decorators';
import { IsSignature } from '#utils/decorators/is-signature.decorator';

class HandlePayloadDto {
@ApiProperty({
Expand Down Expand Up @@ -44,7 +45,9 @@ export class HandleRequestDto {
example:
'0x065d733ca151c9e65b78f2ba77348224d31647e6913c44ad2765c6e8ba06f834dc21d8182447d01c30f84a41d90a8f2e58001d825c6f0d61b0afe89f984eec85',
})
@IsHexValue({ minLength: 128, maxLength: 128, message: 'Proof should be a 64 bytes value in hex format!' })
@IsSignature({
message: 'Proof should be a 64 (or 65 if it is MultiSignature type) bytes value in hex format!',
})
proof: HexString;
}

Expand Down
13 changes: 5 additions & 8 deletions libs/types/src/dtos/account/keys.request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IsHexValue } from '#utils/decorators';
import { IsIntValue } from '#utils/decorators/is-int-value.decorator';
import { IsAccountIdOrAddress } from '#utils/decorators/is-account-id-address.decorator';
import { Type } from 'class-transformer';
import { IsSignature } from '#utils/decorators/is-signature.decorator';

class KeysRequestPayloadDto {
@ApiProperty({ description: 'Msa Id representing the target of this request', type: String, example: '3' })
Expand Down Expand Up @@ -42,10 +43,8 @@ export class KeysRequestDto {
example:
'0x065d733ca151c9e65b78f2ba77348224d31647e6913c44ad2765c6e8ba06f834dc21d8182447d01c30f84a41d90a8f2e58001d825c6f0d61b0afe89f984eec85',
})
@IsHexValue({
minLength: 128,
maxLength: 128,
message: 'msaOwnerSignature should be a 64 bytes value in hex format!',
@IsSignature({
message: 'msaOwnerSignature should be a 64 (or 65 if it is MultiSignature type) bytes value in hex format!',
})
msaOwnerSignature: HexString;

Expand All @@ -55,10 +54,8 @@ export class KeysRequestDto {
example:
'0x065d733ca151c9e65b78f2ba77348224d31647e6913c44ad2765c6e8ba06f834dc21d8182447d01c30f84a41d90a8f2e58001d825c6f0d61b0afe89f984eec85',
})
@IsHexValue({
minLength: 128,
maxLength: 128,
message: 'msaOwnerSignature should be a 64 bytes value in hex format!',
@IsSignature({
message: 'newKeyOwnerSignature should be a 64 (or 65 if it is MultiSignature type) bytes value in hex format!',
})
newKeyOwnerSignature: HexString;

Expand Down
7 changes: 3 additions & 4 deletions libs/types/src/dtos/account/revokeDelegation.request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TransactionType } from '#types/enums/account-enums';
import { IsAccountIdOrAddress } from '#utils/decorators/is-account-id-address.decorator';
import { IsMsaId } from '#utils/decorators/is-msa-id.decorator';
import { IsHexValue } from '#utils/decorators';
import { IsSignature } from '#utils/decorators/is-signature.decorator';

export class RevokeDelegationPayloadResponseDto {
@ApiProperty({
Expand Down Expand Up @@ -43,10 +44,8 @@ export class RevokeDelegationPayloadRequestDto extends RevokeDelegationPayloadRe
example:
'0x065d733ca151c9e65b78f2ba77348224d31647e6913c44ad2765c6e8ba06f834dc21d8182447d01c30f84a41d90a8f2e58001d825c6f0d61b0afe89f984eec85',
})
@IsHexValue({
minLength: 128,
maxLength: 128,
message: 'signature should be a 64 bytes value in hex format!',
@IsSignature({
message: 'signature should be a 64 (or 65 if it is MultiSignature type) bytes value in hex format!',
})
signature: HexString;
}
Expand Down
8 changes: 5 additions & 3 deletions libs/types/src/dtos/account/wallet.login.request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {
type WalletProxyResponse,
} from '@projectlibertylabs/siwf';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ArrayNotEmpty, IsArray, IsNotEmpty, IsOptional, IsString, MinLength, ValidateNested } from 'class-validator';
import { IsArray, IsNotEmpty, IsOptional, IsString, MinLength, ValidateNested } from 'class-validator';
import { HexString } from '@polkadot/util/types';
import { TransactionType } from '#types/enums/account-enums';
import { IsHexValue } from '#utils/decorators';
import { Type } from 'class-transformer';
import { IsSignature } from '#utils/decorators/is-signature.decorator';

export class ErrorResponseDto implements ErrorResponse {
@ApiProperty({
Expand All @@ -35,7 +36,9 @@ export class SiwsPayloadDto implements SiwsPayload {
example:
'0x64f8dd8846ba72cbb1954761ec4b2e44b886abb4b4ef7455b869355f17b4ce4a601ad26eabc57a682244a97bc9a2001b59469ae76fea105b724e988967d4928d',
})
@IsHexValue({ minLength: 128, maxLength: 128, message: 'signature should be a 64 bytes value in hex format!' })
@IsSignature({
message: 'signature should be a 64 (or 65 if it is MultiSignature type) bytes value in hex format!',
})
signature: string | HexString;
}
export class SignInResponseDto implements SignInResponse {
Expand Down Expand Up @@ -77,7 +80,6 @@ export class SignUpResponseDto implements SignUpResponse {
@ApiPropertyOptional({ type: [EncodedExtrinsicDto] })
@ValidateNested({ each: true })
@IsArray()
@ArrayNotEmpty()
@Type(() => EncodedExtrinsicDto)
extrinsics?: EncodedExtrinsicDto[] | undefined;

Expand Down
25 changes: 25 additions & 0 deletions libs/utils/src/decorators/is-signature.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';

export function IsSignature(validationOptions?: ValidationOptions) {
// eslint-disable-next-line func-names
return function (object: object, propertyName: string) {
registerDecorator({
name: 'IsSignature',
target: object.constructor,
propertyName,
options: validationOptions,
validator: {
validate(value: unknown, _args: ValidationArguments) {
const re = /^0x[A-F0-9]{128,130}$/i;

if (typeof value !== 'string') {
return false;
}

// ensure the length is always even
return re.test(value) && value.length % 2 === 0;
},
},
});
};
}

0 comments on commit 811a738

Please sign in to comment.