1import mock 2import os.path 3from .base import ServiceTest, AbstractServiceTest 4import bugwarrior.services.gmail as gmail 5 6TEST_THREAD = { 7 "messages": [ 8 { 9 "payload": { 10 "headers": [ 11 { 12 "name": "From", 13 "value": "Foo Bar <foobar@example.com>" 14 }, 15 { 16 "name": "Subject", 17 "value": "Regarding Bugwarrior" 18 }, 19 { 20 "name": "To", 21 "value": "ct@example.com" 22 } 23 ], 24 "parts": [ 25 { 26 } 27 ] 28 }, 29 "snippet": "Bugwarrior is great", 30 "threadId": "1234", 31 "labelIds": [ 32 "IMPORTANT", 33 "Label_1", 34 "Label_43", 35 "CATEGORY_PERSONAL" 36 ], 37 "id": "9999" 38 } 39 ], 40 "id": "1234" 41 } 42 43TEST_LABELS = [ 44 {'id': 'IMPORTANT', 'name': 'IMPORTANT'}, 45 {'id': 'CATEGORY_PERSONAL', 'name': 'CATEGORY_PERSONAL'}, 46 {'id': 'Label_1', 'name': 'sticky'}, 47 {'id': 'Label_43', 'name': 'postit'}, 48] 49 50class TestGmailIssue(AbstractServiceTest, ServiceTest): 51 SERVICE_CONFIG = { 52 'gmail.add_tags': 'added', 53 'gmail.login_name': 'test@example.com', 54 } 55 56 def setUp(self): 57 super(TestGmailIssue, self).setUp() 58 59 mock_api = mock.Mock() 60 mock_api().users().labels().list().execute.return_value = {'labels': TEST_LABELS} 61 mock_api().users().threads().list().execute.return_value = {'threads': [{'id': TEST_THREAD['id']}]} 62 mock_api().users().threads().get().execute.return_value = TEST_THREAD 63 gmail.GmailService.build_api = mock_api 64 self.service = self.get_mock_service(gmail.GmailService, section='test_section') 65 66 def test_config_paths(self): 67 credentials_path = os.path.join( 68 self.service.config.data.path, 69 'gmail_credentials_test_example_com.json') 70 self.assertEqual(self.service.credentials_path, credentials_path) 71 72 def test_to_taskwarrior(self): 73 thread = TEST_THREAD 74 issue = self.service.get_issue_for_record( 75 thread, 76 gmail.thread_extras(thread, self.service.get_labels())) 77 expected = { 78 'gmailthreadid': '1234', 79 'gmailsnippet': 'Bugwarrior is great', 80 'gmaillastsender': 'Foo Bar', 81 'tags': set(['sticky', 'postit']), 82 'gmailsubject': 'Regarding Bugwarrior', 83 'gmailurl': 'https://mail.google.com/mail/u/0/#all/1234', 84 'gmaillabels': 'CATEGORY_PERSONAL IMPORTANT postit sticky', 85 'priority': 'M', 86 'gmaillastsenderaddr': 'foobar@example.com'} 87 88 taskwarrior = issue.to_taskwarrior() 89 taskwarrior['tags'] = set(taskwarrior['tags']) 90 91 self.assertEqual(taskwarrior, expected) 92 93 def test_issues(self): 94 issue = next(self.service.issues()) 95 expected = { 96 'gmailthreadid': '1234', 97 'gmailsnippet': 'Bugwarrior is great', 98 'gmaillastsender': 'Foo Bar', 99 'description': u'(bw)Is#1234 - Regarding Bugwarrior .. https://mail.google.com/mail/u/0/#all/1234', 100 'priority': 'M', 101 'tags': set(['sticky', 'postit', 'added']), 102 'gmailsubject': 'Regarding Bugwarrior', 103 'gmailurl': 'https://mail.google.com/mail/u/0/#all/1234', 104 'gmaillabels': 'CATEGORY_PERSONAL IMPORTANT postit sticky', 105 'gmaillastsenderaddr': 'foobar@example.com'} 106 107 taskwarrior = issue.get_taskwarrior_record() 108 taskwarrior['tags'] = set(taskwarrior['tags']) 109 110 self.assertEqual(taskwarrior, expected) 111 112 def test_last_sender(self): 113 test_thread = { 114 'messages': [ 115 { 116 'payload': 117 { 118 'headers': [ 119 {'name': 'From', 'value': 'Xyz <xyz@example.com'} 120 ] 121 } 122 }, 123 { 124 'payload': 125 { 126 'headers': [ 127 {'name': 'From', 'value': 'Foo Bar <foobar@example.com'} 128 ] 129 } 130 }, 131 ] 132 } 133 self.assertEqual(gmail.thread_last_sender(test_thread), ('Foo Bar', 'foobar@example.com')) 134