Skip to content

Commit 2730f5a

Browse files
committed
Add e2e test for GitHub token
1 parent 5c41acf commit 2730f5a

11 files changed

+85
-12
lines changed

test/e2e/baseFixture.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ export const describe = (
1313
name: string,
1414
includeCredentials: boolean,
1515
codeServerArgs: string[],
16+
codeServerEnv: NodeJS.ProcessEnv,
1617
fn: (codeServer: CodeServer) => void,
1718
) => {
1819
test.describe(name, () => {
1920
// This will spawn on demand so nothing is necessary on before.
20-
const codeServer = new CodeServer(name, codeServerArgs)
21+
const codeServer = new CodeServer(name, codeServerArgs, codeServerEnv)
2122

2223
// Kill code-server after the suite has ended. This may happen even without
2324
// doing it explicitly but it seems prudent to be sure.

test/e2e/codeServer.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { promises as fs } from "fs"
22
import * as path from "path"
33
import { describe, test, expect } from "./baseFixture"
44

5-
describe("CodeServer", true, [], () => {
5+
describe("CodeServer", true, [], {}, () => {
66
test("should navigate to home page", async ({ codeServerPage }) => {
77
// We navigate codeServer before each test
88
// and we start the test with a storage state

test/e2e/extensions.test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as path from "path"
12
import { describe, test } from "./baseFixture"
23

34
function runTestExtensionTests() {
@@ -11,10 +12,16 @@ function runTestExtensionTests() {
1112
})
1213
}
1314

14-
describe("Extensions", true, [], () => {
15+
16+
const flags = [
17+
"--extensions-dir",
18+
path.join(__dirname, "./extensions"),
19+
]
20+
21+
describe("Extensions", true, flags, {}, () => {
1522
runTestExtensionTests()
1623
})
1724

18-
describe("Extensions with --cert", true, ["--cert"], () => {
25+
describe("Extensions with --cert", true, [...flags, "--cert"], {}, () => {
1926
runTestExtensionTests()
2027
})

test/e2e/github.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { test as base } from "@playwright/test"
2+
import { describe, expect, test } from "./baseFixture"
3+
4+
if (process.env.GITHUB_TOKEN) {
5+
describe("GitHub token", true, [], {}, () => {
6+
test("should be logged in to pull requests extension", async ({ codeServerPage }) => {
7+
await codeServerPage.exec("git init")
8+
await codeServerPage.exec("git remote add origin https://github.com/coder/code-server")
9+
await codeServerPage.installExtension("GitHub.vscode-pull-request-github")
10+
await codeServerPage.executeCommandViaMenus("View: Show Github")
11+
await codeServerPage.page.click("text=Sign in")
12+
await codeServerPage.page.click("text=Allow")
13+
// It should ask to select an account, one of which will be the one we
14+
// pre-injected.
15+
expect(await codeServerPage.page.isVisible("text=Select an account")).toBe(false)
16+
})
17+
})
18+
19+
describe("No GitHub token", true, [], { GITHUB_TOKEN: "" }, () => {
20+
test("should not be logged in to pull requests extension", async ({ codeServerPage }) => {
21+
await codeServerPage.exec("git init")
22+
await codeServerPage.exec("git remote add origin https://github.com/coder/code-server")
23+
await codeServerPage.installExtension("GitHub.vscode-pull-request-github")
24+
await codeServerPage.executeCommandViaMenus("View: Show Github")
25+
await codeServerPage.page.click("text=Sign in")
26+
await codeServerPage.page.click("text=Allow")
27+
// Since there is no account it will ask directly for the token (because
28+
// we are on localhost; otherwise it would initiate the oauth flow).
29+
expect(await codeServerPage.page.isVisible("text=GitHub Personal Access Token")).toBe(false)
30+
})
31+
})
32+
} else {
33+
base.describe("GitHub token", () => {
34+
base.skip("skipped because GITHUB_TOKEN is not set", () => {
35+
// Playwright will not show this without a function.
36+
})
37+
})
38+
}

test/e2e/globalSetup.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, test, expect } from "./baseFixture"
22

33
// This test is to make sure the globalSetup works as expected
44
// meaning globalSetup ran and stored the storageState
5-
describe("globalSetup", true, [], () => {
5+
describe("globalSetup", true, [], {}, () => {
66
test("should keep us logged in using the storageState", async ({ codeServerPage }) => {
77
// Make sure the editor actually loaded
88
expect(await codeServerPage.isEditorVisible()).toBe(true)

test/e2e/login.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PASSWORD } from "../utils/constants"
22
import { describe, test, expect } from "./baseFixture"
33

4-
describe("login", false, [], () => {
4+
describe("login", false, [], {}, () => {
55
test("should see the login page", async ({ codeServerPage }) => {
66
// It should send us to the login page
77
expect(await codeServerPage.page.title()).toBe("code-server login")

test/e2e/logout.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// NOTE@jsjoeio commenting out until we can figure out what's wrong
22
// import { describe, test, expect } from "./baseFixture"
33

4-
// describe("logout", true, [], () => {
4+
// describe("logout", true, [], {}, () => {
55
// test("should be able logout", async ({ codeServerPage }) => {
66
// // Recommended by Playwright for async navigation
77
// // https://github.com/microsoft/playwright/issues/1987#issuecomment-620182151

test/e2e/models/CodeServer.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as cp from "child_process"
33
import { promises as fs } from "fs"
44
import * as path from "path"
55
import { Page } from "playwright"
6+
import util from "util"
67
import { logError, plural } from "../../../src/common/util"
78
import { onLine } from "../../../src/node/util"
89
import { PASSWORD, workspaceDir } from "../../utils/constants"
@@ -39,7 +40,7 @@ export class CodeServer {
3940
private closed = false
4041
private _workspaceDir: Promise<string> | undefined
4142

42-
constructor(name: string, private readonly codeServerArgs: string[]) {
43+
constructor(name: string, private readonly codeServerArgs: string[], private readonly codeServerEnv: NodeJS.ProcessEnv) {
4344
this.logger = logger.named(name)
4445
}
4546

@@ -96,6 +97,8 @@ export class CodeServer {
9697
"node",
9798
[
9899
process.env.CODE_SERVER_TEST_ENTRY || ".",
100+
"--extensions-dir",
101+
path.join(dir, "extensions"),
99102
...this.codeServerArgs,
100103
// Using port zero will spawn on a random port.
101104
"--bind-addr",
@@ -107,15 +110,14 @@ export class CodeServer {
107110
path.join(dir, "config.yaml"),
108111
"--user-data-dir",
109112
dir,
110-
"--extensions-dir",
111-
path.join(__dirname, "../extensions"),
112113
// The last argument is the workspace to open.
113114
dir,
114115
],
115116
{
116117
cwd: path.join(__dirname, "../../.."),
117118
env: {
118119
...process.env,
120+
...this.codeServerEnv,
119121
PASSWORD,
120122
},
121123
},
@@ -462,4 +464,24 @@ export class CodeServerPage {
462464
await this.reloadUntilEditorIsReady()
463465
}
464466
}
467+
468+
/**
469+
* Execute a command in t root of the instance's workspace directory.
470+
*/
471+
async exec(command: string): Promise<void> {
472+
await util.promisify(cp.exec)(command, {
473+
cwd: await this.workspaceDir,
474+
})
475+
}
476+
477+
/**
478+
* Install an extension by ID to the instance's temporary extension
479+
* directory.
480+
*/
481+
async installExtension(id: string): Promise<void> {
482+
const dir = path.join(await this.workspaceDir, "extensions")
483+
await util.promisify(cp.exec)(`node . --install-extension ${id} --extensions-dir ${dir}`, {
484+
cwd: path.join(__dirname, "../../.."),
485+
})
486+
}
465487
}

test/e2e/openHelpAbout.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, test, expect } from "./baseFixture"
22

3-
describe("Open Help > About", true, [], () => {
3+
describe("Open Help > About", true, [], {}, () => {
44
test("should see code-server version in about dialog", async ({ codeServerPage }) => {
55
// Open using the menu.
66
await codeServerPage.navigateMenus(["Help", "About"])

test/e2e/terminal.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import util from "util"
44
import { clean, tmpdir } from "../utils/helpers"
55
import { describe, expect, test } from "./baseFixture"
66

7-
describe("Integrated Terminal", true, [], () => {
7+
describe("Integrated Terminal", true, [], {}, () => {
88
const testName = "integrated-terminal"
99
test.beforeAll(async () => {
1010
await clean(testName)

test/unit/node/cli.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,11 @@ describe("parser", () => {
393393
it("should ignore optional strings set to false", async () => {
394394
expect(parse(["--cert=false"])).toEqual({})
395395
})
396+
it("should use last flag", async () => {
397+
expect(parse(["--port", "8081", "--port", "8082"])).toEqual({
398+
port: 8082,
399+
})
400+
})
396401
})
397402

398403
describe("cli", () => {

0 commit comments

Comments
 (0)