test structure restructed a bit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
# coding=UTF-8
|
||||
|
||||
import unittest2
|
||||
import base64
|
||||
from api import create_app
|
||||
|
||||
try:
|
||||
import httplib
|
||||
except ImportError:
|
||||
# support for python 3
|
||||
import http.client as httplib
|
||||
|
||||
|
||||
class ApiTest(unittest2.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.app = create_app('tests.config')
|
||||
self.client = self.app.test_client()
|
||||
|
||||
|
||||
class TestHttpBasicAuth(ApiTest):
|
||||
|
||||
api_key = 'secret'
|
||||
|
||||
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()
|
||||
resp = self.client.get('/')
|
||||
self.assertEqual(resp.status_code, httplib.UNAUTHORIZED)
|
||||
|
||||
def test_correct_credentials(self):
|
||||
self._enable_basic_auth()
|
||||
headers = self._create_auth_headers(self.api_key)
|
||||
resp = self.client.get('/', headers=headers)
|
||||
self.assertEqual(resp.status_code, httplib.OK)
|
||||
|
||||
def test_basic_auth_disabled(self):
|
||||
resp = self.client.get('/')
|
||||
self.assertEqual(resp.status_code, httplib.OK)
|
||||
Reference in New Issue
Block a user