-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add validation on data.method when using transport.request (#801)
* fix: add validation on data.method when using tranport.request Signed-off-by: SuZhoue-Joe <[email protected]> * feat: add validation on endpoint Signed-off-by: SuZhoue-Joe <[email protected]> * feat: add unit test Signed-off-by: SuZhoue-Joe <[email protected]> * feat: add more protect Signed-off-by: SuZhoue-Joe <[email protected]> --------- Signed-off-by: SuZhoue-Joe <[email protected]>
- Loading branch information
1 parent
c821a2a
commit 13fb97f
Showing
2 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { | ||
ILegacyCustomClusterClient, | ||
OpenSearchDashboardsRequest, | ||
OpenSearchDashboardsResponseFactory, | ||
RequestHandlerContext, | ||
} from "opensearch-dashboards/server"; | ||
import CommonService from "./CommonService"; | ||
|
||
const contextMock = { | ||
core: {}, | ||
} as RequestHandlerContext; | ||
const responseMock = ({ | ||
custom: jest.fn((args) => args), | ||
} as unknown) as OpenSearchDashboardsResponseFactory; | ||
|
||
const mockedClient = { | ||
callAsCurrentUser: jest.fn(), | ||
callAsInternalUser: jest.fn(), | ||
close: jest.fn(), | ||
asScoped: jest.fn(() => ({ | ||
callAsCurrentUser: jest.fn((...args) => args), | ||
callAsInternalUser: jest.fn(), | ||
})), | ||
} as any; | ||
|
||
describe("CommonService spec", () => { | ||
it("http method should valid when calling transport.request", async () => { | ||
const commonService = new CommonService(mockedClient); | ||
const result = await commonService.apiCaller( | ||
contextMock, | ||
{ | ||
body: { | ||
endpoint: "transport.request", | ||
data: { | ||
method: "invalid method", | ||
}, | ||
}, | ||
} as OpenSearchDashboardsRequest, | ||
responseMock | ||
); | ||
expect(result).toEqual({ | ||
statusCode: 200, | ||
body: { | ||
ok: false, | ||
error: `Method must be one of, case insensitive ['HEAD', 'GET', 'POST', 'PUT', 'DELETE']. Received 'invalid method'.`, | ||
}, | ||
}); | ||
}); | ||
|
||
it("should return error when no endpoint is provided", async () => { | ||
const commonService = new CommonService(mockedClient); | ||
const result = await commonService.apiCaller( | ||
contextMock, | ||
{ | ||
body: { | ||
endpoint: "", | ||
}, | ||
} as OpenSearchDashboardsRequest, | ||
responseMock | ||
); | ||
expect(result).toEqual({ | ||
statusCode: 200, | ||
body: { | ||
ok: false, | ||
error: `Expected non-empty string on endpoint`, | ||
}, | ||
}); | ||
}); | ||
|
||
it("should patch path when data.path does not start with /", async () => { | ||
const commonService = new CommonService(mockedClient); | ||
const result = await commonService.apiCaller( | ||
contextMock, | ||
{ | ||
body: { | ||
endpoint: "transport.request", | ||
data: { | ||
path: "", | ||
}, | ||
}, | ||
} as OpenSearchDashboardsRequest, | ||
responseMock | ||
); | ||
expect(result).toEqual({ | ||
statusCode: 200, | ||
body: { | ||
ok: true, | ||
response: [ | ||
"transport.request", | ||
{ | ||
path: "/", | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters