itrm-tools 1.3.2 → 1.3.5-viio.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.
Files changed (175) hide show
  1. package/.github/workflows/node.js.yml +30 -0
  2. package/README.md +2 -2
  3. package/dist/api/APIRequest.js +22 -29
  4. package/dist/api/APIRequest.js.map +1 -1
  5. package/dist/api/APIRouter.js +3 -1
  6. package/dist/api/APIRouter.js.map +1 -1
  7. package/dist/api/APIService.js +11 -17
  8. package/dist/api/APIService.js.map +1 -1
  9. package/dist/api/HttpsAPIService.js +5 -13
  10. package/dist/api/HttpsAPIService.js.map +1 -1
  11. package/dist/api/alegra/AlegraConnectionManager.js +23 -50
  12. package/dist/api/alegra/AlegraConnectionManager.js.map +1 -1
  13. package/dist/api/channel/ServerSentEventsManager.js +2 -1
  14. package/dist/api/channel/ServerSentEventsManager.js.map +1 -1
  15. package/dist/api/check/APIChecker.js +11 -25
  16. package/dist/api/check/APIChecker.js.map +1 -1
  17. package/dist/api/check/CheckableAPIRequest.js +17 -29
  18. package/dist/api/check/CheckableAPIRequest.js.map +1 -1
  19. package/dist/api/check/CheckableAPIRouter.js +17 -29
  20. package/dist/api/check/CheckableAPIRouter.js.map +1 -1
  21. package/dist/api/check/request/CheckableFileUploadingPostRequest.js +2 -0
  22. package/dist/api/check/request/CheckableFileUploadingPostRequest.js.map +1 -1
  23. package/dist/api/check/route/CorsCheckApiRouter.js +2 -13
  24. package/dist/api/check/route/CorsCheckApiRouter.js.map +1 -1
  25. package/dist/api/check/route/SimpleCheckableAPIRouter.js +2 -13
  26. package/dist/api/check/route/SimpleCheckableAPIRouter.js.map +1 -1
  27. package/dist/api/external/ExternalApiSigner.js +2 -0
  28. package/dist/api/external/ExternalApiSigner.js.map +1 -1
  29. package/dist/api/external/cetus/CetusStrategyApiManager.js +8 -20
  30. package/dist/api/external/cetus/CetusStrategyApiManager.js.map +1 -1
  31. package/dist/api/request/RequestAxiosCall.js +20 -37
  32. package/dist/api/request/RequestAxiosCall.js.map +1 -1
  33. package/dist/api/request/crud/CrudServiceManager.js +6 -17
  34. package/dist/api/request/crud/CrudServiceManager.js.map +1 -1
  35. package/dist/api/request/docs/DocumentationRequest.js +4 -7
  36. package/dist/api/request/docs/DocumentationRequest.js.map +1 -1
  37. package/dist/api/route/SimpleAPIRouter.js +2 -13
  38. package/dist/api/route/SimpleAPIRouter.js.map +1 -1
  39. package/dist/database/mongoose/MongooseModelManager.d.ts +8 -40
  40. package/dist/database/mongoose/MongooseModelManager.js +5 -14
  41. package/dist/database/mongoose/MongooseModelManager.js.map +1 -1
  42. package/dist/database/redis/RedisConnection.js +1 -0
  43. package/dist/database/redis/RedisConnection.js.map +1 -1
  44. package/dist/database/redis/RedisModelManager.js +1 -0
  45. package/dist/database/redis/RedisModelManager.js.map +1 -1
  46. package/dist/database/sequelize/SequelizeConnection.js +1 -0
  47. package/dist/database/sequelize/SequelizeConnection.js.map +1 -1
  48. package/dist/database/sequelize/SequelizeModelManager.js +6 -15
  49. package/dist/database/sequelize/SequelizeModelManager.js.map +1 -1
  50. package/dist/index.d.ts +0 -13
  51. package/dist/index.js +1 -28
  52. package/dist/index.js.map +1 -1
  53. package/dist/log/Logger.js +2 -0
  54. package/dist/log/Logger.js.map +1 -1
  55. package/dist/log/LoggerManager.js +11 -21
  56. package/dist/log/LoggerManager.js.map +1 -1
  57. package/dist/log/LoggerWebSocket.js +4 -2
  58. package/dist/log/LoggerWebSocket.js.map +1 -1
  59. package/dist/web3/coingecko/CoingeckoPriceFinder.js +24 -36
  60. package/dist/web3/coingecko/CoingeckoPriceFinder.js.map +1 -1
  61. package/jest.config.js +7 -0
  62. package/package.json +68 -74
  63. package/sequelize.sqlite +0 -0
  64. package/src/algorithm/encryption/EncryptionAlgorithm.ts +4 -0
  65. package/src/api/APIRequest.ts +176 -0
  66. package/src/api/APIRequestBuilder.ts +28 -0
  67. package/src/api/APIRouter.ts +26 -0
  68. package/src/api/APIService.ts +117 -0
  69. package/src/api/ExpressConfiguration.ts +5 -0
  70. package/src/api/HttpsAPIService.ts +35 -0
  71. package/src/api/alegra/AlegraConnectionManager.ts +54 -0
  72. package/src/api/alegra/AlegraTypes.ts +150 -0
  73. package/src/api/builder/StandardGetRequestBuilder.ts +34 -0
  74. package/src/api/channel/ServerSentEventsManager.ts +28 -0
  75. package/src/api/check/APIChecker.ts +50 -0
  76. package/src/api/check/CheckableAPIRequest.ts +33 -0
  77. package/src/api/check/CheckableAPIRouter.ts +39 -0
  78. package/src/api/check/request/CheckableDeleteRequest.ts +17 -0
  79. package/src/api/check/request/CheckableFileUploadingPostRequest.ts +26 -0
  80. package/src/api/check/request/CheckableGetRequest.ts +17 -0
  81. package/src/api/check/request/CheckablePostRequest.ts +17 -0
  82. package/src/api/check/request/CheckablePutRequest.ts +17 -0
  83. package/src/api/check/route/CorsCheckApiRouter.ts +22 -0
  84. package/src/api/check/route/SimpleCheckableAPIRouter.ts +14 -0
  85. package/src/api/express/ExpressFileUploadingConfiguration.ts +22 -0
  86. package/src/api/express/ExpressLogableConfig.ts +11 -0
  87. package/src/api/express/ExpressStandardConfiguration.ts +25 -0
  88. package/src/api/external/ExternalApiSigner.ts +29 -0
  89. package/src/api/external/cetus/CetusStrategyApiManager.ts +48 -0
  90. package/src/api/request/DeleteRequest.ts +16 -0
  91. package/src/api/request/GetRequest.ts +16 -0
  92. package/src/api/request/PostRequest.ts +16 -0
  93. package/src/api/request/PutRequest.ts +16 -0
  94. package/src/api/request/RequestAxiosCall.ts +38 -0
  95. package/src/api/request/crud/CrudManager.ts +11 -0
  96. package/src/api/request/crud/CrudServiceManager.ts +117 -0
  97. package/src/api/request/docs/DocumentationModels.ts +121 -0
  98. package/src/api/request/docs/DocumentationRequest.ts +132 -0
  99. package/src/api/route/SimpleAPIRouter.ts +12 -0
  100. package/src/database/mongoose/MongooseModelManager.ts +65 -0
  101. package/src/database/redis/RedisConnection.ts +18 -0
  102. package/src/database/redis/RedisModelManager.ts +23 -0
  103. package/src/database/sequelize/SequelizeConnection.ts +45 -0
  104. package/src/database/sequelize/SequelizeModelManager.ts +71 -0
  105. package/src/index.ts +51 -0
  106. package/src/log/Logger.ts +27 -0
  107. package/src/log/LoggerManager.ts +50 -0
  108. package/src/log/LoggerWebSocket.ts +13 -0
  109. package/src/math/Utils.ts +4 -0
  110. package/src/types/payments.ts +811 -0
  111. package/src/web3/Coin.ts +18 -0
  112. package/src/web3/coingecko/CoingeckoPriceFinder.ts +75 -0
  113. package/test/api/APIRouter.test.ts +78 -0
  114. package/test/api/APIService.test.ts +50 -0
  115. package/test/api/APITestDefinitions.ts +7 -0
  116. package/test/api/CORS.test.ts +56 -0
  117. package/test/api/alegra/AlegraConnectionManager.test.ts +32 -0
  118. package/test/api/check/CheckDefinitions.ts +47 -0
  119. package/test/api/check/CheckableAPIRouter.test.ts +67 -0
  120. package/test/api/check/CheckableDeleteRequest.test.ts +92 -0
  121. package/test/api/check/CheckableGetRequest.test.ts +99 -0
  122. package/test/api/check/CheckablePostRequest.test.ts +108 -0
  123. package/test/api/check/CheckablePutRequest.test.ts +99 -0
  124. package/test/api/docs/DocumentationRequest.test.ts +192 -0
  125. package/test/api/request/DeleteRequest.test.ts +117 -0
  126. package/test/api/request/GetRequest.test.ts +140 -0
  127. package/test/api/request/PostRequest.test.ts +237 -0
  128. package/test/api/request/PutRequest.test.ts +166 -0
  129. package/test/database/mongoose/MongooseModelManager.test.ts +139 -0
  130. package/test/database/redis/RedisConnection.test.ts +7 -0
  131. package/test/database/sequelize/SequealizeDefinitions.ts +6 -0
  132. package/test/database/sequelize/SequelizeConnection.test.ts +30 -0
  133. package/test/database/sequelize/SequelizeModelManager.test.ts +206 -0
  134. package/test/log/Logger.test.ts +44 -0
  135. package/test/web3/coingecko/CoingeckoPriceFinder.test.ts +72 -0
  136. package/tsconfig.json +23 -0
  137. package/dist/api/check/RoleServicePermissionsChecker.d.ts +0 -13
  138. package/dist/api/check/RoleServicePermissionsChecker.js +0 -92
  139. package/dist/api/check/RoleServicePermissionsChecker.js.map +0 -1
  140. package/dist/api/check/ServiceSessionChecker.d.ts +0 -28
  141. package/dist/api/check/ServiceSessionChecker.js +0 -119
  142. package/dist/api/check/ServiceSessionChecker.js.map +0 -1
  143. package/dist/api/request/crud/common/ActivityHistoryManager.d.ts +0 -40
  144. package/dist/api/request/crud/common/ActivityHistoryManager.js +0 -86
  145. package/dist/api/request/crud/common/ActivityHistoryManager.js.map +0 -1
  146. package/dist/api/request/crud/common/ApplicationManager.d.ts +0 -39
  147. package/dist/api/request/crud/common/ApplicationManager.js +0 -72
  148. package/dist/api/request/crud/common/ApplicationManager.js.map +0 -1
  149. package/dist/api/request/crud/common/AvailablePlatformManager.d.ts +0 -39
  150. package/dist/api/request/crud/common/AvailablePlatformManager.js +0 -86
  151. package/dist/api/request/crud/common/AvailablePlatformManager.js.map +0 -1
  152. package/dist/api/request/crud/common/ComponentManager.d.ts +0 -56
  153. package/dist/api/request/crud/common/ComponentManager.js +0 -78
  154. package/dist/api/request/crud/common/ComponentManager.js.map +0 -1
  155. package/dist/api/request/crud/common/KeyManager.d.ts +0 -39
  156. package/dist/api/request/crud/common/KeyManager.js +0 -88
  157. package/dist/api/request/crud/common/KeyManager.js.map +0 -1
  158. package/dist/api/request/crud/common/PlatformManager.d.ts +0 -44
  159. package/dist/api/request/crud/common/PlatformManager.js +0 -75
  160. package/dist/api/request/crud/common/PlatformManager.js.map +0 -1
  161. package/dist/api/request/crud/common/RoleManager.d.ts +0 -56
  162. package/dist/api/request/crud/common/RoleManager.js +0 -75
  163. package/dist/api/request/crud/common/RoleManager.js.map +0 -1
  164. package/dist/api/request/crud/common/StatusManager.d.ts +0 -34
  165. package/dist/api/request/crud/common/StatusManager.js +0 -74
  166. package/dist/api/request/crud/common/StatusManager.js.map +0 -1
  167. package/dist/api/request/crud/common/TypeManager.d.ts +0 -33
  168. package/dist/api/request/crud/common/TypeManager.js +0 -76
  169. package/dist/api/request/crud/common/TypeManager.js.map +0 -1
  170. package/dist/api/request/crud/common/UserCredentialsManager.d.ts +0 -39
  171. package/dist/api/request/crud/common/UserCredentialsManager.js +0 -84
  172. package/dist/api/request/crud/common/UserCredentialsManager.js.map +0 -1
  173. package/dist/api/request/crud/common/UserManager.d.ts +0 -35
  174. package/dist/api/request/crud/common/UserManager.js +0 -80
  175. package/dist/api/request/crud/common/UserManager.js.map +0 -1
