|
| 1 | +/** |
| 2 | + * @prettier |
| 3 | + */ |
| 4 | +import { fromJS } from "immutable" |
| 5 | +import { authorize, logout } from "corePlugins/auth/wrap-actions" |
| 6 | + |
| 7 | +describe("Cookie based apiKey persistence in document.cookie", () => { |
| 8 | + beforeEach(() => { |
| 9 | + let cookieJar = "" |
| 10 | + jest.spyOn(document, "cookie", "set").mockImplementation((cookie) => { |
| 11 | + cookieJar += cookie |
| 12 | + }) |
| 13 | + jest.spyOn(document, "cookie", "get").mockImplementation(() => cookieJar) |
| 14 | + }) |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + jest.restoreAllMocks() |
| 18 | + }) |
| 19 | + |
| 20 | + describe("given persistAuthorization=true", () => { |
| 21 | + it("should persist cookie in document.cookie", () => { |
| 22 | + const system = { |
| 23 | + getConfigs: () => ({ |
| 24 | + persistAuthorization: true, |
| 25 | + }), |
| 26 | + } |
| 27 | + const payload = { |
| 28 | + api_key: { |
| 29 | + schema: fromJS({ |
| 30 | + type: "apiKey", |
| 31 | + name: "apiKeyCookie", |
| 32 | + in: "cookie", |
| 33 | + }), |
| 34 | + value: "test", |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + authorize(jest.fn(), system)(payload) |
| 39 | + |
| 40 | + expect(document.cookie).toEqual( |
| 41 | + "apiKeyCookie=test; SameSite=None; Secure" |
| 42 | + ) |
| 43 | + }) |
| 44 | + |
| 45 | + it("should delete cookie from document.cookie", () => { |
| 46 | + const payload = fromJS({ |
| 47 | + api_key: { |
| 48 | + schema: { |
| 49 | + type: "apiKey", |
| 50 | + name: "apiKeyCookie", |
| 51 | + in: "cookie", |
| 52 | + }, |
| 53 | + value: "test", |
| 54 | + }, |
| 55 | + }) |
| 56 | + const system = { |
| 57 | + getConfigs: () => ({ |
| 58 | + persistAuthorization: true, |
| 59 | + }), |
| 60 | + authSelectors: { |
| 61 | + authorized: () => payload, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + logout(jest.fn(), system)(["api_key"]) |
| 66 | + |
| 67 | + expect(document.cookie).toEqual("apiKeyCookie=; Max-Age=-99999999") |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + describe("given persistAuthorization=false", () => { |
| 72 | + it("shouldn't persist cookie in document.cookie", () => { |
| 73 | + const system = { |
| 74 | + getConfigs: () => ({ |
| 75 | + persistAuthorization: false, |
| 76 | + }), |
| 77 | + } |
| 78 | + const payload = { |
| 79 | + api_key: { |
| 80 | + schema: fromJS({ |
| 81 | + type: "apiKey", |
| 82 | + name: "apiKeyCookie", |
| 83 | + in: "cookie", |
| 84 | + }), |
| 85 | + value: "test", |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + authorize(jest.fn(), system)(payload) |
| 90 | + |
| 91 | + expect(document.cookie).toEqual("") |
| 92 | + }) |
| 93 | + |
| 94 | + it("should delete cookie from document.cookie", () => { |
| 95 | + const payload = fromJS({ |
| 96 | + api_key: { |
| 97 | + schema: { |
| 98 | + type: "apiKey", |
| 99 | + name: "apiKeyCookie", |
| 100 | + in: "cookie", |
| 101 | + }, |
| 102 | + value: "test", |
| 103 | + }, |
| 104 | + }) |
| 105 | + const system = { |
| 106 | + getConfigs: () => ({ |
| 107 | + persistAuthorization: false, |
| 108 | + }), |
| 109 | + authSelectors: { |
| 110 | + authorized: () => payload, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + logout(jest.fn(), system)(["api_key"]) |
| 115 | + |
| 116 | + expect(document.cookie).toEqual("") |
| 117 | + }) |
| 118 | + }) |
| 119 | +}) |
0 commit comments