This commit is contained in:
lpm0073 2022-10-06 12:45:15 -05:00
parent 6d1a58b9d6
commit 39fd2384d0
3 changed files with 70 additions and 24 deletions

View File

@ -1,6 +1,8 @@
{ {
"access_token": "bftc1ostvp3fucj9sxzkdgonnhcoi9uuqq2da93m",
"date_joined": "2022-10-04 00:46:37", "date_joined": "2022-10-04 00:46:37",
"email": "lpm0073@gmail.com", "email": "lpm0073@gmail.com",
"expires_in": 3600,
"first_name": "Lawrence", "first_name": "Lawrence",
"fullname": "Lawrence McDaniel", "fullname": "Lawrence McDaniel",
"id": 6, "id": 6,

48
data/tests.py Normal file
View File

@ -0,0 +1,48 @@
import json
# Opening JSON file
f = open('get_user_details_extended.json')
# returns JSON object as
# a dictionary
response = json.load(f)
def is_valid_user_details(response) -> bool:
"""
validate that the object passed is a dict containing at least the keys
in qc_keys.
"""
qc_keys = [
"id",
"date_joined",
"email",
"first_name",
"fullname",
"is_staff",
"is_superuser",
"last_name",
"username",
]
if all(key in response for key in qc_keys):
return True
return False
def is_wp_oauth_refresh_token_response(response) -> bool:
"""
validate that the structure of the response contains the keys of
a refresh token dict.
"""
if not is_valid_user_details(response):
return False
qc_keys = ["access_token", "expires_in", "refresh_token", "scope", "token_type"]
if all(key in response for key in qc_keys):
return True
return False
print('is_valid_user_details')
print(is_valid_user_details(response))
print('is_wp_oauth_refresh_token_response')
print(is_wp_oauth_refresh_token_response(response))

View File

@ -111,8 +111,9 @@ class StepwiseMathWPOAuth2(BaseOAuth2):
def is_valid_user_details(self, response) -> bool: def is_valid_user_details(self, response) -> bool:
""" """
validate that the object passed is a dict containing at least the keys validate that the object passed is a json dict containing at least
in qc_keys. the keys in qc_keys. These are the dict keys created in get_user_details()
default return object.
""" """
if not type(response) == dict: if not type(response) == dict:
logger.warning( logger.warning(
@ -188,7 +189,7 @@ class StepwiseMathWPOAuth2(BaseOAuth2):
""" """
if not self.is_valid_user_details(response): if not self.is_valid_user_details(response):
return False return False
qc_keys = ["access_token" "expires_in", "refresh_token", "scope", "token_type"] qc_keys = ["access_token", "expires_in", "refresh_token", "scope", "token_type"]
if all(key in response for key in qc_keys): if all(key in response for key in qc_keys):
return True return True
return False return False
@ -205,7 +206,17 @@ class StepwiseMathWPOAuth2(BaseOAuth2):
return True return True
return False return False
def get_response_type(self, response): def is_valid_get_user_details_response(self, response) -> bool:
"""
True if the response object can be processed by get_user_details()
"""
if self.is_valid_user_details(response):
return True
if self.is_wp_oauth_response(response):
return True
return False
def get_response_type(self, response) -> str:
if type(response) != dict: if type(response) != dict:
return "unknown response of type {t}".format(t=type(response)) return "unknown response of type {t}".format(t=type(response))
if self.is_wp_oauth_error(response): if self.is_wp_oauth_error(response):
@ -270,11 +281,9 @@ class StepwiseMathWPOAuth2(BaseOAuth2):
# see https://python-social-auth.readthedocs.io/en/latest/backends/implementation.html # see https://python-social-auth.readthedocs.io/en/latest/backends/implementation.html
# Return user details from the Wordpress user account # Return user details from the Wordpress user account
def get_user_details(self, response) -> dict: def get_user_details(self, response) -> dict:
if not ( if not self.is_valid_get_user_details_response(response):
self.is_valid_user_details(response) or self.is_wp_oauth_response(response)
):
logger.error( logger.error(
"get_user_details() - received an unrecognized response of {t}. Cannot continue: {response}".format( "get_user_details() - received an invalid response object of {t}. Cannot continue: {response}".format(
t=self.get_response_type(response), t=self.get_response_type(response),
response=json.dumps(response, sort_keys=True, indent=4), response=json.dumps(response, sort_keys=True, indent=4),
) )
@ -284,7 +293,8 @@ class StepwiseMathWPOAuth2(BaseOAuth2):
if VERBOSE_LOGGING: if VERBOSE_LOGGING:
logger.info( logger.info(
"get_user_details() begin with response: {response}".format( "get_user_details() received {t}: {response}".format(
t=self.get_response_type(response),
response=json.dumps(response, sort_keys=True, indent=4) response=json.dumps(response, sort_keys=True, indent=4)
) )
) )
@ -303,7 +313,7 @@ class StepwiseMathWPOAuth2(BaseOAuth2):
# ------------------------------------------------------------- # -------------------------------------------------------------
if VERBOSE_LOGGING: if VERBOSE_LOGGING:
logger.info( logger.info(
"get_user_details() - detected an enhanced get_user_details() dict in the response: {response}".format( "get_user_details() - detected an extended get_user_details() dict in the response: {response}".format(
response=json.dumps(response, sort_keys=True, indent=4) response=json.dumps(response, sort_keys=True, indent=4)
) )
) )
@ -388,20 +398,6 @@ class StepwiseMathWPOAuth2(BaseOAuth2):
user_details = self.get_user_details(response) user_details = self.get_user_details(response)
if VERBOSE_LOGGING:
logger.info(
"user_data() local variable user_details: {user_details}".format(
user_details=json.dumps(user_details, sort_keys=True, indent=4)
)
)
if VERBOSE_LOGGING:
logger.info(
"user_data() class property value of self.user_details: {user_details}".format(
user_details=json.dumps(
self.user_details, sort_keys=True, indent=4
)
)
)
except ValueError as e: except ValueError as e:
logger.error("user_data() {err}".format(err=e)) logger.error("user_data() {err}".format(err=e))
return None return None