Merge branch 'master' into next
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { loaded } from "corePlugins/auth/configs-extensions/wrap-actions"
|
||||
|
||||
describe("loaded hook", () => {
|
||||
describe("authorization data restoration", () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
})
|
||||
it("retrieve `authorized` value from `localStorage`", () => {
|
||||
const system = {
|
||||
getConfigs: () => ({
|
||||
persistAuthorization: true
|
||||
}),
|
||||
authActions: {
|
||||
|
||||
}
|
||||
}
|
||||
jest.spyOn(Object.getPrototypeOf(window.localStorage), "getItem")
|
||||
|
||||
loaded(jest.fn(), system)()
|
||||
expect(localStorage.getItem).toHaveBeenCalled()
|
||||
expect(localStorage.getItem).toHaveBeenCalledWith("authorized")
|
||||
})
|
||||
it("restore authorization data when a value exists", () => {
|
||||
const system = {
|
||||
getConfigs: () => ({
|
||||
persistAuthorization: true
|
||||
}),
|
||||
authActions: {
|
||||
restoreAuthorization: jest.fn(() => {})
|
||||
}
|
||||
}
|
||||
const mockData = {"api_key": {}}
|
||||
localStorage.setItem("authorized", JSON.stringify(mockData))
|
||||
loaded(jest.fn(), system)()
|
||||
expect(system.authActions.restoreAuthorization).toHaveBeenCalled()
|
||||
expect(system.authActions.restoreAuthorization).toHaveBeenCalledWith({
|
||||
authorized: mockData
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,4 @@
|
||||
|
||||
import { execute } from "corePlugins/auth/spec-wrap-actions"
|
||||
import { execute } from "corePlugins/auth/spec-extensions/wrap-actions"
|
||||
|
||||
describe("spec plugin - actions", function(){
|
||||
|
||||
119
test/unit/core/plugins/auth/wrap-actions.js
Normal file
119
test/unit/core/plugins/auth/wrap-actions.js
Normal file
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* @prettier
|
||||
*/
|
||||
import { fromJS } from "immutable"
|
||||
import { authorize, logout } from "corePlugins/auth/wrap-actions"
|
||||
|
||||
describe("Cookie based apiKey persistence in document.cookie", () => {
|
||||
beforeEach(() => {
|
||||
let cookieJar = ""
|
||||
jest.spyOn(document, "cookie", "set").mockImplementation((cookie) => {
|
||||
cookieJar += cookie
|
||||
})
|
||||
jest.spyOn(document, "cookie", "get").mockImplementation(() => cookieJar)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe("given persistAuthorization=true", () => {
|
||||
it("should persist cookie in document.cookie", () => {
|
||||
const system = {
|
||||
getConfigs: () => ({
|
||||
persistAuthorization: true,
|
||||
}),
|
||||
}
|
||||
const payload = {
|
||||
api_key: {
|
||||
schema: fromJS({
|
||||
type: "apiKey",
|
||||
name: "apiKeyCookie",
|
||||
in: "cookie",
|
||||
}),
|
||||
value: "test",
|
||||
},
|
||||
}
|
||||
|
||||
authorize(jest.fn(), system)(payload)
|
||||
|
||||
expect(document.cookie).toEqual(
|
||||
"apiKeyCookie=test; SameSite=None; Secure"
|
||||
)
|
||||
})
|
||||
|
||||
it("should delete cookie from document.cookie", () => {
|
||||
const payload = fromJS({
|
||||
api_key: {
|
||||
schema: {
|
||||
type: "apiKey",
|
||||
name: "apiKeyCookie",
|
||||
in: "cookie",
|
||||
},
|
||||
value: "test",
|
||||
},
|
||||
})
|
||||
const system = {
|
||||
getConfigs: () => ({
|
||||
persistAuthorization: true,
|
||||
}),
|
||||
authSelectors: {
|
||||
authorized: () => payload,
|
||||
},
|
||||
}
|
||||
|
||||
logout(jest.fn(), system)(["api_key"])
|
||||
|
||||
expect(document.cookie).toEqual("apiKeyCookie=; Max-Age=-99999999")
|
||||
})
|
||||
})
|
||||
|
||||
describe("given persistAuthorization=false", () => {
|
||||
it("shouldn't persist cookie in document.cookie", () => {
|
||||
const system = {
|
||||
getConfigs: () => ({
|
||||
persistAuthorization: false,
|
||||
}),
|
||||
}
|
||||
const payload = {
|
||||
api_key: {
|
||||
schema: fromJS({
|
||||
type: "apiKey",
|
||||
name: "apiKeyCookie",
|
||||
in: "cookie",
|
||||
}),
|
||||
value: "test",
|
||||
},
|
||||
}
|
||||
|
||||
authorize(jest.fn(), system)(payload)
|
||||
|
||||
expect(document.cookie).toEqual("")
|
||||
})
|
||||
|
||||
it("should delete cookie from document.cookie", () => {
|
||||
const payload = fromJS({
|
||||
api_key: {
|
||||
schema: {
|
||||
type: "apiKey",
|
||||
name: "apiKeyCookie",
|
||||
in: "cookie",
|
||||
},
|
||||
value: "test",
|
||||
},
|
||||
})
|
||||
const system = {
|
||||
getConfigs: () => ({
|
||||
persistAuthorization: false,
|
||||
}),
|
||||
authSelectors: {
|
||||
authorized: () => payload,
|
||||
},
|
||||
}
|
||||
|
||||
logout(jest.fn(), system)(["api_key"])
|
||||
|
||||
expect(document.cookie).toEqual("")
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,4 @@
|
||||
import { downloadConfig } from "corePlugins/configs/spec-actions"
|
||||
import { loaded } from "corePlugins/configs/actions"
|
||||
|
||||
describe("configs plugin - actions", () => {
|
||||
|
||||
@@ -23,43 +22,4 @@ describe("configs plugin - actions", () => {
|
||||
expect(fetchSpy).toHaveBeenCalledWith(req)
|
||||
})
|
||||
})
|
||||
|
||||
describe("loaded hook", () => {
|
||||
describe("authorization data restoration", () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
})
|
||||
it("retrieve `authorized` value from `localStorage`", () => {
|
||||
const system = {
|
||||
getConfigs: () => ({
|
||||
persistAuthorization: true
|
||||
}),
|
||||
authActions: {
|
||||
|
||||
}
|
||||
}
|
||||
jest.spyOn(Object.getPrototypeOf(window.localStorage), "getItem")
|
||||
loaded()(system)
|
||||
expect(localStorage.getItem).toHaveBeenCalled()
|
||||
expect(localStorage.getItem).toHaveBeenCalledWith("authorized")
|
||||
})
|
||||
it("restore authorization data when a value exists", () => {
|
||||
const system = {
|
||||
getConfigs: () => ({
|
||||
persistAuthorization: true
|
||||
}),
|
||||
authActions: {
|
||||
restoreAuthorization: jest.fn(() => {})
|
||||
}
|
||||
}
|
||||
const mockData = {"api_key": {}}
|
||||
localStorage.setItem("authorized", JSON.stringify(mockData))
|
||||
loaded()(system)
|
||||
expect(system.authActions.restoreAuthorization).toHaveBeenCalled()
|
||||
expect(system.authActions.restoreAuthorization).toHaveBeenCalledWith({
|
||||
authorized: mockData
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user