123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- import { fromJS } from "immutable"
- import reducer from "corePlugins/oas3/reducers"
- describe("oas3 plugin - reducer", function () {
- describe("SET_REQUEST_BODY_VALIDATE_ERROR", () => {
- const setRequestBodyValidateError = reducer["oas3_set_request_body_validate_error"]
- describe("missingBodyValue exists, e.g. application/json", () => {
- it("should set errors", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: "",
- requestContentType: "application/json"
- }
- }
- }
- })
- const result = setRequestBodyValidateError(state, {
- payload: {
- path: "/pet",
- method: "post",
- validationErrors: {
- missingBodyValue: true,
- missingRequiredKeys: []
- },
- }
- })
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: "",
- requestContentType: "application/json",
- errors: ["Required field is not provided"]
- }
- }
- }
- }
- expect(result.toJS()).toEqual(expectedResult)
- })
- })
- describe("missingRequiredKeys exists with length, e.g. application/x-www-form-urleconded", () => {
- it("should set nested errors", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- },
- name: {
- value: "",
- },
- },
- requestContentType: "application/x-www-form-urlencoded"
- }
- }
- }
- })
- const result = setRequestBodyValidateError(state, {
- payload: {
- path: "/pet",
- method: "post",
- validationErrors: {
- missingBodyValue: null,
- missingRequiredKeys: ["name"]
- },
- }
- })
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- },
- name: {
- value: "",
- errors: ["Required field is not provided"]
- },
- },
- requestContentType: "application/x-www-form-urlencoded",
- }
- }
- }
- }
- expect(result.toJS()).toEqual(expectedResult)
- })
- it("should overwrite nested errors, for keys listed in missingRequiredKeys", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- },
- name: {
- value: "",
- errors: ["some fake error"]
- },
- },
- requestContentType: "application/x-www-form-urlencoded"
- }
- }
- }
- })
- const result = setRequestBodyValidateError(state, {
- payload: {
- path: "/pet",
- method: "post",
- validationErrors: {
- missingBodyValue: null,
- missingRequiredKeys: ["name"]
- },
- }
- })
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- },
- name: {
- value: "",
- errors: ["Required field is not provided"]
- },
- },
- requestContentType: "application/x-www-form-urlencoded",
- }
- }
- }
- }
- expect(result.toJS()).toEqual(expectedResult)
- })
- it("should not overwrite nested errors, for keys not listed in missingRequiredKeys", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- errors: ["random error should not be overwritten"]
- },
- name: {
- value: "",
- errors: ["some fake error"]
- },
- },
- requestContentType: "application/x-www-form-urlencoded"
- }
- }
- }
- })
- const result = setRequestBodyValidateError(state, {
- payload: {
- path: "/pet",
- method: "post",
- validationErrors: {
- missingBodyValue: null,
- missingRequiredKeys: ["name"]
- },
- }
- })
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- errors: ["random error should not be overwritten"]
- },
- name: {
- value: "",
- errors: ["Required field is not provided"]
- },
- },
- requestContentType: "application/x-www-form-urlencoded",
- }
- }
- }
- }
- expect(result.toJS()).toEqual(expectedResult)
- })
- it("should set multiple nested errors", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "",
- },
- name: {
- value: "",
- },
- },
- requestContentType: "application/x-www-form-urlencoded"
- }
- }
- }
- })
- const result = setRequestBodyValidateError(state, {
- payload: {
- path: "/pet",
- method: "post",
- validationErrors: {
- missingBodyValue: null,
- missingRequiredKeys: ["id", "name"]
- },
- }
- })
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "",
- errors: ["Required field is not provided"]
- },
- name: {
- value: "",
- errors: ["Required field is not provided"]
- },
- },
- requestContentType: "application/x-www-form-urlencoded",
- }
- }
- }
- }
- expect(result.toJS()).toEqual(expectedResult)
- })
- })
- describe("missingRequiredKeys is empty list", () => {
- it("should not set any errors, and return state unchanged", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- },
- name: {
- value: "",
- },
- },
- requestContentType: "application/x-www-form-urlencoded"
- }
- }
- }
- })
- const result = setRequestBodyValidateError(state, {
- payload: {
- path: "/pet",
- method: "post",
- validationErrors: {
- missingBodyValue: null,
- missingRequiredKeys: []
- },
- }
- })
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- },
- name: {
- value: "",
- },
- },
- requestContentType: "application/x-www-form-urlencoded",
- }
- }
- }
- }
- expect(result.toJS()).toEqual(expectedResult)
- })
- })
- describe("other unexpected payload, e.g. no missingBodyValue or missingRequiredKeys", () => {
- it("should not throw error if receiving unexpected validationError format. return state unchanged", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- },
- name: {
- value: "",
- },
- },
- requestContentType: "application/x-www-form-urlencoded"
- }
- }
- }
- })
- const result = setRequestBodyValidateError(state, {
- payload: {
- path: "/pet",
- method: "post",
- validationErrors: {
- missingBodyValue: null,
- // missingRequiredKeys: ["none provided"]
- },
- }
- })
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- },
- name: {
- value: "",
- },
- },
- requestContentType: "application/x-www-form-urlencoded",
- }
- }
- }
- }
- expect(result.toJS()).toEqual(expectedResult)
- })
- })
- })
- describe("CLEAR_REQUEST_BODY_VALIDATE_ERROR", function() {
- const clearRequestBodyValidateError = reducer["oas3_clear_request_body_validate_error"]
- describe("bodyValue is String, e.g. application/json", () => {
- it("should clear errors", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: "{}",
- requestContentType: "application/json"
- }
- }
- }
- })
- const result = clearRequestBodyValidateError(state, {
- payload: {
- path: "/pet",
- method: "post",
- }
- })
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: "{}",
- requestContentType: "application/json",
- errors: []
- }
- }
- }
- }
- expect(result.toJS()).toEqual(expectedResult)
- })
- })
- describe("bodyValue is Map with entries, e.g. application/x-www-form-urleconded", () => {
- it("should clear nested errors, and apply empty error list to all entries", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- errors: ["some random error"]
- },
- name: {
- value: "doggie",
- errors: ["Required field is not provided"]
- },
- status: {
- value: "available"
- }
- },
- requestContentType: "application/x-www-form-urlencoded"
- }
- }
- }
- })
- const result = clearRequestBodyValidateError(state, {
- payload: {
- path: "/pet",
- method: "post",
- }
- })
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- errors: [],
- },
- name: {
- value: "doggie",
- errors: [],
- },
- status: {
- value: "available",
- errors: [],
- },
- },
- requestContentType: "application/x-www-form-urlencoded",
- }
- }
- }
- }
- expect(result.toJS()).toEqual(expectedResult)
- })
- })
- describe("bodyValue is empty Map", () => {
- it("should return state unchanged", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: {},
- requestContentType: "application/x-www-form-urlencoded"
- }
- }
- }
- })
- const result = clearRequestBodyValidateError(state, {
- payload: {
- path: "/pet",
- method: "post",
- }
- })
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- },
- requestContentType: "application/x-www-form-urlencoded",
- }
- }
- }
- }
- expect(result.toJS()).toEqual(expectedResult)
- })
- })
- })
- describe("CLEAR_REQUEST_BODY_VALUE", function () {
- const clearRequestBodyValue = reducer["oas3_clear_request_body_value"]
- describe("when requestBodyValue is a String", () => {
- it("should clear requestBodyValue with empty String", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: "some random string",
- requestContentType: "application/json"
- }
- }
- }
- })
-
- const result = clearRequestBodyValue(state, {
- payload: {
- pathMethod: ["/pet", "post"],
- }
- })
-
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: "",
- requestContentType: "application/json",
- }
- }
- }
- }
-
- expect(result.toJS()).toEqual(expectedResult)
- })
- })
- describe("when requestBodyValue is a Map", () => {
- it("should clear requestBodyValue with empty Map", () => {
- const state = fromJS({
- requestData: {
- "/pet": {
- post: {
- bodyValue: {
- id: {
- value: "10",
- },
- },
- requestContentType: "application/x-www-form-urlencoded"
- }
- }
- }
- })
-
- const result = clearRequestBodyValue(state, {
- payload: {
- pathMethod: ["/pet", "post"],
- }
- })
-
- const expectedResult = {
- requestData: {
- "/pet": {
- post: {
- bodyValue: {},
- requestContentType: "application/x-www-form-urlencoded",
- }
- }
- }
- }
-
- expect(result.toJS()).toEqual(expectedResult)
- })
- })
- })
- })
|