| 1 |
import os.path |
| 2 |
import pickle |
| 3 |
from copy import copy |
| 4 |
from datetime import datetime, timedelta |
| •5 |
from unittest import mock, skip |
| 6 |
from unittest.mock import patch |
| 7 |
|
| 8 |
from dateutil.tz import tzutc |
| 9 |
from google.oauth2.credentials import Credentials |
| 10 |
from six.moves import configparser |
| 11 |
|
| 12 |
import bugwarrior.services.gmail as gmail |
| 13 |
from bugwarrior.config import ServiceConfig |
| 14 |
from bugwarrior.services.gmail import GmailService |
| 15 |
|
| 16 |
from .base import AbstractServiceTest, ConfigTest, ServiceTest |
| 17 |
|
| 18 |
TEST_CREDENTIAL = { |
| 19 |
"token": "itsatokeneveryone", |
| 20 |
"refresh_token": "itsarefreshtokeneveryone", |
| 21 |
"token_uri": "https://oauth2.googleapis.com/token", |
| 22 |
"client_id": "example.apps.googleusercontent.com", |
| 23 |
"client_secret": "itsasecrettoeveryone", |
| 24 |
"scopes": ["https://www.googleapis.com/auth/gmail.readonly"], |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
class TestGmailService(ConfigTest): |
| 29 |
|
| 30 |
def setUp(self): |
| 31 |
super(TestGmailService, self).setUp() |
| 32 |
self.config = configparser.RawConfigParser() |
| 33 |
self.config.add_section("general") |
| 34 |
self.config.add_section("myservice") |
| 35 |
|
| 36 |
mock_data = mock.Mock() |
| 37 |
mock_data.path = self.tempdir |
| 38 |
self.config.data = mock_data |
| 39 |
|
| 40 |
self.service_config = ServiceConfig(GmailService.CONFIG_PREFIX, self.config, "myservice") |
| 41 |
|
| 42 |
def test_get_credentials_exists_and_valid(self): |
| 43 |
mock_api = mock.Mock() |
| 44 |
gmail.GmailService.build_api = mock_api |
| 45 |
service = GmailService(self.config, "general", "myservice") |
| 46 |
|
| 47 |
expected = Credentials(**copy(TEST_CREDENTIAL)) |
| 48 |
self.assertEqual(expected.valid, True) |
| 49 |
with open(service.credentials_path, "wb") as token: |
| 50 |
pickle.dump(expected, token) |
| 51 |
|
| 52 |
self.assertEqual(service.get_credentials().to_json(), expected.to_json()) |
| 53 |
|
| •54 |
@skip('Fails for reproducible builds in expired_credential.valid') |
| 55 |
def test_get_credentials_with_refresh(self): |
| 56 |
mock_api = mock.Mock() |
| 57 |
gmail.GmailService.build_api = mock_api |
| 58 |
service = GmailService(self.config, "general", "myservice") |
| 59 |
|
| 60 |
expired_credential = Credentials(**copy(TEST_CREDENTIAL)) |
| 61 |
expired_credential.expiry = datetime.now() |
| 62 |
self.assertEqual(expired_credential.valid, False) |
| 63 |
with open(service.credentials_path, "wb") as token: |
| 64 |
pickle.dump(expired_credential, token) |
| 65 |
|
| 66 |
with patch("google.oauth2._client.refresh_grant") as mock_refresh_grant: |
| 67 |
access_token = "newaccesstoken" |
| 68 |
refresh_token = "newrefreshtoken" |
| 69 |
expiry = datetime.now() + timedelta(hours=24) |
| 70 |
grant_response = {"id_token": "idtoken"} |
| 71 |
mock_refresh_grant.return_value = access_token, refresh_token, expiry, grant_response |
| 72 |
refreshed_credential = service.get_credentials() |
| 73 |
self.assertEqual(refreshed_credential.valid, True) |
| 74 |
|
| 75 |
|
| 76 |
TEST_THREAD = { |
| 77 |
"messages": [ |
| 78 |
{ |
| 79 |
"payload": { |
| 80 |
"headers": [ |
| 81 |
{"name": "From", "value": "Foo Bar <[email protected]>"}, |
| 82 |
{"name": "Subject", "value": "Regarding Bugwarrior"}, |
| 83 |
{"name": "To", "value": "[email protected]"}, |
| 84 |
{ |
| 85 |
"name": "Message-ID", |
| 86 |
"value": "<CMCRSF+6r=x5JtW4wlRYR5qdfRq+iAtSoec5NqrHvRpvVgHbHdg@mail.gmail.com>", |
| 87 |
}, |
| 88 |
], |
| 89 |
"parts": [{}], |
| 90 |
}, |
| 91 |
"snippet": "Bugwarrior is great", |
| 92 |
"internalDate": 1546722467000, |
| 93 |
"threadId": "1234", |
| 94 |
"labelIds": ["IMPORTANT", "Label_1", "Label_43", "CATEGORY_PERSONAL"], |
| 95 |
"id": "9999", |
| 96 |
} |
| 97 |
], |
| 98 |
"id": "1234", |
| 99 |
} |
| 100 |
|
| 101 |
TEST_LABELS = [ |
| 102 |
{"id": "IMPORTANT", "name": "IMPORTANT"}, |
| 103 |
{"id": "CATEGORY_PERSONAL", "name": "CATEGORY_PERSONAL"}, |
| 104 |
{"id": "Label_1", "name": "sticky"}, |
| 105 |
{"id": "Label_43", "name": "postit"}, |
| 106 |
] |
| 107 |
|
| 108 |
|
| 109 |
class TestGmailIssue(AbstractServiceTest, ServiceTest): |
| 110 |
SERVICE_CONFIG = { |
| 111 |
'gmail.add_tags': 'added', |
| 112 |
'gmail.login_name': '[email protected]', |
| 113 |
} |
| 114 |
|
| 115 |
def setUp(self): |
| 116 |
super(TestGmailIssue, self).setUp() |
| 117 |
|
| 118 |
mock_api = mock.Mock() |
| 119 |
mock_api().users().labels().list().execute.return_value = {'labels': TEST_LABELS} |
| 120 |
mock_api().users().threads().list().execute.return_value = {'threads': [{'id': TEST_THREAD['id']}]} |
| 121 |
mock_api().users().threads().get().execute.return_value = TEST_THREAD |
| 122 |
gmail.GmailService.build_api = mock_api |
| 123 |
self.service = self.get_mock_service(gmail.GmailService, section='test_section') |
| 124 |
|
| 125 |
def test_config_paths(self): |
| 126 |
credentials_path = os.path.join( |
| 127 |
self.service.config.data.path, |
| 128 |
'gmail_credentials_test_example_com.pickle') |
| 129 |
self.assertEqual(self.service.credentials_path, credentials_path) |
| 130 |
|
| 131 |
def test_to_taskwarrior(self): |
| 132 |
thread = TEST_THREAD |
| 133 |
issue = self.service.get_issue_for_record( |
| 134 |
thread, |
| 135 |
gmail.thread_extras(thread, self.service.get_labels())) |
| 136 |
expected = { |
| 137 |
'annotations': [], |
| 138 |
'entry': datetime(2019, 1, 5, 21, 7, 47, tzinfo=tzutc()), |
| 139 |
'gmailthreadid': '1234', |
| 140 |
'gmaillastmessageid': 'CMCRSF+6r=x5JtW4wlRYR5qdfRq+iAtSoec5NqrHvRpvVgHbHdg@mail.gmail.com', |
| 141 |
'gmailsnippet': 'Bugwarrior is great', |
| 142 |
'gmaillastsender': 'Foo Bar', |
| 143 |
'tags': {'postit', 'sticky'}, |
| 144 |
'gmailsubject': 'Regarding Bugwarrior', |
| 145 |
'gmailurl': 'https://mail.google.com/mail/u/0/#all/1234', |
| 146 |
'gmaillabels': 'CATEGORY_PERSONAL IMPORTANT postit sticky', |
| 147 |
'priority': u'M', |
| 148 |
'gmaillastsenderaddr': '[email protected]'} |
| 149 |
|
| 150 |
taskwarrior = issue.to_taskwarrior() |
| 151 |
taskwarrior['tags'] = set(taskwarrior['tags']) |
| 152 |
|
| 153 |
self.assertEqual(taskwarrior, expected) |
| 154 |
|
| 155 |
def test_issues(self): |
| 156 |
issue = next(self.service.issues()) |
| 157 |
expected = { |
| 158 |
'annotations': ['@Foo Bar - Regarding Bugwarrior'], |
| 159 |
'entry': datetime(2019, 1, 5, 21, 7, 47, tzinfo=tzutc()), |
| 160 |
'gmailthreadid': '1234', |
| 161 |
'gmaillastmessageid': 'CMCRSF+6r=x5JtW4wlRYR5qdfRq+iAtSoec5NqrHvRpvVgHbHdg@mail.gmail.com', |
| 162 |
'gmailsnippet': 'Bugwarrior is great', |
| 163 |
'gmaillastsender': 'Foo Bar', |
| 164 |
'description': u'(bw)Is#1234 - Regarding Bugwarrior .. https://mail.google.com/mail/u/0/#all/1234', |
| 165 |
'priority': u'M', |
| 166 |
'tags': {'added', 'postit', 'sticky'}, |
| 167 |
'gmailsubject': 'Regarding Bugwarrior', |
| 168 |
'gmailurl': 'https://mail.google.com/mail/u/0/#all/1234', |
| 169 |
'gmaillabels': 'CATEGORY_PERSONAL IMPORTANT postit sticky', |
| 170 |
'gmaillastsenderaddr': '[email protected]'} |
| 171 |
|
| 172 |
taskwarrior = issue.get_taskwarrior_record() |
| 173 |
taskwarrior['tags'] = set(taskwarrior['tags']) |
| 174 |
|
| 175 |
self.assertEqual(taskwarrior, expected) |
| 176 |
|
| 177 |
def test_last_sender(self): |
| 178 |
test_thread = { |
| 179 |
'messages': [ |
| 180 |
{ |
| 181 |
'payload': |
| 182 |
{ |
| 183 |
'headers': [ |
| 184 |
{'name': 'From', 'value': 'Xyz <[email protected]'} |
| 185 |
] |
| 186 |
} |
| 187 |
}, |
| 188 |
{ |
| 189 |
'payload': |
| 190 |
{ |
| 191 |
'headers': [ |
| 192 |
{'name': 'From', 'value': 'Foo Bar <[email protected]'} |
| 193 |
] |
| 194 |
} |
| 195 |
}, |
| 196 |
] |
| 197 |
} |
| 198 |
self.assertEqual(gmail.thread_last_sender(test_thread), ('Foo Bar', '[email protected]')) |