Co-authored-by: Theo Markovic <theodore.markovic@svenskaspel.se>
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Map } from "immutable"
|
import { Map } from "immutable"
|
||||||
import {
|
import {
|
||||||
authorizeRequest,
|
authorizeRequest,
|
||||||
authorizeAccessCodeWithFormParams,
|
authorizeAccessCodeWithFormParams,
|
||||||
authorizeWithPersistOption,
|
authorizeWithPersistOption,
|
||||||
authorizeOauth2WithPersistOption,
|
authorizeOauth2WithPersistOption,
|
||||||
logoutWithPersistOption,
|
logoutWithPersistOption,
|
||||||
@@ -57,7 +57,7 @@ describe("auth plugin - actions", () => {
|
|||||||
},
|
},
|
||||||
"http://specs/authorize"
|
"http://specs/authorize"
|
||||||
],
|
],
|
||||||
].forEach(([{oas3, server, effectiveServer, scheme, host, url}, expectedFetchUrl]) => {
|
].forEach(([{ oas3, server, effectiveServer, scheme, host, url }, expectedFetchUrl]) => {
|
||||||
it("should resolve authorization endpoint against the server URL", () => {
|
it("should resolve authorization endpoint against the server URL", () => {
|
||||||
|
|
||||||
// Given
|
// Given
|
||||||
@@ -72,6 +72,9 @@ describe("auth plugin - actions", () => {
|
|||||||
authSelectors: {
|
authSelectors: {
|
||||||
getConfigs: () => ({})
|
getConfigs: () => ({})
|
||||||
},
|
},
|
||||||
|
errActions: {
|
||||||
|
newAuthErr: () => ({})
|
||||||
|
},
|
||||||
oas3Selectors: {
|
oas3Selectors: {
|
||||||
selectedServer: () => server,
|
selectedServer: () => server,
|
||||||
serverEffectiveValue: () => effectiveServer || server
|
serverEffectiveValue: () => effectiveServer || server
|
||||||
@@ -89,7 +92,7 @@ describe("auth plugin - actions", () => {
|
|||||||
|
|
||||||
// Then
|
// Then
|
||||||
expect(system.fn.fetch.mock.calls.length).toEqual(1)
|
expect(system.fn.fetch.mock.calls.length).toEqual(1)
|
||||||
expect(system.fn.fetch.mock.calls[0][0]).toEqual(expect.objectContaining({url: expectedFetchUrl}))
|
expect(system.fn.fetch.mock.calls[0][0]).toEqual(expect.objectContaining({ url: expectedFetchUrl }))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -111,6 +114,9 @@ describe("auth plugin - actions", () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
errActions: {
|
||||||
|
newAuthErr: () => ({})
|
||||||
|
},
|
||||||
specSelectors: {
|
specSelectors: {
|
||||||
isOAS3: () => false,
|
isOAS3: () => false,
|
||||||
operationScheme: () => "https",
|
operationScheme: () => "https",
|
||||||
@@ -139,6 +145,9 @@ describe("auth plugin - actions", () => {
|
|||||||
fn: {
|
fn: {
|
||||||
fetch: jest.fn().mockImplementation(() => Promise.resolve())
|
fetch: jest.fn().mockImplementation(() => Promise.resolve())
|
||||||
},
|
},
|
||||||
|
errActions: {
|
||||||
|
newAuthErr: () => ({})
|
||||||
|
},
|
||||||
getConfigs: () => ({}),
|
getConfigs: () => ({}),
|
||||||
authSelectors: {
|
authSelectors: {
|
||||||
getConfigs: () => ({
|
getConfigs: () => ({
|
||||||
@@ -167,7 +176,7 @@ describe("auth plugin - actions", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("tokenRequest", function() {
|
describe("tokenRequest", function () {
|
||||||
it("should send the code verifier when set", () => {
|
it("should send the code verifier when set", () => {
|
||||||
const data = {
|
const data = {
|
||||||
auth: {
|
auth: {
|
||||||
@@ -200,17 +209,17 @@ describe("auth plugin - actions", () => {
|
|||||||
const data = {
|
const data = {
|
||||||
"api_key": {}
|
"api_key": {}
|
||||||
}
|
}
|
||||||
const system = {
|
const system = {
|
||||||
getConfigs: () => ({}),
|
getConfigs: () => ({}),
|
||||||
authActions: {
|
authActions: {
|
||||||
authorize: jest.fn(()=>{}),
|
authorize: jest.fn(() => { }),
|
||||||
persistAuthorizationIfNeeded: jest.fn(()=>{})
|
persistAuthorizationIfNeeded: jest.fn(() => { })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When
|
// When
|
||||||
authorizeWithPersistOption(data)(system)
|
authorizeWithPersistOption(data)(system)
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
expect(system.authActions.authorize).toHaveBeenCalled()
|
expect(system.authActions.authorize).toHaveBeenCalled()
|
||||||
expect(system.authActions.authorize).toHaveBeenCalledWith(data)
|
expect(system.authActions.authorize).toHaveBeenCalledWith(data)
|
||||||
@@ -223,17 +232,17 @@ describe("auth plugin - actions", () => {
|
|||||||
const data = {
|
const data = {
|
||||||
"api_key": {}
|
"api_key": {}
|
||||||
}
|
}
|
||||||
const system = {
|
const system = {
|
||||||
getConfigs: () => ({}),
|
getConfigs: () => ({}),
|
||||||
authActions: {
|
authActions: {
|
||||||
authorizeOauth2: jest.fn(()=>{}),
|
authorizeOauth2: jest.fn(() => { }),
|
||||||
persistAuthorizationIfNeeded: jest.fn(()=>{})
|
persistAuthorizationIfNeeded: jest.fn(() => { })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When
|
// When
|
||||||
authorizeOauth2WithPersistOption(data)(system)
|
authorizeOauth2WithPersistOption(data)(system)
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
expect(system.authActions.authorizeOauth2).toHaveBeenCalled()
|
expect(system.authActions.authorizeOauth2).toHaveBeenCalled()
|
||||||
expect(system.authActions.authorizeOauth2).toHaveBeenCalledWith(data)
|
expect(system.authActions.authorizeOauth2).toHaveBeenCalledWith(data)
|
||||||
@@ -246,23 +255,23 @@ describe("auth plugin - actions", () => {
|
|||||||
const data = {
|
const data = {
|
||||||
"api_key": {}
|
"api_key": {}
|
||||||
}
|
}
|
||||||
const system = {
|
const system = {
|
||||||
getConfigs: () => ({}),
|
getConfigs: () => ({}),
|
||||||
authActions: {
|
authActions: {
|
||||||
logout: jest.fn(()=>{}),
|
logout: jest.fn(() => { }),
|
||||||
persistAuthorizationIfNeeded: jest.fn(()=>{})
|
persistAuthorizationIfNeeded: jest.fn(() => { })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When
|
// When
|
||||||
logoutWithPersistOption(data)(system)
|
logoutWithPersistOption(data)(system)
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
expect(system.authActions.logout).toHaveBeenCalled()
|
expect(system.authActions.logout).toHaveBeenCalled()
|
||||||
expect(system.authActions.logout).toHaveBeenCalledWith(data)
|
expect(system.authActions.logout).toHaveBeenCalledWith(data)
|
||||||
expect(system.authActions.persistAuthorizationIfNeeded).toHaveBeenCalled()
|
expect(system.authActions.persistAuthorizationIfNeeded).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("persistAuthorizationIfNeeded", () => {
|
describe("persistAuthorizationIfNeeded", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -270,18 +279,18 @@ describe("auth plugin - actions", () => {
|
|||||||
})
|
})
|
||||||
it("should skip if `persistAuthorization` is turned off", () => {
|
it("should skip if `persistAuthorization` is turned off", () => {
|
||||||
// Given
|
// Given
|
||||||
const system = {
|
const system = {
|
||||||
getConfigs: () => ({
|
getConfigs: () => ({
|
||||||
persistAuthorization: false
|
persistAuthorization: false
|
||||||
}),
|
}),
|
||||||
authSelectors: {
|
authSelectors: {
|
||||||
authorized: jest.fn(()=>{})
|
authorized: jest.fn(() => { })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When
|
// When
|
||||||
persistAuthorizationIfNeeded()(system)
|
persistAuthorizationIfNeeded()(system)
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
expect(system.authSelectors.authorized).not.toHaveBeenCalled()
|
expect(system.authSelectors.authorized).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
@@ -290,22 +299,25 @@ describe("auth plugin - actions", () => {
|
|||||||
const data = {
|
const data = {
|
||||||
"api_key": {}
|
"api_key": {}
|
||||||
}
|
}
|
||||||
const system = {
|
const system = {
|
||||||
getConfigs: () => ({
|
getConfigs: () => ({
|
||||||
persistAuthorization: true
|
persistAuthorization: true
|
||||||
}),
|
}),
|
||||||
|
errActions: {
|
||||||
|
newAuthErr: () => ({})
|
||||||
|
},
|
||||||
authSelectors: {
|
authSelectors: {
|
||||||
authorized: jest.fn(()=>Map(data))
|
authorized: jest.fn(() => Map(data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jest.spyOn(Object.getPrototypeOf(window.localStorage), "setItem")
|
jest.spyOn(Object.getPrototypeOf(window.localStorage), "setItem")
|
||||||
|
|
||||||
// When
|
// When
|
||||||
persistAuthorizationIfNeeded()(system)
|
persistAuthorizationIfNeeded()(system)
|
||||||
|
|
||||||
expect(localStorage.setItem).toHaveBeenCalled()
|
expect(localStorage.setItem).toHaveBeenCalled()
|
||||||
expect(localStorage.setItem).toHaveBeenCalledWith("authorized", JSON.stringify(data))
|
expect(localStorage.setItem).toHaveBeenCalledWith("authorized", JSON.stringify(data))
|
||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user