Skip to content

Commit

Permalink
Fix mock.call_args usage in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cuu508 committed May 3, 2023
1 parent db1b75e commit 5113b96
Show file tree
Hide file tree
Showing 24 changed files with 114 additions and 176 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ https://api.slack.com/apps/APP_ID/oauth?).

To enable Discord integration, you will need to:

* register a new application on https://discordapp.com/developers/applications/me
* register a new application on https://discord.com/developers/applications/me
* add a redirect URI to your Discord application. The URI format is
`SITE_ROOT/integrations/add_discord/`. For example, if you are running a
development server on `localhost:8000` then the redirect URI would be
Expand Down
3 changes: 1 addition & 2 deletions hc/api/tests/test_check_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ def test_it_prunes_object_storage(self, remove_objects):
Ping.objects.create(owner=check, n=1, object_size=1000)

check.prune()
args, kwargs = remove_objects.call_args
code, upto_n = args
code, upto_n = remove_objects.call_args.args
self.assertEqual(code, check.code)
self.assertEqual(upto_n, 1)
3 changes: 1 addition & 2 deletions hc/api/tests/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def test_call(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
payload = kwargs["data"]
payload = mock_post.call_args.kwargs["data"]
self.assertEqual(payload["To"], "+1234567890")

n = Notification.objects.get()
Expand Down
15 changes: 6 additions & 9 deletions hc/api/tests/test_notify_discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def test_it_works(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
self.assertEqual(args[1], "https://example.org/slack")
url = mock_post.call_args.args[1]
self.assertEqual(url, "https://example.org/slack")

attachment = kwargs["json"]["attachments"][0]
attachment = mock_post.call_args.kwargs["json"]["attachments"][0]
fields = {f["title"]: f["value"] for f in attachment["fields"]}
self.assertEqual(fields["Last Ping"], "Success, an hour ago")

Expand All @@ -54,10 +54,8 @@ def test_it_rewrites_discordapp_com(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
url = args[1]

# discordapp.com is deprecated. For existing webhook URLs, wwe should
url = mock_post.call_args.args[1]
# discordapp.com is deprecated. For existing webhook URLs, we should
# rewrite discordapp.com to discord.com:
self.assertEqual(url, "https://discord.com/foo/slack")

Expand All @@ -73,7 +71,6 @@ def test_it_handles_last_ping_failure(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
attachment = kwargs["json"]["attachments"][0]
attachment = mock_post.call_args.kwargs["json"]["attachments"][0]
fields = {f["title"]: f["value"] for f in attachment["fields"]}
self.assertEqual(fields["Last Ping"], "Failure, an hour ago")
3 changes: 1 addition & 2 deletions hc/api/tests/test_notify_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ def test_it_loads_body_from_object_storage(self, get_object):
html = email.alternatives[0][0]
self.assertIn("Line 1<br>Line2", html)

args, kwargs = get_object.call_args
code, n = args
code, n = get_object.call_args.args
self.assertEqual(code, self.check.code)
self.assertEqual(n, 1)

Expand Down
12 changes: 4 additions & 8 deletions hc/api/tests/test_notify_gotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def test_it_works(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertEqual(payload["title"], "Foo is DOWN")
self.assertIn(self.check.cloaked_url(), payload["message"])

Expand All @@ -52,8 +51,7 @@ def test_it_shows_all_other_checks_up_note(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertIn("All the other checks are up.", payload["message"])

@patch("hc.api.transports.curl.request")
Expand All @@ -68,8 +66,7 @@ def test_it_lists_other_down_checks(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertIn("The following checks are also down", payload["message"])
self.assertIn("Foobar", payload["message"])
self.assertIn(other.cloaked_url(), payload["message"])
Expand All @@ -87,7 +84,6 @@ def test_it_does_not_show_more_than_10_other_checks(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertNotIn("Foobar", payload["message"])
self.assertIn("11 other checks are also down.", payload["message"])
8 changes: 3 additions & 5 deletions hc/api/tests/test_notify_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ def test_it_works(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
headers = kwargs["headers"]
params = kwargs["params"]
headers = mock_post.call_args.kwargs["headers"]
params = mock_post.call_args.kwargs["params"]
self.assertEqual(headers["Authorization"], "Bearer fake-token")
self.assertIn("""The check "Foo" is DOWN""", params["message"])

Expand All @@ -49,6 +48,5 @@ def test_it_does_not_escape_message(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
params = kwargs["params"]
params = mock_post.call_args.kwargs["params"]
self.assertEqual(params["message"], 'The check "Foo & Bar" is now UP.')
15 changes: 6 additions & 9 deletions hc/api/tests/test_notify_mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def test_it_works(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
self.assertEqual(args[1], "https://example.org")
url = mock_post.call_args.args[1]
self.assertEqual(url, "https://example.org")

attachment = kwargs["json"]["attachments"][0]
attachment = mock_post.call_args.kwargs["json"]["attachments"][0]
fields = {f["title"]: f["value"] for f in attachment["fields"]}
self.assertEqual(fields["Last Ping"], "Success, an hour ago")

Expand Down Expand Up @@ -71,8 +71,7 @@ def test_it_shows_last_ping_body(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
attachment = kwargs["json"]["attachments"][0]
attachment = mock_post.call_args.kwargs["json"]["attachments"][0]
fields = {f["title"]: f["value"] for f in attachment["fields"]}
self.assertEqual(fields["Last Ping Body"], "```\nHello World\n```")

Expand All @@ -87,8 +86,7 @@ def test_it_shows_truncated_last_ping_body(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
attachment = kwargs["json"]["attachments"][0]
attachment = mock_post.call_args.kwargs["json"]["attachments"][0]
fields = {f["title"]: f["value"] for f in attachment["fields"]}
self.assertIn("[truncated]", fields["Last Ping Body"])

Expand All @@ -103,7 +101,6 @@ def test_it_skips_last_ping_body_containing_backticks(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
attachment = kwargs["json"]["attachments"][0]
attachment = mock_post.call_args.kwargs["json"]["attachments"][0]
fields = {f["title"]: f["value"] for f in attachment["fields"]}
self.assertNotIn("Last Ping Body", fields)
29 changes: 12 additions & 17 deletions hc/api/tests/test_notify_msteams.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def test_it_works(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertEqual(payload["@type"], "MessageCard")

# summary and title should be the same, except
Expand All @@ -67,9 +66,8 @@ def test_msteams_escapes_html_and_markdown_in_desc(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
text = kwargs["json"]["sections"][0]["text"]

payload = mock_post.call_args.kwargs["json"]
text = payload["sections"][0]["text"]
self.assertIn(r"\_underscore\_", text)
self.assertIn(r"\`backticks\`", text)
self.assertIn("&lt;u&gt;underline&lt;/u&gt;", text)
Expand All @@ -93,8 +91,7 @@ def test_it_handles_last_ping_fail(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
facts = {f["name"]: f["value"] for f in payload["sections"][0]["facts"]}
self.assertEqual(facts["Last Ping:"], "Failure, an hour ago")

Expand All @@ -107,8 +104,7 @@ def test_it_handles_last_ping_log(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
facts = {f["name"]: f["value"] for f in payload["sections"][0]["facts"]}
self.assertEqual(facts["Last Ping:"], "Log, an hour ago")

Expand All @@ -123,8 +119,7 @@ def test_it_shows_ignored_nonzero_exitstatus(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
facts = {f["name"]: f["value"] for f in payload["sections"][0]["facts"]}
self.assertEqual(facts["Last Ping:"], "Ignored, an hour ago")

Expand All @@ -138,8 +133,8 @@ def test_it_shows_last_ping_body(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
section = kwargs["json"]["sections"][-1]
payload = mock_post.call_args.kwargs["json"]
section = payload["sections"][-1]
self.assertIn("```\nHello World\n```", section["text"])

@patch("hc.api.transports.curl.request")
Expand All @@ -152,8 +147,8 @@ def test_it_shows_truncated_last_ping_body(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
section = kwargs["json"]["sections"][-1]
payload = mock_post.call_args.kwargs["json"]
section = payload["sections"][-1]
self.assertIn("[truncated]", section["text"])

@patch("hc.api.transports.curl.request")
Expand All @@ -166,6 +161,6 @@ def test_it_skips_last_ping_body_containing_backticks(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
section = kwargs["json"]["sections"][-1]
payload = mock_post.call_args.kwargs["json"]
section = payload["sections"][-1]
self.assertNotIn("Hello", section["text"])
12 changes: 4 additions & 8 deletions hc/api/tests/test_notify_ntfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def test_it_works(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertEqual(payload["title"], "Foo is DOWN")
self.assertEqual(payload["actions"][0]["url"], self.check.cloaked_url())
self.assertNotIn("All the other checks are up.", payload["message"])
Expand All @@ -60,8 +59,7 @@ def test_it_shows_all_other_checks_up_note(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertIn("All the other checks are up.", payload["message"])

@patch("hc.api.transports.curl.request")
Expand All @@ -76,8 +74,7 @@ def test_it_lists_other_down_checks(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertIn("The following checks are also down", payload["message"])
self.assertIn("Foobar", payload["message"])

Expand All @@ -94,7 +91,6 @@ def test_it_does_not_show_more_than_10_other_checks(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertNotIn("Foobar", payload["message"])
self.assertIn("11 other checks are also down.", payload["message"])
13 changes: 6 additions & 7 deletions hc/api/tests/test_notify_opsgenie.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def test_opsgenie_with_legacy_value(self, mock_post):
self.assertEqual(n.error, "")

self.assertEqual(mock_post.call_count, 1)
args, kwargs = mock_post.call_args
self.assertIn("api.opsgenie.com", args[1])
payload = kwargs["json"]
url = mock_post.call_args.args[1]
self.assertIn("api.opsgenie.com", url)
payload = mock_post.call_args.kwargs["json"]
self.assertIn("DOWN", payload["message"])

@patch("hc.api.transports.curl.request")
Expand All @@ -52,8 +52,7 @@ def test_opsgenie_up(self, mock_post):
self.assertEqual(n.error, "")

self.assertEqual(mock_post.call_count, 1)
args, kwargs = mock_post.call_args
method, url = args
method, url = mock_post.call_args.args
self.assertTrue(str(self.check.code) in url)

@patch("hc.api.transports.curl.request")
Expand All @@ -66,8 +65,8 @@ def test_opsgenie_with_json_value(self, mock_post):
self.assertEqual(n.error, "")

self.assertEqual(mock_post.call_count, 1)
args, kwargs = mock_post.call_args
self.assertIn("api.eu.opsgenie.com", args[1])
url = mock_post.call_args.args[1]
self.assertIn("api.eu.opsgenie.com", url)

@patch("hc.api.transports.curl.request")
def test_opsgenie_returns_error(self, mock_post):
Expand Down
6 changes: 2 additions & 4 deletions hc/api/tests/test_notify_pagertree.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def test_it_works(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertEqual(payload["event_type"], "trigger")

@override_settings(PAGERTREE_ENABLED=False)
Expand All @@ -54,6 +53,5 @@ def test_it_does_not_escape_title(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertEqual(payload["title"], "Foo & Bar is DOWN")
9 changes: 3 additions & 6 deletions hc/api/tests/test_notify_pd.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def test_it_works(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertEqual(payload["description"], "Foo is DOWN")
self.assertEqual(payload["details"]["Description"], "Description goes here")
self.assertEqual(payload["event_type"], "trigger")
Expand All @@ -52,8 +51,7 @@ def test_pd_complex(self, mock_post):
self.channel.notify(self.check)
assert Notification.objects.count() == 1

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertEqual(payload["event_type"], "trigger")
self.assertEqual(payload["service_key"], "456")

Expand All @@ -75,6 +73,5 @@ def test_it_does_not_escape_description(self, mock_post):

self.channel.notify(self.check)

args, kwargs = mock_post.call_args
payload = kwargs["json"]
payload = mock_post.call_args.kwargs["json"]
self.assertEqual(payload["description"], "Foo & Bar is DOWN")
Loading

0 comments on commit 5113b96

Please sign in to comment.