|
| 1 | +import subprocess |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def run_userthrottle(tool_path, redis): |
| 8 | + def _run_userthrottle(auth='foo', ip='10.11.112.113', mfrom='foo@example.com', hfrom=None, nrcpts=1): |
| 9 | + env = { |
| 10 | + 'SIMTA_AUTH_ID': auth, |
| 11 | + 'SIMTA_REMOTE_IP': ip, |
| 12 | + 'SIMTA_SMTP_MAIL_FROM': mfrom, |
| 13 | + 'SIMTA_NRCPTS': str(nrcpts), |
| 14 | + } |
| 15 | + |
| 16 | + if hfrom: |
| 17 | + env['SIMTA_HEADER_FROM'] = hfrom |
| 18 | + |
| 19 | + res = subprocess.check_output( |
| 20 | + [ |
| 21 | + tool_path('userthrottle'), |
| 22 | + '-p', str(redis), |
| 23 | + '-d', 'example.com', |
| 24 | + ], |
| 25 | + env=env, |
| 26 | + ) |
| 27 | + |
| 28 | + return res.strip() |
| 29 | + return _run_userthrottle |
| 30 | + |
| 31 | + |
| 32 | +def test_single(run_userthrottle): |
| 33 | + assert run_userthrottle() == '1' |
| 34 | + |
| 35 | + |
| 36 | +def test_multiple(run_userthrottle): |
| 37 | + assert run_userthrottle() == '1' |
| 38 | + assert run_userthrottle() == '2' |
| 39 | + |
| 40 | + |
| 41 | +def test_nrcpt(run_userthrottle): |
| 42 | + assert run_userthrottle() == '1' |
| 43 | + assert run_userthrottle(nrcpts=5) == '6' |
| 44 | + |
| 45 | + |
| 46 | +def test_multiple_ips(run_userthrottle): |
| 47 | + assert run_userthrottle() == '1' |
| 48 | + assert run_userthrottle(ip='10.12.112.114') == '2' |
| 49 | + |
| 50 | + |
| 51 | +def test_mfrom_mismatch(run_userthrottle): |
| 52 | + assert run_userthrottle(mfrom='bar@example.com') == '2' |
| 53 | + |
| 54 | + |
| 55 | +def test_multiple_addresses(run_userthrottle): |
| 56 | + assert run_userthrottle() == '1' |
| 57 | + assert run_userthrottle(mfrom='bar@example.com') == '5' |
| 58 | + assert run_userthrottle(mfrom='baz@example.com') == '17' |
| 59 | + assert run_userthrottle() == '23' |
| 60 | + |
| 61 | + |
| 62 | +def test_multiple_auth(run_userthrottle): |
| 63 | + assert run_userthrottle() == '1' |
| 64 | + assert run_userthrottle(auth='bar', mfrom='bar@example.com') == '1' |
| 65 | + |
| 66 | + |
| 67 | +def test_multiple_mfrom(run_userthrottle): |
| 68 | + assert run_userthrottle() == '1' |
| 69 | + assert run_userthrottle(mfrom='bar@example.com') == '5' |
| 70 | + |
| 71 | + |
| 72 | +def test_hfrom_match(run_userthrottle): |
| 73 | + assert run_userthrottle(hfrom='foo@example.com') == '1' |
| 74 | + assert run_userthrottle(hfrom='foo@example.com') == '2' |
| 75 | + |
| 76 | + |
| 77 | +def test_hfrom_mismatch(run_userthrottle): |
| 78 | + assert run_userthrottle(hfrom='bar@example.com') == '2' |
| 79 | + assert run_userthrottle(hfrom='bar@example.com') == '4' |
| 80 | + |
| 81 | + |
| 82 | +def test_domain_mismatch(run_userthrottle): |
| 83 | + assert run_userthrottle(mfrom='foo@example.edu') == '2' |
| 84 | + assert run_userthrottle(hfrom='foo@example.com') == '4' |
| 85 | + |
| 86 | + |
| 87 | +def test_worst_case(run_userthrottle): |
| 88 | + assert run_userthrottle() == '1' |
| 89 | + assert run_userthrottle(mfrom='bar@example.edu') == '9' |
| 90 | + assert run_userthrottle(mfrom='baz@example.edu', hfrom='baz@example.com') == '57' |
| 91 | + assert run_userthrottle(mfrom='baz@example.edu', hfrom='baz@example.com', nrcpts=10) == '537' |
| 92 | + assert run_userthrottle(auth='bar', mfrom='baz@example.edu', hfrom='baz@example.com') == '8' |
| 93 | + assert run_userthrottle(auth='bar', mfrom='foo@example.edu', hfrom='baz@example.com') == '24' |
0 commit comments