File size: 9,520 Bytes
2b86c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""Exercise Phase 1 against an isolated local demo API and production frontend.

Account/session/isolation/credential persistence checks use the real local backend.
Only provider verification responses are intercepted in-browser to exercise UI states;
those checks are explicitly marked wire fixtures and do not verify live integration.
Run this only against a disposable database. No real credentials or purchases are used.
"""
import argparse
import json
import secrets
from pathlib import Path
from urllib.parse import urlsplit
from playwright.sync_api import sync_playwright, expect


def verify(base, output):
    if urlsplit(base).hostname not in ('127.0.0.1', 'localhost'):
        raise ValueError('Only an isolated localhost demo server is accepted')
    checks, errors = [], []
    password = secrets.token_urlsafe(24)
    names = ['phase1_' + secrets.token_hex(8) for _ in range(2)]
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=True)
        context = browser.new_context(viewport={'width': 1440, 'height': 1100})
        context.set_default_timeout(15000)
        context.route('**/*', lambda route: route.continue_() if urlsplit(route.request.url).hostname in ('127.0.0.1', 'localhost') else route.abort())
        page = context.new_page()
        page.on('pageerror', lambda error: errors.append(str(error)))

        def panel():
            if not page.get_by_role('heading', name='Connect workspace', exact=True).is_visible():
                page.get_by_role('button', name='Workspace', exact=True).click()
            return page.get_by_role('region', name='Workspace connection')

        def auth(username, register=False):
            workspace = panel()
            workspace.get_by_role('button', name='Create account' if register else 'Sign in', exact=True).first.click()
            workspace.get_by_label('Username', exact=True).fill(username)
            workspace.get_by_label('Password', exact=True).fill(password)
            workspace.locator('form button[type=submit]').click()
            expect(page.get_by_text(username + ' 路 USER', exact=True)).to_be_visible()
            return page.evaluate("localStorage.getItem('hf_session_token')")

        def call(path, method='GET', body=None, token=None):
            kwargs = {'headers': {'Authorization': 'Bearer ' + token} if token else {}}
            if body is not None:
                kwargs['data'] = body
            return context.request.fetch(base + '/api/v2' + path, method=method, **kwargs)

        def sign_out():
            workspace = panel()
            workspace.get_by_role('button', name='Sign out', exact=True).click()
            expect(page.get_by_text('DISCONNECTED', exact=True)).to_be_visible()

        page.goto(base)
        first_token = auth(names[0], register=True)
        me = call('/auth/me', token=first_token)
        assert me.ok and me.json()['role'] == 'user'
        checks.append({'name': 'self-registration-server-role', 'evidence': 'real_local_backend'})
        forged = call('/auth/register', 'POST', {'username': 'forged_' + secrets.token_hex(5), 'password': password, 'role': 'admin'})
        assert forged.status == 422, forged.text()
        checks.append({'name': 'registration-role-forgery-rejected', 'evidence': 'real_local_backend'})
        page.reload()
        expect(page.get_by_text(names[0] + ' 路 USER', exact=True)).to_be_visible()
        checks.append({'name': 'session-rehydrated-after-reload', 'evidence': 'real_browser_and_backend'})
        workspace = panel()
        workspace.get_by_text('Manual token entry (advanced)', exact=True).click()
        fake_token = 'synthetic-browser-test-' + secrets.token_hex(16)
        workspace.get_by_label('Swiggy access token', exact=True).fill(fake_token)
        workspace.get_by_role('button', name='Store credentials', exact=True).click()
        expect(workspace.get_by_text('Credential stored 路 stored_unverified', exact=True)).to_be_visible()
        expect(workspace.get_by_text('Expiry unknown.', exact=False)).to_be_visible()
        expect(workspace.get_by_label('Swiggy access token', exact=True)).to_have_value('')
        status = call('/provider/credentials', token=first_token).json()
        assert status['configured'] and not status['expiry_known'] and status['verification_status'] == 'stored_unverified'
        assert fake_token not in json.dumps(status)
        checks.append({'name': 'stored-is-unverified-unknown-expiry-secret-not-returned', 'evidence': 'real_browser_and_backend'})

        # Browser-only verification state rendering. Never forward synthetic tokens upstream.
        context.route('**/api/v2/provider/verify', lambda route: route.fulfill(status=200, json={'credential_status': {**status, 'verification_status': 'verified', 'verified_at': '2026-10-20T00:00:00+00:00'}}))
        workspace.get_by_role('button', name='Check provider access', exact=True).click()
        expect(workspace.get_by_text('Credential stored 路 verified', exact=True)).to_be_visible()
        expect(workspace.get_by_text('purchases and other journeys remain unverified.', exact=False)).to_be_visible()
        checks.append({'name': 'read-only-check-rendered-with-limited-scope', 'evidence': 'browser_wire_fixture_only'})
        context.unroute('**/api/v2/provider/verify')
        context.route('**/api/v2/provider/verify', lambda route: route.fulfill(status=401, json={'detail': 'Provider authorization expired; reconnect required'}))
        workspace.get_by_role('button', name='Check provider access', exact=True).click()
        expect(workspace.get_by_text('Provider authorization expired; reconnect required', exact=True)).to_be_visible()
        checks.append({'name': 'verification-failure-visible', 'evidence': 'browser_wire_fixture_only'})
        context.unroute('**/api/v2/provider/verify')
        workspace.get_by_label('Swiggy access token', exact=True).fill('sensitive-draft-must-clear')
        sign_out()
        assert call('/auth/me', token=first_token).status in (401, 403)
        checks.append({'name': 'logout-revokes-server-session', 'evidence': 'real_browser_and_backend'})
        second_token = auth(names[1], register=True)
        second_status = call('/provider/credentials', token=second_token).json()
        assert not second_status['configured']
        workspace = panel()
        workspace.get_by_text('Manual token entry (advanced)', exact=True).click()
        expect(workspace.get_by_label('Swiggy access token', exact=True)).to_have_value('')
        checks.append({'name': 'second-account-cannot-see-first-credential-or-draft', 'evidence': 'real_browser_and_backend'})
        sign_out()
        fresh_first_token = auth(names[0])
        workspace = panel()
        expect(workspace.get_by_text('Credential stored 路 stored_unverified', exact=True)).to_be_visible()
        workspace.get_by_role('button', name='Disconnect credential', exact=True).click()
        expect(workspace.get_by_text('Provider credential disconnected. Swiggy live mode is deactivated.', exact=True)).to_be_visible()
        assert not call('/provider/credentials', token=fresh_first_token).json()['configured']
        checks.append({'name': 'sign-in-and-disconnect-persisted-credential', 'evidence': 'real_browser_and_backend'})
        sign_out()
        panel().get_by_role('button', name='Open isolated reviewer workspace', exact=True).click()
        expect(page.get_by_text('ISOLATED SIMULATION', exact=False).first).to_be_visible()
        reviewer_token = page.evaluate("localStorage.getItem('hf_session_token')")
        reviewer_id = call('/auth/me', token=reviewer_token).json()['id']
        workspace = panel()
        expect(workspace.get_by_text('Reviewer Simulation Mode:', exact=False)).to_be_visible()
        assert workspace.get_by_role('button', name='Store credentials', exact=True).count() == 0
        assert call('/provider/credentials', 'POST', {'token': fake_token}, reviewer_token).status == 403
        sign_out()
        panel().get_by_role('button', name='Open isolated reviewer workspace', exact=True).click()
        expect(page.get_by_text('ISOLATED SIMULATION', exact=False).first).to_be_visible()
        new_reviewer_token = page.evaluate("localStorage.getItem('hf_session_token')")
        assert call('/auth/me', token=new_reviewer_token).json()['id'] != reviewer_id
        checks.append({'name': 'reviewer-isolation-and-live-credential-denial', 'evidence': 'real_browser_and_backend'})
        assert not errors, errors
        output.parent.mkdir(parents=True, exist_ok=True)
        page.screenshot(path=str(output.with_suffix('.png')), full_page=True)
        browser.close()
    result = {'suite': 'phase1-workspace-v1', 'passed': len(checks), 'checks': checks,
              'page_errors': errors, 'live_provider_requests': 0, 'real_purchases': 0,
              'limits': 'Verification rendering uses clearly marked browser wire fixtures. Backend verification and live integration require separate evidence.'}
    output.write_text(json.dumps(result, indent=2) + '\n')
    print(json.dumps({'passed': len(checks), 'output': str(output), 'page_errors': errors}))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--base', default='http://127.0.0.1:58244')
    parser.add_argument('--output', type=Path, default=Path('.local/evidence/phase1/browser-results.json'))
    args = parser.parse_args()
    verify(args.base.rstrip('/'), args.output)