@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.
Files changed (139) hide show
  1. package/.editorconfig +14 -0
  2. package/.gitlab-ci.yml +159 -0
  3. package/.prettierignore +3 -0
  4. package/CHANGELOG.md +10 -0
  5. package/LICENSE +19 -0
  6. package/README.md +166 -2
  7. package/eslint.config.js +14 -0
  8. package/package.json +52 -3
  9. package/pnpm-workspace.yaml +19 -0
  10. package/prettier.config.js +3 -0
  11. package/src/address/api-endpoint.ts +30 -0
  12. package/src/address/model.ts +79 -0
  13. package/src/address/repository.ts +18 -0
  14. package/src/address/validator.ts +34 -0
  15. package/src/api-endpoint/abstract-api-endpoint.ts +310 -0
  16. package/src/api-endpoint/configuration.ts +125 -0
  17. package/src/api-endpoint/http/fetch-http-client.ts +29 -0
  18. package/src/api-endpoint/http/http.ts +75 -0
  19. package/src/api-endpoint/http/index.ts +2 -0
  20. package/src/api-endpoint/version.ts +4 -0
  21. package/src/card/api-endpoint.ts +39 -0
  22. package/src/card/enum/funding.ts +11 -0
  23. package/src/card/enum/nature.ts +7 -0
  24. package/src/card/enum/network.ts +16 -0
  25. package/src/card/model.ts +139 -0
  26. package/src/card/repository.ts +11 -0
  27. package/src/card/validator.ts +79 -0
  28. package/src/core/abstract-model.ts +126 -0
  29. package/src/core/abstract-repository.ts +201 -0
  30. package/src/core/common/branded-type.ts +15 -0
  31. package/src/core/common/handler/date-handler.ts +31 -0
  32. package/src/core/common/handler/enum-handler.ts +18 -0
  33. package/src/core/common/handler/handler.ts +64 -0
  34. package/src/core/common/handler/id-handler.ts +27 -0
  35. package/src/core/common/id-validator.ts +23 -0
  36. package/src/core/common/mutation-tracked-object.ts +49 -0
  37. package/src/core/endpoint-interface.ts +43 -0
  38. package/src/core/error/base-error.ts +1 -0
  39. package/src/core/error/endpoint/api/api-conflict-error.ts +7 -0
  40. package/src/core/error/endpoint/api/api-error.ts +10 -0
  41. package/src/core/error/endpoint/api/api-not-found-error.ts +7 -0
  42. package/src/core/error/endpoint/api/api-unauthorized-error.ts +7 -0
  43. package/src/core/error/endpoint/api/api-unknown-error.ts +7 -0
  44. package/src/core/error/endpoint/api/api-validation-error.ts +13 -0
  45. package/src/core/error/endpoint/api/index.ts +6 -0
  46. package/src/core/error/endpoint/route-inaccessible-error.ts +8 -0
  47. package/src/core/error/unpopulated-error.ts +3 -0
  48. package/src/core/error/unsendable-error.ts +3 -0
  49. package/src/core/error/validation-error.ts +3 -0
  50. package/src/core/validator.ts +129 -0
  51. package/src/customer/api-endpoint.ts +29 -0
  52. package/src/customer/model.ts +113 -0
  53. package/src/customer/repository.ts +14 -0
  54. package/src/customer/validator.ts +26 -0
  55. package/src/payment/api-endpoint.ts +118 -0
  56. package/src/payment/enum/currency.ts +14 -0
  57. package/src/payment/enum/method.ts +2 -0
  58. package/src/payment/enum/status.ts +21 -0
  59. package/src/payment/model.ts +378 -0
  60. package/src/payment/repository.ts +73 -0
  61. package/src/payment/validator.ts +96 -0
  62. package/src/payment-intent/api-endpoint.ts +123 -0
  63. package/src/payment-intent/enum/status.ts +21 -0
  64. package/src/payment-intent/enum/threeds.ts +2 -0
  65. package/src/payment-intent/model.ts +266 -0
  66. package/src/payment-intent/repository.ts +85 -0
  67. package/src/payment-intent/validator.ts +45 -0
  68. package/src/payout/model.ts +8 -0
  69. package/src/refund/api-endpoint.ts +33 -0
  70. package/src/refund/enum/refund-status.ts +11 -0
  71. package/src/refund/model.ts +90 -0
  72. package/src/refund/repository.ts +14 -0
  73. package/src/refund/validator.ts +44 -0
  74. package/src/sepa/api-endpoint.ts +26 -0
  75. package/src/sepa/model.ts +80 -0
  76. package/src/sepa/repository.ts +11 -0
  77. package/src/sepa/validator.ts +56 -0
  78. package/test/fixtures/endpoint.stub.ts +22 -0
  79. package/test/fixtures/list-endpoint.stub.ts +33 -0
  80. package/test/fixtures/list-model.stub.ts +24 -0
  81. package/test/fixtures/model.stub.ts +12 -0
  82. package/test/functional/address.test.ts +126 -0
  83. package/test/functional/card.test.ts +175 -0
  84. package/test/functional/customer.test.ts +174 -0
  85. package/test/functional/payment-intent.test.ts +215 -0
  86. package/test/functional/payment.test.ts +206 -0
  87. package/test/functional/refund.test.ts +112 -0
  88. package/test/functional/sepa.test.ts +136 -0
  89. package/test/integration/stub-integration.test.ts +88 -0
  90. package/test/stub/stub-endpoint.ts +42 -0
  91. package/test/stub/stub-model.ts +82 -0
  92. package/test/stub/stub-repository.ts +11 -0
  93. package/test/test-helper.ts +133 -0
  94. package/test/unit/address/api-endpoint.test.ts +134 -0
  95. package/test/unit/address/fixtures.ts +60 -0
  96. package/test/unit/address/model.test.ts +75 -0
  97. package/test/unit/address/repository.test.ts +148 -0
  98. package/test/unit/address/validator.test.ts +49 -0
  99. package/test/unit/api-endpoint/http/fetch-http-client.test.ts +44 -0
  100. package/test/unit/api-endpoint/http/http.test.ts +152 -0
  101. package/test/unit/card/api-endpoint.test.ts +170 -0
  102. package/test/unit/card/card-helper.ts +116 -0
  103. package/test/unit/card/fixtures.ts +87 -0
  104. package/test/unit/card/model.test.ts +167 -0
  105. package/test/unit/card/repository.test.ts +146 -0
  106. package/test/unit/card/validator.test.ts +96 -0
  107. package/test/unit/core/common/mutation-tracked-object.test.ts +24 -0
  108. package/test/unit/customer/api-endpoint.test.ts +143 -0
  109. package/test/unit/customer/fixtures.ts +73 -0
  110. package/test/unit/customer/model.test.ts +90 -0
  111. package/test/unit/customer/repository.test.ts +148 -0
  112. package/test/unit/customer/validator.test.ts +62 -0
  113. package/test/unit/payment/api-endpoint.test.ts +188 -0
  114. package/test/unit/payment/fixtures.ts +116 -0
  115. package/test/unit/payment/model.test.ts +240 -0
  116. package/test/unit/payment/repository.test.ts +270 -0
  117. package/test/unit/payment/validator.test.ts +137 -0
  118. package/test/unit/payment-intent/api-endpoint.test.ts +142 -0
  119. package/test/unit/payment-intent/fixtures.ts +94 -0
  120. package/test/unit/payment-intent/model.test.ts +157 -0
  121. package/test/unit/payment-intent/repository.test.ts +263 -0
  122. package/test/unit/payment-intent/validator.test.ts +136 -0
  123. package/test/unit/refund/api-endpoint.test.ts +129 -0
  124. package/test/unit/refund/fixtures.ts +58 -0
  125. package/test/unit/refund/model.test.ts +89 -0
  126. package/test/unit/refund/repository.test.ts +157 -0
  127. package/test/unit/refund/validator.test.ts +89 -0
  128. package/test/unit/sepa/api-endpoint.test.ts +170 -0
  129. package/test/unit/sepa/fixture.ts +62 -0
  130. package/test/unit/sepa/model.test.ts +86 -0
  131. package/test/unit/sepa/repository.test.ts +149 -0
  132. package/test/unit/sepa/sepa-helper.ts +71 -0
  133. package/test/unit/sepa/validator.test.ts +58 -0
  134. package/test/unit/stub/stub-endpoint.test.ts +236 -0
  135. package/test/unit/stub/stub-list-endpoint.test.ts +86 -0
  136. package/test/unit/stub/stub-model.test.ts +111 -0
  137. package/test/unit/stub/stub-repository.test.ts +144 -0
  138. package/tsconfig.json +50 -0
  139. package/vitest.config.ts +20 -0
