Skip to content

Commit

Permalink
Fixed flake8 linting errors
Browse files Browse the repository at this point in the history
Fixed typo in .flake8 comment

Fixed dictionary format
  • Loading branch information
Briar Scott committed Oct 29, 2020
1 parent 33f0116 commit 9d1e80d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 54 deletions.
4 changes: 2 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
# Ignore line too long errors
ignore = E501
# Ignore line too long errors and errors with importing for __init__.py
ignore = E501,F403,F401
2 changes: 1 addition & 1 deletion demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def login_post():
password = request.form.get('password')

# Check user's first factor
if password == None or password == "":
if password is None or password == "":
return render_template("login.html",
message="Missing password")

Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import os.path
import codecs


def read(rel_path):
here = os.path.abspath(os.path.dirname(__file__))
with codecs.open(os.path.join(here, rel_path), 'r') as fp:
return fp.read()


def get_version(rel_path):
for line in read(rel_path).splitlines():
if line.startswith('__version__'):
Expand All @@ -15,6 +17,7 @@ def get_version(rel_path):
else:
raise RuntimeError("Unable to find version string.")


requirements_filename = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'requirements.txt')

Expand Down
18 changes: 8 additions & 10 deletions tests/test_create_jwt_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
RAND_ALPHANUMERIC_STR = "deadbeef"

SUCCESS_JWT_ARGS = {
'iss': CLIENT_ID,
'sub': CLIENT_ID,
'aud': client.OAUTH_V1_TOKEN_ENDPOINT,
'exp': EXPIRATION_TIME,
'jti': RAND_ALPHANUMERIC_STR
}
'iss': CLIENT_ID,
'sub': CLIENT_ID,
'aud': client.OAUTH_V1_TOKEN_ENDPOINT,
'exp': EXPIRATION_TIME,
'jti': RAND_ALPHANUMERIC_STR
}


class TestCreateJwtArgs(unittest.TestCase):
Expand All @@ -32,10 +32,8 @@ def test_create_jwt_args_success(self):
"""
Test that _create_jwt_args creates proper jwt arguments
"""
self.client._generate_rand_alphanumeric = MagicMock(
return_value=RAND_ALPHANUMERIC_STR)
actual_jwt_args = self.client._create_jwt_args(
client.OAUTH_V1_TOKEN_ENDPOINT)
self.client._generate_rand_alphanumeric = MagicMock(return_value=RAND_ALPHANUMERIC_STR)
actual_jwt_args = self.client._create_jwt_args(client.OAUTH_V1_TOKEN_ENDPOINT)
self.assertEqual(SUCCESS_JWT_ARGS, actual_jwt_args)


Expand Down
58 changes: 31 additions & 27 deletions tests/test_exchange_authorization_code.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from jwt.exceptions import InvalidSignatureError
from mock import MagicMock, patch
from duo_universal import client
import json
import unittest
import requests
import jwt
Expand All @@ -26,15 +24,19 @@

REQUESTS_POST_ERROR = 400
REQUESTS_POST_SUCCESS = 200
ERROR_WRONG_DUO_CODE = {'error': 'invalid_grant',
'error_description': 'The provided authorization grant or '
'refresh token is invalid, expired, '
'revoked, does not match '
'the redirection URI.'}
ERROR_WRONG_CLIENT_ID = {'error': 'invalid_client',
'error_description': 'Invalid Client assertion: '
'The `iss` claim must match '
'the supplied client_id'}
ERROR_WRONG_DUO_CODE = {
'error': 'invalid_grant',
'error_description': 'The provided authorization grant or '
'refresh token is invalid, expired, '
'revoked, does not match '
'the redirection URI.'
}
ERROR_WRONG_CLIENT_ID = {
'error': 'invalid_client',
'error_description': 'Invalid Client assertion: '
'The `iss` claim must match '
'the supplied client_id'
}
NONE = None


Expand All @@ -43,17 +45,20 @@ class TestExchangeAuthCodeInputs(unittest.TestCase):
def setUp(self):
self.client = client.Client(CLIENT_ID, CLIENT_SECRET, HOST, REDIRECT_URI)
self.client_wrong_client_id = client.Client(WRONG_CLIENT_ID, CLIENT_SECRET,
HOST, REDIRECT_URI)
self.jwt_decode = {'auth_result':
{'result': 'allow',
'status': 'allow',
'status_msg': 'Login Successful'},
'aud': CLIENT_ID,
'auth_time': time.time(),
'exp': time.time() + client.FIVE_MINUTES_IN_SECONDS,
'iat': time.time() + 1,
'iss': 'https://{}/oauth/v1/token'.format(HOST),
'preferred_username': USERNAME}
HOST, REDIRECT_URI)
self.jwt_decode = {
"auth_result": {
"result": "allow",
"status": "allow",
"status_msg": "Login Successful",
},
"aud": CLIENT_ID,
"auth_time": time.time(),
"exp": time.time() + client.FIVE_MINUTES_IN_SECONDS,
"iat": time.time() + 1,
"iss": "https://{}/oauth/v1/token".format(HOST),
"preferred_username": USERNAME,
}

