All files / src/tests users.spec.ts

100% Statements 27/27
100% Branches 0/0
100% Functions 5/5
100% Lines 24/24

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 801x 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, 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 { getSelf, getUser } = contract.protected.users;
const { signup } = contract.public.auth;
 
const existingUser = generateMock(Auth$UserSchema, { seed });
 
describe(testFor(getSelf), () => {
  testProtected(getSelf);
 
  it("should return our own user data", async function () {
    // Login
    const authedRequest = await authenticate({
      username: existingUser.username,
      password: existingUser.password,
    });
 
    // Get self
    const response = await testRoute(authedRequest, getSelf).expect(200);
 
    expect(response.body.user).to.deep.equal({
      username: existingUser.username,
      firstName: existingUser.firstName,
      lastName: existingUser.lastName,
    });
  });
});
 
describe(testFor(getUser), () => {
  testProtected(getUser);
 
  it("should return our own user data", async function () {
    // Login
    const authedRequest = await authenticate({
      username: existingUser.username,
      password: existingUser.password,
    });
 
    // Get self
    const response = await testRoute(authedRequest, getUser, {
      username: existingUser.username,
    });
 
    expect(response.body.user).to.deep.equal({
      username: existingUser.username,
      firstName: existingUser.firstName,
      lastName: existingUser.lastName,
    });
  });
 
  it("should return other user data", async function () {
    const newUser = generateMock(Auth$UserSchema, { seed: seed + 1 });
 
    // Create other user, should succeed
    await testRoute(request(), signup).send(newUser).expect(200);
 
    // Login
    const authedRequest = await authenticate({
      username: newUser.username,
      password: newUser.password,
    });
 
    // Get other user's data
    const response = await testRoute(authedRequest, getUser, {
      username: newUser.username,
    });
 
    expect(response.body.user).to.deep.equal({
      username: newUser.username,
      firstName: newUser.firstName,
      lastName: newUser.lastName,
    });
  });
});