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
35 changes: 35 additions & 0 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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: '1.27.4' }
- name: gocyclo (router stays small)
run: |
go install github.com/fzipp/gocyclo/cmd/gocyclo@v0.6.0
gocyclo -over 15 web.go stats.go
- 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ Off by default. Start with `--zabbix` and a token of 16+ characters in `PINGPING

**Before restarting a service**, `pingping check` with the same flags says whether it would start and why not (flags, token, data directory, database, port), without touching the running one. Every start logs a short security summary; anything exposed is marked `!`.

### HTTP API

The console, the tray icon and the Zabbix template all use one JSON API: [docs/API.md](docs/API.md) · OpenAPI 3.1: [docs/openapi.yaml](docs/openapi.yaml).

### No ads, no outbound requests

pingping has no advertising and makes no requests of its own beyond the probes you configure.
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/
95 changes: 95 additions & 0 deletions browser/login_capslock.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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');
const catalogue = require('../i18n/strings.json');

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');
await page.evaluate(() => window.i18nReady);
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(), '');

// Every language the console ships says it in that language.
const want = catalogue.strings['login.capsLock'];
for (const lang of catalogue.langs) {
await page.evaluate(l => localStorage.setItem('pingping_lang', l), lang);
await page.reload();
await page.evaluate(() => window.i18nReady);
await page.locator('#pass').focus();
await key(page, '#pass', 'keydown', true);
assert.equal(await hint.textContent(), want[lang], lang);
}

// Switching language while it shows updates it (focus stays in the field).
await page.evaluate(() => {
const sel = document.querySelector('.lang-picker');
sel.value = 'en';
sel.dispatchEvent(new Event('change'));
});
await page.waitForFunction(() => document.documentElement.lang === 'en');
assert.equal(await hint.textContent(), want.en);

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": "pingping-browser-tests",
"private": true,
"scripts": {
"test": "node --test"
},
"devDependencies": {
"playwright": "1.62.1"
}
}
39 changes: 39 additions & 0 deletions browser_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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/")
}
s, err := NewStore(t.TempDir(), nil)
if err != nil {
t.Fatal(err)
}
defer s.Close()
if err := s.CreateUser("admin", "browser-test-password", RoleAdmin); err != nil {
t.Fatal(err)
}
run := NewRunner(ProbeCfg{Packets: 1, TimeoutMs: 50}, s, NewDetector(s))
defer run.Stop()
srv := httptest.NewServer(newMux(&Config{}, s, run))
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 @@ pingping's console, its notification-area icon and the Zabbix template talk to
pingping through this API. The same rules hold in fogping 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