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 | 1411x 1x 1x 1x 1x 204x 1x 64x 64x 64x 64x 64x 1x 65x 65x 65x 390x 390x 390x 65x 1x 23x 1x 257x 257x 74x 92x 92x 257x 1x 20x 20x 1x 40x 40x 40x 1x 6x 6x 6x 6x 6x 4x 6x 6x 1x 16x 16x 16x 16x 16x 1x 7x 7x 7x 7x 7x 2x 7x 7x 7x 7x 7x | import { default as supertestRequest } from "supertest"; import app from "@cooper/backend/src/server"; import TestAgent from "supertest/lib/agent"; import Test from "supertest/lib/test"; import { expect } from "chai"; import { contract } from "@cooper/ts-rest/src/contract"; import { Budgeting$Account, Budgeting$Category, Budgeting$Transaction } from "@cooper/ts-rest/src/types"; /** * Factory to create supertest object with our backend Express app */ const request = function () { return supertestRequest(app); }; /** * Factory to create an authed supertest object factory, * where cookies are persisted with subsequent requests */ const authenticate = async function ({ username, password }: { username: string; password: string }) { const newAuthedRequest = supertestRequest.agent(app); // Login const response = await newAuthedRequest.post("/api/auth/login").send({ username, password, }); // Put the sessionId cookie in the cookie jar for subsequent requests const cookies = extractCookies(response); newAuthedRequest.jar.setCookie(`id=${cookies[0]["id"]}`); return newAuthedRequest; }; /** * Extracts the set-cookie header on a supertest response * @param response Supertest response object * @returns Object containing cookie fields, keys are strings, values are strings or undefined */ function extractCookies(response: { headers: { [key: string]: string } }): { [key: string]: string; }[] { Iif (!("set-cookie" in response.headers)) throw new Error("Set-Cookie not in response headers"); const cookies = Object.values(response.headers["set-cookie"]).map((cookieStr: string) => { return cookieStr.split("; ").reduce((obj, item) => { const [key, value] = item.split("="); obj[key] = value; return obj; }, {}); }); return cookies; } /** * Given a ts-rest route object, returns the a string representing * what the test is testing for, e.g. POST /api/auth/login * @param route ts-rest route we are testing for * @returns {string} String in format [route.method] [route.path] */ function testFor(route: { method: string; path: string }): string { return `${route.method} ${route.path}`; } /** * After passing a Supertest request object, the ts-rest route to * be tested and optionally args used as route path arguments, * returns the Supertest route test with method and path already set * * @param request Supertest request object * @param route ts-rest route to be testing * @param args Optional args object, keys represent pattern in route path * and values are strings that will replace the pattern in the path. E.g. * * Path: /api/auth/users/:username * Args: { * username: "ianyeoh" * } * * Resulting path: /api/auth/users/ianyeoh * * @returns Supertest route test with method and path already set */ function testRoute( request: TestAgent, route: { method: "GET" | "POST" | "DELETE" | "PUT" | "PATCH"; path: string; }, args?: { [key: string]: string; }, ): Test { let routePath = route.path; if (args) { Object.entries(args).forEach(([key, value]) => { if (routePath.includes(`:${key}`)) { routePath = routePath.replace(`:${key}`, value); } else E{ throw new Error(`:${key} does not exist in path ${routePath}`); } }); } return request[route.method.toLowerCase()](routePath); } /** * Ensures route cannot be accessed by an unauthenticated session * @param route ts-rest route to be tested * @returns Fully configured mocha unit test */ function testProtected(route: { method: "GET" | "POST" | "DELETE" | "PUT" | "PATCH"; path: string }) { return it("should fail because not logged in", function (done) { testRoute(request(), route).expect(401, done); }); } async function createWorkspace( authedRequest: TestAgent<Test>, workspace: { name: string; }, ) { const { newWorkspace } = contract.protected.budgeting.workspaces; // Create workspace const response = await testRoute(authedRequest, newWorkspace).send(workspace).expect(200); // Basic generic validation expect(response.body.workspace).to.contain.keys("workspaceId"); return response.body.workspace; } async function getUsersWorkspaces( authedRequest: TestAgent<Test>, opts?: { size?: number; includes?: number; excludes?: number; }, ) { const { getWorkspaces } = contract.protected.budgeting.workspaces; const response = await testRoute(authedRequest, getWorkspaces).expect(200); // Basic generic validation expect(response.body.workspaces).to.be.an("array"); if (opts) { // That the array contains opts.size number of workspaces Iif (opts.size) { expect(response.body.workspaces).to.have.lengthOf(opts.size); } // That the array contains the workspace with matching workspaceId if (opts.includes) { expect( response.body.workspaces.some((workspace: { workspaceId: number }) => { return workspace.workspaceId === opts.includes; }), ).to.equal(true); } // That the array does not contain an account with workspaceId if (opts.excludes) { expect( response.body.workspaces.some((workspace: { workspaceId: number }) => { return workspace.workspaceId === opts.excludes; }), ).to.equal(false); } } return response.body.workspaces; } async function createAccount(authedRequest: TestAgent<Test>, workspaceId: string, account: Budgeting$Account) { const newAccount = contract.protected.budgeting.workspaces.byId.accounts.newAccount; const request = await testRoute(authedRequest, newAccount, { workspaceId, }) .send({ description: account.description, name: account.name, bank: account.bank }) .expect(200); expect(request.body.account).to.deep.include({ description: account.description, name: account.name, bank: account.bank, workspace: workspaceId, }); expect(request.body.account).to.have.own.property("accountId"); return request.body.account; } async function createCategory(authedRequest: TestAgent<Test>, workspaceId: string, category: Budgeting$Category) { const newCategory = contract.protected.budgeting.workspaces.byId.categories.newCategory; const request = await testRoute(authedRequest, newCategory, { workspaceId, }) .send({ name: category.name }) .expect(200); expect(request.body.category).to.deep.include({ name: category.name, workspace: workspaceId, }); expect(request.body.category).to.have.own.property("categoryId"); return request.body.category; } async function createTransaction( authedRequest: TestAgent<Test>, workspaceId: string, accountId: string, transaction: Budgeting$Transaction, ) { const newTransaction = contract.protected.budgeting.workspaces.byId.transactions.newTransaction; const request = await testRoute(authedRequest, newTransaction, { workspaceId, }) .send({ account: accountId, date: transaction.date, description: transaction.description, category: transaction.category, amount: transaction.amount, comments: transaction.comments, }) .expect(200); expect(request.body.transaction).to.deep.include({ account: accountId, description: transaction.description, category: transaction.category, amount: transaction.amount, comments: transaction.comments, workspace: workspaceId, }); expect(request.body.transaction).to.have.own.property("transactionId"); return request.body.transaction; } export { extractCookies, request, authenticate, testFor, testRoute, testProtected, createWorkspace, getUsersWorkspaces, createAccount, createCategory, createTransaction, }; |