def test_no_duo_code(self):
"""
Expand All @@ -65,7 +70,7 @@ def test_no_duo_code(self):
self.assertEqual(e, client.ERR_DUO_CODE)

@patch('requests.post', MagicMock(
side_effect=requests.Timeout(ERROR_TIMEOUT)))
side_effect=requests.Timeout(ERROR_TIMEOUT)))
def test_exchange_authorization_code_timeout_error(self):
"""
Test that exchange_authorization_code_for_2fa_result
Expand All @@ -76,8 +81,7 @@ def test_exchange_authorization_code_timeout_error(self):
self.assertEqual(e, ERROR_TIMEOUT)

@patch('requests.post', MagicMock(
side_effect=requests.ConnectionError(
ERROR_NETWORK_CONNECTION_FAILED)))
side_effect=requests.ConnectionError(ERROR_NETWORK_CONNECTION_FAILED)))
def test_exchange_authorization_code_duo_down_error(self):
"""
Test that exchange_authorization_code_for_2fa_result
Expand Down Expand Up @@ -120,7 +124,7 @@ def test_exchange_authorization_code_wrong_duo_code(self,
@patch('requests.post')
def test_exchange_authorization_code_wrong_cert(self, requests_mock):
"""
Test that a wrong Duo Cert causes the client to throw a DuoException
Test that a wrong Duo Cert causes the client to throw a DuoException
"""
requests_mock.side_effect = requests.exceptions.SSLError(WRONG_CERT_ERROR)
with self.assertRaises(client.DuoException) as e:
Expand Down Expand Up @@ -167,7 +171,7 @@ def test_invalid_signing_key_failure(self, mock_post):
encoded_jwt = jwt.encode(self.jwt_decode, WRONG_CLIENT_SECRET, algorithm='HS512')
id_token = {"id_token": encoded_jwt}
mock_post.return_value = MockResponse(id_token)
with self.assertRaises(client.DuoException):
with self.assertRaises(client.DuoException) as e:
self.client.exchange_authorization_code_for_2fa_result(DUO_CODE, USERNAME)
self.assertEqual(e.message, "Signature verification failed")

Expand Down
27 changes: 15 additions & 12 deletions tests/test_health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@
REDIRECT_URI = "https://www.example.com"

WRONG_CERT_ERROR = 'certificate verify failed'
SUCCESS_CHECK = {'response': {'timestamp': 1573068322}, 'stat': 'OK'}
SUCCESS_CHECK = {
'response': {'timestamp': 1573068322},
'stat': 'OK'
}
ERROR_TIMEOUT = "Connection to api-xxxxxxx.test.duosecurity.com timed out."
ERROR_NETWORK_CONNECTION_FAILED = "Failed to establish a new connection"
ERROR_WRONG_CLIENT_ID = {'message': 'invalid_client',
'code': 40002, 'stat': 'FAIL',
'message_detail': 'The provided client_assertion'
'was invalid.',
'timestamp': 1573053670}
ERROR_WRONG_CLIENT_ID = {
'message': 'invalid_client',
'code': 40002, 'stat': 'FAIL',
'message_detail': 'The provided client_assertion was invalid.',
'timestamp': 1573053670
}


class TestHealthCheck(unittest.TestCase):

def setUp(self):
self.client = client.Client(CLIENT_ID, CLIENT_SECRET, HOST, REDIRECT_URI)
self.client_wrong_client_id = client.Client(WRONG_CLIENT_ID, CLIENT_SECRET,
HOST, REDIRECT_URI)
HOST, REDIRECT_URI)

@patch('requests.post', MagicMock(side_effect=requests.Timeout(
ERROR_TIMEOUT)))
@patch('requests.post', MagicMock(side_effect=requests.Timeout(ERROR_TIMEOUT)))
def test_health_check_timeout_error(self):
"""
Test health check failure due to a timeout
Expand All @@ -38,7 +41,7 @@ def test_health_check_timeout_error(self):
self.assertEqual(e, ERROR_TIMEOUT)

@patch('requests.post', MagicMock(side_effect=requests.ConnectionError(
ERROR_NETWORK_CONNECTION_FAILED)))
ERROR_NETWORK_CONNECTION_FAILED)))
def test_health_check_duo_down_error(self):
"""
Test health check failure due to a connection error,
Expand All @@ -65,9 +68,9 @@ def test_health_check_bad_cert(self, requests_mock):
Test health check failure due to bad Duo Cert
"""
requests_mock.side_effect = requests.exceptions.SSLError(WRONG_CERT_ERROR)
with self.assertRaises(client.DuoException):
with self.assertRaises(client.DuoException) as e:
self.client.health_check()
self.assertEqual(e, WRONG_CERT_ERROR);
self.assertEqual(e, WRONG_CERT_ERROR)

@patch('requests.post')
@patch('json.loads')
Expand Down
3 changes: 1 addition & 2 deletions tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ def test_success(self):
Test that _generate_rand_alphanumeric
returns string with length STATE_LENGTH
"""
generate = self.client._generate_rand_alphanumeric(
client.STATE_LENGTH)
generate = self.client._generate_rand_alphanumeric(client.STATE_LENGTH)
self.assertEqual(client.STATE_LENGTH, len(generate))


Expand Down

0 comments on commit 9d1e80d

Please sign in to comment.