-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(purrr): enable unit tests without AK (#149)
Improve mocking, and expect dummy GitHub and Slack clients instead of `ConnectionInitError` when the AutoKitteh Python SDK is running within unit tests. ~This PR depends on autokitteh/autokitteh#964 being merged, and a new AutoKitteh Python SDK version released in PyPI - which is why unit tests are currently failing.~ Also found and fixed a small bug in `slack_cmd.py` Refs: INT-159
- Loading branch information
Showing
7 changed files
with
106 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,19 @@ | |
|
||
import collections | ||
import unittest | ||
from unittest.mock import MagicMock | ||
from unittest import mock | ||
|
||
import markdown | ||
import users | ||
from autokitteh import github, slack | ||
|
||
|
||
# This is needed before calling "import markdown" to avoid "ConnectionInitError" | ||
# when initializing these clients in Purrr modules, due to the lack of AutoKitteh | ||
# environment variables. This is also the reason for the "noqa" comments below. | ||
github.github_client = mock.MagicMock() | ||
slack.slack_client = mock.MagicMock() | ||
|
||
import markdown # noqa: E402 | ||
import users # noqa: E402 | ||
|
||
|
||
class MarkdownGithubToSlackTest(unittest.TestCase): | ||
|
@@ -93,9 +102,9 @@ def test_nested_lists(self): | |
" • 111\n ◦ 222\n ◦ 333\n • 444", | ||
) | ||
|
||
def test_user_mentions(self): | ||
users.github_username_to_slack_user_id = MagicMock() | ||
users.github_username_to_slack_user_id.side_effect = ["U123", None, None] | ||
@mock.patch.object(users, "github_username_to_slack_user_id", autospec=True) | ||
def test_user_mentions(self, mock_func): | ||
mock_func.side_effect = ["U123", None, None] | ||
|
||
# Slack user found. | ||
self.assertEqual( | ||
|
@@ -182,11 +191,11 @@ def test_links(self): | |
self.assertEqual(markdown.slack_to_github("<url|>"), "[](url)") | ||
self.assertEqual(markdown.slack_to_github("<url>"), "<url>") | ||
|
||
def test_channel(self): | ||
markdown._slack_channel_name = MagicMock() | ||
markdown._slack_channel_name.return_value = "channel" | ||
markdown._slack_team_id = MagicMock() | ||
markdown._slack_team_id.return_value = "TEAM_ID" | ||
@mock.patch.object(markdown, "_slack_team_id", autospec=True) | ||
@mock.patch.object(markdown, "_slack_channel_name", autospec=True) | ||
def test_channel(self, mock_channel_name, mock_team_id): | ||
mock_channel_name.return_value = "channel" | ||
mock_team_id.return_value = "TEAM_ID" | ||
|
||
self.assertEqual( | ||
markdown.slack_to_github("<#C123>"), | ||
|
@@ -205,72 +214,57 @@ def test_channel(self): | |
FakeGithubUser = collections.namedtuple("FakeGithubUser", ["name", "login"]) | ||
|
||
|
||
@mock.patch.object(users, "_slack_user_info", autospec=True) | ||
@mock.patch.object(users, "_github_users", autospec=True) | ||
@mock.patch.object(users, "_email_to_github_user_id", autospec=True) | ||
class MarkdownSlackToGithubUserMentionsTest(unittest.TestCase): | ||
"""Unit tests for user mentions in the "slack_to_github" function.""" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
|
||
self._slack_user_info = users._slack_user_info | ||
users._slack_user_info = MagicMock() | ||
|
||
self.__email_to_github_user_id = users._email_to_github_user_id | ||
users._email_to_github_user_id = MagicMock() | ||
|
||
self._github_users = users._github_users | ||
users._github_users = MagicMock() | ||
|
||
def tearDown(self): | ||
users._github_users = self._github_users | ||
users._email_to_github_user_id = self.__email_to_github_user_id | ||
users._slack_user_info = self._slack_user_info | ||
super().tearDown() | ||
|
||
def test_slack_user_info_error(self): | ||
users._slack_user_info.return_value = {} | ||
def test_slack_user_info_error(self, mock_email, mock_github, mock_slack): | ||
mock_slack.return_value = {} | ||
self.assertEqual(markdown.slack_to_github("<@U123>"), "Someone") | ||
|
||
def test_email_and_name_not_found_in_slack_profile(self): | ||
users._slack_user_info.return_value = {"profile": {"foo": "bar"}} | ||
def test_email_and_name_not_found_in_slack_profile( | ||
self, mock_email, mock_github, mock_slack | ||
): | ||
mock_slack.return_value = {"profile": {"foo": "bar"}} | ||
self.assertEqual(markdown.slack_to_github("<@U123>"), "Someone") | ||
|
||
def test_named_and_unnamed_slack_apps(self): | ||
users._slack_user_info.side_effect = [ | ||
def test_named_and_unnamed_slack_apps(self, mock_email, mock_github, mock_slack): | ||
mock_slack.side_effect = [ | ||
{"is_bot": True, "profile": {"real_name": "Mr. Robot"}}, | ||
{"is_bot": True}, | ||
] | ||
self.assertEqual(markdown.slack_to_github("<@U123>"), "Mr. Robot") | ||
self.assertEqual(markdown.slack_to_github("<@U123>"), "Some Slack app") | ||
|
||
def test_match_by_email(self): | ||
users._slack_user_info.return_value = {"profile": {"email": "[email protected]"}} | ||
users._email_to_github_user_id.return_value = "username" | ||
def test_match_by_email(self, mock_email, mock_github, mock_slack): | ||
mock_slack.return_value = {"profile": {"email": "[email protected]"}} | ||
mock_email.return_value = "username" | ||
self.assertEqual(markdown.slack_to_github("<@U123>"), "@username") | ||
|
||
def test_match_by_name(self): | ||
users._slack_user_info.return_value = { | ||
def test_match_by_name(self, mock_email, mock_github, mock_slack): | ||
mock_slack.return_value = { | ||
"profile": {"email": "[email protected]", "real_name": "John Doe"} | ||
} | ||
users._email_to_github_user_id.return_value = "" | ||
users._github_users.return_value = [ | ||
FakeGithubUser("John Doe", "username"), | ||
] | ||
mock_email.return_value = "" | ||
mock_github.return_value = [FakeGithubUser("John Doe", "username")] | ||
self.assertEqual(markdown.slack_to_github("<@U123>"), "@username") | ||
|
||
def test_no_matches_by_name(self): | ||
users._slack_user_info.return_value = { | ||
def test_no_matches_by_name(self, mock_email, mock_github, mock_slack): | ||
mock_slack.return_value = { | ||
"profile": {"email": "[email protected]", "real_name": "John Doe"} | ||
} | ||
users._email_to_github_user_id.return_value = "" | ||
users._github_users.return_value = [] | ||
mock_email.return_value = "" | ||
mock_github.return_value = [] | ||
self.assertEqual(markdown.slack_to_github("<@U123>"), "John Doe") | ||
|
||
def test_too_many_matches_by_name(self): | ||
users._slack_user_info.return_value = { | ||
def test_too_many_matches_by_name(self, mock_email, mock_github, mock_slack): | ||
mock_slack.return_value = { | ||
"profile": {"email": "[email protected]", "real_name": "John Doe"} | ||
} | ||
users._email_to_github_user_id.return_value = "" | ||
users._github_users.return_value = [ | ||
mock_email.return_value = "" | ||
mock_github.return_value = [ | ||
FakeGithubUser("John Doe", "username1"), | ||
FakeGithubUser("john doe", "username2"), | ||
FakeGithubUser("JOHN DOE", "username3"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
pytest | ||
ruff | ||
|
||
autokitteh | ||
google-auth | ||
PyGithub | ||
slack-sdk |