Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: quality

# Keeps three things from drifting: the router's complexity, the OpenAPI spec
# against the registered routes, and the sign-in page's Caps Lock hint in a
# real browser.
on:
push:
branches: [main]
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
with: { go-version-file: go.mod }
- name: gocyclo (router stays small)
run: |
go install github.com/fzipp/gocyclo/cmd/gocyclo@v0.6.0
gocyclo -over 15 web.go stats.go
- name: OpenAPI spec matches the routes
run: go test -count=1 -run 'TestOpenAPI|TestAPI' -v .
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with: { node-version: '22', cache: npm, cache-dependency-path: browser/package-lock.json }
- name: OpenAPI lint
run: npx --yes @redocly/cli@2.82.0 lint docs/openapi.yaml
- name: Browser tests
working-directory: browser
run: |
npm ci --ignore-scripts
npx playwright install --with-deps chromium
npm test
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Everything else lives in the **[full guide](docs/GUIDE.md)**:
[reading the chart](docs/GUIDE.md#reading-the-chart) ·
[upgrading](docs/GUIDE.md#upgrading) ·
[troubleshooting](docs/GUIDE.md#troubleshooting) ·
[HTTP API](docs/API.md) ([OpenAPI 3.1](docs/openapi.yaml)) ·
[build from source](docs/GUIDE.md#build-from-source)

🌐 [English / 中文 / Español / Français / Português / Deutsch / Русский / 日本語 / 한국어 / Bahasa Indonesia / Tiếng Việt / العربية / हिन्दी / বাংলা / اردو / Türkçe / ไทย](docs/LANGUAGES.md)
Expand Down
1 change: 1 addition & 0 deletions browser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
73 changes: 73 additions & 0 deletions browser/login_capslock.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Browser test for the sign-in page's Caps Lock hint, against the real Go
// router (so the page's CSP applies). Run: cd browser && npm ci && npm test
const {test} = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
const {spawn} = require('node:child_process');
const {chromium} = require(process.env.PLAYWRIGHT_MODULE || 'playwright');

function startServer() {
const child = spawn('go', ['test', '-count=1', '-run', '^TestBrowserServer$'], {
cwd: path.join(__dirname, '..'),
env: {...process.env, BROWSER_TEST_SERVE: '1'},
stdio: ['ignore', 'pipe', 'inherit'],
detached: true, // its own process group, so the test binary stops with it
});
return new Promise((resolve, reject) => {
let out = '';
const timer = setTimeout(() => reject(new Error('server did not start\n' + out)), 120000);
child.stdout.on('data', b => {
out += b;
const m = out.match(/BROWSER_TEST_URL=(\S+)/);
if (m) { clearTimeout(timer); resolve({child, url: m[1]}); }
});
child.on('exit', code => reject(new Error('server exited ' + code + '\n' + out)));
});
}

// Chromium cannot toggle the real Caps Lock from a test, so the events carry
// the modifier state the way a browser reports it.
const key = (page, sel, type, caps) => page.locator(sel).evaluate((el, [type, caps]) =>
el.dispatchEvent(new KeyboardEvent(type, {key: caps ? 'A' : 'a', bubbles: true, modifierCapsLock: caps})),
[type, caps]);

test('login page shows a Caps Lock hint only while it is on', {timeout: 180000}, async () => {
const {child, url} = await startServer();
const browser = await chromium.launch();
try {
const page = await browser.newPage();
const errors = [];
page.on('console', m => { if (m.type() === 'error') errors.push(m.text()); });
page.on('pageerror', e => errors.push(e.message));
await page.goto(url + '/login');
const hint = page.locator('#caps');
assert.equal(await hint.getAttribute('aria-live'), 'polite');
assert.match(await page.locator('#pass').getAttribute('aria-describedby'), /\bcaps\b/);
assert.equal(await hint.textContent(), '');

await page.locator('#pass').focus();
await key(page, '#pass', 'keydown', true);
assert.match(await hint.textContent(), /\S/);
assert.ok(await hint.isVisible(), 'hint is visible');

await key(page, '#pass', 'keyup', false);
assert.equal(await hint.textContent(), '');

// On, then focus moves away: hidden; back to the password field: shown.
await key(page, '#pass', 'keydown', true);
await page.locator('#user').focus();
assert.equal(await hint.textContent(), '');
await page.locator('#pass').focus();
assert.match(await hint.textContent(), /\S/);

// Typing in the username field with Caps Lock on shows nothing there.
await page.locator('#user').focus();
await key(page, '#user', 'keydown', true);
assert.equal(await hint.textContent(), '');

assert.deepEqual(errors, [], 'no console errors (a CSP violation would be one)');
} finally {
await browser.close();
process.kill(-child.pid, 'SIGTERM');
}
});
60 changes: 60 additions & 0 deletions browser/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions browser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "fogping-browser-tests",
"private": true,
"scripts": {
"test": "node --test"
},
"devDependencies": {
"playwright": "1.62.1"
}
}
29 changes: 29 additions & 0 deletions browser_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"context"
"fmt"
"net/http/httptest"
"os"
"os/signal"
"syscall"
"testing"
"time"
)

// TestBrowserServer serves the real router, login and CSP included, on a
// random loopback port for the browser tests in browser/. It is skipped
// unless they start it, and stops when they signal it (or after 5 minutes).
func TestBrowserServer(t *testing.T) {
if os.Getenv("BROWSER_TEST_SERVE") == "" {
t.Skip("started by the browser tests in browser/")
}
srv := httptest.NewServer(newMux(&Config{}, nil, nil, map[string]string{"admin": "browser-test-password"}))
defer srv.Close()
fmt.Println("BROWSER_TEST_URL=" + srv.URL)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
<-ctx.Done()
}
3 changes: 3 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ fogping's web UI and the Zabbix template talk to fogping through this API. The
same rules hold in pingping and vrf-ping, so a client written for one reads the
others the same way.

The same API in machine-readable form: [openapi.yaml](openapi.yaml) (OpenAPI 3.1).
A test (`TestOpenAPIMatchesRoutes`) fails when it and the registered routes differ.

## Rules

These are enforced by tests (`api_test.go`): code that breaks one does not pass CI.
Expand Down
Loading
Loading