{"size": 1072, "ext": "js", "max_stars_repo_path": "src/__tests__/guardMiddleware.test.js", "max_stars_repo_name": "yeldiRium/telegraf-authentication-middleware", "max_stars_repo_stars_event_min_datetime": "2020-04-09T15:38:56.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-08T19:43:35.000Z", "max_issues_repo_path": "src/__tests__/guardMiddleware.test.js", "max_issues_repo_name": "yeldiRium/telegraf-authentication-middleware", "max_issues_repo_issues_event_min_datetime": "2019-11-09T00:13:38.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-08T07:39:29.000Z", "max_forks_repo_path": "src/__tests__/guardMiddleware.test.js", "max_forks_repo_name": "yeldiRium/telegraf-authentication-middleware", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": 200, "max_forks_count": null, "avg_line_length": 29.7777777778, "max_line_length": 90, "alphanum_fraction": 0.6632462687}
const guardMiddleware = require("../guardMiddleware");

describe("guard middleware", () => {
  it("is a function", async () => {
    expect(typeof guardMiddleware).toEqual("function");
  });

  it("throws an error if parameter unauthenticatedAccessHandler is missing", async () => {
    expect(() => guardMiddleware()).toThrow();
  });

  it("calls the unauthenticated access handler if no user is in the ctx", async () => {
    const unauthenticatedAccessHandler = jest.fn();
    const ctx = {};
    const next = jest.fn();

    await guardMiddleware({ unauthenticatedAccessHandler })(ctx, next);

    expect(unauthenticatedAccessHandler).toHaveBeenCalled();
    expect(next).not.toHaveBeenCalled();
  });

  it("calls next if a user is in the ctx", async () => {
    const unauthenticatedAccessHandler = jest.fn();
    const ctx = {
      user: {},
    };
    const next = jest.fn();

    await guardMiddleware({ unauthenticatedAccessHandler })(ctx, next);

    expect(unauthenticatedAccessHandler).not.toHaveBeenCalled();
    expect(next).toHaveBeenCalled();
  });
});
