Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 7x 39x 7x 2x 7x 1x 7x 8x 7x 2x 7x 1x 7x 1x 7x 1x 7x 1x 7x 1x 7x 1x 7x 1x 7x 1x 7x 7x 7x 7x 7x 7x 51x 7x 11x 7x 1x 7x 2x 7x 65x 19x 65x 65x 65x 87x 87x 87x 65x 65x 65x 65x 65x 65x 62x 62x 65x 65x 65x 65x 65x 65x | // @ts-nocheck import nock from "nock"; import TransactionTestHelper from "./core/utils/transactionTestHelper"; import { gmailPaymentDetailsTestCases } from "./gmail/types/gmailPaymentDetailsTestCases"; import Constants from "./constants"; import { URL, URLSearchParams } from "url"; import * as googleOAuth2Middleware from './web/middleware/googleOAuth2Middleware'; import * as transactionsController from "./web/controllers/transactionsController"; import * as gmailTransactionsController from "./web/controllers/gmailTransactionsController"; import * as groupsController from './web/controllers/groupsController'; import * as groupRulesController from './web/controllers/groupRulesController'; import { NextFunction, Request, Response } from "express"; import queryString from "node:querystring"; const gmailApiBaseUrl = 'https://gmail.googleapis.com'; const oauth2ApiBaseUrl = 'https://oauth2.googleapis.com'; const localApiBaseUrl = Constants.baseUrl; const messageListUri = /\/gmail\/v1\/users\/[^\/%]+\/messages/; const messageUri = /\/gmail\/v1\/users\/[^/]+\/messages\/([^/]+)$/; const attachmentsUri = /\/gmail\/v1\/users\/[^/]+\/messages\/([^/]+)\/attachments\/[^/]+/; const tokenUri = '/token'; const tokenInfoUri = '/tokeninfo'; const oauthCallbackUri = '/api/oauthcallback'; const transactionsUri = /\/api\/transactions(?!\/).*/; const transactionsSaveUri = '/api/transactions/save'; const transactionsUpdateUri = '/api/transactions/update'; const transactionsLastIdsUri = /\/api\/transactions\/gmail\/ids\/last\/([^?]*)[?]*.*/; const transactionsGmailResolveUri = '/api/transactions/gmail/resolve'; const groupsUri = '/api/groups'; const groupsAllUri = '/api/groups/all'; const groupUri = /^\/api\/groups\/(?!all)([^\/?]+)[?]?$/; const groupRulesNewUri = /^\/api\/groups\/[^\/]+\/rules$/; const groupRulesAllUri = /^\/api\/groups\/[^\/]+\/rules\/all$/; const groupRuleUri = /^\/api\/groups\/[^\/]+\/rules\/(?!all)([^\/?]+)[?]?$/; export const applyGoogleMocksAsync = async () => { const gmailScope = nock(gmailApiBaseUrl); const oauth2Scope = nock(oauth2ApiBaseUrl); gmailScope .get(messageUri) .reply(messageCallback) .persist(); gmailScope .get(attachmentsUri) .reply(200, attachmentsCallback) .persist(); gmailScope .get(messageListUri) .reply(200, messageListCallback) .persist(); oauth2Scope .post(tokenUri, () => true) .reply(oauth2TokenCallback) .persist(); oauth2Scope .post(tokenInfoUri, () => true) .reply(200, { scope: Constants.scopes.join(' '), email: Constants.Mock.userEmail, }) .persist(); } export const applyLocalMocks = () => { const appScope = nock(localApiBaseUrl); appScope .post(oauthCallbackUri) .reply(function (uri, body) { return localPostCallback(uri, body, this.req.headers, googleOAuth2Middleware.redirect); }) .persist(); appScope .get(transactionsUri) .reply(function (uri, body) { return localGetCallback(uri, body, this.req.headers, transactionsController.get); }) .persist(); appScope .post(transactionsSaveUri) .reply(function (uri, body) { return localPostCallback(uri, body, this.req.headers, transactionsController.save); }) .persist(); appScope .patch(transactionsUpdateUri) .reply(function (uri, body) { return localPatchCallback(uri, body, this.req.headers, transactionsController.update); }) .persist(); appScope .get(transactionsLastIdsUri) .reply(function (uri, body) { return localGetCallback(uri, body, this.req.headers, googleOAuth2Middleware.protect, gmailTransactionsController.getLast); }) .persist(); appScope .post(transactionsGmailResolveUri) .reply(function (uri, body) { return localPostCallback(uri, body, this.req.headers, googleOAuth2Middleware.protect, gmailTransactionsController.resolve); }) .persist(); appScope .post(groupsUri) .reply(function (uri, body) { return localPostCallback(uri, body, this.req.headers, groupsController.new); }) .persist(); appScope .get(groupsAllUri) .reply(function (uri, body) { return localGetCallback(uri, body, this.req.headers, groupsController.getAll); }) .persist(); appScope .get(groupUri) .reply(function (uri, body) { return localGetCallback(uri, body, this.req.headers, groupsController.get); }) .persist(); appScope .delete(groupUri) .reply(function (uri, body) { return localDeleteCallback(uri, body, this.req.headers, groupsController.delete); }) .persist(); appScope .post(groupRulesNewUri) .reply(function (uri, body) { return localPostCallback(uri, body, this.req.headers, groupRulesController.new); }) .persist(); appScope .get(groupRulesAllUri) .reply(function (uri, body) { return localGetCallback(uri, body, this.req.headers, groupRulesController.getAll); }) .persist(); appScope .get(groupRuleUri) .reply(function (uri, body) { return localGetCallback(uri, body, this.req.headers, groupRulesController.get); }) .persist(); appScope .delete(groupRuleUri) .reply(function (uri, body) { return localDeleteCallback(uri, body, this.req.headers, groupRulesController.delete); }) .persist(); return appScope; } const messageListCallback = (uri: string, requestBody: nock.Body) => { const pageToken = new URL(gmailApiBaseUrl.concat(uri)).searchParams.get('pageToken'); const nextPageToken = new TransactionTestHelper() .withTestCases(gmailPaymentDetailsTestCases) .randomCount(true) // ensure test cases go through at least two pages .resolveTransactionIds() .reduce((_, i) => i); return pageToken === null ? { nextPageToken: nextPageToken, messages: new TransactionTestHelper() .withTestCases(gmailPaymentDetailsTestCases) .resolveTransactionIds(undefined, nextPageToken) .map(t => ({ id: t })), } : { messages: new TransactionTestHelper() .withTestCases(gmailPaymentDetailsTestCases) .resolveTransactionIds(pageToken) .map(t => ({ id: t })), }; }; const messageCallback = (uri: string, requestBody: nock.Body) => { const matches = uri.match(messageUri); Iif (matches === null) { throw new Error(`Failed to resolve message id from URI: ${uri}`); } const messageId = decodeURIComponent(matches[1]); Iif (messageId === Constants.Mock.errorTransactionSourceId) { return [500, { error: messageId, }]; } return [200, { id: messageId, payload: { parts: [{}, { body: { attachmentId: messageId } }] } }]; }; const attachmentsCallback = (uri: string, requestBody: nock.Body) => { const matches = uri.match(attachmentsUri); Iif (matches === null) { throw new Error(`Failed to resolve message id from URI: ${uri}`); } const messageId = decodeURIComponent(matches[1]); Iif (messageId == Constants.Mock.emptyTransactionSourceId) { return ""; } const testAttachmentDataBase64 = new TransactionTestHelper() .useGmailContext() .resolveTransactionDataSource(messageId, true); return { data: testAttachmentDataBase64 }; }; const oauth2TokenCallback = (uri: string, requestBody: nock.Body) => { const code = new URLSearchParams(requestBody).get('code'); Iif (code === Constants.Mock.authorizationCodeError) { return [500, { error: code, error_description: code, }]; } return [200, { access_token: Constants.Mock.accessToken, refresh_token: Constants.Mock.refreshToken, redirect_uri: Constants.defaultRedirectUri, }]; }; type Handler = (req: Request, res: Response, next: NextFunction) => Promise<void | Response>; const localGetCallback = async (uri: string, body: nock.Body, headers: Record<string, string>, ...handlers: Array<Handler>) => { return localGenericCallback(uri, body, headers, 'GET', ...handlers); } const localPostCallback = async (uri: string, body: nock.Body, headers: Record<string, string>, ...handlers: Array<Handler>) => { return localGenericCallback(uri, body, headers, 'POST', ...handlers); } const localPatchCallback = async (uri: string, body: nock.Body, headers: Record<string, string>, ...handlers: Array<Handler>) => { return localGenericCallback(uri, body, headers, 'PATCH', ...handlers); } const localDeleteCallback = async (uri: string, body: nock.Body, headers: Record<string, string>, ...handlers: Array<Handler>) => { return localGenericCallback(uri, body, headers, 'DELETE', ...handlers); } const localGenericCallback = async (uri: string, body: nock.Body, headers: Record<string, string>, method: string, ...handlers: Array<Handler>) => { const getHeaderHandler = (name: string) => { return headers[name.toLowerCase()]; }; const resolveParams = (uri: string) => { const matches = uri.replace(/[?].+/, '').match(/\/([^/]+)\/([^/]+)/g) || []; return matches.reduce((params, match) => { const [key, value] = match.split('/').filter(Boolean); params[key] = value; return params; }, {} as Record<string, string>); }; const request = { method: method, ...(method === 'POST' || method === 'PATCH') && { body: body }, ...(method === 'GET') && { query: uri.indexOf('?') < 0 ? {} : queryString.parse(uri.substring(uri.indexOf('?')+1)) }, get: getHeaderHandler, params: resolveParams(uri), } as Request; let statusCode: number = 0; let data: any; const statusHandler = (code: number) => { statusCode = code; return response; } const jsonHandler = (body?: any) => { data = body; return response; } const response = { status: statusHandler, json: jsonHandler, end: () => response, locals: {}, } as Response; let currentHandlerId = 0; const next = () => handlers.at(++currentHandlerId)?.(request, response, next); await handlers.at(0)?.(request, response, next); return [statusCode, data]; }; |