propro-utils 1.4.25 → 1.4.26

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 (40) hide show
  1. package/.babelrc +13 -0
  2. package/.github/workflows/test.yml +30 -0
  3. package/coverage/clover.xml +212 -0
  4. package/coverage/coverage-final.json +8 -0
  5. package/coverage/lcov-report/base.css +224 -0
  6. package/coverage/lcov-report/block-navigation.js +87 -0
  7. package/coverage/lcov-report/favicon.png +0 -0
  8. package/coverage/lcov-report/index.html +161 -0
  9. package/coverage/lcov-report/middlewares/access_token.js.html +343 -0
  10. package/coverage/lcov-report/middlewares/account_info.js.html +334 -0
  11. package/coverage/lcov-report/middlewares/index.html +131 -0
  12. package/coverage/lcov-report/prettify.css +1 -0
  13. package/coverage/lcov-report/prettify.js +2 -0
  14. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  15. package/coverage/lcov-report/sorter.js +196 -0
  16. package/coverage/lcov-report/src/server/index.html +116 -0
  17. package/coverage/lcov-report/src/server/index.js.html +664 -0
  18. package/coverage/lcov-report/src/server/middleware/index.html +131 -0
  19. package/coverage/lcov-report/src/server/middleware/validateEnv.js.html +139 -0
  20. package/coverage/lcov-report/src/server/middleware/verifyToken.js.html +604 -0
  21. package/coverage/lcov-report/utils/index.html +131 -0
  22. package/coverage/lcov-report/utils/redis.js.html +133 -0
  23. package/coverage/lcov-report/utils/testUtils.js.html +172 -0
  24. package/coverage/lcov-report/verifyToken.js.html +589 -0
  25. package/coverage/lcov.info +361 -0
  26. package/jest.config.js +15 -0
  27. package/jest.setup.js +5 -0
  28. package/middlewares/access_token.test.js +84 -0
  29. package/package.json +14 -34
  30. package/src/server/index.js +6 -2
  31. package/src/server/index.test.js +88 -0
  32. package/src/server/middleware/validateEnv.js +8 -2
  33. package/src/server/middleware/validateEnv.test.js +24 -0
  34. package/src/server/middleware/verifyToken.js +111 -102
  35. package/src/server/middleware/verifyToken.test.js +293 -0
  36. package/src/server/server.test.js +8 -32
  37. package/utils/redis.js +12 -14
  38. package/utils/redis.test.js +117 -0
  39. package/utils/testUtils.js +29 -0
  40. package/.github/workflows/.deploy +0 -31
