feat(web): add bilingual website and central account frontend
This commit is contained in:
commit
622f396364
20 changed files with 1315 additions and 0 deletions
43
tests/api.test.js
Normal file
43
tests/api.test.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { request, isSession, safeAvatar, ApiError } from '../src/api.js';
|
||||
import { messages, detectLanguage } from '../src/i18n.js';
|
||||
|
||||
test('catalogs match and never silently fall back to a missing translation',()=>{
|
||||
assert.deepEqual(Object.keys(messages.ru).sort(),Object.keys(messages.en).sort());
|
||||
for (const catalog of Object.values(messages)) for (const value of Object.values(catalog)) assert.ok(typeof value==='string' && value.length>0);
|
||||
});
|
||||
test('language preference is bounded and follows the OS without storage',()=>{
|
||||
assert.equal(detectLanguage('en',['ru-RU']),'en');
|
||||
assert.equal(detectLanguage('bad',['ru-RU']),'ru');
|
||||
assert.equal(detectLanguage(undefined,['pl-PL']),'en');
|
||||
assert.equal(detectLanguage('RU',['en-US']),'en');
|
||||
});
|
||||
test('API keeps authentication on the same origin and sends CSRF only in header',async()=>{
|
||||
let observed;
|
||||
const result=await request('/v1/auth/logout',{method:'POST',csrf:'fixture-csrf',fetcher:async(path,options)=>{
|
||||
observed={path,options};return {ok:true,status:204};
|
||||
}});
|
||||
assert.equal(result,null);
|
||||
assert.equal(observed.path,'/v1/auth/logout');
|
||||
assert.equal(observed.options.credentials,'same-origin');
|
||||
assert.equal(observed.options.cache,'no-store');
|
||||
assert.equal(observed.options.headers['X-CSRF-Token'],'fixture-csrf');
|
||||
assert.equal(observed.options.method,'POST');
|
||||
await assert.rejects(request('https://evil.example/v1/auth/session'),TypeError);
|
||||
await assert.rejects(request('//evil.example/v1/auth/session'),TypeError);
|
||||
});
|
||||
test('API failures expose a generic code and preserve status for expired sessions',async()=>{
|
||||
await assert.rejects(request('/v1/auth/session',{fetcher:async()=>({ok:false,status:401})}),error=>error instanceof ApiError && error.status===401 && error.message==='request_failed');
|
||||
});
|
||||
test('session validation rejects malformed authenticated payloads',()=>{
|
||||
assert.ok(isSession({authenticated:false}));
|
||||
assert.ok(isSession({authenticated:true,csrf_token:'fixture',user:{user_id:'fixture',handle:'test',display_name:'Test'}}));
|
||||
assert.equal(isSession({authenticated:true,user:{}}),false);
|
||||
assert.equal(isSession({authenticated:'true'}),false);
|
||||
assert.equal(isSession({authenticated:true,csrf_token:'',user:{user_id:'fixture',handle:'test',display_name:'Test'}}),false);
|
||||
});
|
||||
test('avatars allow only uncredentialed HTTPS GitHub image origin',()=>{
|
||||
assert.equal(safeAvatar('https://avatars.githubusercontent.com/u/1?v=4'),'https://avatars.githubusercontent.com/u/1?v=4');
|
||||
for (const url of ['javascript:alert(1)','data:image/svg+xml,<svg/>','http://avatars.githubusercontent.com/u/1','https://avatars.githubusercontent.com.evil.example/u/1','https://evil@avatars.githubusercontent.com/u/1','https://avatars.githubusercontent.com:8443/u/1','https://evil.example/u/1',null]) assert.equal(safeAvatar(url),null);
|
||||
});
|
||||
104
tests/browser_qa.py
Normal file
104
tests/browser_qa.py
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
"""Optional browser integration QA. Requires locally installed Playwright + Chromium.
|
||||
Run with an already running npm run dev/preview. All API responses are test fixtures.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse
|
||||
from playwright.sync_api import sync_playwright
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--url', default='http://127.0.0.1:4176')
|
||||
parser.add_argument('--artifacts', default='/tmp/vnox-web-qa')
|
||||
args = parser.parse_args()
|
||||
if urlparse(args.url).hostname not in {'127.0.0.1', 'localhost', '::1'}:
|
||||
raise SystemExit('Browser fixture QA is restricted to a loopback preview.')
|
||||
artifacts = Path(args.artifacts)
|
||||
artifacts.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with sync_playwright() as p:
|
||||
browser = p.chromium.launch(executable_path='/usr/bin/chromium', headless=True, args=['--no-sandbox'])
|
||||
context = browser.new_context(viewport={'width': 1440, 'height': 1000}, locale='ru-RU')
|
||||
state = {'info': {'capabilities': {'github_auth': 'enabled'}}, 'session': {'authenticated': False}, 'info_status': 200, 'session_status': 200, 'logout_status': 204}
|
||||
logouts = []
|
||||
def api(route):
|
||||
path = urlparse(route.request.url).path
|
||||
if path == '/v1/info': route.fulfill(status=state['info_status'], json=state['info'])
|
||||
elif path == '/v1/auth/session': route.fulfill(status=state['session_status'], json=state['session'])
|
||||
elif path == '/v1/auth/logout':
|
||||
logouts.append({'method': route.request.method, 'headers': route.request.all_headers()})
|
||||
route.fulfill(status=state['logout_status'], body='' if state['logout_status'] == 204 else '{}')
|
||||
else: route.fulfill(status=404, json={'error': 'fixture_not_found'})
|
||||
context.route('**/v1/**', api)
|
||||
page = context.new_page()
|
||||
errors = []
|
||||
page.on('pageerror', lambda error: errors.append(str(error)))
|
||||
page.goto(args.url)
|
||||
page.locator('#service-status').filter(has_text='отвечает').wait_for()
|
||||
page.screenshot(path=str(artifacts / 'desktop-hero.png'))
|
||||
page.screenshot(path=str(artifacts / 'desktop-full.png'), full_page=True)
|
||||
page.locator('[data-tab="voice"]').click()
|
||||
assert page.locator('.voice-orbit').count() == 1
|
||||
page.locator('[data-tab="voice"]').press('ArrowRight')
|
||||
assert page.locator('[data-tab="theme"]').get_attribute('aria-selected') == 'true'
|
||||
page.locator('[data-theme="light"]').click()
|
||||
assert page.locator('.product-diagram').get_attribute('data-preview-theme') == 'light'
|
||||
page.locator('[data-language="en"]').click()
|
||||
assert page.locator('h1').inner_text() == 'Your people.\nYour connection.'
|
||||
assert page.locator('[data-tab="theme"]').get_attribute('aria-selected') == 'true'
|
||||
assert page.locator('[data-theme="light"]').get_attribute('aria-pressed') == 'true'
|
||||
for locale in ['en', 'ru']:
|
||||
page.locator(f'[data-language="{locale}"]').click()
|
||||
for width in [320, 390, 768, 1440]:
|
||||
page.set_viewport_size({'width': width, 'height': 844})
|
||||
assert not page.evaluate('document.documentElement.scrollWidth > innerWidth'), (locale, width)
|
||||
page.set_viewport_size({'width': 390, 'height': 844})
|
||||
page.screenshot(path=str(artifacts / 'mobile-full.png'), full_page=True)
|
||||
page.goto(args.url + '/account')
|
||||
page.locator('.github-login[href="/v1/auth/github/start"]').wait_for()
|
||||
assert page.locator('.github-login').get_attribute('aria-disabled') is None
|
||||
page.screenshot(path=str(artifacts / 'account-login-mobile.png'), full_page=True)
|
||||
state['info'] = {'capabilities': {'github_auth': 'not_enabled'}}
|
||||
page.reload()
|
||||
page.locator('#login-availability').filter(has_text='не включён').wait_for()
|
||||
assert page.locator('.github-login').get_attribute('href') is None
|
||||
state['info_status'] = 503
|
||||
page.reload()
|
||||
page.locator('#retry-info:not([hidden])').wait_for()
|
||||
state['session_status'] = 503
|
||||
page.reload()
|
||||
page.locator('#retry-session').wait_for()
|
||||
state['info_status'] = 200
|
||||
state['session_status'] = 200
|
||||
state['info'] = {'capabilities': {'github_auth': 'enabled'}}
|
||||
state['session'] = {'authenticated': True, 'csrf_token': 'fixture-csrf-token', 'user': {'user_id': 'fixture-id', 'handle': '<script>window.__injected=true</script>', 'display_name': '<img src=x onerror="window.__injected=true">', 'avatar_url': 'javascript:alert(1)'}}
|
||||
page.locator('#retry-session').click()
|
||||
page.locator('#profile-id').wait_for()
|
||||
assert page.locator('#profile-name').inner_text() == state['session']['user']['display_name']
|
||||
assert page.locator('#profile-name img, #profile-handle script, #profile-avatar img').count() == 0
|
||||
assert page.evaluate('typeof window.__injected') == 'undefined'
|
||||
assert page.evaluate('Object.keys(localStorage)') == ['vnox-web-language']
|
||||
state['session']['user'] = {'user_id': 'c11ca6c3-296a-40f6-a420-8a097d401e26', 'handle': 'u_preview', 'display_name': 'VNOX Preview', 'avatar_url': None}
|
||||
page.reload()
|
||||
page.locator('#profile-id').wait_for()
|
||||
page.set_viewport_size({'width': 1440, 'height': 1000})
|
||||
page.screenshot(path=str(artifacts / 'account-profile-desktop.png'))
|
||||
for locale in ['ru', 'en']:
|
||||
page.locator(f'[data-language="{locale}"]').click()
|
||||
for width in [320,390,768,1440]:
|
||||
page.set_viewport_size({'width':width,'height':844})
|
||||
assert not page.evaluate('document.documentElement.scrollWidth > innerWidth'), ('account',locale,width)
|
||||
page.locator('#logout').click()
|
||||
page.locator('.github-login').wait_for()
|
||||
assert logouts[-1]['method'] == 'POST'
|
||||
assert logouts[-1]['headers']['x-csrf-token'] == 'fixture-csrf-token'
|
||||
assert logouts[-1]['headers']['origin'] == args.url
|
||||
assert page.evaluate('sessionStorage.length') == 0
|
||||
state['logout_status'] = 401
|
||||
page.reload()
|
||||
page.locator('#profile-id').wait_for()
|
||||
page.locator('#logout').click()
|
||||
page.locator('.github-login').wait_for()
|
||||
assert not errors, errors
|
||||
browser.close()
|
||||
print(json.dumps({'result':'passed','assertions':'tabs, theme, RU/EN, 320/390/768/1440 layout, login enabled/disabled/errors, session/profile XSS safety, CSRF logout/expired session, no persisted secrets','artifacts':str(artifacts)}))
|
||||
16
tests/server.test.js
Normal file
16
tests/server.test.js
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { once } from 'node:events';
|
||||
import http from 'node:http';
|
||||
function fetchRaw(port,path){return new Promise((resolve,reject)=>{http.get({host:'127.0.0.1',port,path},response=>{let body='';response.on('data',chunk=>body+=chunk);response.on('end',()=>resolve({status:response.statusCode,body}));}).on('error',reject);});}
|
||||
test('preview serves only public/source assets and blocks encoded traversal',async t=>{
|
||||
const port=24176;
|
||||
const process=spawn(globalThis.process.execPath,['scripts/serve.mjs'],{env:{...globalThis.process.env,PORT:String(port)},stdio:['ignore','pipe','pipe']});
|
||||
t.after(()=>process.kill('SIGTERM'));
|
||||
await Promise.race([once(process.stdout,'data'),once(process,'exit').then(()=>{throw new Error('preview failed to start');})]);
|
||||
for(const path of ['/','/account','/account/','/src/app.js','/assets/vnox-mark.png']) assert.equal((await fetchRaw(port,path)).status,200,path);
|
||||
for(const path of ['/src/%2e%2e%2fpackage.json','/src/%2e%2e%2f.env','/src/%2e%2e%2fLICENSE']) assert.equal((await fetchRaw(port,path)).status,403,path);
|
||||
assert.equal((await fetchRaw(port,'/package.json')).status,404);
|
||||
assert.equal((await fetchRaw(port,'/v1/auth/session')).status,503);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue