Archived
68 lines
4.1 KiB
Python
68 lines
4.1 KiB
Python
"""Exercise the authorization guard through the HTTP stack in an isolated process."""
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import textwrap
|
|
|
|
|
|
def test_user_is_restricted_to_assigned_interface(tmp_path):
|
|
root = Path(__file__).resolve().parents[1]
|
|
env = dict(os.environ, WGTOOLS_DATABASE=str(tmp_path / "test.db"),
|
|
WGTOOLS_SESSION_KEY_FILE=str(tmp_path / "session.key"))
|
|
script = textwrap.dedent('''
|
|
from unittest.mock import patch
|
|
from fastapi.testclient import TestClient
|
|
import app.main as m
|
|
from app.core.auth import create_user, set_user_access
|
|
from app.core.preferences import update_branding
|
|
create_user('admin', 'password123')
|
|
user = create_user('tester', 'password123')
|
|
set_user_access(user['id'], 'user', ['wg1'])
|
|
update_branding('', '#123456', '')
|
|
def helper(action, payload=None, **kwargs):
|
|
if action == 'interfaces_list':
|
|
return {'ok': True, 'interfaces': [{'name': 'wg1'}, {'name': 'wg2'}]}
|
|
if action == 'snapshot':
|
|
return {'ok': True, 'interface': kwargs.get('interface'), 'config': {'interface': {'post_up': ['secret hook']}}, 'peers': []}
|
|
return {'ok': True, 'interface': kwargs.get('interface')}
|
|
with patch.object(m, 'run_helper', side_effect=helper), patch.object(m, 'merge_wireguard_last_seen', return_value=[]), patch.object(m, 'list_vps', return_value=[]):
|
|
c = TestClient(m.app)
|
|
assert 'WGTools' not in c.get('/login').text
|
|
assert c.post('/api/auth/login', json={'username':'tester','password':'password123'}).status_code == 200
|
|
client_page = c.get('/clients')
|
|
assert client_page.status_code == 200
|
|
assert 'Przekierowania portów' in client_page.text
|
|
assert 'href="/server"' not in client_page.text
|
|
assert c.post('/api/interfaces/delete', json={'interface':'wg1'}).status_code == 403
|
|
assert c.post('/api/server/config/apply', json={'content':''}).status_code == 403
|
|
assert c.post('/api/branding', json={}).status_code == 403
|
|
assert c.post('/api/update/upload', content=b'invalid').status_code == 403
|
|
assert c.get('/api/extended/ip-groups').status_code == 403
|
|
assert [x['name'] for x in c.get('/api/interfaces').json()['interfaces']] == ['wg1']
|
|
c.cookies.set('wgtools_interface', 'wg2')
|
|
assert c.get('/api/snapshot').json()['interface'] == 'wg1'
|
|
assert c.get('/api/extended/forwardings').json()['interface'] == 'wg1'
|
|
assert c.get('/').status_code == 200
|
|
c.cookies.set('wgtools_interface', 'wg1')
|
|
response = c.get('/api/snapshot')
|
|
assert response.status_code == 200
|
|
assert 'post_up' not in response.json()['config']['interface']
|
|
assert c.post('/api/clients/create', json={'name':'bad','type':'site_to_site','remote_networks':['10.1.0.0/16']}).status_code == 403
|
|
set_user_access(user['id'], 'user', ['wg1'], {'wg1':['add']})
|
|
assert c.post('/api/extended/forwardings', json={'public_ip':'192.0.2.1','public_port':1234,'target_ip':'10.0.0.2','target_port':1234,'protocol':'tcp'}).status_code == 403
|
|
assert c.post('/api/clients/update', json={'name':'peer','public_key':'x'*44}).status_code == 403
|
|
c.post('/api/auth/logout')
|
|
assert c.post('/api/auth/login', json={'username':'admin','password':'password123'}).status_code == 200
|
|
server = c.get('/server')
|
|
assert server.status_code == 200
|
|
for prefix in ['setup', 'new-interface', 'server-edit']:
|
|
assert f'id="{prefix}-listen-mode"' in server.text
|
|
assert f'id="{prefix}-snat-mode"' in server.text
|
|
assert f'id="{prefix}-forward-sources"' in server.text
|
|
settings = c.get('/settings')
|
|
assert 'id="user-access-modal"' in settings.text
|
|
assert 'data-settings-tab="branding"' in settings.text
|
|
''')
|
|
subprocess.run([sys.executable, '-c', script], cwd=root, env=env, check=True, timeout=30)
|