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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { expect } from "chai"; import { request, authenticate, extractCookies, testFor, testRoute, testProtected, } from "@cooper/backend/src/tests/utils"; import { generateMock } from "@anatine/zod-mock"; import { Auth$UserSchema } from "@cooper/ts-rest/src/types"; import { seed } from "@cooper/backend/src/tests/mocking"; import { contract } from "@cooper/ts-rest/src/contract"; const { login, logout, signup, validSession, getSessions } = contract.public.auth; const existingUser = generateMock(Auth$UserSchema, { seed }); describe(testFor(signup), () => { it("should fail with bad data", async function () { // No body await testRoute(request(), signup).expect(400); // Empty fields await testRoute(request(), signup) .send({ username: "", firstName: "", lastName: "", password: "", }) .expect(400); }); it("should succeed and allow login", async function () { const newUser = generateMock(Auth$UserSchema, { seed: seed + 1 }); // Should fail here, user does not exist await testRoute(request(), login) .send({ username: newUser.username, password: newUser.password, }) .expect(401); // Create our user, should succeed await testRoute(request(), signup).send(newUser).expect(200); // Login should work now await testRoute(request(), login) .send({ username: newUser.username, password: newUser.password, }) .expect(200); }); }); describe(testFor(login), () => { // Exploring every possible branch here because login is a critical function it("should fail because of empty body", function (done) { testRoute(request(), login).expect(400, done); }); it("should fail because of invalid body format", function (done) { testRoute(request(), login) .send({ not: "a valid field", }) .expect(400, done); }); it("should fail because of invalid data types", function (done) { testRoute(request(), login) .send({ username: 123, password: new Date(), }) .expect(400, done); }); it("should fail because of a missing field", function (done) { testRoute(request(), login) .send({ username: "ianyeoh", }) .expect(400, done); }); it("should fail because user does not exist", function (done) { testRoute(request(), login) .send({ username: "user that does not exist", password: "password", }) .expect(401, done); }); it("should succeed and set-cookie session id (secure, http-only, same-site, with expiry)", async function () { const response = await testRoute(request(), login).send({ username: existingUser.username, password: existingUser.password, }); const cookies = extractCookies(response); const sessionCookie = cookies.find((cookie) => { return cookie["id"] != null; }); expect(sessionCookie).to.not.equal(null); Iif (sessionCookie == undefined) return; expect(sessionCookie["id"]).to.not.equal(null); expect(sessionCookie["Expires"]).to.not.equal(null); expect(sessionCookie["SameSite"]).to.equal("Strict"); expect(sessionCookie["Secure"]).to.equal(undefined); expect(sessionCookie["HttpOnly"]).to.equal(undefined); }); it("should give access to a protected route", async function () { const authedRequest = await authenticate({ username: existingUser.username, password: existingUser.password, }); await testRoute(authedRequest, contract.protected.users.getSelf).expect(200); }); }); describe(testFor(logout), () => { // Testing security critical paths in logging out it("should fail because not logged in", function (done) { testRoute(request(), logout).expect(401, done); }); it("should succeed after logging in", async function () { const authedRequest = await authenticate({ username: existingUser.username, password: existingUser.password, }); await testRoute(authedRequest, logout).expect(200); }); it("should fail when logging out twice", async function () { const authedRequest = await authenticate({ username: existingUser.username, password: existingUser.password, }); await testRoute(authedRequest, logout).expect(200); await testRoute(authedRequest, logout).expect(401); }); it("should prevent access to protected routes after logging in", async function () { const authedRequest = await authenticate({ username: existingUser.username, password: existingUser.password, }); await testRoute(authedRequest, logout).expect(200); await testRoute(authedRequest, contract.protected.users.getSelf).expect(401); }); }); describe(testFor(validSession), () => { testProtected(validSession); it("should succeed when logged in, and fail after logging out", async function () { const authedRequest = await authenticate({ username: existingUser.username, password: existingUser.password, }); await testRoute(authedRequest, validSession).expect(200); // Logout and test again await testRoute(authedRequest, logout).expect(200); await testRoute(authedRequest, validSession).expect(401); }); }); describe(testFor(getSessions), () => { testProtected(getSessions); it("should succeed when logged in", async function () { const authedRequest = await authenticate({ username: existingUser.username, password: existingUser.password, }); await testRoute(authedRequest, getSessions).expect(200); }); }); |