From d8717739e1e4976144530554cd5d3d9248d2f1c0 Mon Sep 17 00:00:00 2001 From: nslandolt Date: Wed, 19 Aug 2020 10:17:19 -0300 Subject: [PATCH 1/6] remove extra default argument assignments --- api/functions/external_graphql_api_request.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/api/functions/external_graphql_api_request.py b/api/functions/external_graphql_api_request.py index b57e1d2c30..2aa971b8e3 100644 --- a/api/functions/external_graphql_api_request.py +++ b/api/functions/external_graphql_api_request.py @@ -8,10 +8,7 @@ from app import logger -def create_transport( - api_domain=os.getenv("DMARC_REPORT_API_URL"), - auth_token=os.getenv("DMARC_REPORT_API_TOKEN"), -) -> RequestsHTTPTransport: +def create_transport(api_domain, auth_token) -> RequestsHTTPTransport: """ This function creates the transport object to send requests to an external API @@ -27,10 +24,7 @@ def create_transport( return transport -def create_client( - api_domain=os.getenv("DMARC_REPORT_API_URL"), - auth_token=os.getenv("DMARC_REPORT_API_TOKEN"), -) -> Client: +def create_client(api_domain, auth_token) -> Client: """ This function is used to create the client that will execute the query :param api_domain: External API URL used to create the transport object From 5b6408342d442915b6031b4e7fe51bab0a0629df Mon Sep 17 00:00:00 2001 From: nslandolt Date: Wed, 19 Aug 2020 11:09:51 -0300 Subject: [PATCH 2/6] Just send the whole error to log --- api/functions/external_graphql_api_request.py | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/api/functions/external_graphql_api_request.py b/api/functions/external_graphql_api_request.py index 2aa971b8e3..8bf637ade6 100644 --- a/api/functions/external_graphql_api_request.py +++ b/api/functions/external_graphql_api_request.py @@ -65,23 +65,11 @@ def send_request( return data except Exception as e: - # Make sure the below stays like so - # error_str = e.__str__().replace("\'", '\"') - # Black will try and change it and it will break !!! - error_str = e.__str__().replace("\'", '\"') - try: - error_dict = json.loads(error_str) - if error_dict.get("message", None): - logger.error( - f"Error occurred on the dmarc-report-api side: {str(error_dict.get('message'))}" - ) - if summary_table: - return {} - else: - raise GraphQLError("Error when querying dmarc-report-api.") + logger.error( + f"Error occurred on the dmarc-report-api side: {str(e)}" + ) + if summary_table: + return {} + else: + raise GraphQLError("Error when querying dmarc-report-api.") - except ValueError as ve: - logger.error( - f"Value Error occurred when receiving data from dmarc-report-api: {str(ve)}" - ) - raise GraphQLError("Error, when querying dmarc-report-api.") From e31c1f00e7443fefd23a30d36f723a733abfffa4 Mon Sep 17 00:00:00 2001 From: nslandolt Date: Wed, 19 Aug 2020 11:10:03 -0300 Subject: [PATCH 3/6] demo arguments are now required --- api/schemas/dmarc_report_summary_table/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/schemas/dmarc_report_summary_table/__init__.py b/api/schemas/dmarc_report_summary_table/__init__.py index fd4437a960..3a6ed6fd06 100644 --- a/api/schemas/dmarc_report_summary_table/__init__.py +++ b/api/schemas/dmarc_report_summary_table/__init__.py @@ -31,12 +31,12 @@ period=graphene.Argument( PeriodEnums, description="The period in which the returned data is relevant to.", - required=False, + required=True, ), year=graphene.Argument( Year, description="The year in which the returned data is relevant to.", - required=False, + required=True, ), description="Query for creating domain summary table.", resolver=resolve_demo_dmarc_report_summary_table, From 2beb0d49961a73d14c7072c261ff7c7285620032 Mon Sep 17 00:00:00 2001 From: nslandolt Date: Wed, 19 Aug 2020 11:11:27 -0300 Subject: [PATCH 4/6] Use arguemnt info rather than from return data --- api/schemas/dmarc_report_summary_table/resolver.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/api/schemas/dmarc_report_summary_table/resolver.py b/api/schemas/dmarc_report_summary_table/resolver.py index 640581b89d..83e044d742 100644 --- a/api/schemas/dmarc_report_summary_table/resolver.py +++ b/api/schemas/dmarc_report_summary_table/resolver.py @@ -89,8 +89,6 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs): thirtyDays: $thirtyDays ) { period { - startDate - endDate categoryTotals { fullPass passSpfOnly @@ -105,7 +103,6 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs): # Send request data = send_request(query=query, variables=variables, summary_table=True,) - temp_dict = data.get("getDmarcSummaryByPeriod", {}).get("period", {}) temp_dict.update({"domain": domain.domain}) data_list.append(temp_dict) @@ -121,11 +118,12 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs): logger.info( f"User: {user_id} successfully retrieved the DmarcReportSummaryTable information for all their domains." ) + return DmarcReportSummaryTable( # Get Month Name - calendar.month_name[int(data_list[0].get("endDate")[5:7].lstrip("0"))], + datetime.strptime(period.lower(),'%b').strftime('%B'), # Get Year - data_list[0].get("endDate")[0:4].lstrip("0"), + year, data_list, ) @@ -145,9 +143,9 @@ def resolve_demo_dmarc_report_summary_table(self, info, **kwargs): data_list.append(temp_dict) return DmarcReportSummaryTable( - # Get Month Name - calendar.month_name[int(data_list[0].get("endDate")[5:7].lstrip("0"))], + # Get Month Name + datetime.strptime(period.lower(),'%b').strftime('%B'), # Get Year - data_list[0].get("endDate")[0:4].lstrip("0"), + year, data_list, ) From d910be4aa82a0520d307ffbbbf844268205821bf Mon Sep 17 00:00:00 2001 From: nslandolt Date: Wed, 19 Aug 2020 11:14:54 -0300 Subject: [PATCH 5/6] fix tests --- api/tests/test_dmarc_report_summary_table.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/api/tests/test_dmarc_report_summary_table.py b/api/tests/test_dmarc_report_summary_table.py index f2e10c7010..9422121188 100644 --- a/api/tests/test_dmarc_report_summary_table.py +++ b/api/tests/test_dmarc_report_summary_table.py @@ -79,7 +79,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_super_admin( query=""" query { dmarcReportSummaryTable ( - period: AUGUST + period: MAY year: "2020" ) { month @@ -170,7 +170,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_org_admin(save, mocker, c query=""" query { dmarcReportSummaryTable ( - period: AUGUST + period: MAY year: "2020" ) { month @@ -253,7 +253,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_user_write(save, mocker, query=""" query { dmarcReportSummaryTable ( - period: AUGUST + period: MAY year: "2020" ) { month @@ -336,7 +336,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_user_read(save, mocker, c query=""" query { dmarcReportSummaryTable ( - period: AUGUST + period: MAY year: "2020" ) { month @@ -418,7 +418,7 @@ def test_dmarc_report_summary_to_ensure_error_occurs_when_no_domains_exist( query=""" query { dmarcReportSummaryTable ( - period: AUGUST + period: MAY year: "2020" ) { month From fad2583f77e94c6ac775899714bf111c790780bc Mon Sep 17 00:00:00 2001 From: nslandolt Date: Wed, 19 Aug 2020 11:26:03 -0300 Subject: [PATCH 6/6] forgot to run black --- api/functions/external_graphql_api_request.py | 5 +---- api/schemas/dmarc_report_summary_table/resolver.py | 6 +++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/api/functions/external_graphql_api_request.py b/api/functions/external_graphql_api_request.py index 8bf637ade6..ba0bc30ba1 100644 --- a/api/functions/external_graphql_api_request.py +++ b/api/functions/external_graphql_api_request.py @@ -65,11 +65,8 @@ def send_request( return data except Exception as e: - logger.error( - f"Error occurred on the dmarc-report-api side: {str(e)}" - ) + logger.error(f"Error occurred on the dmarc-report-api side: {str(e)}") if summary_table: return {} else: raise GraphQLError("Error when querying dmarc-report-api.") - diff --git a/api/schemas/dmarc_report_summary_table/resolver.py b/api/schemas/dmarc_report_summary_table/resolver.py index 83e044d742..75004b1182 100644 --- a/api/schemas/dmarc_report_summary_table/resolver.py +++ b/api/schemas/dmarc_report_summary_table/resolver.py @@ -121,7 +121,7 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs): return DmarcReportSummaryTable( # Get Month Name - datetime.strptime(period.lower(),'%b').strftime('%B'), + datetime.strptime(period.lower(), "%b").strftime("%B"), # Get Year year, data_list, @@ -143,8 +143,8 @@ def resolve_demo_dmarc_report_summary_table(self, info, **kwargs): data_list.append(temp_dict) return DmarcReportSummaryTable( - # Get Month Name - datetime.strptime(period.lower(),'%b').strftime('%B'), + # Get Month Name + datetime.strptime(period.lower(), "%b").strftime("%B"), # Get Year year, data_list,