zerver/tests/test_helpers.py: Use text_type for api_keys.

Also encode/decode strings appropriately when using api_keys to generate
basic auth header.
Also fix clashing annotations in zerver/tests/test_external.py.
This commit is contained in:
Eklavya Sharma
2016-06-29 01:50:38 +05:30
parent eb3bde40a0
commit d9eb711e5e
2 changed files with 8 additions and 7 deletions

View File

@@ -50,7 +50,7 @@ from zerver.lib.str_utils import NonBinaryStr
from contextlib import contextmanager from contextlib import contextmanager
import six import six
API_KEYS = {} # type: Dict[str, str] API_KEYS = {} # type: Dict[text_type, text_type]
@contextmanager @contextmanager
def stub(obj, name, f): def stub(obj, name, f):
@@ -285,16 +285,16 @@ class AuthedTestCase(TestCase):
'terms': True}) 'terms': True})
def get_api_key(self, email): def get_api_key(self, email):
# type: (str) -> str # type: (text_type) -> text_type
if email not in API_KEYS: if email not in API_KEYS:
API_KEYS[email] = get_user_profile_by_email(email).api_key API_KEYS[email] = get_user_profile_by_email(email).api_key
return API_KEYS[email] return API_KEYS[email]
def api_auth(self, email): def api_auth(self, email):
# type: (str) -> Dict[str, str] # type: (text_type) -> Dict[str, text_type]
credentials = "%s:%s" % (email, self.get_api_key(email)) credentials = u"%s:%s" % (email, self.get_api_key(email))
return { return {
'HTTP_AUTHORIZATION': 'Basic ' + base64.b64encode(credentials) 'HTTP_AUTHORIZATION': u'Basic ' + base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
} }
def get_streams(self, email): def get_streams(self, email):

View File

@@ -22,9 +22,10 @@ import DNS
import mock import mock
import time import time
import ujson import ujson
from six.moves import urllib
from six.moves import urllib
from six.moves import range from six.moves import range
from six import text_type
class MITNameTest(TestCase): class MITNameTest(TestCase):
def test_valid_hesiod(self): def test_valid_hesiod(self):
@@ -66,7 +67,7 @@ class RateLimitTests(AuthedTestCase):
remove_ratelimit_rule(1, 5) remove_ratelimit_rule(1, 5)
def send_api_message(self, email, api_key, content): def send_api_message(self, email, api_key, content):
# type: (str, str, str) -> HttpResponse # type: (text_type, text_type, text_type) -> HttpResponse
return self.client.post("/api/v1/send_message", {"type": "stream", return self.client.post("/api/v1/send_message", {"type": "stream",
"to": "Verona", "to": "Verona",
"client": "test suite", "client": "test suite",