|
| 1 | +import React from "react"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { render } from "ink-testing-library"; |
| 5 | +import type { UserInfo } from "../whoami"; |
| 6 | +import { getUserInfo, WhoAmI } from "../whoami"; |
| 7 | +import { runInTempDir } from "./run-in-tmp"; |
| 8 | +import { mkdirSync, writeFileSync } from "node:fs"; |
| 9 | +import { setMockResponse } from "./mock-cfetch"; |
| 10 | +import { initialise } from "../user"; |
| 11 | + |
| 12 | +const ORIGINAL_CF_API_TOKEN = process.env.CF_API_TOKEN; |
| 13 | +const ORIGINAL_CF_ACCOUNT_ID = process.env.CF_ACCOUNT_ID; |
| 14 | + |
| 15 | +describe("getUserInfo()", () => { |
| 16 | + runInTempDir(); |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + // Clear the environment variables, so we can control them in the tests |
| 20 | + delete process.env.CF_API_TOKEN; |
| 21 | + delete process.env.CF_ACCOUNT_ID; |
| 22 | + // Override where the home directory is so that we can specify a user config |
| 23 | + mkdirSync("./home"); |
| 24 | + jest.spyOn(os, "homedir").mockReturnValue("./home"); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + // Reset any changes to the environment variables |
| 29 | + process.env.CF_API_TOKEN = ORIGINAL_CF_API_TOKEN; |
| 30 | + process.env.CF_ACCOUNT_ID = ORIGINAL_CF_ACCOUNT_ID; |
| 31 | + }); |
| 32 | + |
| 33 | + it("should return undefined if there is no config file", async () => { |
| 34 | + await initialise(); |
| 35 | + const userInfo = await getUserInfo(); |
| 36 | + expect(userInfo).toBeUndefined(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should return undefined if there is an empty config file", async () => { |
| 40 | + writeUserConfig(); |
| 41 | + await initialise(); |
| 42 | + const userInfo = await getUserInfo(); |
| 43 | + expect(userInfo).toBeUndefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should return the user's email and accounts if authenticated via config token", async () => { |
| 47 | + writeUserConfig("some-oauth-token"); |
| 48 | + setMockResponse("/user", () => { |
| 49 | + return { email: "[email protected]" }; |
| 50 | + }); |
| 51 | + setMockResponse("/accounts", () => { |
| 52 | + return [ |
| 53 | + { name: "Account One", id: "account-1" }, |
| 54 | + { name: "Account Two", id: "account-2" }, |
| 55 | + { name: "Account Three", id: "account-3" }, |
| 56 | + ]; |
| 57 | + }); |
| 58 | + |
| 59 | + await initialise(); |
| 60 | + const userInfo = await getUserInfo(); |
| 61 | + |
| 62 | + expect(userInfo).toEqual({ |
| 63 | + authType: "OAuth", |
| 64 | + apiToken: "some-oauth-token", |
| 65 | + |
| 66 | + accounts: [ |
| 67 | + { name: "Account One", id: "account-1" }, |
| 68 | + { name: "Account Two", id: "account-2" }, |
| 69 | + { name: "Account Three", id: "account-3" }, |
| 70 | + ], |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe("WhoAmI component", () => { |
| 76 | + it("should return undefined if there is no user", async () => { |
| 77 | + const { lastFrame } = render(<WhoAmI user={undefined}></WhoAmI>); |
| 78 | + |
| 79 | + expect(lastFrame()).toMatchInlineSnapshot( |
| 80 | + `"You are not authenticated. Please run \`wrangler login\`."` |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should display the user's email and accounts", async () => { |
| 85 | + const user: UserInfo = { |
| 86 | + authType: "OAuth", |
| 87 | + apiToken: "some-oauth-token", |
| 88 | + |
| 89 | + accounts: [ |
| 90 | + { name: "Account One", id: "account-1" }, |
| 91 | + { name: "Account Two", id: "account-2" }, |
| 92 | + { name: "Account Three", id: "account-3" }, |
| 93 | + ], |
| 94 | + }; |
| 95 | + |
| 96 | + const { lastFrame } = render(<WhoAmI user={user}></WhoAmI>); |
| 97 | + |
| 98 | + expect(lastFrame()).toMatchInlineSnapshot(` |
| 99 | + "👋 You are logged in with an OAuth Token, associated with the email '[email protected]'! |
| 100 | + [1m┌[22m[1m───────────────[22m[1m┬[22m[1m────────────[22m[1m┐[22m |
| 101 | + [1m│[22m[1m[34m Account Name [22m[39m[1m│[22m[1m[34m Account ID [22m[39m[1m│[22m |
| 102 | + [1m├[22m[1m───────────────[22m[1m┼[22m[1m────────────[22m[1m┤[22m |
| 103 | + [1m│[22m Account One [1m│[22m account-1 [1m│[22m |
| 104 | + [1m├[22m[1m───────────────[22m[1m┼[22m[1m────────────[22m[1m┤[22m |
| 105 | + [1m│[22m Account Two [1m│[22m account-2 [1m│[22m |
| 106 | + [1m├[22m[1m───────────────[22m[1m┼[22m[1m────────────[22m[1m┤[22m |
| 107 | + [1m│[22m Account Three [1m│[22m account-3 [1m│[22m |
| 108 | + [1m└[22m[1m───────────────[22m[1m┴[22m[1m────────────[22m[1m┘[22m" |
| 109 | + `); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +function writeUserConfig( |
| 114 | + oauth_token?: string, |
| 115 | + refresh_token?: string, |
| 116 | + expiration_time?: string |
| 117 | +) { |
| 118 | + const lines: string[] = []; |
| 119 | + if (oauth_token) { |
| 120 | + lines.push(`oauth_token = "${oauth_token}"`); |
| 121 | + } |
| 122 | + if (refresh_token) { |
| 123 | + lines.push(`refresh_token = "${refresh_token}"`); |
| 124 | + } |
| 125 | + if (expiration_time) { |
| 126 | + lines.push(`expiration_time = "${expiration_time}"`); |
| 127 | + } |
| 128 | + const configPath = path.join(os.homedir(), ".wrangler/config"); |
| 129 | + mkdirSync(configPath, { recursive: true }); |
| 130 | + writeFileSync( |
| 131 | + path.join(configPath, "default.toml"), |
| 132 | + lines.join("\n"), |
| 133 | + "utf-8" |
| 134 | + ); |
| 135 | +} |
0 commit comments