Skip to content

Commit

Permalink
Add type param to SEP-12 GET /customer requests (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeUrban authored Aug 5, 2021
1 parent 1405482 commit 1de3976
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 34 deletions.
2 changes: 1 addition & 1 deletion @stellar/anchor-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stellar/anchor-tests",
"version": "0.1.6",
"version": "0.1.7",
"description": "stellar-anchor-tests is a library and command line interface for testing Stellar anchors.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
61 changes: 55 additions & 6 deletions @stellar/anchor-tests/src/tests/sep12/getCustomer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request } from "node-fetch";
import { validate } from "jsonschema";
import { Keypair } from "stellar-sdk";
import { URLSearchParams } from "url";

import { Test, Config, Result, NetworkCall } from "../../types";
import { hasNetworkPassphrase } from "../sep1/tests";
Expand All @@ -15,6 +16,17 @@ import { canCreateCustomer } from "./putCustomer";
const getCustomerGroup = "GET /customer";
const tests: Test[] = [];

const hasCreateCustomer = (config: Config): Boolean =>
Boolean(
config?.sepConfig?.["12"]?.customers?.[
config?.sepConfig?.["12"]?.createCustomer
],
);
const getCreateCustomerType = (config: Config): string | undefined =>
config?.sepConfig?.["12"]?.customers?.[
config?.sepConfig?.["12"]?.createCustomer
].type;

