Skip to content

Commit 2bf56e8

Browse files
authored
Please fix the dmarc-report-api issues (canada-ca#785)
* remove extra default argument assignments * Just send the whole error to log * demo arguments are now required * Use arguemnt info rather than from return data * fix tests * forgot to run black
1 parent 22524d3 commit 2bf56e8

File tree

4 files changed

+19
-42
lines changed

4 files changed

+19
-42
lines changed

api/functions/external_graphql_api_request.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
from app import logger
99

1010

11-
def create_transport(
12-
api_domain=os.getenv("DMARC_REPORT_API_URL"),
13-
auth_token=os.getenv("DMARC_REPORT_API_TOKEN"),
14-
) -> RequestsHTTPTransport:
11+
def create_transport(api_domain, auth_token) -> RequestsHTTPTransport:
1512
"""
1613
This function creates the transport object to send requests to an external
1714
API
@@ -27,10 +24,7 @@ def create_transport(
2724
return transport
2825

2926

30-
def create_client(
31-
api_domain=os.getenv("DMARC_REPORT_API_URL"),
32-
auth_token=os.getenv("DMARC_REPORT_API_TOKEN"),
33-
) -> Client:
27+
def create_client(api_domain, auth_token) -> Client:
3428
"""
3529
This function is used to create the client that will execute the query
3630
:param api_domain: External API URL used to create the transport object
@@ -71,23 +65,8 @@ def send_request(
7165
return data
7266

7367
except Exception as e:
74-
# Make sure the below stays like so
75-
# error_str = e.__str__().replace("\'", '\"')
76-
# Black will try and change it and it will break !!!
77-
error_str = e.__str__().replace("\'", '\"')
78-
try:
79-
error_dict = json.loads(error_str)
80-
if error_dict.get("message", None):
81-
logger.error(
82-
f"Error occurred on the dmarc-report-api side: {str(error_dict.get('message'))}"
83-
)
84-
if summary_table:
85-
return {}
86-
else:
87-
raise GraphQLError("Error when querying dmarc-report-api.")
88-
89-
except ValueError as ve:
90-
logger.error(
91-
f"Value Error occurred when receiving data from dmarc-report-api: {str(ve)}"
92-
)
93-
raise GraphQLError("Error, when querying dmarc-report-api.")
68+
logger.error(f"Error occurred on the dmarc-report-api side: {str(e)}")
69+
if summary_table:
70+
return {}
71+
else:
72+
raise GraphQLError("Error when querying dmarc-report-api.")

api/schemas/dmarc_report_summary_table/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
period=graphene.Argument(
3232
PeriodEnums,
3333
description="The period in which the returned data is relevant to.",
34-
required=False,
34+
required=True,
3535
),
3636
year=graphene.Argument(
3737
Year,
3838
description="The year in which the returned data is relevant to.",
39-
required=False,
39+
required=True,
4040
),
4141
description="Query for creating domain summary table.",
4242
resolver=resolve_demo_dmarc_report_summary_table,

api/schemas/dmarc_report_summary_table/resolver.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs):
8989
thirtyDays: $thirtyDays
9090
) {
9191
period {
92-
startDate
93-
endDate
9492
categoryTotals {
9593
fullPass
9694
passSpfOnly
@@ -105,7 +103,6 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs):
105103

106104
# Send request
107105
data = send_request(query=query, variables=variables, summary_table=True,)
108-
109106
temp_dict = data.get("getDmarcSummaryByPeriod", {}).get("period", {})
110107
temp_dict.update({"domain": domain.domain})
111108
data_list.append(temp_dict)
@@ -121,11 +118,12 @@ def resolve_dmarc_report_summary_table(self, info, **kwargs):
121118
logger.info(
122119
f"User: {user_id} successfully retrieved the DmarcReportSummaryTable information for all their domains."
123120
)
121+
124122
return DmarcReportSummaryTable(
125123
# Get Month Name
126-
calendar.month_name[int(data_list[0].get("endDate")[5:7].lstrip("0"))],
124+
datetime.strptime(period.lower(), "%b").strftime("%B"),
127125
# Get Year
128-
data_list[0].get("endDate")[0:4].lstrip("0"),
126+
year,
129127
data_list,
130128
)
131129

@@ -146,8 +144,8 @@ def resolve_demo_dmarc_report_summary_table(self, info, **kwargs):
146144

147145
return DmarcReportSummaryTable(
148146
# Get Month Name
149-
calendar.month_name[int(data_list[0].get("endDate")[5:7].lstrip("0"))],
147+
datetime.strptime(period.lower(), "%b").strftime("%B"),
150148
# Get Year
151-
data_list[0].get("endDate")[0:4].lstrip("0"),
149+
year,
152150
data_list,
153151
)

api/tests/test_dmarc_report_summary_table.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_super_admin(
7979
query="""
8080
query {
8181
dmarcReportSummaryTable (
82-
period: AUGUST
82+
period: MAY
8383
year: "2020"
8484
) {
8585
month
@@ -170,7 +170,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_org_admin(save, mocker, c
170170
query="""
171171
query {
172172
dmarcReportSummaryTable (
173-
period: AUGUST
173+
period: MAY
174174
year: "2020"
175175
) {
176176
month
@@ -253,7 +253,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_user_write(save, mocker,
253253
query="""
254254
query {
255255
dmarcReportSummaryTable (
256-
period: AUGUST
256+
period: MAY
257257
year: "2020"
258258
) {
259259
month
@@ -336,7 +336,7 @@ def test_valid_get_dmarc_report_summary_table_query_as_user_read(save, mocker, c
336336
query="""
337337
query {
338338
dmarcReportSummaryTable (
339-
period: AUGUST
339+
period: MAY
340340
year: "2020"
341341
) {
342342
month
@@ -418,7 +418,7 @@ def test_dmarc_report_summary_to_ensure_error_occurs_when_no_domains_exist(
418418
query="""
419419
query {
420420
dmarcReportSummaryTable (
421-
period: AUGUST
421+
period: MAY
422422
year: "2020"
423423
) {
424424
month

0 commit comments

Comments
 (0)