Files
api2/tests/test_web.py
T
2016-06-30 17:20:43 +02:00

36 lines
1.0 KiB
Python

# coding=UTF-8
import flask_testing
import base64
from api import create_app
class TestHttpBasicAuth(flask_testing.TestCase):
api_key = 'secret'
def create_app(self):
return create_app('tests.config')
def _enable_basic_auth(self):
self.app.config['API_KEYS'] = [self.api_key]
def _create_auth_headers(self, username, password=''):
data = base64.b64encode(bytes(':'.join([username, password]), 'ascii')).decode('ascii')
headers = [('Authorization', 'Basic %s' % data)]
return headers
def test_missing_credentials(self):
self._enable_basic_auth()
response = self.client.get('/')
self.assert401(response)
def test_correct_credentials(self):
self._enable_basic_auth()
headers = self._create_auth_headers(self.api_key)
response = self.client.get('/', headers=headers)
self.assert200(response)
def test_basic_auth_disabled(self):
response = self.client.get('/')
self.assert200(response)