@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,112 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { Configuration } from '#api/configuration.js';
|
|
5
|
+
import { idCard } from '#card/model.js';
|
|
6
|
+
import { PaymentRepository } from '#payment/repository.js';
|
|
7
|
+
import { RefundModel, idRefund, refundAmount } from '#refund/model.js';
|
|
8
|
+
import { RefundRepository } from '#refund/repository.js';
|
|
9
|
+
import { randomNumber, randomString } from '#test/test-helper.js';
|
|
10
|
+
|
|
11
|
+
const config = Configuration;
|
|
12
|
+
const amount = refundAmount(randomNumber(1, 50));
|
|
13
|
+
const paymentRepository = new PaymentRepository();
|
|
14
|
+
|
|
15
|
+
describe('refund functional', async () => {
|
|
16
|
+
config.setKey({
|
|
17
|
+
privateLiveKey: null,
|
|
18
|
+
privateTestKey: process.env['API_KEY'] ?? null,
|
|
19
|
+
});
|
|
20
|
+
config._host = process.env['API_HOST'] ?? 'api.stancer.com';
|
|
21
|
+
|
|
22
|
+
const repository = new RefundRepository();
|
|
23
|
+
describe('refund post', async () => {
|
|
24
|
+
const payment = await paymentRepository.create();
|
|
25
|
+
payment.setCard(idCard('card_Y2QKuArL3x7A18OjLZdSizoI'));
|
|
26
|
+
payment.setAmount(1000);
|
|
27
|
+
payment.setCurrency('eur');
|
|
28
|
+
await paymentRepository.send(payment);
|
|
29
|
+
it('create a refund with id', { concurrent: false }, async () => {
|
|
30
|
+
const refund = await repository.create({ amount, payment: payment.id });
|
|
31
|
+
await repository.send(refund);
|
|
32
|
+
expect(refund.id).toBeDefined();
|
|
33
|
+
expect(refund.id).toContain('refd_');
|
|
34
|
+
expect(refund.amount).toBe(amount);
|
|
35
|
+
expect(refund.currency).toBeDefined();
|
|
36
|
+
expect(refund.status).toBeDefined();
|
|
37
|
+
await paymentRepository.populateRefunds(payment);
|
|
38
|
+
expect(payment.refundedAmount).toBe(refund.amount);
|
|
39
|
+
});
|
|
40
|
+
it('create a refund with paymentModel', { concurrent: false }, async () => {
|
|
41
|
+
const refund = await repository.create({ amount, payment: payment.id });
|
|
42
|
+
await repository.send(refund);
|
|
43
|
+
expect(refund.id).toBeDefined();
|
|
44
|
+
expect(refund.id).toContain('refd_');
|
|
45
|
+
expect(refund.amount).toBe(amount);
|
|
46
|
+
expect(refund.currency).toBeDefined();
|
|
47
|
+
expect(refund.status).toBeDefined();
|
|
48
|
+
await paymentRepository.populateRefunds(payment);
|
|
49
|
+
expect(payment.refundedAmount).toBe(amount * 2);
|
|
50
|
+
});
|
|
51
|
+
it('need a payment', async () => {
|
|
52
|
+
const amount = refundAmount(randomNumber(1, 50));
|
|
53
|
+
const refund = await repository.create({ amount });
|
|
54
|
+
await expect(() => repository.send(refund)).rejects.toThrow();
|
|
55
|
+
});
|
|
56
|
+
it('need an amount', async () => {
|
|
57
|
+
const refund = await repository.create({ payment: payment.id });
|
|
58
|
+
await expect(() => repository.send(refund)).rejects.toThrow();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
describe('refund other verbs', async () => {
|
|
62
|
+
const payment = await paymentRepository.create();
|
|
63
|
+
payment.setCard(idCard('card_Y2QKuArL3x7A18OjLZdSizoI'));
|
|
64
|
+
payment.setAmount(1000);
|
|
65
|
+
payment.setCurrency('eur');
|
|
66
|
+
await paymentRepository.send(payment);
|
|
67
|
+
const repository = new RefundRepository();
|
|
68
|
+
it('cannot patch', async () => {
|
|
69
|
+
const amount = refundAmount(randomNumber(1, 50));
|
|
70
|
+
const refund = await repository.create({ amount, payment: payment.id });
|
|
71
|
+
await repository.send(refund);
|
|
72
|
+
refund.setAmount(refundAmount(200));
|
|
73
|
+
|
|
74
|
+
await expect(async () => await repository.send(refund)).rejects.toThrow();
|
|
75
|
+
});
|
|
76
|
+
it('cannot delete', async () => {
|
|
77
|
+
const amount = refundAmount(randomNumber(1, 50));
|
|
78
|
+
const refund = await repository.create({ amount, payment: payment.id });
|
|
79
|
+
await repository.send(refund);
|
|
80
|
+
await expect(async () => await repository.delete(refund)).rejects.toThrow();
|
|
81
|
+
});
|
|
82
|
+
it('list', async () => {
|
|
83
|
+
const expectedCount = 3;
|
|
84
|
+
const refunds = repository.list({ limit: expectedCount });
|
|
85
|
+
let length = 0;
|
|
86
|
+
for await (const item of refunds) {
|
|
87
|
+
expect(item).toBeInstanceOf(RefundModel);
|
|
88
|
+
expect(item.amount).toBeDefined();
|
|
89
|
+
expect(item.currency).toEqual('eur');
|
|
90
|
+
length++;
|
|
91
|
+
if (length > expectedCount) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
expect(length).toBe(expectedCount);
|
|
96
|
+
});
|
|
97
|
+
it('get', async () => {
|
|
98
|
+
const id = idRefund('refd_07vi5SIqsierzO8nuaOFBX7v');
|
|
99
|
+
const refund = await repository.create(id);
|
|
100
|
+
expect(refund.id).toBe(id);
|
|
101
|
+
expect(refund.amount).toBe(44);
|
|
102
|
+
expect(refund.currency).toBe('eur');
|
|
103
|
+
expect(refund.createdAt).toStrictEqual(Temporal.Instant.fromEpochMilliseconds(1788350401 * 1000));
|
|
104
|
+
expect(refund.payment).toBe('paym_tGY2hPlbr33GDEnfC0ZyQA92');
|
|
105
|
+
expect(refund.status).toBe('to_refund');
|
|
106
|
+
});
|
|
107
|
+
it('get a 404', async () => {
|
|
108
|
+
const id = `refd_${randomString(24)}`;
|
|
109
|
+
await expect(() => repository.create(id)).rejects.toThrow();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
import 'temporal-polyfill/global';
|
|
3
|
+
import { describe, expect, it } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import { Configuration } from '#api/configuration.js';
|
|
6
|
+
import { IdSepa } from '#sepa/model.js';
|
|
7
|
+
import { SepaRepository } from '#sepa/repository.js';
|
|
8
|
+
import { randomMonth, randomNumber, randomString } from '#test/test-helper.js';
|
|
9
|
+
import { bicDataProvider, validIbanDataProvider } from '#test/unit/sepa/sepa-helper.js';
|
|
10
|
+
|
|
11
|
+
import { idCustomer } from '../../src/customer/model';
|
|
12
|
+
|
|
13
|
+
const config = Configuration;
|
|
14
|
+
|
|
15
|
+
config.setKey({
|
|
16
|
+
privateLiveKey: null,
|
|
17
|
+
privateTestKey: process.env['API_KEY'] ?? null,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
config._host = process.env['API_HOST'] ?? 'api.stancer.com';
|
|
21
|
+
|
|
22
|
+
describe('sepa functional', () => {
|
|
23
|
+
describe('sepa creation', () => {
|
|
24
|
+
const iban = validIbanDataProvider(true);
|
|
25
|
+
const name = randomString(3, 64);
|
|
26
|
+
|
|
27
|
+
it('create an sepa with minimum information', async () => {
|
|
28
|
+
const repository = new SepaRepository();
|
|
29
|
+
|
|
30
|
+
const sepa = await repository.create({ iban, name });
|
|
31
|
+
await repository.send(sepa);
|
|
32
|
+
expect(sepa.id).toBeTruthy();
|
|
33
|
+
expect(sepa.id).toContain('sepa_');
|
|
34
|
+
expect(sepa.name).toBe(name);
|
|
35
|
+
expect(sepa.last4).toBe(iban.slice(iban.length - 4));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('create a complete flat sepa', async () => {
|
|
39
|
+
const repository = new SepaRepository();
|
|
40
|
+
const mandate = randomString(1, 35);
|
|
41
|
+
const name = randomString(3, 64);
|
|
42
|
+
const dateMandate = Temporal.PlainDate.from(
|
|
43
|
+
`${String(randomNumber(1971, 2020))}-${String(randomMonth()).padStart(2, '0')}-${String(randomNumber(1, 28)).padStart(2, '0')}`,
|
|
44
|
+
);
|
|
45
|
+
const iban = validIbanDataProvider(true) as string;
|
|
46
|
+
const bic = bicDataProvider(true) as string;
|
|
47
|
+
const customer = idCustomer('cust_SsySBRtGUq3pG9zbSOhJSKCa');
|
|
48
|
+
const last4 = iban.slice(iban.length - 4);
|
|
49
|
+
|
|
50
|
+
const sepa = await repository.create({
|
|
51
|
+
mandate,
|
|
52
|
+
name,
|
|
53
|
+
dateMandate,
|
|
54
|
+
iban,
|
|
55
|
+
bic,
|
|
56
|
+
customer,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
await repository.send(sepa);
|
|
60
|
+
|
|
61
|
+
expect(sepa.id).toBeTruthy();
|
|
62
|
+
|
|
63
|
+
expect(sepa._data.mandate).toBe(sepa.mandate);
|
|
64
|
+
expect(sepa._data.dateMandate).toBe(sepa.dateMandate);
|
|
65
|
+
expect(sepa._data.last4).toBe(last4);
|
|
66
|
+
expect(sepa._data.name).toBe(sepa.name);
|
|
67
|
+
expect(sepa._data.bic).toBe(sepa.bic);
|
|
68
|
+
});
|
|
69
|
+
it('need required name', async () => {
|
|
70
|
+
const iban = validIbanDataProvider(true) as string;
|
|
71
|
+
|
|
72
|
+
const repository = new SepaRepository();
|
|
73
|
+
|
|
74
|
+
const sepaWithoutRequiredValue = await repository.create({ iban });
|
|
75
|
+
|
|
76
|
+
await expect(async () => await repository.send(sepaWithoutRequiredValue)).rejects.toThrow(
|
|
77
|
+
'name is required to send SepaModel',
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
it('need required iban', async () => {
|
|
81
|
+
const name = randomString(3, 64);
|
|
82
|
+
|
|
83
|
+
const repository = new SepaRepository();
|
|
84
|
+
|
|
85
|
+
const sepaWithoutRequiredValue = await repository.create({ name });
|
|
86
|
+
|
|
87
|
+
await expect(async () => await repository.send(sepaWithoutRequiredValue)).rejects.toThrow(
|
|
88
|
+
'iban is required to send SepaModel',
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('patch', () => {
|
|
93
|
+
it('is available on sepa', async () => {
|
|
94
|
+
const repository = new SepaRepository();
|
|
95
|
+
const name = randomString(3, 64);
|
|
96
|
+
const iban = validIbanDataProvider(true) as string;
|
|
97
|
+
|
|
98
|
+
const sepa = await repository.create({ name, iban });
|
|
99
|
+
|
|
100
|
+
await repository.send(sepa);
|
|
101
|
+
const id = sepa.id;
|
|
102
|
+
expect(id).toBeTruthy();
|
|
103
|
+
expect(sepa.last4).toBe(iban.slice(iban.length - 4));
|
|
104
|
+
expect(sepa.name).toBe(name);
|
|
105
|
+
|
|
106
|
+
const newName = randomString(3, 64);
|
|
107
|
+
sepa.setName(newName);
|
|
108
|
+
await repository.send(sepa);
|
|
109
|
+
expect(id).toBe(sepa.id);
|
|
110
|
+
expect(sepa.name).toBe(newName);
|
|
111
|
+
expect(sepa.last4).toBe(iban.slice(iban.length - 4));
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('get', () => {
|
|
116
|
+
it('get the correct object', async () => {
|
|
117
|
+
const repository = new SepaRepository();
|
|
118
|
+
const id = 'sepa_XbEkAt8hAKTbh6Yel7xjEk7O' as IdSepa;
|
|
119
|
+
const sepa = await repository.create(id);
|
|
120
|
+
expect(sepa.id).toBe(id);
|
|
121
|
+
expect(sepa.bic).toBe('TESTFRPP');
|
|
122
|
+
expect(sepa.last4).toBe('2606');
|
|
123
|
+
expect(sepa.dateMandate).toEqual(Temporal.PlainDate.from('2024-12-25'));
|
|
124
|
+
expect(sepa.mandate).toBe('libraries_mandate_id');
|
|
125
|
+
expect(sepa.name).toBe('Patrice Dinde');
|
|
126
|
+
expect(sepa.createdAt.epochMilliseconds).toBe(1758551637000);
|
|
127
|
+
expect(sepa.deleted).toBe(false);
|
|
128
|
+
});
|
|
129
|
+
it('get a 404 exception if unknown', async () => {
|
|
130
|
+
const id = 'sepa_' + randomString(24);
|
|
131
|
+
const repository = new SepaRepository();
|
|
132
|
+
await expect(() => repository.create(id)).rejects.toThrow();
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { StancerError } from '#core/error/base-error.js';
|
|
4
|
+
import { stubSerialized } from '#test/fixtures/endpoint.stub.js';
|
|
5
|
+
import { StubEndpoint } from '#test/stub/stub-endpoint.js';
|
|
6
|
+
import { StubRepository } from '#test/stub/stub-repository.js';
|
|
7
|
+
import { randomString } from '#test/test-helper.js';
|
|
8
|
+
|
|
9
|
+
vi.mock('#api/http/fetch-http-client.js', () => {
|
|
10
|
+
return {
|
|
11
|
+
httpClient: {
|
|
12
|
+
makeRequest: vi.fn(() => {
|
|
13
|
+
return Promise.resolve({
|
|
14
|
+
status: 200,
|
|
15
|
+
json: () => {
|
|
16
|
+
return {
|
|
17
|
+
...stubSerialized,
|
|
18
|
+
} as Record<string, unknown>;
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}),
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
describe('stub', () => {
|
|
26
|
+
describe('send', () => {
|
|
27
|
+
it("doesn't resend previously sent object construction", async () => {
|
|
28
|
+
const endpoint = new StubEndpoint();
|
|
29
|
+
const spyonPatch = vi.spyOn(endpoint, 'patch');
|
|
30
|
+
const spyonPost = vi.spyOn(endpoint, 'post');
|
|
31
|
+
const defaultValue = randomString(12);
|
|
32
|
+
const repository = new StubRepository(endpoint);
|
|
33
|
+
const model = await repository.create({ default: defaultValue, allAuthorized: true });
|
|
34
|
+
expect(model.mutations).toStrictEqual({ default: defaultValue, allAuthorized: true });
|
|
35
|
+
|
|
36
|
+
await repository.send(model);
|
|
37
|
+
expect(spyonPatch).toHaveBeenCalledTimes(0);
|
|
38
|
+
expect(spyonPost).toHaveBeenCalledTimes(1);
|
|
39
|
+
|
|
40
|
+
expect(model.mutations).toStrictEqual({});
|
|
41
|
+
await expect(() => repository.send(model)).rejects.toThrow(StancerError);
|
|
42
|
+
|
|
43
|
+
expect(spyonPatch).toHaveBeenCalledTimes(0);
|
|
44
|
+
expect(spyonPost).toHaveBeenCalledTimes(1);
|
|
45
|
+
});
|
|
46
|
+
it("doesn't resend previously sent object setting", async () => {
|
|
47
|
+
const endpoint = new StubEndpoint();
|
|
48
|
+
const spyonPatch = vi.spyOn(endpoint, 'patch');
|
|
49
|
+
const spyonPost = vi.spyOn(endpoint, 'post');
|
|
50
|
+
const defaultValue = randomString(12);
|
|
51
|
+
const repository = new StubRepository(endpoint);
|
|
52
|
+
const model = await repository.create();
|
|
53
|
+
model.setDefault(defaultValue);
|
|
54
|
+
expect(model.mutations).toStrictEqual({ default: defaultValue });
|
|
55
|
+
|
|
56
|
+
await repository.send(model);
|
|
57
|
+
expect(spyonPatch).toHaveBeenCalledTimes(0);
|
|
58
|
+
expect(spyonPost).toHaveBeenCalledTimes(1);
|
|
59
|
+
expect(model.mutations).toStrictEqual({});
|
|
60
|
+
|
|
61
|
+
await expect(() => repository.send(model)).rejects.toThrow(StancerError);
|
|
62
|
+
expect(spyonPatch).toHaveBeenCalledTimes(0);
|
|
63
|
+
expect(spyonPost).toHaveBeenCalledTimes(1);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
it("doesn't repatch", async () => {
|
|
67
|
+
const endpoint = new StubEndpoint();
|
|
68
|
+
const spyonPatch = vi.spyOn(endpoint, 'patch');
|
|
69
|
+
const spyonPost = vi.spyOn(endpoint, 'post');
|
|
70
|
+
const id = 'stub_' + randomString(24);
|
|
71
|
+
const repository = new StubRepository(endpoint);
|
|
72
|
+
const model = await repository.create(id);
|
|
73
|
+
model.setAllAuthorized(true);
|
|
74
|
+
expect(model.mutations).toStrictEqual({});
|
|
75
|
+
|
|
76
|
+
model.setAllAuthorized(false);
|
|
77
|
+
expect(model.mutations).toStrictEqual({ allAuthorized: false });
|
|
78
|
+
|
|
79
|
+
await repository.send(model);
|
|
80
|
+
expect(spyonPatch).toHaveBeenCalledOnce();
|
|
81
|
+
expect(spyonPost).toHaveBeenCalledTimes(0);
|
|
82
|
+
|
|
83
|
+
expect(model.mutations).toStrictEqual({});
|
|
84
|
+
await expect(() => repository.send(model)).rejects.toThrow(StancerError);
|
|
85
|
+
expect(spyonPatch).toHaveBeenCalledOnce();
|
|
86
|
+
expect(spyonPost).toHaveBeenCalledTimes(0);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ApiEndpoint, Restriction, baseHandler } from '#api/abstract-api-endpoint.js';
|
|
2
|
+
import { ModelType } from '#core/abstract-model.js';
|
|
3
|
+
import { IdValue } from '#core/common/branded-type.js';
|
|
4
|
+
import { defaultHandler } from '#core/common/handler/handler.js';
|
|
5
|
+
import { IdHandler } from '#core/common/handler/id-handler.js';
|
|
6
|
+
|
|
7
|
+
export interface StubData extends ModelType {
|
|
8
|
+
id: IdValue;
|
|
9
|
+
default: string;
|
|
10
|
+
defaultNumber: number;
|
|
11
|
+
untouchable: boolean;
|
|
12
|
+
allAuthorized: boolean;
|
|
13
|
+
ungettable: boolean;
|
|
14
|
+
unmodifiable: boolean;
|
|
15
|
+
}
|
|
16
|
+
const StubModel = { regex: /^stub_[a-zA-Z0-9]{24}$/ };
|
|
17
|
+
export class StubEndpoint extends ApiEndpoint<StubData> {
|
|
18
|
+
protected _handler = {
|
|
19
|
+
id: IdHandler(StubModel.regex),
|
|
20
|
+
default: defaultHandler<StubData['default']>('string'),
|
|
21
|
+
defaultNumber: defaultHandler<StubData['defaultNumber']>('number'),
|
|
22
|
+
untouchable: defaultHandler<StubData['untouchable']>('boolean', {
|
|
23
|
+
gettable: false,
|
|
24
|
+
postable: false,
|
|
25
|
+
patchable: false,
|
|
26
|
+
}),
|
|
27
|
+
allAuthorized: defaultHandler<StubData['allAuthorized']>('boolean'),
|
|
28
|
+
ungettable: defaultHandler<StubData['ungettable']>('boolean', { gettable: false }),
|
|
29
|
+
unmodifiable: defaultHandler<StubData['unmodifiable']>('boolean', { postable: false, patchable: false }),
|
|
30
|
+
...baseHandler('created'),
|
|
31
|
+
};
|
|
32
|
+
constructor(restriction?: Restriction) {
|
|
33
|
+
super({
|
|
34
|
+
name: 'stubs',
|
|
35
|
+
...restriction, // We should not create a new object with variable restriction but it is more practical for testing.
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
endpointUrl(id?: IdValue) {
|
|
40
|
+
return this._getResourceUri(id);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { AbstractModel, type ModelData } from '#core/abstract-model.js';
|
|
2
|
+
import { IdValue } from '#core/common/branded-type.js';
|
|
3
|
+
import { isValidID } from '#core/common/id-validator.js';
|
|
4
|
+
import { Validator, createdAtValidator } from '#core/validator.js';
|
|
5
|
+
|
|
6
|
+
//#region Data interface
|
|
7
|
+
|
|
8
|
+
export type IdStub = IdValue<`stub_${string}`>;
|
|
9
|
+
export const STUB_DEFAULT_MAX_LENGTH = 64;
|
|
10
|
+
export const STUB_DEFAULT_NUMBER_MAX_LENGTH = 264;
|
|
11
|
+
export interface StubData extends ModelData {
|
|
12
|
+
id: IdValue;
|
|
13
|
+
default: string;
|
|
14
|
+
defaultNumber: number;
|
|
15
|
+
untouchable: boolean;
|
|
16
|
+
allAuthorized: boolean;
|
|
17
|
+
ungettable: boolean;
|
|
18
|
+
unmodifiable: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region validator
|
|
23
|
+
export const StubValidator: Validator<StubData> = {
|
|
24
|
+
id: {
|
|
25
|
+
validate: (arg0) => isValidID<IdStub>(arg0, StubModel.regex),
|
|
26
|
+
errorMessage: (value: string) => `The id (${value}) doesn't verify the regex ${StubModel.regex}`,
|
|
27
|
+
},
|
|
28
|
+
default: {
|
|
29
|
+
validate: (arg0) => arg0.length < STUB_DEFAULT_MAX_LENGTH,
|
|
30
|
+
errorMessage: (value: string) => `The length of value (${value.length}) is too long`,
|
|
31
|
+
},
|
|
32
|
+
defaultNumber: {
|
|
33
|
+
validate: (arg0) => arg0 < STUB_DEFAULT_NUMBER_MAX_LENGTH,
|
|
34
|
+
errorMessage: (value: string) => `DefaultNumber is too big of a number (${value})`,
|
|
35
|
+
},
|
|
36
|
+
ungettable: true,
|
|
37
|
+
unmodifiable: true,
|
|
38
|
+
untouchable: true,
|
|
39
|
+
allAuthorized: true,
|
|
40
|
+
createdAt: createdAtValidator,
|
|
41
|
+
deleted: true,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
|
|
46
|
+
export class StubModel extends AbstractModel<StubData> {
|
|
47
|
+
//#region properties
|
|
48
|
+
static regex = /^stub_[a-zA-Z0-9]{24}$/;
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region setter & getter
|
|
52
|
+
|
|
53
|
+
get allAuthorized() {
|
|
54
|
+
return this._data.allAuthorized;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
setAllAuthorized(value: boolean) {
|
|
58
|
+
this._data.allAuthorized = value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get default() {
|
|
62
|
+
return this._data.default;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setDefault(value: string) {
|
|
66
|
+
this._data.default = value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get defaultNumber() {
|
|
70
|
+
return this._data.defaultNumber;
|
|
71
|
+
}
|
|
72
|
+
setDefaultNumber(value: number) {
|
|
73
|
+
this._data.defaultNumber = value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
setUngettable(value: boolean) {
|
|
77
|
+
this._data.ungettable = value;
|
|
78
|
+
}
|
|
79
|
+
get unmodifiable() {
|
|
80
|
+
return this._data.unmodifiable;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AbstractRepository, type Repository } from '#core/abstract-repository.js';
|
|
2
|
+
import { EndpointInterface } from '#core/endpoint-interface.js';
|
|
3
|
+
|
|
4
|
+
import { type StubData, StubEndpoint } from './stub-endpoint.js';
|
|
5
|
+
import { StubModel, StubValidator } from './stub-model.js';
|
|
6
|
+
|
|
7
|
+
export class StubRepository extends AbstractRepository<StubData, StubModel> implements Repository<StubData, StubModel> {
|
|
8
|
+
constructor(endpoint?: EndpointInterface<StubData>) {
|
|
9
|
+
super({ endpoint: endpoint ?? new StubEndpoint(), validator: StubValidator, model: StubModel });
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
import { expect, vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { Http } from '#api/http/index.js';
|
|
5
|
+
import { ID_SUFFIX_LENGTH } from '#core/common/id-validator.js';
|
|
6
|
+
|
|
7
|
+
type Boundaries = { min?: number; max?: number };
|
|
8
|
+
|
|
9
|
+
export const checkType = (
|
|
10
|
+
setter: CallableFunction,
|
|
11
|
+
type: ('string' | 'object' | 'number' | 'boolean')[],
|
|
12
|
+
validValue: unknown,
|
|
13
|
+
) => {
|
|
14
|
+
if (!type.includes('object')) {
|
|
15
|
+
expect(() => setter({})).toThrow();
|
|
16
|
+
}
|
|
17
|
+
if (!type.includes('string')) {
|
|
18
|
+
expect(() => setter('a')).toThrow();
|
|
19
|
+
}
|
|
20
|
+
if (!type.includes('number')) {
|
|
21
|
+
expect(() => setter(0)).toThrow();
|
|
22
|
+
}
|
|
23
|
+
if (!type.includes('boolean')) {
|
|
24
|
+
expect(() => setter(true)).toThrow();
|
|
25
|
+
}
|
|
26
|
+
expect(() => setter(validValue)).not.toThrow();
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const goldilocks = (validation: CallableFunction, type: 'number' | 'string', boundaries: Boundaries) => {
|
|
30
|
+
const randomFunction = type === 'number' ? randomNumber : randomString;
|
|
31
|
+
|
|
32
|
+
const min = boundaries?.min !== undefined && boundaries.min > 0 ? boundaries.min : 0;
|
|
33
|
+
const max = boundaries?.max;
|
|
34
|
+
if (max !== undefined) {
|
|
35
|
+
expect(() => validation(randomFunction(max + 1, 100000))).toThrow();
|
|
36
|
+
}
|
|
37
|
+
if (min) {
|
|
38
|
+
expect(() => validation(randomFunction(0, min - 1))).toThrow();
|
|
39
|
+
}
|
|
40
|
+
expect(() => validation(randomFunction(min ?? 0, max ?? 100))).not.toThrow();
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const goldilocksId = (setter: CallableFunction, prefix: string) => {
|
|
44
|
+
const IDLength = prefix.length + ID_SUFFIX_LENGTH;
|
|
45
|
+
expect.soft(() => setter(randomString(1, IDLength - 1))).toThrow();
|
|
46
|
+
expect.soft(() => setter(randomString(IDLength + 1, 50))).toThrow();
|
|
47
|
+
expect.soft(() => setter(randomString(IDLength))).toThrow();
|
|
48
|
+
expect.soft(() => setter(prefix + randomString(ID_SUFFIX_LENGTH))).not.toThrow();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
* @param resolver
|
|
54
|
+
* @returns cleanup function that must be called after using mockfetch to reinitialise the global stub
|
|
55
|
+
*/
|
|
56
|
+
export const mockFetch = (resolver: <T = unknown>(arg0: string, arg1: Http.Request) => T): (() => void) => {
|
|
57
|
+
vi.stubGlobal(
|
|
58
|
+
'fetch',
|
|
59
|
+
vi.fn().mockImplementation((url: string, req: Http.Request) => {
|
|
60
|
+
return Promise.resolve({
|
|
61
|
+
ok: true,
|
|
62
|
+
json: () => Promise.resolve(resolver(url, req)),
|
|
63
|
+
status: 200,
|
|
64
|
+
statusText: 'OK',
|
|
65
|
+
clone: function () {
|
|
66
|
+
return { ...this };
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}),
|
|
70
|
+
);
|
|
71
|
+
return () => vi.unstubAllGlobals();
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const randomCVC = (): string => {
|
|
75
|
+
return randomString(3, 3, '0123456789');
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const randomDate = (): Temporal.PlainDate => {
|
|
79
|
+
return new Temporal.PlainDate(randomNumber(2000, 2099), randomMonth(), randomNumber(1, 28));
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export const randomExpYear = (): number => {
|
|
83
|
+
const date = new Date();
|
|
84
|
+
const min = date.getFullYear() + 1;
|
|
85
|
+
return randomNumber(min, 2099);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const randomMonth = (): number => {
|
|
89
|
+
return randomNumber(1, 12);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export const randomNumber = (min: number, max?: number): number => {
|
|
93
|
+
if (!max) {
|
|
94
|
+
max = min;
|
|
95
|
+
}
|
|
96
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export const randomPastDate = (): Temporal.PlainDate => {
|
|
100
|
+
const date = new Date();
|
|
101
|
+
const max = date.getFullYear() - 1;
|
|
102
|
+
const pastYear = randomNumber(1900, max);
|
|
103
|
+
return new Temporal.PlainDate(pastYear, randomMonth(), randomNumber(1, 28));
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export const randomString = (
|
|
107
|
+
min: number,
|
|
108
|
+
max?: number,
|
|
109
|
+
allowedString = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
110
|
+
): string => {
|
|
111
|
+
const stringLength = randomNumber(min, max);
|
|
112
|
+
let string = '';
|
|
113
|
+
for (let i = 0; i < stringLength; i++) {
|
|
114
|
+
string = string.concat(allowedString.charAt(randomNumber(0, allowedString.length - 1)));
|
|
115
|
+
}
|
|
116
|
+
return string;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export const SetterGetter = (
|
|
120
|
+
callable: CallableFunction,
|
|
121
|
+
getter: CallableFunction,
|
|
122
|
+
entryValue: unknown,
|
|
123
|
+
exitValue?: unknown,
|
|
124
|
+
) => {
|
|
125
|
+
expect(getter()).toBeNullable();
|
|
126
|
+
callable(entryValue);
|
|
127
|
+
expect(getter()).toStrictEqual(exitValue ?? entryValue);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export const SetterGetterId = (Instanciation: CallableFunction, value: unknown = undefined) => {
|
|
131
|
+
const object = Instanciation(value);
|
|
132
|
+
expect(object.id).toStrictEqual(value);
|
|
133
|
+
};
|