@@ -0,0 +1,117 @@
1
+ const { getOrSetCache } = require('./redis');
2
+
3
+ let client;
4
+
5
+ jest.mock('redis', () => {
6
+ return {
7
+ createClient: jest.fn().mockReturnValue({
8
+ connect: jest.fn().mockResolvedValue(),
9
+ get: jest.fn(),
10
+ setex: jest.fn(),
11
+ quit: jest.fn().mockResolvedValue(),
12
+ }),
13
+ };
14
+ });
15
+
16
+ const mockGet = jest.fn();
17
+ const mockSetEx = jest.fn();
18
+ const redisClient = {
19
+ get: mockGet,
20
+ setEx: mockSetEx,
21
+ };
22
+
23
+ describe('getOrSetCache', () => {
24
+ beforeEach(() => {
25
+ mockGet.mockReset();
26
+ mockSetEx.mockReset();
27
+ });
28
+
29
+ it('should return cached value if available (cache hit)', async () => {
30
+ const key = 'testKey';
31
+ const cachedValue = { foo: 'bar' };
32
+ mockGet.mockResolvedValue(JSON.stringify(cachedValue));
33
+
34
+ const service = jest.fn();
35
+
36
+ const result = await getOrSetCache(redisClient, key, service);
37
+
38
+ expect(mockGet).toHaveBeenCalledWith(key);
39
+ expect(service).not.toHaveBeenCalled();
40
+ expect(result).toEqual(cachedValue);
41
+ });
42
+
43
+ it('should fetch and cache value if not available in cache (cache miss)', async () => {
44
+ const key = 'testKey';
45
+ const valueToCache = { foo: 'bar' };
46
+ mockGet.mockResolvedValue(null);
47
+ mockSetEx.mockResolvedValue('OK');
48
+
49
+ const service = jest.fn().mockResolvedValue(valueToCache);
50
+
51
+ const result = await getOrSetCache(redisClient, key, service);
52
+
53
+ expect(mockGet).toHaveBeenCalledWith(key);
54
+ expect(service).toHaveBeenCalled();
55
+ expect(mockSetEx).toHaveBeenCalledWith(
56
+ key,
57
+ 1800,
58
+ JSON.stringify(valueToCache)
59
+ );
60
+ expect(result).toEqual(valueToCache);
61
+ });
62
+
63
+ it('should throw an error if the service function throws', async () => {
64
+ const key = 'testKey';
65
+ const error = new Error('Service error');
66
+ mockGet.mockResolvedValue(null);
67
+
68
+ const service = jest.fn().mockRejectedValue(error);
69
+
70
+ await expect(getOrSetCache(redisClient, key, service)).rejects.toThrow(
71
+ 'Service error'
72
+ );
73
+
74
+ expect(mockGet).toHaveBeenCalledWith(key);
75
+ expect(service).toHaveBeenCalled();
76
+ expect(mockSetEx).not.toHaveBeenCalled();
77
+ });
78
+
79
+ it('should throw an error if Redis get operation throws', async () => {
80
+ const key = 'testKey';
81
+ const error = new Error('Redis get error');
82
+ mockGet.mockRejectedValue(error);
83
+
84
+ const service = jest.fn();
85
+
86
+ await expect(getOrSetCache(redisClient, key, service)).rejects.toThrow(
87
+ 'Redis get error'
88
+ );
89
+
90
+ expect(mockGet).toHaveBeenCalledWith(key);
91
+ expect(service).not.toHaveBeenCalled();
92
+ expect(mockSetEx).not.toHaveBeenCalled();
93
+ });
94
+
95
+ it('should throw an error if Redis setEx operation throws', async () => {
96
+ const key = 'testKey';
97
+ const valueToCache = { foo: 'bar' };
98
+ const error = new Error('Redis setEx error');
99
+ mockGet.mockResolvedValue(null);
100
+ mockSetEx.mockRejectedValue(error);
101
+
102
+ const service = jest.fn().mockResolvedValue(valueToCache);
103
+
104
+ await expect(getOrSetCache(redisClient, key, service)).rejects.toThrow(
105
+ 'Redis setEx error'
106
+ );
107
+
108
+ expect(mockGet).toHaveBeenCalledWith(key);
109
+
110
+ expect(service).toHaveBeenCalled();
111
+ expect(mockSetEx).toHaveBeenCalledWith(
112
+ key,
113
+ 1800,
114
+ JSON.stringify(valueToCache)
115
+ );
116
+ });
117
+ });
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Creates a mock Express context for testing purposes.
3
+ *
4
+ * @param {string} path - The path for the mock request.
5
+ * @param {object} options - Additional options for the mock request.
6
+ * @param {object} options.cookies - The cookies for the mock request.
7
+ * @param {object} options.query - The query parameters for the mock request.
8
+ * @returns {object} - The mock Express context containing the request, response, and next objects.
9
+ */
10
+ function createMockExpressContext(path, options = {}) {
11
+ const req = {
12
+ path,
13
+ cookies: options.cookies || {},
14
+ query: options.query || {},
15
+ };
16
+ const res = {
17
+ status: jest.fn().mockReturnThis(),
18
+ json: jest.fn().mockReturnThis(),
19
+ cookie: jest.fn().mockReturnThis(),
20
+ redirect: jest.fn().mockReturnThis(),
21
+ };
22
+ const next = jest.fn();
23
+
24
+ return { req, res, next };
25
+ }
26
+
27
+ module.exports = {
28
+ createMockExpressContext,
29
+ };
@@ -1,31 +0,0 @@
1
- name: Publish to npm
2
-
3
- on:
4
- push:
5
- tags:
6
- - "*"
7
- pull_request:
8
- branches:
9
- - "*"
10
-
11
- jobs:
12
- publish-npm:
13
- runs-on: ubuntu-latest
14
- steps:
15
- - uses: actions/checkout@v2
16
- - name: Use Node.js 18.x
17
- uses: actions/setup-node@v1
18
- with:
19
- node-version: 18
20
- registry-url: "https://registry.npmjs.org"
21
-
22
- - name: Install dependencies
23
- run: npm install
24
-
25
- - name: Run tests
26
- run: npm run test
27
-
28
- - name: Publish to npm
29
- run: npm publish
30
- env:
31
- NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}