{"size": 1293, "ext": "js", "max_stars_repo_path": "auth/authMiddleware.test.js", "max_stars_repo_name": "bw-devDeskQueue2/back-end", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "auth/authMiddleware.test.js", "max_issues_repo_name": "bw-devDeskQueue2/back-end", "max_issues_repo_issues_event_min_datetime": "2021-09-02T12:49:06.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T12:49:06.000Z", "max_forks_repo_path": "auth/authMiddleware.test.js", "max_forks_repo_name": "bw-devDeskQueue2/back-end", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 30.0697674419, "max_line_length": 67, "alphanum_fraction": 0.5815931941}
const request = require("supertest");
const server = require("../server");
const knex = require("../data/dbConfig");
const { getStudentToken } = require("./authTestHelperFunctions");

describe("authMiddleware", () => {
  const bU = "/api/auth_test";
  beforeAll(async done => {
    await knex.seed.run();
    studentToken = await getStudentToken();
    done();
  });
  it("Returns an error to users who don't send a token", () =>
    request(server)
      .get(bU)
      .expect(401)
      .then(r => {
        expect(r.body.message).toContain("token");
      }));
  it("Returns an error to users send an invalid token", () =>
    request(server)
      .get(bU)
      .set("Authorization", "Bearer invalid_jwt")
      .expect(403)
      .then(r => {
        expect(r.body.message).toContain("malformed");
      }));
  it("Allows users to proceed with a valid token", () =>
    request(server)
      .get(bU)
      .set("Authorization", "Bearer " + studentToken)
      .expect(200)
      .then(({ body }) => {
        expect(body.id).toBe(1);
        expect(body.username).toBe("test_student");
        expect(body.roles).toEqual(["student"]);
      }));
  afterAll(async () => {
    // avoid jest open handle error
    await new Promise(resolve => setTimeout(() => resolve(), 100));
  });
});