@@ -0,0 +1,170 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+
3
+ import { CardApiEndpoint } from '#card/api-endpoint.js';
4
+ import type { IdCard } from '#card/model.js';
5
+ import { randomExpYear, randomMonth, randomString } from '#test/test-helper.js';
6
+
7
+ import { randomCardNumber } from './card-helper.js';
8
+ import { creationResource, 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('card api endpoint', () => {
36
+ describe('Serialize/Deserialize resources', () => {
37
+ const endpoint = new CardApiEndpoint();
38
+
39
+ it('test deserialize resource', () => {
40
+ const deserializedResources = endpoint.deserializeResource(endpointResource);
41
+
42
+ //Double match to guaranty exact same shape
43
+ expect(deserializedResources).toMatchObject(endpointResourceDeserialized);
44
+ expect(endpointResourceDeserialized).toMatchObject(deserializedResources);
45
+ });
46
+
47
+ it('test post serialize resource', () => {
48
+ const serializedResources = endpoint.postObjectSerializer({
49
+ ...endpointResourceDeserialized,
50
+ number: creationResource.number,
51
+ tokenize: creationResource.tokenize,
52
+ cvc: creationResource.cvc,
53
+ }) as typeof endpointResource;
54
+ // The fields not included in the partial object are API generated
55
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
56
+ const { brand, country, created, deleted, funding, id, last4, nature, network, ...partialObject } =
57
+ endpointResource;
58
+ const postObject = {
59
+ ...partialObject,
60
+ number: creationResource.number,
61
+ tokenize: creationResource.tokenize,
62
+ cvc: creationResource.cvc,
63
+ };
64
+ //Double match to guaranty exact same shape
65
+ expect(serializedResources).toMatchObject(postObject);
66
+ expect(postObject).toMatchObject(serializedResources);
67
+ });
68
+
69
+ it('test patch serialize resource (not available in repository)', () => {
70
+ const serializedResources = endpoint.patchObjectSerializer(
71
+ endpointResourceDeserialized,
72
+ ) as typeof endpointResource;
73
+ //The object with all the patchable field
74
+ const patchObject = {
75
+ exp_month: endpointResource.exp_month,
76
+ exp_year: endpointResource.exp_year,
77
+ external_id: endpointResource.external_id,
78
+ zip_code: endpointResource.zip_code,
79
+ };
80
+ //Double match to guaranty exact same shape
81
+ expect(serializedResources).toMatchObject(patchObject);
82
+ expect(patchObject).toMatchObject(serializedResources);
83
+ });
84
+ });
85
+
86
+ describe('endpoint path', () => {
87
+ it('endpoint url', () => {
88
+ const endpoint = new CardApiEndpoint();
89
+ const id = ('card_' + randomString(24)) as IdCard;
90
+ expect(endpoint._getResourceUri(id)).toStrictEqual(`https://api.stancer.com/v2/cards/${id}`);
91
+ expect(endpoint._getResourceUri()).toStrictEqual('https://api.stancer.com/v2/cards/');
92
+ });
93
+ });
94
+
95
+ describe('verbs', () => {
96
+ const cardEndpoint = new CardApiEndpoint();
97
+
98
+ it('get', async () => {
99
+ const responseWithId = await cardEndpoint.get(('card_' + randomString(24)) as IdCard);
100
+ expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
101
+ });
102
+
103
+ it('post no deserialization', async () => {
104
+ const responseWithId = await cardEndpoint.post({
105
+ id: 'card_' + randomString(24),
106
+ expMonth: randomMonth(),
107
+ expYear: randomExpYear(),
108
+ number: randomCardNumber(),
109
+ });
110
+ expect(responseWithId).toStrictEqual(endpointResourceDeserialized);
111
+ });
112
+
113
+ it('patch', async () => {
114
+ const patchResponse = await cardEndpoint.patch(('card_' + randomString(24)) as IdCard, endpointResource);
115
+ expect(patchResponse).toStrictEqual(endpointResourceDeserialized);
116
+ });
117
+
118
+ it('delete', async () => {
119
+ expect(await cardEndpoint.delete(('card_' + randomString(24)) as IdCard)).toStrictEqual(
120
+ endpointResourceDeserialized,
121
+ );
122
+ });
123
+ });
124
+
125
+ describe('send', () => {
126
+ const { expYear, expMonth } = endpointResourceDeserialized;
127
+
128
+ it('send with required data', async () => {
129
+ const endpoint = new CardApiEndpoint();
130
+ await expect(endpoint.send(undefined, { expYear, expMonth, number: randomCardNumber() })).resolves.toStrictEqual(
131
+ endpointResourceDeserialized,
132
+ );
133
+ });
134
+
135
+ it('does not post with missing expYear', async () => {
136
+ const endpoint = new CardApiEndpoint();
137
+ expect(() => endpoint.send(undefined, { expMonth, number: randomCardNumber() })).toThrow();
138
+ });
139
+
140
+ it('does not post with missing expMonth', async () => {
141
+ const endpoint = new CardApiEndpoint();
142
+ expect(() => endpoint.send(undefined, { expYear, number: randomCardNumber() })).toThrow();
143
+ });
144
+
145
+ it('does not post with missing number', async () => {
146
+ const endpoint = new CardApiEndpoint();
147
+ expect(() => endpoint.send(undefined, { expYear, expMonth })).toThrow();
148
+ });
149
+
150
+ it('Patch if id', async () => {
151
+ const endpoint = new CardApiEndpoint();
152
+ const spyonPatch = vi.spyOn(endpoint, 'patch');
153
+ const spyonPost = vi.spyOn(endpoint, 'post');
154
+ await expect(endpoint.send('card_' + randomString(24), { expYear, expMonth })).resolves.toStrictEqual(
155
+ endpointResourceDeserialized,
156
+ );
157
+ expect(spyonPatch).toHaveBeenCalledTimes(1);
158
+ expect(spyonPost).toHaveBeenCalledTimes(0);
159
+ });
160
+
161
+ it('post if no id', () => {
162
+ const endpoint = new CardApiEndpoint();
163
+ const spyonPatch = vi.spyOn(endpoint, 'patch');
164
+ const spyonPost = vi.spyOn(endpoint, 'post');
165
+ endpoint.send(undefined, { expYear, expMonth, number: randomCardNumber() });
166
+ expect(spyonPatch).toHaveBeenCalledTimes(0);
167
+ expect(spyonPost).toHaveBeenCalledTimes(1);
168
+ });
169
+ });
170
+ });
@@ -0,0 +1,116 @@
1
+ import { randomNumber } from '#test/test-helper.js';
2
+
3
+ export const randomCardNumbers = () => {
4
+ // Card number found on https://www.freeformatter.com/credit-card-number-generator-validator.html
5
+ const data = [];
6
+
7
+ // VISA
8
+ data.push('4532160583905253');
9
+ data.push('4103344114503410');
10
+ data.push('4716929813250776300');
11
+
12
+ // MasterCard
13
+ data.push('5312580044202748');
14
+ data.push('2720995588028031');
15
+ data.push('5217849688268117');
16
+
17
+ // American Express (AMEX)
18
+ data.push('370301138747716');
19
+ data.push('340563568138644');
20
+ data.push('371461161518951');
21
+
22
+ // Discover
23
+ data.push('6011651456571367');
24
+ data.push('6011170656779399');
25
+ data.push('6011693048292929421');
26
+
27
+ // JCB
28
+ data.push('3532433013111566');
29
+ data.push('3544337258139297');
30
+ data.push('3535502591342895821');
31
+
32
+ // Diners Club - North America
33
+ data.push('5480649643931654');
34
+ data.push('5519243149714783');
35
+ data.push('5509141180527803');
36
+
37
+ // Diners Club - Carte Blanche
38
+ data.push('30267133988393');
39
+ data.push('30089013015810');
40
+ data.push('30109478108973');
41
+
42
+ // Diners Club - International
43
+ data.push('36052879958170');
44
+ data.push('36049904526204');
45
+ data.push('36768208048819');
46
+
47
+ // Maestro
48
+ data.push('5893433915020244');
49
+ data.push('6759761854174320');
50
+ data.push('6759998953884124');
51
+
52
+ // Visa Electron
53
+ data.push('4026291468019846');
54
+ data.push('4844059039871494');
55
+ data.push('4913054050962393');
56
+
57
+ // InstaPayment
58
+ data.push('6385037148943057');
59
+ data.push('6380659492219803');
60
+ data.push('6381454097795863');
61
+
62
+ // Classic one
63
+ data.push('4111111111111111');
64
+ data.push('4242424242424242');
65
+ data.push('4444333322221111');
66
+
67
+ return data;
68
+ };
69
+
70
+ export const randomCardNumber = () => {
71
+ const data = randomCardNumbers();
72
+ return data[randomNumber(0, data.length - 1)];
73
+ };
74
+
75
+ export const getValidCardNumber = (): string => {
76
+ const cards = getCardBranded();
77
+ const card = Object.values(cards).reduce((prev, curr) => prev.concat(curr));
78
+
79
+ return card[randomNumber(0, card.length - 1)];
80
+ };
81
+
82
+ const getCardBranded = (): { visa: string[]; mastercard: string[]; national: string[] } => {
83
+ return {
84
+ visa: [
85
+ '4000000400000008',
86
+ '4000000560000004',
87
+ '4000002080000001',
88
+ '4000002460000001',
89
+ '4000002760000016',
90
+ '4000003720000005',
91
+ '4000003800000008',
92
+ '4000005280000002',
93
+ '4000005780000007',
94
+ '4000006200000007',
95
+ '4000006430000009',
96
+ '4000007240000007',
97
+ '4000007520000008',
98
+ '4000007560000009',
99
+ '4000008260000000',
100
+ '4242424242424242',
101
+ '4444333322221111',
102
+ // '4000000000003055', This card is only for card patching (see youtrack API-549)
103
+ '4000000760000002',
104
+ '4000001240000000',
105
+ '4000004840000008',
106
+ ],
107
+ mastercard: [
108
+ '5555555555554444',
109
+ '5200828282828210',
110
+ '5105105105105100',
111
+ ],
112
+ national: [
113
+ '4000002500000003',
114
+ ],
115
+ };
116
+ };
@@ -0,0 +1,87 @@
1
+ import { Temporal } from 'temporal-polyfill';
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 { idCard } from '#card/model.js';
7
+ import { idCustomer } from '#customer/model.ts';
8
+ import { randomCVC, randomExpYear, randomMonth, randomNumber, randomString } from '#test/test-helper.js';
9
+
10
+ import { randomCardNumber } from './card-helper';
11
+
12
+ const id = `card_${randomString(24)}`;
13
+
14
+ const brand = randomString(2, 10);
15
+ const country = randomString(2);
16
+ const customer = `cust_${randomString(24)}`;
17
+ const cvc = randomCVC();
18
+ const created = randomNumber(1786460000, 2000000000);
19
+ const deleted = false;
20
+ const expMonth = randomMonth();
21
+ const expYear = randomExpYear();
22
+ const externalId = randomString(1, 36);
23
+ const funding = FundingCardEnum[randomNumber(0, FundingCardEnum.length - 1)];
24
+ const number = randomCardNumber();
25
+ const last4 = number.slice(number.length - 4);
26
+ const name = randomString(1, 64);
27
+ const nature = NatureCardEnum[randomNumber(0, NatureCardEnum.length - 1)];
28
+ const network = NetworkCardEnum[randomNumber(0, NetworkCardEnum.length - 1)];
29
+ const preferredNetwork = PreferredNetworkCardEnum[randomNumber(0, PreferredNetworkCardEnum.length - 1)];
30
+ const tokenize = Boolean(randomNumber(0, 1));
31
+ const zipCode = randomString(2, 8);
32
+
33
+ export const endpointResource = {
34
+ id,
35
+ brand,
36
+ country,
37
+ customer,
38
+ cvc,
39
+ created,
40
+ deleted,
41
+ exp_month: expMonth,
42
+ exp_year: expYear,
43
+ external_id: externalId,
44
+ funding,
45
+ last4,
46
+ name,
47
+ nature,
48
+ network,
49
+ number,
50
+ preferred_network: preferredNetwork,
51
+ zip_code: zipCode,
52
+ };
53
+
54
+ export const endpointResourceDeserialized = {
55
+ id: idCard(id),
56
+ brand,
57
+ country,
58
+ customer: idCustomer(customer),
59
+ createdAt: Temporal.Instant.fromEpochMilliseconds(created * 1000),
60
+ deleted,
61
+ expMonth,
62
+ expYear,
63
+ externalId,
64
+ funding,
65
+ last4,
66
+ name,
67
+ nature,
68
+ network,
69
+ preferredNetwork,
70
+ zipCode,
71
+ };
72
+
73
+ export const creationResource = {
74
+ cvc,
75
+ number,
76
+ tokenize,
77
+ };
78
+ export const listResource = [
79
+ {
80
+ ...endpointResource,
81
+ name: 'item1',
82
+ },
83
+ {
84
+ ...endpointResource,
85
+ name: 'item2',
86
+ },
87
+ ];
@@ -0,0 +1,167 @@
1
+ import { describe, 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 { CardData, CardModel } from '#card/model.js';
7
+ import { Validator } from '#core/validator.js';
8
+ import { IdCustomer } from '#customer/model.ts';
9
+ import { SetterGetter, randomExpYear, randomMonth, randomNumber, randomString } from '#test/test-helper.js';
10
+
11
+ import { randomCardNumbers } from './card-helper';
12
+
13
+ describe('card Class', () => {
14
+ const card = new CardModel((() => true) as unknown as Validator<CardData>);
15
+
16
+ describe('Setter & Getter', () => {
17
+ it('brand', () => {
18
+ SetterGetter(
19
+ (value: string) => (card._data.brand = value),
20
+ () => card.brand,
21
+ randomString(30),
22
+ );
23
+ });
24
+
25
+ it('deleted (unsettable)', () => {
26
+ SetterGetter(
27
+ (value: boolean) => (card._data.deleted = value),
28
+ () => card.deleted,
29
+ true,
30
+ );
31
+ card._data.deleted = null;
32
+ SetterGetter(
33
+ (value: boolean) => (card._data.deleted = value),
34
+ () => card.deleted,
35
+ false,
36
+ );
37
+ });
38
+
39
+ it('country', () => {
40
+ SetterGetter(
41
+ (value: string) => (card._data.country = value),
42
+ () => card.country,
43
+ randomString(2),
44
+ );
45
+ });
46
+
47
+ it('customer', () => {
48
+ SetterGetter(
49
+ (value: IdCustomer) => card.setCustomer(value),
50
+ () => card.customer,
51
+ ``,
52
+ );
53
+ });
54
+
55
+ it('cvc', () => {
56
+ SetterGetter(
57
+ (value: string) => card.setCvc(value),
58
+ () => card._data.cvc,
59
+ randomString(3),
60
+ );
61
+ });
62
+
63
+ it('expMonth', () => {
64
+ SetterGetter(
65
+ (value: number) => card.setExpMonth(value),
66
+ () => card.expMonth,
67
+ randomMonth(),
68
+ );
69
+ });
70
+
71
+ it('expYear', () => {
72
+ SetterGetter(
73
+ (value: string) => card.setExpYear(value),
74
+ () => card.expYear,
75
+ randomExpYear(),
76
+ );
77
+ });
78
+
79
+ it('externalId', () => {
80
+ SetterGetter(
81
+ (value: string) => card.setExternalId(value),
82
+ () => card.externalId,
83
+ randomString(1, 36),
84
+ );
85
+ });
86
+
87
+ it.each([FundingCardEnum.map((value) => [value])])('funding', (value) => {
88
+ SetterGetter(
89
+ (value: (typeof FundingCardEnum)[number]) => (card._data.funding = value),
90
+ () => card.funding,
91
+ value,
92
+ );
93
+ });
94
+
95
+ it('last4', () => {
96
+ SetterGetter(
97
+ (value: string) => (card._data.last4 = value),
98
+ () => card.last4,
99
+ String(randomNumber(0, 9999)).padStart(4, '0'),
100
+ );
101
+ });
102
+
103
+ it('name', () => {
104
+ SetterGetter(
105
+ (value: string) => card.setName(value),
106
+ () => card.name,
107
+ randomString(1, 64),
108
+ );
109
+ });
110
+
111
+ it.each([NatureCardEnum.map((value) => [value])])('nature', (value) => {
112
+ SetterGetter(
113
+ (value: (typeof NatureCardEnum)[number]) => (card._data.nature = value),
114
+ () => card.nature,
115
+ value,
116
+ );
117
+ });
118
+
119
+ it.each([NetworkCardEnum.map((value) => [value])])('network', (value) => {
120
+ SetterGetter(
121
+ (value: (typeof NetworkCardEnum)[number]) => (card._data.network = value),
122
+ () => card.network,
123
+ value,
124
+ );
125
+ });
126
+
127
+ it.each((randomCardNumbers() as string[]).map((value) => [value]))('number %s', (number) => {
128
+ card._data.number = null;
129
+ SetterGetter(
130
+ (value: string) => card.setNumber(value),
131
+ () => card._data.number,
132
+ number,
133
+ );
134
+ });
135
+
136
+ it.each(PreferredNetworkCardEnum.map((value) => [value]))('preferredNetwork %s', (value) => {
137
+ card._data.preferredNetwork = null;
138
+ SetterGetter(
139
+ (value: (typeof PreferredNetworkCardEnum)[number]) => card.setPreferredNetwork(value),
140
+ () => card.preferredNetwork,
141
+ value,
142
+ );
143
+ });
144
+
145
+ it('tokenize (unsettable)', () => {
146
+ SetterGetter(
147
+ (value: boolean) => (card._data.tokenize = value),
148
+ () => card._data.tokenize,
149
+ true,
150
+ );
151
+ card._data.tokenize = null;
152
+ SetterGetter(
153
+ (value: boolean) => (card._data.tokenize = value),
154
+ () => card._data.tokenize,
155
+ false,
156
+ );
157
+ });
158
+
159
+ it('zipCode', () => {
160
+ SetterGetter(
161
+ (value: string) => card.setZipCode(value),
162
+ () => card._data.zipCode,
163
+ randomString(2, 8),
164
+ );
165
+ });
166
+ });
167
+ });
@@ -0,0 +1,146 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+
3
+ import { CardApiEndpoint } from '#card/api-endpoint.js';
4
+ import { CardModel, IdCard } from '#card/model.js';
5
+ import { CardRepository } from '#card/repository.js';
6
+ import { cardValidator } from '#card/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 = 'card';
15
+ const id = `${idPrefix}_${randomString(24)}` as IdCard;
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
+ delete() {
29
+ return endpointResource;
30
+ }
31
+ send(id, data: Record<string, unknown>) {
32
+ if (id) {
33
+ if (id === stancerErrorID) {
34
+ throw new StancerError();
35
+ }
36
+ if (id === stancerRouteInaccessibleId) {
37
+ throw new StancerRouteInaccessibleError('PATCH', 'stub');
38
+ }
39
+ if (id === dataUnmodified) {
40
+ return;
41
+ }
42
+ }
43
+ return { ...data, id: dataHasBeenSentId };
44
+ }
45
+ get() {
46
+ return endpointResource;
47
+ }
48
+ }
49
+
50
+ it('can be instanciated', () => {
51
+ const instance = new CardRepository();
52
+ expect(instance).toBeInstanceOf(CardRepository);
53
+ expect(instance).toBeInstanceOf(AbstractRepository);
54
+ //accessing private method & property
55
+ expect(instance.endpoint).toBeInstanceOf(CardApiEndpoint);
56
+ expect(instance.validator).toBe(cardValidator);
57
+ expect(instance.staticModel).toBe(CardModel);
58
+ });
59
+
60
+ describe('repository', () => {
61
+ const mockRepo = new MockApiEndpoint() as unknown as CardApiEndpoint; // I lie, for convenience sake
62
+ const repository = new CardRepository(mockRepo);
63
+
64
+ describe('create', () => {
65
+ it("doesn't accept bad data", async () => {
66
+ const badID = randomString(30, 56);
67
+ // using puposefuly bad ID
68
+ await expect(async () => await repository.create(badID)).rejects.toThrow();
69
+ });
70
+
71
+ it('accept good data', async () => {
72
+ const model = await repository.create(id);
73
+ expect(model).toBeInstanceOf(CardModel);
74
+ });
75
+ });
76
+
77
+ it('delete', async () => {
78
+ const modelWithoutId = await repository.create();
79
+ await expect(() => repository.delete(modelWithoutId)).rejects.toThrow();
80
+
81
+ const model = await repository.create({ id: endpointResource.id, ...creationResource });
82
+ await expect((await repository.delete(model))._data).toStrictEqual(endpointResource);
83
+ });
84
+
85
+ describe('send', () => {
86
+ it('should return model', async () => {
87
+ const model = await repository.create(creationResource);
88
+ const sentModel = await repository.send(model);
89
+ expect(sentModel).toStrictEqual(model);
90
+ expect(sentModel._data.id).toEqual(dataHasBeenSentId);
91
+ });
92
+
93
+ it('should throw on inprecise error', async () => {
94
+ const model = await repository.create({ ...creationResource, id: stancerErrorID as IdCard });
95
+ await expect(() => repository.send(model)).rejects.toThrow(StancerError);
96
+ });
97
+
98
+ it('should return error on StancerRouteInacessibleError', async () => {
99
+ const model = await repository.create({ ...creationResource, id: stancerRouteInaccessibleId as IdCard });
100
+ await expect(async () => await repository.send(model)).rejects.toThrow(StancerRouteInaccessibleError);
101
+ });
102
+
103
+ it('should return unmodified model when no new Data', async () => {
104
+ const model = await repository.create({ ...creationResource, id: dataUnmodified as IdCard });
105
+ const sentModel = await repository.send(model);
106
+ expect(model).toStrictEqual(sentModel);
107
+ expect(sentModel._data.id).toEqual(dataUnmodified);
108
+ });
109
+ });
110
+
111
+ describe('populate', () => {
112
+ it('should not modify model if no Id', async () => {
113
+ const getEndpoint = vi.spyOn(mockRepo, 'get');
114
+ const model = await repository.create(creationResource);
115
+ expect(model).toBeInstanceOf(CardModel);
116
+ expect(getEndpoint).toHaveBeenCalledTimes(0);
117
+ });
118
+
119
+ it('should get model if only id', async () => {
120
+ const getEndpoint = vi.spyOn(mockRepo, 'get');
121
+ const model = await repository.create(id);
122
+ expect(model).toBeInstanceOf(CardModel);
123
+ expect(getEndpoint).toHaveBeenCalledTimes(1);
124
+ });
125
+ });
126
+
127
+ describe('list', async () => {
128
+ it('return correct model one by one', async () => {
129
+ const listStub = repository.list();
130
+ const item1 = await listStub.next();
131
+ expect(item1.done).toBeFalsy();
132
+ expect(item1.value).toHaveProperty('id');
133
+ expect(item1.value).toBeInstanceOf(CardModel);
134
+ expect(item1.value.name).toBe('item1');
135
+
136
+ const item2 = await listStub.next();
137
+ expect(item2.done).toBeFalsy();
138
+ expect(item2.value).toHaveProperty('id');
139
+ expect(item2.value).toBeInstanceOf(CardModel);
140
+ expect(item2.value.name).toBe('item2');
141
+
142
+ const item3 = await listStub.next();
143
+ expect(item3.done).toBeTruthy();
144
+ });
145
+ });
146
+ });