Skip to content

Commit

Permalink
test: test main funtion
Browse files Browse the repository at this point in the history
  • Loading branch information
hairmare committed Jun 8, 2020
1 parent edf28fe commit d8c0747
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
6 changes: 5 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ def kb(mocker):
This mock is used by various tests and can be extended with more methods should they be needed.
"""
kb = mocker.Mock(kanboard.Client)
kb.add_group_member = mocker.Mock()
kb.create_comment = mocker.Mock()
kb.create_task = mocker.Mock()
kb.create_task_file = mocker.Mock()
kb.create_user = mocker.Mock()
kb.get_all_users = mocker.Mock()
kb.get_project_by_name = mocker.Mock()
kb.get_task = mocker.Mock()
kb.open_task = mocker.Mock()
kb.update_task = mocker.Mock()
Expand All @@ -25,6 +29,6 @@ def email_message(mocker):
"""
Mock an email.message.EmailMessage object
"""
mail = mocker.Mock(EmailMessage)
mail = mocker.MagicMock(EmailMessage)
mail.walk.return_value = []
return mail
6 changes: 3 additions & 3 deletions src/tasks_from_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
sys.path.append('/etc/tasks_from_email')
try:
from tasks_from_email_config import *
except ImportError:
except ImportError: # pragma: no cover
if os.path.exists('/etc/tasks_from_email'):
print('ERROR: Make sure tasks_from_email_config.py exists in "/etc/task_from_email", are readable and contain valid settings.')
else:
Expand Down Expand Up @@ -203,7 +203,7 @@ def main():
kb_user_id = create_user_for_sender(kb, email_address)

""" add user to group """
if KANBOARD_GROUP_ID > 0:
if KANBOARD_GROUP_ID > 0: # pragma: no cover - will get tested once config is refactored
kb.add_group_member(group_id=KANBOARD_GROUP_ID, user_id=kb_user_id)

""" get id from project specified """
Expand Down Expand Up @@ -235,5 +235,5 @@ def main():
imap_close(imap_connection)


if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
main()
111 changes: 111 additions & 0 deletions src/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import pytest

import email
import kanboard

import tasks_from_email


class TestMain:
def test_call_no_results(self, mocker):
mocker.patch("tasks_from_email.imap_connect")
mocker.patch("tasks_from_email.imap_search_unseen")
mocker.patch("tasks_from_email.imap_close")

tasks_from_email.imap_search_unseen.return_value = ("typ", [""])

tasks_from_email.main()

tasks_from_email.imap_connect.assert_called_once()
tasks_from_email.imap_search_unseen.assert_called_once()
tasks_from_email.imap_close.assert_called_once()

@pytest.mark.parametrize("create", [(True), (False)])
def test_call_with_mocks(self, mocker, kb, email_message, create):
mocker.patch("tasks_from_email.imap_connect")
mocker.patch("tasks_from_email.imap_search_unseen")
mocker.patch("tasks_from_email.imap_close")
mocker.patch("tasks_from_email.convert_to_kb_date")
mocker.patch("tasks_from_email.create_user_for_sender")
mocker.patch("tasks_from_email.get_task_if_subject_matches")
mocker.patch("tasks_from_email.reopen_and_update")
mocker.patch("email.message_from_bytes")
mocker.patch("email.header.make_header")
mocker.patch("kanboard.Client")

imap_connection = mocker.Mock()
imap_connection.fetch.return_value = ("typ", [(None, b"raw")])
tasks_from_email.imap_connect.return_value = imap_connection
tasks_from_email.imap_search_unseen.return_value = ("typ", ["a"])
email.message_from_bytes.return_value = email_message
mock_data = {
"Date": "rfcdate",
"From": "[email protected]",
"To": "[email protected]",
"Subject": "subject",
}

def getitem(name):
return mock_data[name]

email_message.__getitem__.side_effect = getitem
tasks_from_email.convert_to_kb_date.return_value = "converted-date"
tasks_from_email.create_user_for_sender.return_value = 2
kanboard.Client.return_value = kb
kb.get_project_by_name.return_value = {"id": 1}
if create:
tasks_from_email.get_task_if_subject_matches.return_value = (None, None)
else:
tasks_from_email.get_task_if_subject_matches.return_value = (
1,
{"is_active": True},
)
email.header.make_header.return_value = "ExampleHeader"
kb.create_task.return_value = 1

tasks_from_email.main()

tasks_from_email.imap_connect.assert_called_once()
tasks_from_email.imap_search_unseen.assert_called_once()
imap_connection.fetch.assert_called_once_with("a", "(RFC822)")
email.message_from_bytes.assert_called_once_with(b"raw")
tasks_from_email.convert_to_kb_date.assert_has_calls(
[mocker.call("rfcdate"), mocker.call("rfcdate", 48)]
)
assert email_message.__getitem__.call_args_list == [
mocker.call("Date"),
mocker.call("Date"),
mocker.call("From"),
mocker.call("To"),
mocker.call("Subject"),
]
kanboard.Client.assert_called_once()
tasks_from_email.create_user_for_sender.called_once_with(kb, "[email protected]")
kb.add_group_member.assert_not_called()
kb.get_project_by_name.assert_called_once_with(name="Support")
tasks_from_email.get_task_if_subject_matches.assert_called_once_with(
kb, "ExampleHeader"
)
if create:
kb.create_task.assert_called_once_with(
project_id="1",
title="ExampleHeader",
creator_id=2,
date_started="converted-date",
date_due="converted-date",
description="From: [email protected]\n\nTo: [email protected]\n\nDate: converted-date\n\nSubject: ExampleHeader\n\nNone",
)
tasks_from_email.reopen_and_update.assert_not_called()
else:
kb.create_task.assert_not_called()
tasks_from_email.reopen_and_update.called_once_with(
kb, {"is_active": True}, 1, 2, "None", "converted-date"
)
kb.create_task_file.assert_called_once_with(
project_id="1",
task_id="1",
filename="ExampleHeader.mbox",
blob="cmF3", # None
)

tasks_from_email.imap_close.assert_called_once()

0 comments on commit d8c0747

Please sign in to comment.