From 9d1e80d0a8f80a3c22058ec9acd07229d764b011 Mon Sep 17 00:00:00 2001 From: Briar Scott Date: Wed, 28 Oct 2020 13:42:52 -0400 Subject: [PATCH] Fixed flake8 linting errors Fixed typo in .flake8 comment Fixed dictionary format --- .flake8 | 4 +- demo/app.py | 2 +- setup.py | 3 ++ tests/test_create_jwt_args.py | 18 ++++--- tests/test_exchange_authorization_code.py | 58 ++++++++++++----------- tests/test_health_check.py | 27 ++++++----- tests/test_state.py | 3 +- 7 files changed, 61 insertions(+), 54 deletions(-) diff --git a/.flake8 b/.flake8 index 43b3645..96cc878 100644 --- a/.flake8 +++ b/.flake8 @@ -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 diff --git a/demo/app.py b/demo/app.py index 559d235..f4ca1ad 100644 --- a/demo/app.py +++ b/demo/app.py @@ -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") diff --git a/setup.py b/setup.py index 4abb356..b39f94f 100644 --- a/setup.py +++ b/setup.py @@ -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__'): @@ -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') diff --git a/tests/test_create_jwt_args.py b/tests/test_create_jwt_args.py index 6c50df0..4a186e5 100644 --- a/tests/test_create_jwt_args.py +++ b/tests/test_create_jwt_args.py @@ -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): @@ -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) diff --git a/tests/test_exchange_authorization_code.py b/tests/test_exchange_authorization_code.py index 5fb464d..35501c3 100644 --- a/tests/test_exchange_authorization_code.py +++ b/tests/test_exchange_authorization_code.py @@ -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 @@ -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 @@ -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): """ @@ -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 @@ -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 @@ -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: @@ -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") diff --git a/tests/test_health_check.py b/tests/test_health_check.py index f9de991..6f0a556 100644 --- a/tests/test_health_check.py +++ b/tests/test_health_check.py @@ -10,14 +10,18 @@ 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): @@ -25,10 +29,9 @@ 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 @@ -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, @@ -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') diff --git a/tests/test_state.py b/tests/test_state.py index 4b1e325..5804896 100644 --- a/tests/test_state.py +++ b/tests/test_state.py @@ -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))