test_vpn_app.py 2.11 KB
Newer Older
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
import tempfile
import shutil
import unittest
from flask import session
from autovpn import vpn_app


class VpnAppTest(unittest.TestCase):

    def setUp(self):
        self.tmpdir = tempfile.mkdtemp()
        self.config = {
            'DEBUG': 'true',
            'SECRET_KEY': 'somesecret',
            'VPN_CA_ROOT': self.tmpdir,
            'VPN_CA_SUBJECT': {'CN': 'test CA', 'O': 'test'},
            'VPN_CA_BITS': 1024,
            'VPN_ENDPOINT': 'vpn.example.com',
            'VPN_SITE_URL': 'http://localhost:4000/',
            'FOOTER': '''
<p class="footer">
  built by <a href="http://www.autistici.org/">autistici.org</a>
</p>
''',
            'AUTH_ENABLE': True,
            'AUTH_FUNCTION': lambda x, y: (x and y and x == y),
            }

        self.app = vpn_app.make_app(self.config)

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def test_login_ok(self):
        with self.app.test_client() as c:
            rv = c.get('/login')
            csrf = session['_csrf']
            rv = c.post('/login', data={
                    '_csrf': csrf,
                    'username': 'admin',
                    'password': 'admin'},
                        follow_redirects=True)
            self.assertTrue('download of the ZIP file' in rv.data)

    def test_login_fail(self):
        with self.app.test_client() as c:
            rv = c.get('/login')
            csrf = session['_csrf']
            rv = c.post('/login', data={
                    '_csrf': csrf,
                    'username': 'user',
                    'password': 'wrong password'},
                        follow_redirects=True)
            self.assertFalse(session.get('dl_ok'))
            self.assertTrue('Authentication failed' in rv.data)

    def test_cert_dl(self):
        with self.app.test_client() as c:
            with c.session_transaction() as sess:
                sess['dl_ok'] = True
                sess['_csrf'] = 'csrf'
                sess['logged_in'] = True
            rv = c.get('/newcertdl?_csrf=csrf')
            self.assertEquals('200 OK', rv.status)
            self.assertEquals('application/zip', rv.content_type)