123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import { fromJS } from "immutable"
- import { preauthorizeBasic, preauthorizeApiKey } from "corePlugins/auth"
- import { authorize } from "corePlugins/auth/actions"
- const S2_SYSTEM = {
- authActions: {
- authorize
- },
- specSelectors: {
- isOAS3: () => false,
- specJson: () => {
- return fromJS({
- swagger: "2.0",
- securityDefinitions: {
- "APIKeyHeader": {
- "type": "apiKey",
- "in": "header",
- "name": "X-API-Key"
- },
- "basicAuth": {
- "type": "basic"
- }
- }
- })
- }
- }
- }
- const OAI3_SYSTEM = {
- authActions: {
- authorize
- },
- specSelectors: {
- isOAS3: () => true,
- specJson: () => {
- return fromJS({
- openapi: "3.0.0",
- components: {
- securitySchemes: {
- basicAuth: {
- type: "http",
- scheme: "basic"
- },
- APIKeyHeader: {
- type: "apiKey",
- in: "header",
- name: "X-API-Key"
- }
- }
- }
- })
- }
- }
- }
- describe("auth plugin - preauthorizers", () => {
- describe("preauthorizeBasic", () => {
- it("should return a valid authorize action in Swagger 2", () => {
- const res = preauthorizeBasic(S2_SYSTEM, "basicAuth", "user", "pass")
- expect(res).toEqual({
- type: "authorize",
- payload: {
- basicAuth: {
- schema: {
- type: "basic"
- },
- value: {
- username: "user",
- password: "pass"
- }
- }
- }
- })
- })
- it("should return a valid authorize action in OpenAPI 3", () => {
- const res = preauthorizeBasic(OAI3_SYSTEM, "basicAuth", "user", "pass")
- expect(res).toEqual({
- type: "authorize",
- payload: {
- basicAuth: {
- schema: {
- type: "http",
- scheme: "basic"
- },
- value: {
- username: "user",
- password: "pass"
- }
- }
- }
- })
- })
- it("should return null when the authorization name is invalid in Swagger 2", () => {
- const res = preauthorizeBasic(S2_SYSTEM, "fakeBasicAuth", "user", "pass")
- expect(res).toEqual(null)
- })
- it("should return null when the authorization name is invalid in OpenAPI 3", () => {
- const res = preauthorizeBasic(OAI3_SYSTEM, "fakeBasicAuth", "user", "pass")
- expect(res).toEqual(null)
- })
- })
- describe("preauthorizeApiKey", () => {
- it("should return a valid authorize action in Swagger 2", () => {
- const res = preauthorizeApiKey(S2_SYSTEM, "APIKeyHeader", "Asdf1234")
- expect(res).toEqual({
- type: "authorize",
- payload: {
- APIKeyHeader: {
- schema: {
- type: "apiKey",
- name: "X-API-Key",
- "in": "header"
- },
- value: "Asdf1234"
- }
- }
- })
- })
- it("should return a valid authorize action in OpenAPI 3", () => {
- const res = preauthorizeApiKey(OAI3_SYSTEM, "APIKeyHeader", "Asdf1234")
- expect(res).toEqual({
- type: "authorize",
- payload: {
- APIKeyHeader: {
- schema: {
- type: "apiKey",
- "in": "header",
- name: "X-API-Key"
- },
- value: "Asdf1234"
- }
- }
- })
- })
- it("should return null when the authorization name is invalid in Swagger 2", () => {
- const res = preauthorizeApiKey(S2_SYSTEM, "FakeAPIKeyHeader", "Asdf1234")
- expect(res).toEqual(null)
- })
- it("should return null when the authorization name is invalid in OpenAPI 3", () => {
- const res = preauthorizeApiKey(OAI3_SYSTEM, "FakeAPIKeyHeader", "Asdf1234")
- expect(res).toEqual(null)
- })
- })
- })
|