-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow error response to return a 200 status code (Fixes #114)
- Loading branch information
1 parent
e3c6e5f
commit f3e6a57
Showing
2 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import unittest | ||
import base64 | ||
from flask import Flask, Response | ||
from flask_httpauth import HTTPBasicAuth | ||
|
||
|
||
class HTTPAuthTestCase(unittest.TestCase): | ||
responses = [ | ||
['error', 401], | ||
[('error', 403), 403], | ||
[('error', 200), 200], | ||
[Response('error'), 200], | ||
[Response('error', 403), 403], | ||
] | ||
|
||
def setUp(self): | ||
app = Flask(__name__) | ||
app.config['SECRET_KEY'] = 'my secret' | ||
|
||
basic_verify_auth = HTTPBasicAuth() | ||
|
||
@basic_verify_auth.verify_password | ||
def basic_verify_auth_verify_password(username, password): | ||
return False | ||
|
||
@basic_verify_auth.error_handler | ||
def error_handler(): | ||
self.assertIsNone(basic_verify_auth.current_user()) | ||
return self.error_response | ||
|
||
@app.route('/') | ||
@basic_verify_auth.login_required | ||
def index(): | ||
return 'index' | ||
|
||
self.app = app | ||
self.basic_verify_auth = basic_verify_auth | ||
self.client = app.test_client() | ||
|
||
def test_default_status_code(self): | ||
creds = base64.b64encode(b'foo:bar').decode('utf-8') | ||
|
||
for r in self.responses: | ||
self.error_response = r[0] | ||
response = self.client.get( | ||
'/', headers={'Authorization': 'Basic ' + creds}) | ||
self.assertEqual(response.status_code, r[1]) |