const requiresJwtToken: Test = {
assertion: "requires a SEP-10 JWT",
sep: 12,
Expand Down Expand Up @@ -92,7 +104,13 @@ const newCustomerValidSchema: Test = {
},
...genericFailures,
},
async run(_config: Config): Promise<Result> {
async run(config: Config): Promise<Result> {
if (!hasCreateCustomer(config)) {
throw new Error(
"SEP-12 configuration data is missing, expected a key within the " +
"'customers' object matching the value assigned to 'createCustomer'",
);
}
const result: Result = { networkCalls: [] };
const clientKeypair = Keypair.random();
const token = await postChallenge(
Expand All @@ -102,17 +120,24 @@ const newCustomerValidSchema: Test = {
result,
);
if (!token) return result;
const customerType = getCreateCustomerType(config);
const requestParamsObj: Record<string, string> = {
account: clientKeypair.publicKey(),
};
if (customerType) requestParamsObj["type"] = customerType;
const searchParams = new URLSearchParams(requestParamsObj);
const getCustomerCall: NetworkCall = {
request: new Request(
this.context.expects.kycServerUrl +
`/customer?account=${clientKeypair.publicKey()}`,
`/customer?${searchParams.toString()}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
},
),
};
result.networkCalls.push(getCustomerCall);
const getCustomerBody = await makeRequest(
getCustomerCall,
200,
Expand Down Expand Up @@ -187,12 +212,24 @@ export const canFetchExistingCustomerById: Test = {
},
...genericFailures,
},
async run(_config: Config): Promise<Result> {
async run(config: Config): Promise<Result> {
if (!hasCreateCustomer(config)) {
throw new Error(
"SEP-12 configuration data is missing, expected a key within the " +
"'customers' object matching the value assigned to 'createCustomer'",
);
}
const result: Result = { networkCalls: [] };
const customerType = getCreateCustomerType(config);
const requestParamsObj: Record<string, string> = {
id: this.context.expects.customerId,
};
if (customerType) requestParamsObj["type"] = customerType;
const searchParams = new URLSearchParams(requestParamsObj);
const getCustomerCall: NetworkCall = {
request: new Request(
this.context.expects.kycServerUrl +
`/customer?id=${this.context.expects.customerId}`,
`/customer?${searchParams.toString()}`,
{
headers: {
Authorization: `Bearer ${this.context.expects.token}`,
Expand Down Expand Up @@ -269,12 +306,24 @@ const canFetchExistingCustomerByAccount: Test = {
},
...genericFailures,
},
async run(_config: Config): Promise<Result> {
async run(config: Config): Promise<Result> {
if (!hasCreateCustomer(config)) {
throw new Error(
"SEP-12 configuration data is missing, expected a key within the " +
"'customers' object matching the value assigned to 'createCustomer'",
);
}
const result: Result = { networkCalls: [] };
const customerType = getCreateCustomerType(config);
const requestParamsObj: Record<string, string> = {
account: this.context.expects.clientKeypair.publicKey(),
};
if (customerType) requestParamsObj["type"] = customerType;
const searchParams = new URLSearchParams(requestParamsObj);
const getCustomerCall: NetworkCall = {
request: new Request(
this.context.expects.kycServerUrl +
`/customer?account=${this.context.expects.clientKeypair.publicKey()}`,
`/customer?${searchParams.toString()}`,
{
headers: {
Authorization: `Bearer ${this.context.expects.token}`,
Expand Down
40 changes: 14 additions & 26 deletions @stellar/anchor-tests/src/tests/sep12/putCustomer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from "node-fetch";
import { Request } from "node-fetch";
import { Request, BodyInit } from "node-fetch";
import { Keypair, Memo } from "stellar-sdk";
import { randomBytes } from "crypto";

Expand All @@ -14,6 +14,11 @@ import { postChallenge } from "../../helpers/sep10";
const putCustomerGroup = "PUT /customer";
const tests: Test[] = [];

const getCreateCustomer = (config: Config): object =>
config?.sepConfig?.["12"]?.customers?.[
config?.sepConfig?.["12"]?.createCustomer
];

const requiresJwt: Test = {
assertion: "requires a SEP-10 JWT",
sep: 12,
Expand All @@ -28,24 +33,17 @@ const requiresJwt: Test = {
failureModes: genericFailures,
async run(config: Config): Promise<Result> {
const result: Result = { networkCalls: [] };
if (
!config.sepConfig ||
!config.sepConfig["12"] ||
!config.sepConfig["12"].customers ||
!config.sepConfig["12"].createCustomer ||
!config.sepConfig["12"].customers[config.sepConfig["12"].createCustomer]
) {
const customerValues = getCreateCustomer(config);
if (!customerValues) {
throw new Error(
"SEP-12 configuration data is missing, expected a key within the " +
"'customers' object matching the value assigned to 'createCustomer'",
);
}
const customerValues =
config.sepConfig["12"].customers[config.sepConfig["12"].createCustomer];
const putCustomerCall: NetworkCall = {
request: new Request(this.context.expects.kycServerUrl + "/customer", {
method: "PUT",
body: customerValues,
body: customerValues as BodyInit,
}),
};
result.networkCalls.push(putCustomerCall);
Expand Down Expand Up @@ -109,20 +107,13 @@ export const canCreateCustomer: Test = {
},
async run(config: Config): Promise<Result> {
const result: Result = { networkCalls: [] };
if (
!config.sepConfig ||
!config.sepConfig["12"] ||
!config.sepConfig["12"].customers ||
!config.sepConfig["12"].createCustomer ||
!config.sepConfig["12"].customers[config.sepConfig["12"].createCustomer]
) {
const customerValues = getCreateCustomer(config);
if (!customerValues) {
throw new Error(
"SEP-12 configuration data is missing, expected a key within the " +
"'customers' object matching the value assigned to 'createCustomer'",
);
}
const customerValues =
config.sepConfig["12"].customers[config.sepConfig["12"].createCustomer];
const putCustomerRequest = makeSep12Request({
url: this.context.expects.kycServerUrl + "/customer",
data: {
Expand All @@ -147,7 +138,7 @@ export const canCreateCustomer: Test = {
}
if (putCustomerCall.response.status !== 202) {
result.failure = makeFailure(this.failureModes.UNEXPECTED_STATUS_CODE, {
customer: config.sepConfig["12"].createCustomer,
customer: config?.sepConfig?.["12"]?.createCustomer,
});
result.expected = 202;
result.actual = putCustomerCall.response.status;
Expand Down Expand Up @@ -267,11 +258,8 @@ export const differentMemosSameAccount: Test = {
);
} else {
if (
!config.sepConfig ||
!config.sepConfig["12"] ||
!config.sepConfig["12"].sameAccountDifferentMemos ||
!config.sepConfig["12"].customers ||
!config.sepConfig["12"].customers[
!config?.sepConfig?.["12"]?.sameAccountDifferentMemos ||
!config.sepConfig["12"]?.customers[
config.sepConfig["12"].sameAccountDifferentMemos[0]
] ||
!config.sepConfig["12"].customers[
Expand Down
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Changelog

This changelog documents all releases and included changes to the @stellar/anchor-tests library.

A breaking change will get clearly marked in this log.

## [v0.1.7](https://github.com/stellar/stellar-anchor-tests/compare/v0.1.6...master)

### Add
- Use `type` parameter in SEP-12 `GET /customer` requests

## [v0.1.6](https://github.com/stellar/stellar-anchor-tests/compare/v0.1.5...v0.1.6)

## [v0.1.5](https://github.com/stellar/stellar-anchor-tests/compare/v0.1.4...v0.1.5)

## [v0.1.4](https://github.com/stellar/stellar-anchor-tests/compare/v0.1.3...v0.1.4)

## [v0.1.3](https://github.com/stellar/stellar-anchor-tests/compare/v0.1.2...v0.1.3)

## [v0.1.2](https://github.com/stellar/stellar-anchor-tests/compare/v0.1.1...v0.1.2)

## [v0.1.1](https://github.com/stellar/stellar-anchor-tests/compare/v0.1.0...v0.1.1)

## v0.1.0
6 changes: 5 additions & 1 deletion ui/src/components/ConfigModalContent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ export const ConfigModalContent: React.FC = () => (
<TextLink href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0009.md">
SEP-9
</TextLink>{" "}
attributes the anchor requires. Currently four customers are required.
attributes the anchor requires, as well as the
<TextLink href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0012.md#type-specification">
type
</TextLink>{" "}
of the customer. Currently four customers are required.
</li>
<li>
<strong>createCustomer</strong>: a string matching one of the keys
Expand Down

0 comments on commit 1de3976

Please sign in to comment.