@@ -0,0 +1,18 @@
1
+ export enum Coin {
2
+ APE = "APE",
3
+ ARTX = "ARTX",
4
+ ATRI = "ATRI",
5
+ BTC = "BTC",
6
+ CUBE = "CUBE",
7
+ DAI = "DAI",
8
+ ETH = "ETH",
9
+ MANA = "MANA",
10
+ MATIC = "MATIC",
11
+ RARI = "RARI",
12
+ SAND = "SAND",
13
+ SCOTT = "SCOTT",
14
+ USDC = "USDC",
15
+ USDT = "USDT",
16
+ WBTC = "WBTC",
17
+ WETH = "WETH"
18
+ }
@@ -0,0 +1,75 @@
1
+ import axios from 'axios';
2
+ import { Coin } from '../Coin';
3
+
4
+ export class CoingeckoPriceFinder {
5
+ private prices: any = {};
6
+ private pair: string;
7
+
8
+ constructor(pair?: string) {
9
+ this.pair = pair ?? "eth";
10
+ }
11
+
12
+ public async getPrice(symbol: Coin, date?: Date) {
13
+ date = date ?? new Date();
14
+ let id = this.parseSymbolId(symbol);
15
+ let key = (10000 * date.getFullYear()) + (100 * (date.getMonth() + 1)) + date.getDate();
16
+ return await this.findPrice(id, "" + this.adjustDate(symbol, key));
17
+ }
18
+
19
+ private adjustDate(symbol: Coin, date: number) {
20
+ switch (symbol) {
21
+ case Coin.APE: return Math.max(date, 20220317);
22
+ case Coin.ARTX: return Math.max(date, 20210404);
23
+ case Coin.ATRI: return Math.max(date, 20201102);
24
+ case Coin.BTC: return Math.max(date, 20130428);
25
+ case Coin.CUBE: return Math.max(date, 20200221);
26
+ case Coin.DAI: return Math.max(date, 20191119);
27
+ case Coin.MANA: return Math.max(date, 20171028);
28
+ case Coin.MATIC: return Math.max(date, 20190427);
29
+ case Coin.RARI: return Math.max(date, 20200717);
30
+ case Coin.SAND: return Math.max(date, 20200814);
31
+ case Coin.SCOTT: return Math.max(date, 20211220);
32
+ case Coin.USDC: return Math.max(date, 20181005);
33
+ case Coin.WBTC: return Math.max(date, 20130428);
34
+ default: return Math.max(date, 20150807);
35
+ }
36
+ }
37
+
38
+ private async findPrice(symbol: string, date: string) {
39
+ let key = symbol + "_" + date;
40
+ if (!this.prices[key])
41
+ return new Promise((resolve, reject) => {
42
+ let url = "https://api.coingecko.com/api/v3/coins/" + symbol + "/history?date="
43
+ + date.substring(6) + "-" + date.substring(4, 6) + "-" + date.substring(0, 4);
44
+ console.log("> url:", url);
45
+ axios.get(url, { headers: {
46
+ 'Content-Type': 'application/json'
47
+ }}).then(response => {
48
+ if (response.data.market_data && response.data.market_data.current_price)
49
+ this.prices[key] = response.data.market_data.current_price[this.pair];
50
+ resolve(this.prices[key]);
51
+ }).catch(err => { resolve(this.prices[key]); });
52
+ });
53
+ return this.prices[key];
54
+ }
55
+
56
+ public parseSymbolId(symbol: Coin) {
57
+ switch (symbol) {
58
+ case Coin.APE: return "apecoin";
59
+ case Coin.ARTX: return "artx";
60
+ case Coin.ATRI: return "atari";
61
+ case Coin.BTC: return "bitcoin";
62
+ case Coin.CUBE: return "somnium-space-cubes";
63
+ case Coin.DAI: return "dai";
64
+ case Coin.MANA: return "decentraland";
65
+ case Coin.MATIC: return "matic-network";
66
+ case Coin.RARI: return "rarible";
67
+ case Coin.SAND: return "the-sandbox";
68
+ case Coin.SCOTT: return "scotty-beam";
69
+ case Coin.USDC: return "usd-coin";
70
+ case Coin.USDT: return "tether";
71
+ case Coin.WBTC: return "bitcoin";
72
+ default: return "ethereum";
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,78 @@
1
+ import { NextFunction, Request, Response, RouterOptions } from "express";
2
+ import { APIService, SimpleAPIRouter, GetRequest, APIRouter, RequestAxiosCall } from "../../dist";
3
+ import { config } from "./APITestDefinitions";
4
+
5
+ class TestRequest extends GetRequest {
6
+ constructor() {
7
+ super({
8
+ path: "/test",
9
+ params: []
10
+ });
11
+ }
12
+
13
+ public async apply(req: Request, res: Response): Promise<any> {
14
+ return res.status(200).json({ message: 'Success' });
15
+ }
16
+ }
17
+
18
+ class TestRouter extends APIRouter {
19
+ constructor(path: string, options?: RouterOptions) {
20
+ super(path, options);
21
+ }
22
+
23
+ protected async runMiddleware(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
24
+ let { cancel } = req.query;
25
+ if (cancel)
26
+ return res.status(400).json({ message: "Cancelled" });
27
+ return next();
28
+ }
29
+ }
30
+
31
+ const service = new APIService(config);
32
+ service.init();
33
+
34
+ const request = new TestRequest();
35
+ const router1 = new SimpleAPIRouter("/route1");
36
+ router1.init();
37
+ router1.addRequest(request);
38
+ service.addRouter(router1);
39
+
40
+ const router2 = new TestRouter("/route2");
41
+ router2.init();
42
+ router2.addRequest(request);
43
+ service.addRouter(router2);
44
+
45
+ describe('testing Router', () => {
46
+ beforeAll(async () => {
47
+ await service.run(() => {});
48
+ });
49
+ afterAll(async () => {
50
+ await service.close(() => {})
51
+ });
52
+ test("> test route1 request", async() => {
53
+ expect.assertions(1);
54
+ await service.addRequest(new TestRequest());
55
+ return RequestAxiosCall.get(`http://localhost:${config.port}/route1/test`)
56
+ .then(async (response: any) => {
57
+ expect(response.message).toMatch("Success");
58
+ });
59
+ });
60
+ test("> test route2 request", async() => {
61
+ expect.assertions(1);
62
+ await service.addRequest(new TestRequest());
63
+ return RequestAxiosCall.get(`http://localhost:${config.port}/route2/test`)
64
+ .then(async (response: any) => {
65
+ expect(response.message).toMatch("Success");
66
+ });
67
+ });
68
+ test("> test route2 middleware cancel request", async() => {
69
+ expect.assertions(3);
70
+ await service.addRequest(new TestRequest());
71
+ return RequestAxiosCall.get(`http://localhost:${config.port}/route2/test?cancel=true`)
72
+ .catch(async err => {
73
+ expect(err.code).toMatch("ERR_BAD_REQUEST");
74
+ expect(err.response.status).toBe(400);
75
+ expect(err.response.data.message).toMatch("Cancelled");
76
+ });
77
+ });
78
+ });
@@ -0,0 +1,50 @@
1
+ import { APIService, APIServiceResponse, RequestAxiosCall } from '../../dist';
2
+ import { config } from './APITestDefinitions';
3
+
4
+ const service = new APIService(config);
5
+ service.init();
6
+
7
+ describe('testing APIService', () => {
8
+ test('running OK', async () => {
9
+ expect.assertions(3);
10
+ return service.run(() => {}).then((response: APIServiceResponse) => {
11
+ expect(response.status).toStrictEqual("Ok");
12
+ expect(response.message).toStrictEqual("Server has been Initialized");
13
+ expect(response.error).toBeUndefined();
14
+ });
15
+ });
16
+ test('Server working!', async() => {
17
+ expect.assertions(1);
18
+ return RequestAxiosCall.get("http://localhost:" + config.port).then(response => {
19
+ expect(response).toMatch("Server working!");
20
+ });
21
+ });
22
+ test('closing OK', async () => {
23
+ expect.assertions(3);
24
+ return service.close(() => {}).then((response: APIServiceResponse) => {
25
+ expect(response.status).toStrictEqual("Ok");
26
+ expect(response.message).toStrictEqual("Server has been closed");
27
+ expect(response.error).toBeUndefined();
28
+ });
29
+ });
30
+ test('running Fail', () => {
31
+ expect.assertions(3);
32
+ return service.run(() => { throw "testing failure"; })
33
+ .catch(async (response: APIServiceResponse) => {
34
+ expect(response.status).toStrictEqual("Error");
35
+ expect(response.message).toStrictEqual("An error has ocurred in the initialization function");
36
+ expect(response.error).toBe("testing failure");
37
+ await service.close(() => {});
38
+ });
39
+ });
40
+ test('closing Fail', async () => {
41
+ expect.assertions(3);
42
+ await service.run(() => {});
43
+ return service.close(() => { throw "testing failure"; })
44
+ .catch(async (response: APIServiceResponse) => {
45
+ expect(response.status).toMatch("Error");
46
+ expect(response.message).toMatch("An error has ocurred in the closing function");
47
+ expect(response.error).toMatch("testing failure");
48
+ });
49
+ });
50
+ });
@@ -0,0 +1,7 @@
1
+ import { APIServiceConfig, ExpressStandardConfiguration } from "../../dist";
2
+
3
+ export const config: APIServiceConfig = {
4
+ name: 'test',
5
+ port: 8150,
6
+ express: new ExpressStandardConfiguration()
7
+ };
@@ -0,0 +1,56 @@
1
+ import { APIService, ITRMAPIServiceGlobal, RequestAxiosCall } from "../../dist";
2
+ import { config } from "./APITestDefinitions";
3
+
4
+ const service = new APIService(config);
5
+ service.init();
6
+
7
+ describe("Testing CORS", () => {
8
+ beforeAll(async () => {
9
+ await service.run(() => {});
10
+ });
11
+ afterAll(async () => {
12
+ await service.close(() => {})
13
+ });
14
+ test('Checking invalid route has not been blocked', async() => {
15
+ expect.assertions(1);
16
+ return RequestAxiosCall.get("http://localhost:" + config.port, {
17
+ headers: { 'Content-Type': 'application/json', 'Origin': 'https://invalid.com' }
18
+ })
19
+ .then(response => {
20
+ expect(response).toMatch("Server working!");
21
+ });
22
+ });
23
+ /*test('Checking invalid route was blocked', async() => {
24
+ ITRMAPIServiceGlobal.whitelist = ["https://valid.com"];
25
+ try {
26
+ await RequestAxiosCall.get("http://localhost:" + config.port, {
27
+ headers: { 'Content-Type': 'application/json', 'Origin': 'https://invalid.com' }
28
+ })
29
+ } catch(err: any) {
30
+ console.log("> err.code:", err.code);
31
+ expect(err.code).toMatch("ECONNREFUSED");
32
+ //expect(err.response.status).toBe(500);
33
+ //expect(err.response.statusText).toMatch("Internal Server Error");
34
+ //expect(err.response.data).toMatch("Not allowed by CORS");
35
+ }
36
+ });*/
37
+ test('Checking valid route was not blocked', async() => {
38
+ expect.assertions(1);
39
+ return RequestAxiosCall.get("http://localhost:" + config.port, {
40
+ headers: { 'Content-Type': 'application/json', 'Origin': 'https://valid.com' }
41
+ })
42
+ .then(response => {
43
+ expect(response).toMatch("Server working!");
44
+ });
45
+ });
46
+ test('Checking invalid route was released', async() => {
47
+ expect.assertions(1);
48
+ ITRMAPIServiceGlobal.whitelist = [];
49
+ return RequestAxiosCall.get("http://localhost:" + config.port, {
50
+ headers: { 'Content-Type': 'application/json', 'Origin': 'https://invalid.com' }
51
+ })
52
+ .then(response => {
53
+ expect(response).toMatch("Server working!");
54
+ });
55
+ });
56
+ });
@@ -0,0 +1,32 @@
1
+ import { AlegraConnectionManager } from "../../../dist";
2
+
3
+ const ALEGRA_URL = "https://api.alegra.com/api";
4
+ const TOKEN = "aXRybWFjaGluZXMuY29udGFiaWxpZGFkQGdtYWlsLmNvbTphZDYwNWEwMzI4ZDk2NjcyYmVhNw==";
5
+
6
+ const connection = new AlegraConnectionManager(ALEGRA_URL, TOKEN);
7
+
8
+ describe('testing Checkable Router', () => {
9
+ beforeAll(async () => { });
10
+ afterAll(async () => { });
11
+ test("> test not found contact", async() => {
12
+ let contact = await connection.findContact('103244436ABC');
13
+ expect(contact).toBeUndefined();
14
+ });
15
+ test("> test find contact", async() => {
16
+ let contact = await connection.findContact('1032444365');
17
+ expect(contact).toBeDefined();
18
+ if (contact) {
19
+ expect(contact.name).toMatch("CAMILO ALEJANDRO ALAGUNA");
20
+ expect(contact.identification).toMatch("1032444365");
21
+ }
22
+ });
23
+ test("> test find payment", async() => {
24
+ let payment = await connection.findPayment(3248);
25
+ expect(payment).toBeDefined();
26
+ });
27
+ test("> test find journal", async() => {
28
+ let journal: any = await connection.findJournal(3248);
29
+ console.log("> journal:", journal);
30
+ console.log("> entries:", journal.entries);
31
+ });
32
+ });
@@ -0,0 +1,47 @@
1
+ import { Request } from 'express';
2
+ import { APICheck, APICheckResult, APICheckConfig } from '../../../dist';
3
+
4
+ export interface TestCheckerConfig extends APICheckConfig {
5
+ key: string
6
+ }
7
+
8
+ export class TestCheck implements APICheck {
9
+
10
+ public getConfig(): TestCheckerConfig {
11
+ return {
12
+ check: 'test',
13
+ key: 'valid'
14
+ };
15
+ }
16
+
17
+ public async apply(config: TestCheckerConfig, req: Request): Promise<APICheckResult> {
18
+ const { key } = req.query;
19
+ if (config.key === key)
20
+ return { approved: true, payload: { message: 'Accepted' } };
21
+ if (key === 'unknown')
22
+ return { approved: false };
23
+ return {
24
+ approved: false,
25
+ rejection: {
26
+ code: 401,
27
+ payload: { message: 'key does not match' }
28
+ }
29
+ };
30
+ }
31
+ }
32
+
33
+ export class UnexpectedErrorTestCheck implements APICheck {
34
+
35
+ public getConfig(): APICheckConfig {
36
+ return {
37
+ check: 'unexpectedError'
38
+ };
39
+ }
40
+
41
+ public async apply(config: TestCheckerConfig, req: Request): Promise<APICheckResult> {
42
+ throw 'Unexpected error';
43
+ }
44
+ }
45
+
46
+ export const check = new TestCheck();
47
+ export const unexpectedErrorCheck = new UnexpectedErrorTestCheck();
@@ -0,0 +1,67 @@
1
+ import { Request, Response } from "express";
2
+ import { APIService, SimpleCheckableAPIRouter, GetRequest, RequestAxiosCall } from "../../../dist";
3
+ import { config } from "../APITestDefinitions";
4
+ import { check, unexpectedErrorCheck } from './CheckDefinitions';
5
+
6
+ class TestRequest extends GetRequest {
7
+ constructor() {
8
+ super({
9
+ path: "/test",
10
+ params: []
11
+ });
12
+ }
13
+
14
+ public async apply(req: Request, res: Response): Promise<any> {
15
+ console.log("> checkResults:", req.body.checkResults);
16
+ return res.status(200).json({ message: 'Success', checkResults: req.body.checkResults });
17
+ }
18
+ }
19
+
20
+ const service = new APIService(config);
21
+ service.init();
22
+
23
+ const request = new TestRequest();
24
+ const router = new SimpleCheckableAPIRouter("/route");
25
+ router.init();
26
+ router.addCheck(check);
27
+ router.addRequest(request);
28
+ service.addRouter(router);
29
+
30
+ describe('testing Checkable Router', () => {
31
+ beforeAll(async () => {
32
+ await service.run(() => {});
33
+ });
34
+ afterAll(async () => {
35
+ await service.close(() => {})
36
+ });
37
+ test("> test valid route request", async() => {
38
+ expect.assertions(3);
39
+ await service.addRequest(new TestRequest());
40
+ return RequestAxiosCall.get(`http://localhost:${config.port}/route/test?key=valid`)
41
+ .then(async (response: any) => {
42
+ expect(response.message).toMatch("Success");
43
+ expect(response.checkResults).toBeDefined();
44
+ expect(response.checkResults[check.getConfig().check]).toMatchObject({ message: 'Accepted' });
45
+ });
46
+ });
47
+ test('> test invalid route request', async () => {
48
+ expect.assertions(4);
49
+ return RequestAxiosCall.get(`http://localhost:${config.port}/route/test?key=invalid`)
50
+ .catch(err => {
51
+ expect(err.code).toMatch("ERR_BAD_REQUEST");
52
+ expect(err.response.status).toBe(401);
53
+ expect(err.response.statusText).toMatch("Unauthorized");
54
+ expect(err.response.data.message).toMatch("key does not match");
55
+ });
56
+ });
57
+ test('Checking invalid route request with no unauthorization reason', async () => {
58
+ expect.assertions(4);
59
+ return RequestAxiosCall.get(`http://localhost:${config.port}/route/test?key=unknown`)
60
+ .catch(err => {
61
+ expect(err.code).toMatch("ERR_BAD_REQUEST");
62
+ expect(err.response.status).toBe(401);
63
+ expect(err.response.statusText).toMatch("Unauthorized");
64
+ expect(err.response.data.message).toMatch("Unknown reason");
65
+ });
66
+ });
67
+ });
@@ -0,0 +1,92 @@
1
+ import { Request, Response } from "express";
2
+ import { APIService, CheckableDeleteRequest, RequestContext, RequestAxiosCall, RequestParameterType } from "../../../dist";
3
+ import { config } from "../APITestDefinitions";
4
+ import { check, unexpectedErrorCheck } from "./CheckDefinitions";
5
+
6
+ const service = new APIService(config);
7
+ service.init();
8
+
9
+ export class TestRequest extends CheckableDeleteRequest {
10
+ constructor() {
11
+ super({
12
+ path: "/check",
13
+ params: [{
14
+ context: RequestContext.QUERY,
15
+ properties: [{ name: "key", type: RequestParameterType.STRING }]
16
+ }]
17
+ });
18
+ }
19
+
20
+ public async apply(req: Request, res: Response): Promise<any> {
21
+ return res.status(200).json({ message: 'Success' });
22
+ }
23
+ }
24
+
25
+ export class InvalidCheckConfigurationTestRequest extends CheckableDeleteRequest {
26
+ constructor() {
27
+ super({
28
+ path: "/invalid-check",
29
+ params: [{
30
+ context: RequestContext.QUERY,
31
+ properties: [{ name: "key", type: RequestParameterType.STRING }]
32
+ }]
33
+ });
34
+ }
35
+
36
+ public async apply(req: Request, res: Response): Promise<any> {
37
+ return res.status(200).json({ message: 'Success' });
38
+ }
39
+ }
40
+
41
+ const validKeyUrl = `http://localhost:${config.port}/check?key=valid`;
42
+ const invalidKeyUrl = `http://localhost:${config.port}/check?key=invalid`;
43
+ const invalidUnknownKeyUrl = `http://localhost:${config.port}/check?key=unknown`;
44
+
45
+ describe('testing Checkable DELETE Request', () => {
46
+ let request = new TestRequest();
47
+ beforeAll(async () => {
48
+ await service.run(() => { });
49
+ });
50
+ afterAll(async () => {
51
+ await service.close(() => { })
52
+ });
53
+ test('Refuse request when API Request does not exist', async () => {
54
+ expect.assertions(3);
55
+ return RequestAxiosCall.delete(validKeyUrl)
56
+ .catch(err => {
57
+ expect(err.code).toMatch("ERR_BAD_REQUEST");
58
+ expect(err.response.status).toBe(404);
59
+ expect(err.response.statusText).toMatch("Not Found");
60
+ });
61
+ });
62
+ test('Checking valid', async () => {
63
+ try {
64
+ request.addCheck(check);
65
+ await service.addRequest(request);
66
+ let response: any = await RequestAxiosCall.delete(validKeyUrl);
67
+ expect(response.message).toMatch("Success");
68
+ } catch (err) {
69
+ console.log("> err:", err);
70
+ }
71
+ });
72
+ test('Checking invalid', async () => {
73
+ expect.assertions(4);
74
+ return RequestAxiosCall.delete(invalidKeyUrl)
75
+ .catch(err => {
76
+ expect(err.code).toMatch("ERR_BAD_REQUEST");
77
+ expect(err.response.status).toBe(401);
78
+ expect(err.response.statusText).toMatch("Unauthorized");
79
+ expect(err.response.data.message).toMatch("key does not match");
80
+ });
81
+ });
82
+ test('Checking invalid with no unauthorization reason', async () => {
83
+ expect.assertions(4);
84
+ return RequestAxiosCall.delete(invalidUnknownKeyUrl)
85
+ .catch(err => {
86
+ expect(err.code).toMatch("ERR_BAD_REQUEST");
87
+ expect(err.response.status).toBe(401);
88
+ expect(err.response.statusText).toMatch("Unauthorized");
89
+ expect(err.response.data.message).toMatch("Unknown reason");
90
+ });
91
+ });
92
+ });
@@ -0,0 +1,99 @@
1
+ import { Request, Response } from "express";
2
+ import { APIService, CheckableGetRequest, RequestContext, RequestAxiosCall, RequestParameterType } from "../../../dist";
3
+ import { config } from "../APITestDefinitions";
4
+ import { check, unexpectedErrorCheck } from "./CheckDefinitions";
5
+
6
+ const service = new APIService(config);
7
+ service.init();
8
+
9
+ export class TestRequest extends CheckableGetRequest {
10
+ constructor() {
11
+ super({
12
+ path: "/check",
13
+ params: [{
14
+ context: RequestContext.QUERY,
15
+ properties: [{ name: "key", type: RequestParameterType.STRING }]
16
+ }]
17
+ });
18
+ }
19
+
20
+ public getCheckConfiguration() {
21
+ return {
22
+ key: 'valid'
23
+ };
24
+ }
25
+
26
+ public async apply(req: Request, res: Response): Promise<any> {
27
+ return res.status(200).json({ message: 'Success' });
28
+ }
29
+ }
30
+
31
+ export class InvalidCheckConfigurationTestRequest extends CheckableGetRequest {
32
+ constructor() {
33
+ super({
34
+ path: "/invalid-check",
35
+ params: [{
36
+ context: RequestContext.QUERY,
37
+ properties: [{ name: "key", type: RequestParameterType.STRING }]
38
+ }]
39
+ });
40
+ }
41
+
42
+ public getCheckConfiguration() { // invalid configuration
43
+ return {
44
+ password: 'valid'
45
+ };
46
+ }
47
+
48
+ public async apply(req: Request, res: Response): Promise<any> {
49
+ return res.status(200).json({ message: 'Success' });
50
+ }
51
+ }
52
+
53
+ const validKeyUrl = `http://localhost:${config.port}/check?key=valid`;
54
+ const invalidKeyUrl = `http://localhost:${config.port}/check?key=invalid`;
55
+ const invalidUnknownKeyUrl = `http://localhost:${config.port}/check?key=unknown`;
56
+
57
+ describe('testing Checkable GET Request', () => {
58
+ let request = new TestRequest();
59
+ beforeAll(async () => {
60
+ await service.run(() => { });
61
+ });
62
+ afterAll(async () => {
63
+ await service.close(() => { })
64
+ });
65
+ test('Refuse request when API Request does not exist', async () => {
66
+ expect.assertions(3);
67
+ return RequestAxiosCall.get(validKeyUrl).catch(err => {
68
+ expect(err.code).toMatch("ERR_BAD_REQUEST");
69
+ expect(err.response.status).toBe(404);
70
+ expect(err.response.statusText).toMatch("Not Found");
71
+ });
72
+ });
73
+ test('Checking valid', async () => {
74
+ expect.assertions(1);
75
+ request.addCheck(check);
76
+ await service.addRequest(request);
77
+ return RequestAxiosCall.get(validKeyUrl).then(async (response: any) => {
78
+ expect(response.message).toMatch("Success");
79
+ });
80
+ });
81
+ test('Checking invalid', async () => {
82
+ expect.assertions(4);
83
+ return RequestAxiosCall.get(invalidKeyUrl).catch(err => {
84
+ expect(err.code).toMatch("ERR_BAD_REQUEST");
85
+ expect(err.response.status).toBe(401);
86
+ expect(err.response.statusText).toMatch("Unauthorized");
87
+ expect(err.response.data.message).toMatch("key does not match");
88
+ });
89
+ });
90
+ test('Checking invalid with no unauthorization reason', async () => {
91
+ expect.assertions(4);
92
+ return RequestAxiosCall.get(invalidUnknownKeyUrl).catch(err => {
93
+ expect(err.code).toMatch("ERR_BAD_REQUEST");
94
+ expect(err.response.status).toBe(401);
95
+ expect(err.response.statusText).toMatch("Unauthorized");
96
+ expect(err.response.data.message).toMatch("Unknown reason");
97
+ });
98
+ });
99
+ });