Created
August 21, 2011 14:52
-
-
Save jarus/1160696 to your computer and use it in GitHub Desktop.
Testing Flask application with basic authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
from myapplication import app | |
class MyTestCase(unittest.TestCase): | |
def setUp(self): | |
self.app = app.test_client() | |
def tearDown(self): | |
pass | |
def open_with_auth(self, url, method, username, password): | |
return self.app.open(url, | |
method=method, | |
headers={ | |
'Authorization': 'Basic ' + base64.b64encode(username + \ | |
":" + password) | |
} | |
) | |
def test_login(self): | |
res = self.open_with_auth('/user/login', 'GET', 'username', | |
'password') |
It's probably too late but seems like b64encode requires binary string - in this case you could do 'Authorization': 'Basic ' + base64.b64encode(bytes(username + ":" + password, 'ascii')).decode('ascii')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry budy, but this does not work for me: TypeError: 'str' does not support the buffer interface :(
Any ideas?