|
| 1 | +/** |
| 2 | + * ENV set to "node" to assert functionality executing on the server. |
| 3 | + * This setting is redundant to the default jest config in `jest.config.js`, |
| 4 | + * but is helpful to have here for clarity. |
| 5 | + * @jest-environment node |
| 6 | + */ |
| 7 | + |
| 8 | +import AuthorizationService from "./index"; |
| 9 | + |
| 10 | +describe("Authorization Service :: Node", () => { |
| 11 | + it("initializes with empty policy map", () => { |
| 12 | + expect(AuthorizationService.getPolicyMap()).toEqual([]); |
| 13 | + }); |
| 14 | + |
| 15 | + it("retrieves profile information asychronously with profileAuthorize() invocation", async () => { |
| 16 | + // mocking fetch for this test, but if it becomes relevant to other tests in this suite, it |
| 17 | + // should be elevated to the whole suite (describe) |
| 18 | + |
| 19 | + // ignoring this type error for now since it is for a mock in test |
| 20 | + // need to revisit this |
| 21 | + // @ts-ignore |
| 22 | + global.fetch = jest.fn(() => |
| 23 | + Promise.resolve({ |
| 24 | + json: () => Promise.resolve({ roles: ["contact_center_associate"] }), |
| 25 | + }), |
| 26 | + ); |
| 27 | + |
| 28 | + const aToken = "a_sample_token"; |
| 29 | + const aTokenHeader = "token-header"; |
| 30 | + const aProfileURL = "sample/profile/url"; |
| 31 | + const profile = await AuthorizationService.profileAuthorize( |
| 32 | + aProfileURL, |
| 33 | + aTokenHeader, |
| 34 | + aToken, |
| 35 | + ); |
| 36 | + |
| 37 | + expect(global.fetch).toHaveBeenCalledWith(aProfileURL, { |
| 38 | + headers: new Headers({ |
| 39 | + cookie: `token-header=a_sample_token`, |
| 40 | + }), |
| 41 | + }); |
| 42 | + expect(profile).toEqual({ roles: ["contact_center_associate"] }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("adds a policy when defineUser() called with new role(s)", () => { |
| 46 | + AuthorizationService.defineRole("contact_center_associate", [ |
| 47 | + "manualPayments", |
| 48 | + "existingOrderCancellation", |
| 49 | + "common_auth", |
| 50 | + ]); |
| 51 | + |
| 52 | + expect(AuthorizationService.getPolicyMap()).toEqual([ |
| 53 | + { |
| 54 | + role: "contact_center_associate", |
| 55 | + policies: [ |
| 56 | + "manualPayments", |
| 57 | + "existingOrderCancellation", |
| 58 | + "common_auth", |
| 59 | + ], |
| 60 | + }, |
| 61 | + ]); |
| 62 | + |
| 63 | + AuthorizationService.defineRole("store_associate", [ |
| 64 | + "ingredientLocking", |
| 65 | + "common_auth", |
| 66 | + ]); |
| 67 | + |
| 68 | + expect(AuthorizationService.getPolicyMap()).toEqual([ |
| 69 | + { |
| 70 | + role: "contact_center_associate", |
| 71 | + policies: [ |
| 72 | + "manualPayments", |
| 73 | + "existingOrderCancellation", |
| 74 | + "common_auth", |
| 75 | + ], |
| 76 | + }, |
| 77 | + { |
| 78 | + role: "store_associate", |
| 79 | + policies: ["ingredientLocking", "common_auth"], |
| 80 | + }, |
| 81 | + ]); |
| 82 | + }); |
| 83 | + |
| 84 | + it("does not add a new policy if the role has already been defined", () => { |
| 85 | + expect( |
| 86 | + AuthorizationService.defineRole("store_associate", ["ingredientLocking"]), |
| 87 | + ).toBe(false); |
| 88 | + |
| 89 | + expect(AuthorizationService.getPolicyMap()).toEqual([ |
| 90 | + { |
| 91 | + role: "contact_center_associate", |
| 92 | + policies: [ |
| 93 | + "manualPayments", |
| 94 | + "existingOrderCancellation", |
| 95 | + "common_auth", |
| 96 | + ], |
| 97 | + }, |
| 98 | + { |
| 99 | + role: "store_associate", |
| 100 | + policies: ["ingredientLocking", "common_auth"], |
| 101 | + }, |
| 102 | + ]); |
| 103 | + }); |
| 104 | + |
| 105 | + it("returns access confirmation or denial based on provided role information", () => { |
| 106 | + expect( |
| 107 | + AuthorizationService.userCan( |
| 108 | + ["contact_center_associate"], |
| 109 | + "manualPayments", |
| 110 | + ), |
| 111 | + ).toBe(true); |
| 112 | + expect( |
| 113 | + AuthorizationService.userCan(["store_associate"], "ingredientLocking"), |
| 114 | + ).toBe(true); |
| 115 | + expect( |
| 116 | + AuthorizationService.userCan( |
| 117 | + ["contact_center_associate"], |
| 118 | + "ingredientLocking", |
| 119 | + ), |
| 120 | + ).toBe(false); |
| 121 | + expect( |
| 122 | + AuthorizationService.userCan(["store_associate"], "manualPayments"), |
| 123 | + ).toBe(false); |
| 124 | + expect( |
| 125 | + AuthorizationService.userCan(["store_associate"], "common_auth"), |
| 126 | + ).toBe(true); |
| 127 | + expect( |
| 128 | + AuthorizationService.userCan( |
| 129 | + ["store_associate", "contact_center_associate"], |
| 130 | + "unregistered_policy", |
| 131 | + ), |
| 132 | + ).toBe(false); |
| 133 | + expect( |
| 134 | + AuthorizationService.userCan( |
| 135 | + ["store_associate", "contact_center_associate"], |
| 136 | + "common_auth", |
| 137 | + ), |
| 138 | + ).toBe(true); |
| 139 | + }); |
| 140 | +}); |
0 commit comments