Skip to content

Commit

Permalink
non-TTY check for required variables:
Browse files Browse the repository at this point in the history
Added a check in non-TTY environments for `account_id`, `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`. If `account_id` exists in `wrangler.toml`
then `CLOUDFLARE_ACCOUNT_ID` is not needed in non-TTY scope. The `CLOUDFLARE_API_TOKEN` is necessary in non-TTY scope and will always error if missing.

resolves #827
  • Loading branch information
JacobMGEvans committed May 2, 2022
1 parent 97f945f commit e50fa70
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 342 deletions.
9 changes: 9 additions & 0 deletions .changeset/eighty-yaks-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

feat: non-TTY check for required variables
Added a check in non-TTY environments for `account_id`, `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`. If `account_id` exists in `wrangler.toml`
then `CLOUDFLARE_ACCOUNT_ID` is not needed in non-TTY scope. The `CLOUDFLARE_API_TOKEN` is necessary in non-TTY scope and will always error if missing.

resolves #827
10 changes: 7 additions & 3 deletions packages/wrangler/src/__tests__/helpers/mock-istty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ORIGINAL_ISTTY = process.stdout.isTTY;
const ORIGINAL_STDOUT_ISTTY = process.stdout.isTTY;
const ORIGINAL_STDIN_ISTTY = process.stdin.isTTY;

/**
* Mock `process.stdout.isTTY`
Expand All @@ -9,14 +10,17 @@ export function useMockIsTTY() {
*/
const setIsTTY = (isTTY: boolean) => {
process.stdout.isTTY = isTTY;
process.stdin.isTTY = isTTY;
};

beforeEach(() => {
process.stdout.isTTY = ORIGINAL_ISTTY;
process.stdout.isTTY = ORIGINAL_STDOUT_ISTTY;
process.stdin.isTTY = ORIGINAL_STDIN_ISTTY;
});

afterEach(() => {
process.stdout.isTTY = ORIGINAL_ISTTY;
process.stdout.isTTY = ORIGINAL_STDOUT_ISTTY;
process.stdin.isTTY = ORIGINAL_STDIN_ISTTY;
});

return { setIsTTY };
Expand Down
28 changes: 26 additions & 2 deletions packages/wrangler/src/__tests__/helpers/mock-oauth-flow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fetchMock from "jest-fetch-mock";
import { Request } from "undici";
import { getCloudflareApiBaseUrl } from "../../cfetch";
import openInBrowser from "../../open-in-browser";
import { mockHttpServer } from "./mock-http-server";

Expand Down Expand Up @@ -85,6 +86,28 @@ export const mockOAuthFlow = () => {
return outcome;
};

const mockGetMemberships = (args: {
success: boolean;
result: { id: string; account: { id: string; name: string } }[];
}) => {
const outcome = {
actual: new Request("https://example.org"),
expected: new Request(`${getCloudflareApiBaseUrl()}/memberships`, {
method: "GET",
}),
};

fetchMock.mockIf(outcome.expected.url, async (req) => {
outcome.actual = req;
return {
status: 200,
body: JSON.stringify(args),
};
});

return outcome;
};

const mockGrantAccessToken = ({
respondWith,
}: {
Expand Down Expand Up @@ -150,10 +173,11 @@ export const mockOAuthFlow = () => {
};

return {
mockOAuthServerCallback,
mockGetMemberships,
mockGrantAccessToken,
mockGrantAuthorization,
mockOAuthServerCallback,
mockRevokeAuthorization,
mockGrantAccessToken,
mockExchangeRefreshTokenForAccessToken,
};
};
Expand Down
Loading

0 comments on commit e50fa70

Please sign in to comment.