@stancer/stancer 0.0.0-stage → 1.0.1
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 +14 -0
- package/LICENSE +19 -0
- package/README.md +166 -2
- package/eslint.config.js +14 -0
- package/package.json +58 -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,96 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { FundingCardEnum } from '#card/enum/funding.js';
|
|
4
|
+
import { NatureCardEnum } from '#card/enum/nature.js';
|
|
5
|
+
import { NetworkCardEnum, PreferredNetworkCardEnum } from '#card/enum/network.js';
|
|
6
|
+
import { CardModel } from '#card/model.js';
|
|
7
|
+
import { cardValidator } from '#card/validator.js';
|
|
8
|
+
import { checkType, goldilocks, randomMonth, randomNumber, randomString } from '#test/test-helper.js';
|
|
9
|
+
|
|
10
|
+
import { IdCustomer } from '../../../src/customer/model';
|
|
11
|
+
import { randomCardNumber } from './card-helper';
|
|
12
|
+
|
|
13
|
+
describe('Goldilocks', () => {
|
|
14
|
+
const card = new CardModel(cardValidator);
|
|
15
|
+
|
|
16
|
+
it('country', () => {
|
|
17
|
+
goldilocks((value: string) => (card._data.country = value), 'string', { min: 2, max: 2 });
|
|
18
|
+
checkType((value: string) => (card._data.country = value), ['string'], randomString(2, 2));
|
|
19
|
+
});
|
|
20
|
+
it('customer', () => {
|
|
21
|
+
const falseCustomer = randomString(24);
|
|
22
|
+
const trueCustomer = `cust_${randomString(24)}` as IdCustomer;
|
|
23
|
+
expect(() => card.setCustomer(falseCustomer)).toThrow();
|
|
24
|
+
card.setCustomer(trueCustomer);
|
|
25
|
+
expect(card.customer).toBe(trueCustomer);
|
|
26
|
+
});
|
|
27
|
+
it('cvc', () => {
|
|
28
|
+
const cvc = String(randomNumber(0, 999)).padStart(3, '0');
|
|
29
|
+
expect(() => card.setCvc(cvc.padStart(4, '0'))).toThrow();
|
|
30
|
+
expect(() => card.setCvc(String(randomNumber(0, 99)).padStart(2, '0'))).toThrow();
|
|
31
|
+
expect(() => card.setCvc(randomString(3))).toThrow();
|
|
32
|
+
|
|
33
|
+
checkType((value: string) => card.setCvc(value), ['string'], cvc);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('expMonth', () => {
|
|
37
|
+
checkType((value: number) => card.setExpMonth(value), ['number'], randomMonth());
|
|
38
|
+
goldilocks((value: number) => card.setExpMonth(value), 'number', { min: 1, max: 12 });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('expYear', () => {
|
|
42
|
+
checkType((value: number) => card.setExpYear(value), ['number'], randomNumber(1000, 9999));
|
|
43
|
+
goldilocks((value: number) => card.setExpYear(value), 'number', { min: 1000, max: 9999 });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('externalId', () => {
|
|
47
|
+
goldilocks((value: string) => card.setExternalId(value), 'string', { min: 1, max: 36 });
|
|
48
|
+
checkType((value: string) => card.setExternalId(value), ['string'], randomString(1, 36));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it.each(FundingCardEnum.map((n) => [n]))('funding', (funding) => {
|
|
52
|
+
const falseFunding = randomString(123, 188);
|
|
53
|
+
expect(() => (card._data.funding = falseFunding)).toThrow();
|
|
54
|
+
card._data.funding = funding;
|
|
55
|
+
expect(card.funding).toBe(funding);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('name', () => {
|
|
59
|
+
goldilocks((value: string) => card.setName(value), 'string', { min: 1, max: 64 });
|
|
60
|
+
checkType((value: string) => card.setName(value), ['string'], randomString(1, 64));
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it.each(NatureCardEnum.map((n) => [n]))('nature', (nature) => {
|
|
64
|
+
const falseNature = randomString(123, 188);
|
|
65
|
+
expect(() => (card._data.nature = falseNature)).toThrow();
|
|
66
|
+
card._data.nature = nature;
|
|
67
|
+
expect(card.nature).toBe(nature);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it.each(NetworkCardEnum.map((n) => [n]))('network', (network) => {
|
|
71
|
+
const falseNetwork = randomString(123, 188);
|
|
72
|
+
expect(() => (card._data.network = falseNetwork)).toThrow();
|
|
73
|
+
card._data.network = network;
|
|
74
|
+
expect(card.network).toBe(network);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('number', () => {
|
|
78
|
+
const falseCardNumber = String(Number(randomCardNumber()) + 1);
|
|
79
|
+
expect(() => card.setNumber(falseCardNumber)).toThrow();
|
|
80
|
+
const trueCardNumber = randomCardNumber();
|
|
81
|
+
card.setNumber(trueCardNumber);
|
|
82
|
+
expect(card._data.number).toBe(trueCardNumber);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it.each(PreferredNetworkCardEnum.map((n) => [n]))('preferredNetwork', (network) => {
|
|
86
|
+
const falsePreferredNetwork = randomString(123, 188);
|
|
87
|
+
expect(() => card.setPreferredNetwork(falsePreferredNetwork)).toThrow();
|
|
88
|
+
card.setPreferredNetwork(network);
|
|
89
|
+
expect(card.preferredNetwork).toBe(network);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('zipCode', () => {
|
|
93
|
+
goldilocks((value: string) => card.setZipCode(value), 'string', { min: 0, max: 8 });
|
|
94
|
+
checkType((value: string) => card.setZipCode(value), ['string'], randomString(0, 8));
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, it } from 'vitest';
|
|
2
|
+
import { expect } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { trackObjectMutations } from '#core/common/mutation-tracked-object.ts';
|
|
5
|
+
|
|
6
|
+
describe('mutation tracked object', () => {
|
|
7
|
+
it('set new data in mutation', () => {
|
|
8
|
+
const validator = () => true;
|
|
9
|
+
const data = {};
|
|
10
|
+
const tracked = trackObjectMutations(data, validator);
|
|
11
|
+
tracked['test'] = 'test';
|
|
12
|
+
expect(tracked.getMutations()).toStrictEqual({ test: 'test' });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('modify initialized data ', () => {
|
|
16
|
+
const validator = () => true;
|
|
17
|
+
const data = { truc: '12', machin: 'x' };
|
|
18
|
+
const tracked = trackObjectMutations(data, validator);
|
|
19
|
+
expect(tracked.getMutations()).toStrictEqual({});
|
|
20
|
+
expect(tracked.toRaw()).toStrictEqual(data);
|
|
21
|
+
tracked['truc'] = 'machin';
|
|
22
|
+
expect(tracked.getMutations()).toStrictEqual({ truc: 'machin' });
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { StancerError } from '#core/error/base-error.js';
|
|
4
|
+
import { CustomerApiEndpoint } from '#customer/api-endpoint.js';
|
|
5
|
+
import type { CustomerData, IdCustomer } from '#customer/model.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('customer api endpoint', () => {
|
|
36
|
+
describe('Serialize/Deserialize resources', () => {
|
|
37
|
+
const endpoint = new CustomerApiEndpoint();
|
|
38
|
+
|
|
39
|
+
it('test deserialize resource', () => {
|
|
40
|
+
const deserializedResources = endpoint.deserializeResource(endpointResource);
|
|
41
|
+
//Double match to guaranty exact same shape
|
|
42
|
+
expect(deserializedResources).toMatchObject(endpointResourceDeserialized);
|
|
43
|
+
expect(endpointResourceDeserialized).toMatchObject(deserializedResources);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('test post serialize resource', () => {
|
|
47
|
+
const serializedResources = endpoint.postObjectSerializer(
|
|
48
|
+
endpointResourceDeserialized,
|
|
49
|
+
) as typeof endpointResource;
|
|
50
|
+
// property not in postObject are API generated or not postable
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
52
|
+
const { deleted, created, id, ...postObject } = endpointResource;
|
|
53
|
+
//Double match to guaranty exact same shape
|
|
54
|
+
expect(serializedResources).toMatchObject(postObject);
|
|
55
|
+
expect(postObject).toMatchObject(serializedResources);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('test patch serialize resource (not available in repository)', () => {
|
|
59
|
+
const serializedResources = endpoint.patchObjectSerializer(
|
|
60
|
+
endpointResourceDeserialized,
|
|
61
|
+
) as typeof endpointResource;
|
|
62
|
+
// property not in patchObject are API generated or not patchable
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
64
|
+
const { deleted, created, id, ...patchObject } = endpointResource;
|
|
65
|
+
//Double match to guaranty exact same shape
|
|
66
|
+
expect(serializedResources).toMatchObject(patchObject);
|
|
67
|
+
expect(patchObject).toMatchObject(serializedResources);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('endpoint path', () => {
|
|
72
|
+
it('endpoint url', () => {
|
|
73
|
+
const endpoint = new CustomerApiEndpoint();
|
|
74
|
+
const id = ('cust_' + randomString(24)) as IdCustomer;
|
|
75
|
+
expect(endpoint._getResourceUri(id)).toStrictEqual(`https://api.stancer.com/v2/customers/${id}`);
|
|
76
|
+
expect(endpoint._getResourceUri()).toStrictEqual('https://api.stancer.com/v2/customers/');
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('verbs', () => {
|
|
81
|
+
const customerEndpoint = new CustomerApiEndpoint();
|
|
82
|
+
|
|
83
|
+
it('get', async () => {
|
|
84
|
+
const responseWithId = await customerEndpoint.get(('cust_' + randomString(24)) as IdCustomer);
|
|
85
|
+
expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('post', async () => {
|
|
89
|
+
const responseWithId = await customerEndpoint.post(endpointResource);
|
|
90
|
+
expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('patch', async () => {
|
|
94
|
+
const patchResponse = await customerEndpoint.patch(('cust_' + randomString(24)) as IdCustomer, endpointResource);
|
|
95
|
+
expect(patchResponse).toStrictEqual(endpointResourceDeserialized);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('delete', async () => {
|
|
99
|
+
expect(await customerEndpoint.delete(('cust_' + randomString(24)) as IdCustomer)).toStrictEqual(
|
|
100
|
+
endpointResourceDeserialized,
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('send', () => {
|
|
106
|
+
const { mobile, email, country } = endpointResourceDeserialized;
|
|
107
|
+
|
|
108
|
+
it.each([
|
|
109
|
+
['mobile', mobile],
|
|
110
|
+
['email', email],
|
|
111
|
+
])('send with partial required data', async (valueName, requiredValue) => {
|
|
112
|
+
const endpoint = new CustomerApiEndpoint();
|
|
113
|
+
const mutationsData = {} as Partial<CustomerData>;
|
|
114
|
+
mutationsData[valueName] = requiredValue;
|
|
115
|
+
await expect(endpoint.send(undefined, mutationsData)).resolves.toStrictEqual(endpointResourceDeserialized);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('does not post with missing partially required data', async () => {
|
|
119
|
+
const endpoint = new CustomerApiEndpoint();
|
|
120
|
+
expect(() => endpoint.send(undefined, { country })).toThrow(StancerError);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('Patch if id', async () => {
|
|
124
|
+
const endpoint = new CustomerApiEndpoint();
|
|
125
|
+
const spyonPatch = vi.spyOn(endpoint, 'patch');
|
|
126
|
+
const spyonPost = vi.spyOn(endpoint, 'post');
|
|
127
|
+
await expect(endpoint.send('cust_' + randomString(24), { mobile, country })).resolves.toStrictEqual(
|
|
128
|
+
endpointResourceDeserialized,
|
|
129
|
+
);
|
|
130
|
+
expect(spyonPatch).toHaveBeenCalledTimes(1);
|
|
131
|
+
expect(spyonPost).toHaveBeenCalledTimes(0);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('post if no id', () => {
|
|
135
|
+
const endpoint = new CustomerApiEndpoint();
|
|
136
|
+
const spyonPatch = vi.spyOn(endpoint, 'patch');
|
|
137
|
+
const spyonPost = vi.spyOn(endpoint, 'post');
|
|
138
|
+
endpoint.send(undefined, { email });
|
|
139
|
+
expect(spyonPatch).toHaveBeenCalledTimes(0);
|
|
140
|
+
expect(spyonPost).toHaveBeenCalledTimes(1);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
|
|
3
|
+
import { IdCustomer } from '#customer/model.js';
|
|
4
|
+
import { randomMonth, randomNumber, randomString } from '#test/test-helper.js';
|
|
5
|
+
|
|
6
|
+
import { IdAddress } from '../../../src/address/temporary-types';
|
|
7
|
+
|
|
8
|
+
const billing_address = 'addr_' + randomString(24);
|
|
9
|
+
const country = randomString(2);
|
|
10
|
+
const created = randomNumber(1786460000, 2000000000);
|
|
11
|
+
const date_birth = `${randomNumber(1000, 2025)}-${randomMonth().toString().padStart(2, '0')}-${randomNumber(1, 28).toString().padStart(2, '0')}`;
|
|
12
|
+
const deleted = false;
|
|
13
|
+
const email = randomString(1, 64);
|
|
14
|
+
const external_id = randomString(1, 36);
|
|
15
|
+
const id = `cust_${randomString(24)}`;
|
|
16
|
+
const legal_id = randomString(1, 16);
|
|
17
|
+
const mobile = randomString(4, 16);
|
|
18
|
+
const name = randomString(3, 64);
|
|
19
|
+
const shipping_address = 'addr_' + randomString(24);
|
|
20
|
+
|
|
21
|
+
export const endpointResource = {
|
|
22
|
+
billing_address,
|
|
23
|
+
country,
|
|
24
|
+
created,
|
|
25
|
+
date_birth,
|
|
26
|
+
deleted,
|
|
27
|
+
email,
|
|
28
|
+
external_id,
|
|
29
|
+
id,
|
|
30
|
+
legal_id,
|
|
31
|
+
mobile,
|
|
32
|
+
name,
|
|
33
|
+
shipping_address,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const endpointResourceDeserialized = {
|
|
37
|
+
createdAt: Temporal.Instant.fromEpochMilliseconds(created * 1000),
|
|
38
|
+
|
|
39
|
+
billingAddress: billing_address as IdAddress,
|
|
40
|
+
country,
|
|
41
|
+
dateBirth: Temporal.PlainDate.from(date_birth),
|
|
42
|
+
deleted,
|
|
43
|
+
email,
|
|
44
|
+
externalId: external_id,
|
|
45
|
+
id: id as IdCustomer,
|
|
46
|
+
legalId: legal_id,
|
|
47
|
+
mobile,
|
|
48
|
+
name,
|
|
49
|
+
shippingAddress: shipping_address as IdAddress,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const creationResource = {
|
|
53
|
+
billingAddress: billing_address,
|
|
54
|
+
country,
|
|
55
|
+
dateBirth: Temporal.PlainDate.from(date_birth),
|
|
56
|
+
email,
|
|
57
|
+
externalId: external_id,
|
|
58
|
+
id,
|
|
59
|
+
legalId: legal_id,
|
|
60
|
+
mobile,
|
|
61
|
+
name,
|
|
62
|
+
shippingAddress: shipping_address,
|
|
63
|
+
};
|
|
64
|
+
export const listResource = [
|
|
65
|
+
{
|
|
66
|
+
...endpointResource,
|
|
67
|
+
name: 'item1',
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
...endpointResource,
|
|
71
|
+
name: 'item2',
|
|
72
|
+
},
|
|
73
|
+
];
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { IdAddress, idAddress } from '#address/model.ts';
|
|
4
|
+
import { Validator } from '#core/validator.js';
|
|
5
|
+
import { CustomerData, CustomerModel } from '#customer/model.js';
|
|
6
|
+
import { SetterGetter, randomString } from '#test/test-helper.js';
|
|
7
|
+
|
|
8
|
+
describe('Customer Class', () => {
|
|
9
|
+
describe('Setter & Getter', () => {
|
|
10
|
+
const customer = new CustomerModel((() => true) as unknown as Validator<CustomerData>);
|
|
11
|
+
|
|
12
|
+
it('country', () => {
|
|
13
|
+
SetterGetter(
|
|
14
|
+
(value: string) => customer.setCountry(value),
|
|
15
|
+
() => customer.country,
|
|
16
|
+
randomString(2),
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('email', () => {
|
|
21
|
+
SetterGetter(
|
|
22
|
+
(value: string) => customer.setEmail(value),
|
|
23
|
+
() => customer.email,
|
|
24
|
+
randomString(30),
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('externalId', () => {
|
|
29
|
+
SetterGetter(
|
|
30
|
+
(value: string) => customer.setExternalId(value),
|
|
31
|
+
() => customer.externalId,
|
|
32
|
+
randomString(30),
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('legalId', () => {
|
|
37
|
+
SetterGetter(
|
|
38
|
+
(value: string) => customer.setLegalId(value),
|
|
39
|
+
() => customer.legalId,
|
|
40
|
+
randomString(15),
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('mobile', () => {
|
|
45
|
+
SetterGetter(
|
|
46
|
+
(value: string) => customer.setMobile(value),
|
|
47
|
+
() => customer.mobile,
|
|
48
|
+
randomString(12),
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('name', () => {
|
|
53
|
+
SetterGetter(
|
|
54
|
+
(value: string) => customer.setName(value),
|
|
55
|
+
() => customer.name,
|
|
56
|
+
randomString(12),
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('billing Address', () => {
|
|
61
|
+
SetterGetter(
|
|
62
|
+
(value: IdAddress) => customer.setBillingAddress(value),
|
|
63
|
+
() => customer.billingAddress,
|
|
64
|
+
idAddress(`addr_${randomString(24)}`),
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('Shipping Address', () => {
|
|
69
|
+
SetterGetter(
|
|
70
|
+
(value: IdAddress) => customer.setShippingAddress(value),
|
|
71
|
+
() => customer.shippingAddress,
|
|
72
|
+
idAddress(`addr_${randomString(24)}`),
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('deleted (unsettable)', () => {
|
|
77
|
+
SetterGetter(
|
|
78
|
+
(value: boolean) => (customer._data.deleted = value),
|
|
79
|
+
() => customer.deleted,
|
|
80
|
+
true,
|
|
81
|
+
);
|
|
82
|
+
customer._data.deleted = null;
|
|
83
|
+
SetterGetter(
|
|
84
|
+
(value: boolean) => (customer._data.deleted = value),
|
|
85
|
+
() => customer.deleted,
|
|
86
|
+
false,
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { AbstractRepository } from '#core/abstract-repository.js';
|
|
4
|
+
import { CustomerApiEndpoint } from '#customer/api-endpoint.js';
|
|
5
|
+
import { CustomerModel, IdCustomer } from '#customer/model.js';
|
|
6
|
+
import { CustomerRepository } from '#customer/repository.js';
|
|
7
|
+
import { customerValidator } from '#customer/validator.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 = 'cust';
|
|
15
|
+
const id = `${idPrefix}_${randomString(24)}` as IdCustomer;
|
|
16
|
+
const stancerErrorID = `${idPrefix}_unidentifiedError${randomString(7)}`;
|
|
17
|
+
const stancerRouteInaccessibleId = `${idPrefix}_StancerRouteInnacessible`;
|
|
18
|
+
const dataHasBeenSentId = `${idPrefix}_DataHasBeenSent${randomString(9)}`;
|
|
19
|
+
const dataUnmodified = `${idPrefix}_unmodifiedData${randomString(10)}`;
|
|
20
|
+
class MockApiEndpoint {
|
|
21
|
+
constructor() {}
|
|
22
|
+
list() {
|
|
23
|
+
async function* listGenerator() {
|
|
24
|
+
yield* listResource;
|
|
25
|
+
}
|
|
26
|
+
return listGenerator();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
delete() {
|
|
30
|
+
return endpointResource;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
send(id: string, data: Record<string, unknown>) {
|
|
34
|
+
if (id) {
|
|
35
|
+
if (id === stancerErrorID) {
|
|
36
|
+
throw new StancerError();
|
|
37
|
+
}
|
|
38
|
+
if (id === stancerRouteInaccessibleId) {
|
|
39
|
+
throw new StancerRouteInaccessibleError('PATCH', 'stub');
|
|
40
|
+
}
|
|
41
|
+
if (id === dataUnmodified) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { ...data, id: dataHasBeenSentId };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get() {
|
|
49
|
+
return endpointResource;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
it('can be instanciated', () => {
|
|
54
|
+
const instance = new CustomerRepository();
|
|
55
|
+
expect(instance).toBeInstanceOf(CustomerRepository);
|
|
56
|
+
expect(instance).toBeInstanceOf(AbstractRepository);
|
|
57
|
+
//accessing private method & property
|
|
58
|
+
expect(instance.endpoint).toBeInstanceOf(CustomerApiEndpoint);
|
|
59
|
+
expect(instance.validator).toBe(customerValidator);
|
|
60
|
+
expect(instance.staticModel).toBe(CustomerModel);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('repository', () => {
|
|
64
|
+
const mockRepo = new MockApiEndpoint() as unknown as CustomerApiEndpoint; // I lie, for convenience sake
|
|
65
|
+
const repository = new CustomerRepository(mockRepo);
|
|
66
|
+
|
|
67
|
+
describe('create', () => {
|
|
68
|
+
it("doesn't accept bad data", async () => {
|
|
69
|
+
const badID = randomString(30, 56);
|
|
70
|
+
// using puposefuly bad ID
|
|
71
|
+
await expect(async () => await repository.create(badID)).rejects.toThrow();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('accept good data', async () => {
|
|
75
|
+
const model = await repository.create(id);
|
|
76
|
+
expect(model).toBeInstanceOf(CustomerModel);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('delete', async () => {
|
|
81
|
+
const modelWithoutId = await repository.create();
|
|
82
|
+
await expect(() => repository.delete(modelWithoutId)).rejects.toThrow();
|
|
83
|
+
const model = await repository.create({ id: endpointResource.id, ...creationResource });
|
|
84
|
+
await expect((await repository.delete(model))._data).toStrictEqual(endpointResource);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('send', () => {
|
|
88
|
+
it('should return model', async () => {
|
|
89
|
+
const model = await repository.create(creationResource);
|
|
90
|
+
const sentModel = await repository.send(model);
|
|
91
|
+
expect(sentModel).toStrictEqual(model);
|
|
92
|
+
expect(sentModel._data.id).toEqual(dataHasBeenSentId);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should throw on inprecise error', async () => {
|
|
96
|
+
const model = await repository.create({ ...creationResource, id: stancerErrorID as IdCustomer });
|
|
97
|
+
await expect(() => repository.send(model)).rejects.toThrow(StancerError);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should return error on StancerRouteInacessibleError', async () => {
|
|
101
|
+
const model = await repository.create({ ...creationResource, id: stancerRouteInaccessibleId as IdCustomer });
|
|
102
|
+
await expect(async () => await repository.send(model)).rejects.toThrow(StancerRouteInaccessibleError);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should return unmodified model when no new Data', async () => {
|
|
106
|
+
const model = await repository.create({ ...creationResource, id: dataUnmodified as IdCustomer });
|
|
107
|
+
const sentModel = await repository.send(model);
|
|
108
|
+
expect(model).toStrictEqual(sentModel);
|
|
109
|
+
expect(sentModel._data.id).toEqual(dataUnmodified);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('populate', () => {
|
|
114
|
+
it('should not modify model if no Id', async () => {
|
|
115
|
+
const getEndpoint = vi.spyOn(mockRepo, 'get');
|
|
116
|
+
const model = await repository.create(creationResource);
|
|
117
|
+
expect(model).toBeInstanceOf(CustomerModel);
|
|
118
|
+
expect(getEndpoint).toHaveBeenCalledTimes(0);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should get model if only id', async () => {
|
|
122
|
+
const getEndpoint = vi.spyOn(mockRepo, 'get');
|
|
123
|
+
const model = await repository.create(id);
|
|
124
|
+
expect(model).toBeInstanceOf(CustomerModel);
|
|
125
|
+
expect(getEndpoint).toHaveBeenCalledTimes(1);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('list', async () => {
|
|
130
|
+
it('return correct model one by one', async () => {
|
|
131
|
+
const listStub = repository.list();
|
|
132
|
+
const item1 = await listStub.next();
|
|
133
|
+
expect(item1.done).toBeFalsy();
|
|
134
|
+
expect(item1.value).toHaveProperty('id');
|
|
135
|
+
expect(item1.value).toBeInstanceOf(CustomerModel);
|
|
136
|
+
expect(item1.value.name).toBe('item1');
|
|
137
|
+
|
|
138
|
+
const item2 = await listStub.next();
|
|
139
|
+
expect(item2.done).toBeFalsy();
|
|
140
|
+
expect(item2.value).toHaveProperty('id');
|
|
141
|
+
expect(item2.value).toBeInstanceOf(CustomerModel);
|
|
142
|
+
expect(item2.value.name).toBe('item2');
|
|
143
|
+
|
|
144
|
+
const item3 = await listStub.next();
|
|
145
|
+
expect(item3.done).toBeTruthy();
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { CustomerModel } from '#customer/model.js';
|
|
5
|
+
import { customerValidator } from '#customer/validator.js';
|
|
6
|
+
import { checkType, goldilocks, randomMonth, randomNumber, randomPastDate, randomString } from '#test/test-helper.js';
|
|
7
|
+
|
|
8
|
+
describe('Goldilocks', () => {
|
|
9
|
+
const customer = new CustomerModel(customerValidator);
|
|
10
|
+
|
|
11
|
+
it('Country', () => {
|
|
12
|
+
goldilocks((value: string) => customer.setCountry(value), 'string', { min: 2, max: 2 });
|
|
13
|
+
checkType((value: string) => customer.setCountry(value), ['string'], randomString(2, 2));
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('Email', () => {
|
|
17
|
+
goldilocks((value: string) => customer.setEmail(value), 'string', { min: 1, max: 64 });
|
|
18
|
+
checkType((value: string) => customer.setEmail(value), ['string'], randomString(1, 64));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('ExternalId', () => {
|
|
22
|
+
goldilocks((value: string) => customer.setExternalId(value), 'string', { min: 1, max: 36 });
|
|
23
|
+
checkType((value: string) => customer.setExternalId(value), ['string'], randomString(1, 36));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('LegalId', () => {
|
|
27
|
+
goldilocks((value: string) => customer.setLegalId(value), 'string', { min: 1, max: 16 });
|
|
28
|
+
checkType((value: string) => customer.setLegalId(value), ['string'], randomString(1, 16));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('Mobile', () => {
|
|
32
|
+
goldilocks((value: string) => customer.setMobile(value), 'string', { min: 1, max: 16 });
|
|
33
|
+
});
|
|
34
|
+
it('Name', () => {
|
|
35
|
+
goldilocks((value: string) => customer.setName(value), 'string', { min: 1, max: 64 });
|
|
36
|
+
checkType((value: string) => customer.setName(value), ['string'], randomString(3, 64));
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('DateBirth', () => {
|
|
40
|
+
const falseDateBirth = new Temporal.PlainDate(9999, randomMonth(), randomNumber(1, 30));
|
|
41
|
+
const DateBirth = randomPastDate();
|
|
42
|
+
expect(() => customer.setDateBirth(falseDateBirth)).toThrow();
|
|
43
|
+
customer.setDateBirth(DateBirth);
|
|
44
|
+
expect(customer.dateBirth).toBe(DateBirth);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('billingAddress', () => {
|
|
48
|
+
const falseAddress = randomString(1, 28);
|
|
49
|
+
const trueAddress = `addr_${randomString(24)}`;
|
|
50
|
+
expect(() => (customer._data.billingAddress = falseAddress)).toThrow();
|
|
51
|
+
customer._data.billingAddress = trueAddress;
|
|
52
|
+
expect(customer.billingAddress).toBe(trueAddress);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('shippingAddress', () => {
|
|
56
|
+
const falseAddress = randomString(1, 28);
|
|
57
|
+
const trueAddress = `addr_${randomString(24)}`;
|
|
58
|
+
expect(() => (customer._data.shippingAddress = falseAddress)).toThrow();
|
|
59
|
+
customer._data.shippingAddress = trueAddress;
|
|
60
|
+
expect(customer.shippingAddress).toBe(trueAddress);
|
|
61
|
+
});
|
|
62
|
+
});
|