-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feed Authz with approved transfers (#57)
Add test coverage for authorization request evaluation Incorporate domain fixtures supported by the zod-fixture library, ensuring type safety in mocks and auto-generated data based on schemas.
- Loading branch information
1 parent
aad087d
commit ed36881
Showing
14 changed files
with
323 additions
and
67 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
apps/orchestration/src/__test__/fixture/authorization-request.fixture.ts
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,66 @@ | ||
import { addressGenerator, chainIdGenerator, hexGenerator } from '@app/orchestration/__test__/fixture/shared.fixture' | ||
import { Approval, AuthorizationRequest, SignTransaction } from '@app/orchestration/policy-engine/core/type/domain.type' | ||
import { readRequestSchema } from '@app/orchestration/policy-engine/persistence/schema/request.schema' | ||
import { readSignTransactionSchema } from '@app/orchestration/policy-engine/persistence/schema/sign-transaction.schema' | ||
import { signatureSchema } from '@app/orchestration/policy-engine/persistence/schema/signature.schema' | ||
import { Decision, Signature } from '@narval/authz-shared' | ||
import { AuthorizationRequestStatus } from '@prisma/client/orchestration' | ||
import { z } from 'zod' | ||
import { Fixture } from 'zod-fixture' | ||
|
||
const approvalSchema = signatureSchema.extend({ | ||
id: z.string().uuid(), | ||
createdAt: z.date() | ||
}) | ||
|
||
const evaluationSchema = z.object({ | ||
id: z.string().uuid(), | ||
decision: z.nativeEnum(Decision), | ||
signature: z.string().nullable(), | ||
createdAt: z.date() | ||
}) | ||
|
||
const authorizationRequestSchema = z.object({ | ||
id: z.string().uuid(), | ||
orgId: z.string().uuid(), | ||
status: z.nativeEnum(AuthorizationRequestStatus), | ||
request: readRequestSchema, | ||
authentication: signatureSchema, | ||
approvals: z.array(approvalSchema), | ||
evaluations: z.array(evaluationSchema), | ||
idempotencyKey: z.string().nullish(), | ||
createdAt: z.date(), | ||
updatedAt: z.date() | ||
}) | ||
|
||
export const generateSignTransactionRequest = (partial?: Partial<SignTransaction>): SignTransaction => { | ||
const fixture = new Fixture() | ||
.extend([hexGenerator, addressGenerator, chainIdGenerator]) | ||
.fromSchema(readSignTransactionSchema) | ||
|
||
return { | ||
...fixture, | ||
...partial | ||
} | ||
} | ||
|
||
export const generateAuthorizationRequest = (partial?: Partial<AuthorizationRequest>): AuthorizationRequest => { | ||
const fixture = new Fixture() | ||
.extend([hexGenerator, addressGenerator, chainIdGenerator]) | ||
.fromSchema(authorizationRequestSchema) | ||
|
||
return { | ||
...fixture, | ||
...partial | ||
} | ||
} | ||
|
||
export const generateApproval = (partial?: Partial<Approval>): Approval => ({ | ||
...new Fixture().fromSchema(approvalSchema), | ||
...partial | ||
}) | ||
|
||
export const generateSignature = (partial?: Partial<Signature>): Signature => ({ | ||
...new Fixture().fromSchema(approvalSchema), | ||
...partial | ||
}) |
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,24 @@ | ||
import { | ||
addressSchema, | ||
hexSchema | ||
} from '@app/orchestration/policy-engine/persistence/schema/transaction-request.schema' | ||
import { faker } from '@faker-js/faker' | ||
import { sample } from 'lodash/fp' | ||
import { z } from 'zod' | ||
import { Generator } from 'zod-fixture' | ||
|
||
export const hexGenerator = Generator({ | ||
schema: hexSchema, | ||
output: () => faker.string.hexadecimal().toLowerCase() | ||
}) | ||
|
||
export const addressGenerator = Generator({ | ||
schema: addressSchema, | ||
output: () => faker.finance.ethereumAddress().toLowerCase() | ||
}) | ||
|
||
export const chainIdGenerator = Generator({ | ||
schema: z.number().min(1), | ||
filter: ({ transform, def }) => transform.utils.checks(def.checks).has('chainId'), | ||
output: () => sample([1, 137]) | ||
}) |
27 changes: 27 additions & 0 deletions
27
apps/orchestration/src/__test__/fixture/transfer-feed.fixture.ts
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,27 @@ | ||
import { addressGenerator, chainIdGenerator } from '@app/orchestration/__test__/fixture/shared.fixture' | ||
import { addressSchema } from '@app/orchestration/policy-engine/persistence/schema/transaction-request.schema' | ||
import { Transfer } from '@app/orchestration/shared/core/type/transfer-feed.type' | ||
import { z } from 'zod' | ||
import { Fixture } from 'zod-fixture' | ||
|
||
const transferFeedSchema = z.object({ | ||
id: z.string().uuid(), | ||
orgId: z.string().uuid(), | ||
amount: z.bigint(), | ||
from: z.string(), | ||
to: z.string(), | ||
chainId: z.number(), | ||
token: z.string(), | ||
rates: z.record(z.string(), z.number()), | ||
initiatedBy: addressSchema, | ||
createdAt: z.date() | ||
}) | ||
|
||
export const generateTransferFeed = (partial?: Partial<Transfer>): Transfer => { | ||
const fixture = new Fixture().extend([addressGenerator, chainIdGenerator]).fromSchema(transferFeedSchema) | ||
|
||
return { | ||
...fixture, | ||
...partial | ||
} | ||
} |
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
Oops, something went wrong.