import {
  getGroups,
  getProviders,
  hasRole,
  Permissions,
  Providers, Roles
} from "./roles";
import { TEST_EXPECTED_GROUPS, TEST_TOKEN } from "./testing";

describe("Roles Helper", () => {
  Roles.MAGNAGROUP = "68273225-9da4-4fa7-aea5-38e16ec471fe"
  Providers.MAGNA = "Magna"

  it("Check FiskerRead permission", () => {
    expect(
      hasRole([Roles.CREATE], Permissions.FiskerRead, [Providers.FISKER])
    ).toEqual(true);
    expect(
      hasRole([Roles.READ], Permissions.FiskerRead, [Providers.FISKER])
    ).toEqual(true);
  });

  it("Check FiskerCreate permission", () => {
    expect(
      hasRole([Roles.CREATE], Permissions.FiskerCreate, [Providers.FISKER])
    ).toEqual(true);
    expect(
      hasRole([Roles.READ], Permissions.FiskerCreate, [Providers.FISKER])
    ).toEqual(false);
  });

  it("Check FiskerDelete permission", () => {
    expect(
      hasRole([Roles.DELETE], Permissions.FiskerDelete, [Providers.FISKER])
    ).toEqual(true);
  });

  it("Check FiskerSupplierAdmin permission", () => {
    expect(
      hasRole([Roles.APPROVESUPPLIERS], Permissions.FiskerSupplierAdmin, [Providers.FISKER])
    ).toEqual(true);
  });

  it("Check FiskerMagnaCertificate permission", () => {
    expect(
      hasRole([Roles.CERTIFICATES], Permissions.FiskerMagnaCertificate, [Providers.FISKER])
    ).toEqual(true);
    expect(
      hasRole([Roles.MAGNAGROUP], Permissions.FiskerMagnaCertificate, [Providers.MAGNA])
    ).toEqual(true);
    expect(
      hasRole([Roles.MANUFACTURE], Permissions.FiskerMagnaCertificate, [Providers.FISKER_QA])
    ).toEqual(true);
  });

  it("Check FiskerTools permission", () => {
    expect(
      hasRole([Roles.APPROVESUPPLIERS], Permissions.FiskerTools, [Providers.FISKER])
    ).toEqual(false);
    expect(
      hasRole([Roles.CERTIFICATES], Permissions.FiskerTools, [Providers.FISKER])
    ).toEqual(true);
    expect(
      hasRole([Roles.MAGNAGROUP], Permissions.FiskerTools, [Providers.MAGNA])
    ).toEqual(true);
    expect(
      hasRole([Roles.MANUFACTURE], Permissions.FiskerTools, [Providers.FISKER_QA])
    ).toEqual(true);
  });

  it("Check FiskerUpdateDeploy permission", () => {
    expect(
      hasRole([Roles.UPDATEDEPLOY], Permissions.FiskerUpdateDeploy, [Providers.FISKER])
    ).toEqual(true);
    expect(
      hasRole([Roles.UPDATEDEPLOY], Permissions.FiskerUpdateDeploy, [Providers.MAGNA])
    ).toEqual(false);
  });

  it("Check Magna permission", () => {
    expect(
      hasRole([Roles.MAGNAGROUP], Permissions.Magna, [Providers.MAGNA])
    ).toEqual(true);
    expect(
      hasRole([Roles.MANUFACTURE], Permissions.Magna, [Providers.FISKER_QA])
    ).toEqual(true);
  });

  it("Check FiskerMagnaRead permission", () => {
    expect(
      hasRole([Roles.CREATE], Permissions.FiskerMagnaRead, [Providers.FISKER])
    ).toEqual(true);
    expect(
      hasRole([Roles.READ], Permissions.FiskerMagnaRead, [Providers.FISKER])
    ).toEqual(true);
    expect(
      hasRole([Roles.MAGNAGROUP], Permissions.FiskerMagnaRead, [Providers.MAGNA])
    ).toEqual(true);
    expect(
      hasRole([Roles.MANUFACTURE], Permissions.FiskerMagnaRead, [Providers.FISKER_QA])
    ).toEqual(true);
  });

  it("Check FiskerMagnaCreate permission", () => {
    expect(
      hasRole([Roles.CREATE], Permissions.FiskerMagnaCreate, [Providers.FISKER])
    ).toEqual(true);
    expect(
      hasRole([Roles.MAGNAGROUP], Permissions.FiskerMagnaCreate, [Providers.MAGNA])
    ).toEqual(true);
    expect(
      hasRole([Roles.MANUFACTURE], Permissions.FiskerMagnaCreate, [Providers.FISKER_QA])
    ).toEqual(true);
  });

  it("Check FiskerMagnaDelete permission", () => {
    expect(
      hasRole([Roles.DELETE], Permissions.FiskerMagnaDelete, [Providers.FISKER])
    ).toEqual(true);
    expect(
      hasRole([Roles.MAGNAGROUP], Permissions.FiskerMagnaDelete, [Providers.MAGNA])
    ).toEqual(true);
    expect(
      hasRole([Roles.MANUFACTURE], Permissions.FiskerMagnaDelete, [Providers.FISKER_QA])
    ).toEqual(true);
  });

  it("Get groups", () => {
    expect(getGroups(TEST_TOKEN)).toEqual(TEST_EXPECTED_GROUPS);
  });

  it("Get providers", () => {
    expect(getProviders(TEST_TOKEN)).toEqual(["AzureAD"]);
  });
});
