83 lines
4.4 KiB
Python
83 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Opt-in acceptance: called by Jest against disposable local Nest/PG only."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import requests
|
|
import seo_performance_tracker as w
|
|
|
|
|
|
def main():
|
|
base = os.environ['SEO_LOCAL_BASE']
|
|
assert base.startswith('http://127.0.0.1:'), 'local test server only'
|
|
session = requests.Session()
|
|
session.headers['Authorization'] = 'Bearer ' + os.environ['CANINA_ADMIN_TOKEN']
|
|
backend = w.Backend(base, session)
|
|
checks = {}
|
|
|
|
def call(method, suffix='', body=None, expected=200):
|
|
response = session.request(method, backend.base + suffix, json=body, timeout=30)
|
|
assert response.status_code == expected, (method, suffix, response.status_code, response.text)
|
|
return response.json()
|
|
|
|
assert requests.get(backend.base, timeout=30).status_code == 401
|
|
assert requests.get(backend.base, headers={'Authorization': 'Bearer invalid'}, timeout=30).status_code == 401
|
|
assert requests.get(backend.base, headers={'Authorization': 'Bearer ' + os.environ['SEO_CUSTOMER_TOKEN']}, timeout=30).status_code == 403
|
|
checks['missing_invalid_nonadmin_denied'] = True
|
|
created = call('POST', body={
|
|
'title': 'Disposable blackseed research acceptance', 'hypothesis': 'Integration fixture only',
|
|
'primaryKeyword': 'blackseed', 'targetKeywords': ['blackseed'],
|
|
'expectedOutcome': 'Measure existing published article without publishing',
|
|
'blogId': os.environ['SEO_FIXTURE_BLOG_ID'],
|
|
}, expected=201)
|
|
suffix = '/' + created['id']
|
|
feedback = call('PATCH', suffix + '/feedback', {'expectedVersion': 0, 'feedback': 'Measure only; do not publish'})
|
|
assert (feedback['version'], feedback['feedbackVersion']) == (1, 1)
|
|
call('PATCH', suffix + '/action', {'expectedVersion': 1, 'feedbackVersion': 0, 'actionId': 'disposable-fixture'}, expected=409)
|
|
action = call('PATCH', suffix + '/action', {'expectedVersion': 1, 'feedbackVersion': 1, 'actionId': 'disposable-fixture', 'status': 'ACTIONED'})
|
|
assert (action['version'], action['actionId']) == (2, 'disposable-fixture')
|
|
checks['feedback_stale_action_and_correct_link'] = True
|
|
finding = next(f for f in backend.findings() if f['id'] == created['id'])
|
|
assert finding['blog']['slug'] == 'dog-black-seed-nigella-sativa'
|
|
with tempfile.TemporaryDirectory(prefix='seo-worker-') as directory:
|
|
store = w.Store(Path(directory) / 'state.sqlite')
|
|
collector = w.GoogleCollector(w.get_google_session())
|
|
payload = w.collect_finding(finding, collector, w.period(), store)
|
|
assert payload is not None
|
|
# Reopen durable worker state before delivery to exercise process restart semantics.
|
|
store.db.close()
|
|
store = w.Store(Path(directory) / 'state.sqlite')
|
|
assert w.deliver_snapshots(store, backend) == 1
|
|
assert w.deliver_snapshots(store, backend) == 0
|
|
history = call('GET', suffix)
|
|
assert len(history['snapshots']) == 1
|
|
first = history['snapshots'][0]
|
|
assert first['attributedSalesValue'] is None
|
|
assert first['ga4ScrollDepthRate'] is None
|
|
key, raw = store.db.execute('SELECT key,payload FROM snapshots').fetchone()
|
|
body = w.snapshot_body(json.loads(raw), key)
|
|
# Actual network retry of identical immutable request, not just local delivered flag.
|
|
retried = call('POST', suffix + '/snapshots', body, expected=201)
|
|
assert retried['id'] == first['id']
|
|
call('POST', suffix + '/snapshots', dict(body, analysisNotes='changed payload'), expected=409)
|
|
assert len(call('GET', suffix)['snapshots']) == 1
|
|
assert not store.snapshot(payload)
|
|
checks['worker_snapshot_history_network_retry_conflict'] = True
|
|
receipt = {
|
|
'observedAt': w.now(), 'backend': 'disposable local Nest module + real PostgreSQL',
|
|
'auth': 'test-only JWT strategy with real JwtAuthGuard/RolesGuard; NOT production identity/API-key',
|
|
'checks': checks, 'google': payload, 'persistedSnapshot': first,
|
|
'productionWrites': False, 'scheduled': False,
|
|
}
|
|
destination = os.environ.get('SEO_HTTP_RECEIPT')
|
|
if destination:
|
|
Path(destination).parent.mkdir(parents=True, exist_ok=True)
|
|
Path(destination).write_text(json.dumps(receipt, ensure_ascii=False, indent=2))
|
|
store.db.close()
|
|
print('REAL_HTTP_ACCEPTANCE_PASSED ' + json.dumps(checks))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|