Stub feature flag API endpoint in tests
This commit is contained in:
parent
6d62c245ec
commit
254816c2d2
12 changed files with 84 additions and 97 deletions
|
|
@ -1,8 +1,11 @@
|
|||
import * as github from "@actions/github";
|
||||
import { TestInterface } from "ava";
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import * as apiClient from "./api-client";
|
||||
import * as CodeQL from "./codeql";
|
||||
import { Logger } from "./logging";
|
||||
import { HTTPError } from "./util";
|
||||
|
||||
type TestContext = {
|
||||
stdoutWrite: any;
|
||||
|
|
@ -119,3 +122,30 @@ export function getRecordingLogger(messages: LoggedMessage[]): Logger {
|
|||
endGroup: () => undefined,
|
||||
};
|
||||
}
|
||||
|
||||
/** Mock the HTTP request to the feature flags enablement API endpoint. */
|
||||
export function mockFeatureFlagApiEndpoint(
|
||||
responseStatusCode: number,
|
||||
response: { [flagName: string]: boolean }
|
||||
) {
|
||||
// Passing an auth token is required, so we just use a dummy value
|
||||
const client = github.getOctokit("123");
|
||||
|
||||
const requestSpy = sinon.stub(client, "request");
|
||||
|
||||
const optInSpy = requestSpy.withArgs(
|
||||
"GET /repos/:owner/:repo/code-scanning/codeql-action/features"
|
||||
);
|
||||
if (responseStatusCode < 300) {
|
||||
optInSpy.resolves({
|
||||
status: responseStatusCode,
|
||||
data: response,
|
||||
headers: {},
|
||||
url: "GET /repos/:owner/:repo/code-scanning/codeql-action/features",
|
||||
});
|
||||
} else {
|
||||
optInSpy.throws(new HTTPError("some error message", responseStatusCode));
|
||||
}
|
||||
|
||||
sinon.stub(apiClient, "getApiClient").value(() => client);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue