@stancer/stancer 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +14 -0
- package/.gitlab-ci.yml +159 -0
- package/.prettierignore +3 -0
- package/CHANGELOG.md +10 -0
- package/LICENSE +19 -0
- package/README.md +166 -2
- package/eslint.config.js +14 -0
- package/package.json +52 -3
- package/pnpm-workspace.yaml +19 -0
- package/prettier.config.js +3 -0
- package/src/address/api-endpoint.ts +30 -0
- package/src/address/model.ts +79 -0
- package/src/address/repository.ts +18 -0
- package/src/address/validator.ts +34 -0
- package/src/api-endpoint/abstract-api-endpoint.ts +310 -0
- package/src/api-endpoint/configuration.ts +125 -0
- package/src/api-endpoint/http/fetch-http-client.ts +29 -0
- package/src/api-endpoint/http/http.ts +75 -0
- package/src/api-endpoint/http/index.ts +2 -0
- package/src/api-endpoint/version.ts +4 -0
- package/src/card/api-endpoint.ts +39 -0
- package/src/card/enum/funding.ts +11 -0
- package/src/card/enum/nature.ts +7 -0
- package/src/card/enum/network.ts +16 -0
- package/src/card/model.ts +139 -0
- package/src/card/repository.ts +11 -0
- package/src/card/validator.ts +79 -0
- package/src/core/abstract-model.ts +126 -0
- package/src/core/abstract-repository.ts +201 -0
- package/src/core/common/branded-type.ts +15 -0
- package/src/core/common/handler/date-handler.ts +31 -0
- package/src/core/common/handler/enum-handler.ts +18 -0
- package/src/core/common/handler/handler.ts +64 -0
- package/src/core/common/handler/id-handler.ts +27 -0
- package/src/core/common/id-validator.ts +23 -0
- package/src/core/common/mutation-tracked-object.ts +49 -0
- package/src/core/endpoint-interface.ts +43 -0
- package/src/core/error/base-error.ts +1 -0
- package/src/core/error/endpoint/api/api-conflict-error.ts +7 -0
- package/src/core/error/endpoint/api/api-error.ts +10 -0
- package/src/core/error/endpoint/api/api-not-found-error.ts +7 -0
- package/src/core/error/endpoint/api/api-unauthorized-error.ts +7 -0
- package/src/core/error/endpoint/api/api-unknown-error.ts +7 -0
- package/src/core/error/endpoint/api/api-validation-error.ts +13 -0
- package/src/core/error/endpoint/api/index.ts +6 -0
- package/src/core/error/endpoint/route-inaccessible-error.ts +8 -0
- package/src/core/error/unpopulated-error.ts +3 -0
- package/src/core/error/unsendable-error.ts +3 -0
- package/src/core/error/validation-error.ts +3 -0
- package/src/core/validator.ts +129 -0
- package/src/customer/api-endpoint.ts +29 -0
- package/src/customer/model.ts +113 -0
- package/src/customer/repository.ts +14 -0
- package/src/customer/validator.ts +26 -0
- package/src/payment/api-endpoint.ts +118 -0
- package/src/payment/enum/currency.ts +14 -0
- package/src/payment/enum/method.ts +2 -0
- package/src/payment/enum/status.ts +21 -0
- package/src/payment/model.ts +378 -0
- package/src/payment/repository.ts +73 -0
- package/src/payment/validator.ts +96 -0
- package/src/payment-intent/api-endpoint.ts +123 -0
- package/src/payment-intent/enum/status.ts +21 -0
- package/src/payment-intent/enum/threeds.ts +2 -0
- package/src/payment-intent/model.ts +266 -0
- package/src/payment-intent/repository.ts +85 -0
- package/src/payment-intent/validator.ts +45 -0
- package/src/payout/model.ts +8 -0
- package/src/refund/api-endpoint.ts +33 -0
- package/src/refund/enum/refund-status.ts +11 -0
- package/src/refund/model.ts +90 -0
- package/src/refund/repository.ts +14 -0
- package/src/refund/validator.ts +44 -0
- package/src/sepa/api-endpoint.ts +26 -0
- package/src/sepa/model.ts +80 -0
- package/src/sepa/repository.ts +11 -0
- package/src/sepa/validator.ts +56 -0
- package/test/fixtures/endpoint.stub.ts +22 -0
- package/test/fixtures/list-endpoint.stub.ts +33 -0
- package/test/fixtures/list-model.stub.ts +24 -0
- package/test/fixtures/model.stub.ts +12 -0
- package/test/functional/address.test.ts +126 -0
- package/test/functional/card.test.ts +175 -0
- package/test/functional/customer.test.ts +174 -0
- package/test/functional/payment-intent.test.ts +215 -0
- package/test/functional/payment.test.ts +206 -0
- package/test/functional/refund.test.ts +112 -0
- package/test/functional/sepa.test.ts +136 -0
- package/test/integration/stub-integration.test.ts +88 -0
- package/test/stub/stub-endpoint.ts +42 -0
- package/test/stub/stub-model.ts +82 -0
- package/test/stub/stub-repository.ts +11 -0
- package/test/test-helper.ts +133 -0
- package/test/unit/address/api-endpoint.test.ts +134 -0
- package/test/unit/address/fixtures.ts +60 -0
- package/test/unit/address/model.test.ts +75 -0
- package/test/unit/address/repository.test.ts +148 -0
- package/test/unit/address/validator.test.ts +49 -0
- package/test/unit/api-endpoint/http/fetch-http-client.test.ts +44 -0
- package/test/unit/api-endpoint/http/http.test.ts +152 -0
- package/test/unit/card/api-endpoint.test.ts +170 -0
- package/test/unit/card/card-helper.ts +116 -0
- package/test/unit/card/fixtures.ts +87 -0
- package/test/unit/card/model.test.ts +167 -0
- package/test/unit/card/repository.test.ts +146 -0
- package/test/unit/card/validator.test.ts +96 -0
- package/test/unit/core/common/mutation-tracked-object.test.ts +24 -0
- package/test/unit/customer/api-endpoint.test.ts +143 -0
- package/test/unit/customer/fixtures.ts +73 -0
- package/test/unit/customer/model.test.ts +90 -0
- package/test/unit/customer/repository.test.ts +148 -0
- package/test/unit/customer/validator.test.ts +62 -0
- package/test/unit/payment/api-endpoint.test.ts +188 -0
- package/test/unit/payment/fixtures.ts +116 -0
- package/test/unit/payment/model.test.ts +240 -0
- package/test/unit/payment/repository.test.ts +270 -0
- package/test/unit/payment/validator.test.ts +137 -0
- package/test/unit/payment-intent/api-endpoint.test.ts +142 -0
- package/test/unit/payment-intent/fixtures.ts +94 -0
- package/test/unit/payment-intent/model.test.ts +157 -0
- package/test/unit/payment-intent/repository.test.ts +263 -0
- package/test/unit/payment-intent/validator.test.ts +136 -0
- package/test/unit/refund/api-endpoint.test.ts +129 -0
- package/test/unit/refund/fixtures.ts +58 -0
- package/test/unit/refund/model.test.ts +89 -0
- package/test/unit/refund/repository.test.ts +157 -0
- package/test/unit/refund/validator.test.ts +89 -0
- package/test/unit/sepa/api-endpoint.test.ts +170 -0
- package/test/unit/sepa/fixture.ts +62 -0
- package/test/unit/sepa/model.test.ts +86 -0
- package/test/unit/sepa/repository.test.ts +149 -0
- package/test/unit/sepa/sepa-helper.ts +71 -0
- package/test/unit/sepa/validator.test.ts +58 -0
- package/test/unit/stub/stub-endpoint.test.ts +236 -0
- package/test/unit/stub/stub-list-endpoint.test.ts +86 -0
- package/test/unit/stub/stub-model.test.ts +111 -0
- package/test/unit/stub/stub-repository.test.ts +144 -0
- package/tsconfig.json +50 -0
- package/vitest.config.ts +20 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { AddressApiEndpoint } from '#address/api-endpoint.js';
|
|
4
|
+
import { IdAddress, idAddress } from '#address/model.js';
|
|
5
|
+
import { StancerRouteInaccessibleError } from '#error/endpoint/route-inaccessible-error.js';
|
|
6
|
+
import { randomString } from '#test/test-helper.js';
|
|
7
|
+
|
|
8
|
+
import { endpointResource, endpointResourceDeserialized } from './fixtures.js';
|
|
9
|
+
|
|
10
|
+
vi.mock('#api/http/http.js', async (importHTTP) => {
|
|
11
|
+
const original: Record<string, Promise<unknown>> = await importHTTP();
|
|
12
|
+
return {
|
|
13
|
+
...original,
|
|
14
|
+
transformResponseToData: vi.fn((value) => value.json()),
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
vi.mock('#api/http/fetch-http-client.js', () => {
|
|
19
|
+
return {
|
|
20
|
+
httpClient: {
|
|
21
|
+
makeRequest: vi.fn(() => {
|
|
22
|
+
return Promise.resolve({
|
|
23
|
+
status: 200,
|
|
24
|
+
json: () => {
|
|
25
|
+
return {
|
|
26
|
+
...endpointResource,
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
}),
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('address api endpoint', () => {
|
|
36
|
+
describe('Serialize/Deserialize Resources', () => {
|
|
37
|
+
const endpoint = new AddressApiEndpoint();
|
|
38
|
+
|
|
39
|
+
it('test deserialize Resource', () => {
|
|
40
|
+
const deserializedResources = endpoint.deserializeResource(endpointResource);
|
|
41
|
+
expect(deserializedResources).toMatchObject(endpointResourceDeserialized);
|
|
42
|
+
expect(endpointResourceDeserialized).toMatchObject(deserializedResources);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('test post serialize Resource', () => {
|
|
46
|
+
const serializedResources = endpoint.postObjectSerializer(
|
|
47
|
+
endpointResourceDeserialized,
|
|
48
|
+
) as typeof endpointResource;
|
|
49
|
+
// We extract the API generated Data and the non postable data from our endpoint resources.
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
51
|
+
const { deleted, created_at, id, ...postObject } = endpointResource;
|
|
52
|
+
expect(serializedResources).toMatchObject(postObject);
|
|
53
|
+
expect(postObject).toMatchObject(serializedResources);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('test patch serialize Resource', () => {
|
|
57
|
+
const serializedResources = endpoint.patchObjectSerializer(
|
|
58
|
+
endpointResourceDeserialized,
|
|
59
|
+
) as typeof endpointResource;
|
|
60
|
+
// We extract the API generated Data and the non patchable data from our endpoint resources.
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
62
|
+
const { deleted, created_at, id, ...patchObject } = endpointResource;
|
|
63
|
+
expect(serializedResources).toMatchObject(patchObject);
|
|
64
|
+
expect(patchObject).toMatchObject(serializedResources);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('endpoint path', () => {
|
|
69
|
+
it('endpoint url', () => {
|
|
70
|
+
const endpoint = new AddressApiEndpoint();
|
|
71
|
+
const id = ('addr_' + randomString(24)) as IdAddress;
|
|
72
|
+
expect(endpoint.endpointUrl(id)).toStrictEqual(`https://api.stancer.com/v2/addresses/${id}`);
|
|
73
|
+
expect(endpoint.endpointUrl()).toStrictEqual('https://api.stancer.com/v2/addresses/');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('verbs', () => {
|
|
78
|
+
const addressEndpoint = new AddressApiEndpoint();
|
|
79
|
+
|
|
80
|
+
it('get', async () => {
|
|
81
|
+
const responseWithId = await addressEndpoint.get(('addr_' + randomString(24)) as IdAddress);
|
|
82
|
+
expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('post', () => {
|
|
86
|
+
it('has all required field', async () => {
|
|
87
|
+
const responseWithId = await addressEndpoint.post({
|
|
88
|
+
zipCode: randomString(2, 16),
|
|
89
|
+
country: randomString(2),
|
|
90
|
+
city: randomString(1, 64),
|
|
91
|
+
});
|
|
92
|
+
expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('patch', async () => {
|
|
97
|
+
expect(() => addressEndpoint.patch(('addr_' + randomString(24), {}) as IdAddress, endpointResource)).toThrow(
|
|
98
|
+
'PATCH is not allowed for addresses',
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('delete', async () => {
|
|
103
|
+
expect(await addressEndpoint.delete(('addr_' + randomString(24)) as IdAddress)).toStrictEqual(
|
|
104
|
+
endpointResourceDeserialized,
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe('send', () => {
|
|
110
|
+
const { city, country, zipCode } = endpointResourceDeserialized;
|
|
111
|
+
|
|
112
|
+
it('send with required data', async () => {
|
|
113
|
+
const endpoint = new AddressApiEndpoint();
|
|
114
|
+
await expect(endpoint.send(undefined, { city, country, zipCode })).resolves.toStrictEqual(
|
|
115
|
+
endpointResourceDeserialized,
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('no patch if id', async () => {
|
|
120
|
+
const endpoint = new AddressApiEndpoint();
|
|
121
|
+
const id = idAddress('addr_' + randomString(24));
|
|
122
|
+
expect(() => endpoint.send(id, { id, city, country, zipCode })).toThrow(StancerRouteInaccessibleError);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('post if no id', () => {
|
|
126
|
+
const endpoint = new AddressApiEndpoint();
|
|
127
|
+
const spyonPatch = vi.spyOn(endpoint, 'patch');
|
|
128
|
+
const spyonPost = vi.spyOn(endpoint, 'post');
|
|
129
|
+
endpoint.send(undefined, { city, country, zipCode });
|
|
130
|
+
expect(spyonPatch).toHaveBeenCalledTimes(0);
|
|
131
|
+
expect(spyonPost).toHaveBeenCalledTimes(1);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
|
|
3
|
+
import { idAddress } from '#address/model.js';
|
|
4
|
+
import { randomNumber, randomString } from '#test/test-helper.js';
|
|
5
|
+
|
|
6
|
+
const city = randomString(1, 50);
|
|
7
|
+
const country = randomString(2);
|
|
8
|
+
// arbitrary future timestamp, we don't really care about it except that is is a timestamp
|
|
9
|
+
const created_at = randomNumber(1786460000, 2000000000);
|
|
10
|
+
const deleted = false;
|
|
11
|
+
const id = idAddress('addr_' + randomString(24));
|
|
12
|
+
const line1 = randomString(1, 50);
|
|
13
|
+
const line2 = randomString(1, 50);
|
|
14
|
+
const line3 = randomString(1, 50);
|
|
15
|
+
const state = randomString(1, 3);
|
|
16
|
+
const zip_code = randomString(2, 16);
|
|
17
|
+
|
|
18
|
+
export const endpointResource = {
|
|
19
|
+
city,
|
|
20
|
+
country,
|
|
21
|
+
created_at, // actual API field in snake_case
|
|
22
|
+
deleted,
|
|
23
|
+
id,
|
|
24
|
+
line1,
|
|
25
|
+
line2,
|
|
26
|
+
line3,
|
|
27
|
+
state,
|
|
28
|
+
zip_code, // actual API field in snake_case
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const endpointResourceDeserialized = {
|
|
32
|
+
city,
|
|
33
|
+
country,
|
|
34
|
+
createdAt: Temporal.Instant.fromEpochMilliseconds(created_at * 1000),
|
|
35
|
+
deleted,
|
|
36
|
+
id,
|
|
37
|
+
line1,
|
|
38
|
+
line2,
|
|
39
|
+
line3,
|
|
40
|
+
state,
|
|
41
|
+
zipCode: zip_code,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const creationResource = {
|
|
45
|
+
city,
|
|
46
|
+
country,
|
|
47
|
+
line1,
|
|
48
|
+
line2,
|
|
49
|
+
line3,
|
|
50
|
+
state,
|
|
51
|
+
zip_code,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const listResource = [
|
|
55
|
+
endpointResource,
|
|
56
|
+
{
|
|
57
|
+
...endpointResource,
|
|
58
|
+
city: randomString(1, 50),
|
|
59
|
+
},
|
|
60
|
+
];
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { describe, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { AddressData, AddressModel } from '#address/model.js';
|
|
4
|
+
import { Validator } from '#core/validator.js';
|
|
5
|
+
import { SetterGetter, randomNumber, randomString } from '#test/test-helper.js';
|
|
6
|
+
|
|
7
|
+
describe('Address Class', () => {
|
|
8
|
+
describe('Test Setter & Getters', () => {
|
|
9
|
+
const address = new AddressModel((() => true) as unknown as Validator<AddressData>);
|
|
10
|
+
|
|
11
|
+
it('line1', () => {
|
|
12
|
+
SetterGetter(
|
|
13
|
+
(value: string) => address.setLine1(value),
|
|
14
|
+
() => address.line1,
|
|
15
|
+
randomString(1, 64),
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('line2', () => {
|
|
20
|
+
SetterGetter(
|
|
21
|
+
(value: string) => address.setLine2(value),
|
|
22
|
+
() => address.line2,
|
|
23
|
+
randomString(1, 64),
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('line3', () => {
|
|
28
|
+
SetterGetter(
|
|
29
|
+
(value: string) => address.setLine3(value),
|
|
30
|
+
() => address.line3,
|
|
31
|
+
randomString(1, 64),
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('country', () => {
|
|
36
|
+
SetterGetter(
|
|
37
|
+
(value: string) => address.setCountry(value),
|
|
38
|
+
() => address.country,
|
|
39
|
+
randomString(2, 2),
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('zipCode', () => {
|
|
44
|
+
SetterGetter(
|
|
45
|
+
(value: string) => address.setZipCode(value),
|
|
46
|
+
() => address.zipCode,
|
|
47
|
+
randomString(1, 16),
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('State', () => {
|
|
52
|
+
SetterGetter(
|
|
53
|
+
(value: string) => address.setState(value),
|
|
54
|
+
() => address.state,
|
|
55
|
+
randomString(1, 3),
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('City', () => {
|
|
60
|
+
SetterGetter(
|
|
61
|
+
(value: string) => address.setCity(value),
|
|
62
|
+
() => address.city,
|
|
63
|
+
randomString(1, 50),
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('deleted', () => {
|
|
68
|
+
SetterGetter(
|
|
69
|
+
(value: boolean) => (address._data.deleted = value),
|
|
70
|
+
() => address.deleted,
|
|
71
|
+
Boolean(randomNumber(0, 1)),
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { AddressApiEndpoint } from '#address/api-endpoint.js';
|
|
4
|
+
import { AddressModel, IdAddress } from '#address/model.js';
|
|
5
|
+
import { AddressRepository } from '#address/repository.js';
|
|
6
|
+
import { addressValidator } from '#address/validator.js';
|
|
7
|
+
import { AbstractRepository } from '#core/abstract-repository.js';
|
|
8
|
+
import { StancerError } from '#error/base-error.js';
|
|
9
|
+
import { StancerRouteInaccessibleError } from '#error/endpoint/route-inaccessible-error.js';
|
|
10
|
+
import { randomString } from '#test/test-helper.js';
|
|
11
|
+
|
|
12
|
+
import { creationResource, endpointResource, listResource } from './fixtures.js';
|
|
13
|
+
|
|
14
|
+
const idPrefix = 'addr';
|
|
15
|
+
const id = `${idPrefix}_${randomString(24)}` as IdAddress;
|
|
16
|
+
const stancerErrorID = `${idPrefix}_unidentifiedError${randomString(7)}`;
|
|
17
|
+
const stancerRouteInaccessibleId = `${idPrefix}_StancerRouteInnacessible` as string;
|
|
18
|
+
const dataHasBeenSentId = `${idPrefix}_DataHasBeenSent${randomString(9)}`;
|
|
19
|
+
const dataUnmodified = `${idPrefix}_unmodifiedData${randomString(10)}`;
|
|
20
|
+
class MockApiEndpoint {
|
|
21
|
+
constructor() {}
|
|
22
|
+
|
|
23
|
+
list() {
|
|
24
|
+
async function* listGenerator() {
|
|
25
|
+
yield* listResource;
|
|
26
|
+
}
|
|
27
|
+
return listGenerator();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
delete() {
|
|
31
|
+
return endpointResource;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
send(id: IdAddress, data: Record<string, unknown>) {
|
|
35
|
+
if (id) {
|
|
36
|
+
if (id === stancerErrorID) {
|
|
37
|
+
throw new StancerError();
|
|
38
|
+
}
|
|
39
|
+
if (id === stancerRouteInaccessibleId) {
|
|
40
|
+
throw new StancerRouteInaccessibleError('PATCH', 'stub');
|
|
41
|
+
}
|
|
42
|
+
if (id === dataUnmodified) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return { ...data, id: dataHasBeenSentId };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get() {
|
|
50
|
+
return endpointResource;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
it('can be instanciated', () => {
|
|
55
|
+
const instance = new AddressRepository();
|
|
56
|
+
expect(instance).toBeInstanceOf(AddressRepository);
|
|
57
|
+
expect(instance).toBeInstanceOf(AbstractRepository);
|
|
58
|
+
//accessing private method & property
|
|
59
|
+
expect(instance.endpoint).toBeInstanceOf(AddressApiEndpoint);
|
|
60
|
+
expect(instance.validator).toBe(addressValidator);
|
|
61
|
+
expect(instance.staticModel).toBe(AddressModel);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('repository', () => {
|
|
65
|
+
const mockRepo = new MockApiEndpoint() as unknown as AddressApiEndpoint; // I lie, for convenience sake
|
|
66
|
+
const repository = new AddressRepository(mockRepo);
|
|
67
|
+
|
|
68
|
+
describe('create', () => {
|
|
69
|
+
it("doesn't accept bad data", async () => {
|
|
70
|
+
const badID = randomString(30, 56);
|
|
71
|
+
// using puposefuly bad ID
|
|
72
|
+
await expect(async () => await repository.create(badID)).rejects.toThrow();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('accept good data', async () => {
|
|
76
|
+
const model = await repository.create(id);
|
|
77
|
+
expect(model).toBeInstanceOf(AddressModel);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('delete', async () => {
|
|
82
|
+
const modelWithoutId = await repository.create();
|
|
83
|
+
await expect(() => repository.delete(modelWithoutId)).rejects.toThrow();
|
|
84
|
+
const zip_code = endpointResource.zip_code;
|
|
85
|
+
const model = await repository.create({ id: endpointResource.id, zip_code });
|
|
86
|
+
await expect((await repository.delete(model))._data).toStrictEqual(endpointResource);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('send', () => {
|
|
90
|
+
it('should return model', async () => {
|
|
91
|
+
const model = await repository.create(creationResource);
|
|
92
|
+
const sentModel = await repository.send(model);
|
|
93
|
+
expect(sentModel).toStrictEqual(model);
|
|
94
|
+
expect(sentModel._data.id).toEqual(dataHasBeenSentId);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should throw on inprecise error', async () => {
|
|
98
|
+
const model = await repository.create({ ...creationResource, id: stancerErrorID as IdAddress });
|
|
99
|
+
await expect(() => repository.send(model)).rejects.toThrow(StancerError);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should return error on StancerRouteInacessibleError', async () => {
|
|
103
|
+
const model = await repository.create({ ...creationResource, id: stancerRouteInaccessibleId as IdAddress });
|
|
104
|
+
expect(async () => await repository.send(model)).rejects.toThrow(StancerRouteInaccessibleError);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should return unmodified model when no new Data', async () => {
|
|
108
|
+
const model = await repository.create({ ...creationResource, id: dataUnmodified as IdAddress });
|
|
109
|
+
const sentModel = await repository.send(model);
|
|
110
|
+
expect(model).toStrictEqual(sentModel);
|
|
111
|
+
expect(sentModel._data.id).toEqual(dataUnmodified);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('populate', () => {
|
|
116
|
+
it('should not modify model if no Id', async () => {
|
|
117
|
+
const getEndpoint = vi.spyOn(mockRepo, 'get');
|
|
118
|
+
const model = await repository.create(creationResource);
|
|
119
|
+
expect(model).toBeInstanceOf(AddressModel);
|
|
120
|
+
expect(getEndpoint).toHaveBeenCalledTimes(0);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('should get model if only id', async () => {
|
|
124
|
+
const getEndpoint = vi.spyOn(mockRepo, 'get');
|
|
125
|
+
const model = await repository.create(id);
|
|
126
|
+
expect(model).toBeInstanceOf(AddressModel);
|
|
127
|
+
expect(getEndpoint).toHaveBeenCalledTimes(1);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('list', async () => {
|
|
132
|
+
it('return correct model one by one', async () => {
|
|
133
|
+
const listStub = repository.list();
|
|
134
|
+
const item1 = await listStub.next();
|
|
135
|
+
expect(item1.done).toBeFalsy();
|
|
136
|
+
expect(item1.value).toHaveProperty('id');
|
|
137
|
+
expect(item1.value).toBeInstanceOf(AddressModel);
|
|
138
|
+
|
|
139
|
+
const item2 = await listStub.next();
|
|
140
|
+
expect(item2.done).toBeFalsy();
|
|
141
|
+
expect(item2.value).toHaveProperty('id');
|
|
142
|
+
expect(item2.value).toBeInstanceOf(AddressModel);
|
|
143
|
+
|
|
144
|
+
const item3 = await listStub.next();
|
|
145
|
+
expect(item3.done).toBeTruthy();
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { describe, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { addressValidator } from '#address/validator.js';
|
|
4
|
+
import { processValidator } from '#core/validator.js';
|
|
5
|
+
import { checkType, goldilocks, randomString } from '#test/test-helper.js';
|
|
6
|
+
|
|
7
|
+
const validator = processValidator(addressValidator);
|
|
8
|
+
|
|
9
|
+
describe('Test boundaries', () => {
|
|
10
|
+
const goldilocksLine = (line: CallableFunction) => goldilocks(line, 'string', { min: 1, max: 50 });
|
|
11
|
+
|
|
12
|
+
it('line1', () => {
|
|
13
|
+
goldilocksLine((value: string) => validator('line1', value));
|
|
14
|
+
checkType((value: string) => validator('line1', value), ['string'], randomString(1, 50));
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('line2', () => {
|
|
18
|
+
goldilocksLine((value: string) => validator('line2', value));
|
|
19
|
+
|
|
20
|
+
checkType((value: string) => validator('line2', value), ['string'], randomString(1, 50));
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('line3', () => {
|
|
24
|
+
goldilocksLine((value: string) => validator('line3', value));
|
|
25
|
+
|
|
26
|
+
checkType((value: string) => validator('line3', value), ['string'], randomString(1, 50));
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('ZipCode', () => {
|
|
30
|
+
goldilocks((value: string) => validator('zipCode', value), 'string', { min: 2, max: 16 });
|
|
31
|
+
checkType((value: string) => validator('zipCode', value), ['string'], randomString(2, 16));
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('state', () => {
|
|
35
|
+
goldilocks((value: string) => validator('state', value), 'string', { min: 1, max: 3 });
|
|
36
|
+
|
|
37
|
+
checkType((value: string) => validator('state', value), ['string'], randomString(1, 3));
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('country', () => {
|
|
41
|
+
goldilocks((value: string) => validator('country', value), 'string', { min: 2, max: 2 });
|
|
42
|
+
checkType((value: string) => validator('country', value), ['string'], randomString(2, 2));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('city', () => {
|
|
46
|
+
goldilocks((value: string) => validator('city', value), 'string', { min: 1, max: 50 });
|
|
47
|
+
checkType((value: string) => validator('city', value), ['string'], randomString(1, 50));
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { httpClient } from '#api/http/fetch-http-client.js';
|
|
4
|
+
import { Http } from '#api/http/index.js';
|
|
5
|
+
import { mockFetch, randomString } from '#test/test-helper.js';
|
|
6
|
+
|
|
7
|
+
describe('test fetch client', () => {
|
|
8
|
+
it('uses base user agent', async () => {
|
|
9
|
+
// as we only touch headers in this function we will just look for headers in the response
|
|
10
|
+
const resolver = (url: string, req: Http.Request) => req.headers;
|
|
11
|
+
|
|
12
|
+
const cleaner = mockFetch(resolver);
|
|
13
|
+
const request: Http.Request = {
|
|
14
|
+
url: randomString(1, 24),
|
|
15
|
+
method: 'DELETE',
|
|
16
|
+
_tag: 'HttpClientRequest',
|
|
17
|
+
headers: {},
|
|
18
|
+
};
|
|
19
|
+
const response = await httpClient.makeRequest(request);
|
|
20
|
+
expect(response).haveOwnProperty('status');
|
|
21
|
+
expect(response).haveOwnProperty('json');
|
|
22
|
+
const json: typeof request.headers = await response.json();
|
|
23
|
+
expect(json).toEqual({ 'user-agent': 'Stancer fetch' });
|
|
24
|
+
cleaner();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('uses custom user agent', async () => {
|
|
28
|
+
const userAgent = randomString(12, 24);
|
|
29
|
+
// as we only touch headers in this function we will just look for headers in the response
|
|
30
|
+
const resolver = (url: string, req: Http.Request) => req.headers;
|
|
31
|
+
const cleaner = mockFetch(resolver);
|
|
32
|
+
const request: Http.Request = {
|
|
33
|
+
url: randomString(1, 12),
|
|
34
|
+
method: 'DELETE',
|
|
35
|
+
headers: { 'user-agent': userAgent },
|
|
36
|
+
};
|
|
37
|
+
const response = await httpClient.makeRequest(request);
|
|
38
|
+
expect(response).haveOwnProperty('status');
|
|
39
|
+
expect(response).haveOwnProperty('json');
|
|
40
|
+
const json: typeof request.headers = await response.json();
|
|
41
|
+
expect(json).toEqual({ 'user-agent': `${userAgent}` });
|
|
42
|
+
cleaner();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { Request, Response, createRequest, handleHttpResponseErrors, transformResponseToData } from '#api/http/http.js';
|
|
4
|
+
import {
|
|
5
|
+
StancerApiConflictError,
|
|
6
|
+
StancerApiError,
|
|
7
|
+
StancerApiNotFoundError,
|
|
8
|
+
StancerApiUnauthorizedError,
|
|
9
|
+
StancerApiValidationError,
|
|
10
|
+
} from '#error/endpoint/api/index.js';
|
|
11
|
+
import { randomString } from '#test/test-helper.js';
|
|
12
|
+
|
|
13
|
+
describe('Test http', () => {
|
|
14
|
+
describe('createRequest', () => {
|
|
15
|
+
it('Create request authenticate, with a body', () => {
|
|
16
|
+
const url = randomString(1, 45);
|
|
17
|
+
const body = randomString(1, 25);
|
|
18
|
+
const method = 'GET';
|
|
19
|
+
const req: Omit<Request, '_tag'> & { authenticated?: boolean } = {
|
|
20
|
+
authenticated: true,
|
|
21
|
+
headers: {
|
|
22
|
+
one: randomString(10, 24),
|
|
23
|
+
two: randomString(11, 45),
|
|
24
|
+
},
|
|
25
|
+
url,
|
|
26
|
+
method,
|
|
27
|
+
body,
|
|
28
|
+
};
|
|
29
|
+
const request = createRequest(req);
|
|
30
|
+
expect(request).toHaveProperty('url');
|
|
31
|
+
expect(request.url).toBe(url);
|
|
32
|
+
expect(request).toHaveProperty('method');
|
|
33
|
+
expect(request.method).toBe(method);
|
|
34
|
+
expect(request).toHaveProperty('body');
|
|
35
|
+
expect(request.body).toBe(body);
|
|
36
|
+
expect(request).toHaveProperty('authenticated');
|
|
37
|
+
expect(request.authenticated).toBe(true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('Create request unauthenticated, with a body', () => {
|
|
41
|
+
const url = randomString(1, 45);
|
|
42
|
+
const body = randomString(1, 25);
|
|
43
|
+
const method = 'GET';
|
|
44
|
+
const req: Omit<Request, '_tag'> & { authenticated?: boolean } = {
|
|
45
|
+
authenticated: false,
|
|
46
|
+
headers: {
|
|
47
|
+
one: randomString(10, 24),
|
|
48
|
+
two: randomString(11, 45),
|
|
49
|
+
},
|
|
50
|
+
url,
|
|
51
|
+
method,
|
|
52
|
+
body,
|
|
53
|
+
};
|
|
54
|
+
const request = createRequest(req);
|
|
55
|
+
expect(request).toHaveProperty('url');
|
|
56
|
+
expect(request.url).toBe(url);
|
|
57
|
+
expect(request).toHaveProperty('method');
|
|
58
|
+
expect(request.method).toBe(method);
|
|
59
|
+
expect(request).toHaveProperty('body');
|
|
60
|
+
expect(request.body).toBe(body);
|
|
61
|
+
expect(request).toHaveProperty('authenticated');
|
|
62
|
+
expect(request.authenticated).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
it('Create request authenticated, with nobody', () => {
|
|
65
|
+
const url = randomString(1, 45);
|
|
66
|
+
const body = undefined;
|
|
67
|
+
const method = 'GET';
|
|
68
|
+
const req: Omit<Request, '_tag'> & { authenticated?: boolean } = {
|
|
69
|
+
authenticated: true,
|
|
70
|
+
headers: {
|
|
71
|
+
one: randomString(10, 24),
|
|
72
|
+
two: randomString(11, 45),
|
|
73
|
+
},
|
|
74
|
+
url,
|
|
75
|
+
method,
|
|
76
|
+
body,
|
|
77
|
+
};
|
|
78
|
+
const request = createRequest(req);
|
|
79
|
+
expect(request).toHaveProperty('url');
|
|
80
|
+
expect(request.url).toBe(url);
|
|
81
|
+
expect(request).toHaveProperty('method');
|
|
82
|
+
expect(request.method).toBe(method);
|
|
83
|
+
expect(request).toHaveProperty('body');
|
|
84
|
+
expect(request.body).toBeUndefined();
|
|
85
|
+
expect(request).toHaveProperty('authenticated');
|
|
86
|
+
expect(request.authenticated).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
it('Create request unauthenticated, with nobody', () => {
|
|
89
|
+
const url = randomString(1, 45);
|
|
90
|
+
const body = undefined;
|
|
91
|
+
const method = 'GET';
|
|
92
|
+
const req: Omit<Request, '_tag'> & { authenticated?: boolean } = {
|
|
93
|
+
authenticated: false,
|
|
94
|
+
headers: {
|
|
95
|
+
one: randomString(10, 24),
|
|
96
|
+
two: randomString(11, 45),
|
|
97
|
+
},
|
|
98
|
+
url,
|
|
99
|
+
method,
|
|
100
|
+
body,
|
|
101
|
+
};
|
|
102
|
+
const request = createRequest(req);
|
|
103
|
+
expect(request).toHaveProperty('url');
|
|
104
|
+
expect(request.url).toBe(url);
|
|
105
|
+
expect(request).toHaveProperty('method');
|
|
106
|
+
expect(request.method).toBe(method);
|
|
107
|
+
expect(request).toHaveProperty('body');
|
|
108
|
+
expect(request.body).toBeUndefined();
|
|
109
|
+
expect(request).toHaveProperty('authenticated');
|
|
110
|
+
expect(request.authenticated).toBe(false);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('handleHttpResponseErrors', () => {
|
|
115
|
+
it.each([
|
|
116
|
+
[401, StancerApiUnauthorizedError],
|
|
117
|
+
[422, StancerApiValidationError],
|
|
118
|
+
[404, StancerApiNotFoundError],
|
|
119
|
+
[409, StancerApiConflictError],
|
|
120
|
+
[400, StancerApiError],
|
|
121
|
+
[500, StancerApiError],
|
|
122
|
+
])('%d throw appropriate error', async (status, expectedError) => {
|
|
123
|
+
const response: Response = {
|
|
124
|
+
status,
|
|
125
|
+
json: async <T>() => {
|
|
126
|
+
return Promise.resolve({ error: randomString(1, 24) }) as T;
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
await expect(handleHttpResponseErrors(response)).rejects.toThrow(expectedError);
|
|
131
|
+
});
|
|
132
|
+
it('is successful(200)', async () => {
|
|
133
|
+
const response: Response = {
|
|
134
|
+
status: Number(200), //we start at 23 to avoid triggering known exceptions
|
|
135
|
+
json: async <T>() => {
|
|
136
|
+
return { error: randomString(1, 24) } as T;
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
await expect(handleHttpResponseErrors(response)).resolves.toStrictEqual(response);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
describe('transform response to data', () => {
|
|
143
|
+
it('return response.json', async () => {
|
|
144
|
+
const information = randomString(1, 24);
|
|
145
|
+
const response: Response = {
|
|
146
|
+
status: 200,
|
|
147
|
+
json: async <T>() => (await Promise.resolve(information)) as T,
|
|
148
|
+
};
|
|
149
|
+
expect(await transformResponseToData<string>(response)).toStrictEqual(information);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|