Stub feature flag API endpoint in tests

This commit is contained in:
Henry Mercer 2021-12-16 13:30:31 +00:00
parent 6d62c245ec
commit 254816c2d2
12 changed files with 84 additions and 97 deletions

View file

@ -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);
}