PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/gdata_tests/contacts/service_test.py

https://gitlab.com/karambir/gdata
Python | 272 lines | 188 code | 53 blank | 31 comment | 10 complexity | 95812e339c57ae361838fb955ff68739 MD5 | raw file
  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2007 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. __author__ = 'api.jscudder (Jeff Scudder)'
  17. import getpass
  18. import re
  19. import unittest
  20. import urllib
  21. import atom
  22. import gdata.contacts.service
  23. import gdata.test_config as conf
  24. conf.options.register_option(conf.TEST_IMAGE_LOCATION_OPTION)
  25. class ContactsServiceTest(unittest.TestCase):
  26. def setUp(self):
  27. self.gd_client = gdata.contacts.service.ContactsService()
  28. conf.configure_service(self.gd_client, 'ContactsServiceTest', 'cp')
  29. self.gd_client.email = conf.options.get_value('username')
  30. def tearDown(self):
  31. conf.close_service(self.gd_client)
  32. def testGetContactsFeed(self):
  33. if not conf.options.get_value('runlive') == 'true':
  34. return
  35. conf.configure_service_cache(self.gd_client, 'testGetContactsFeed')
  36. feed = self.gd_client.GetContactsFeed()
  37. self.assert_(isinstance(feed, gdata.contacts.ContactsFeed))
  38. def testDefaultContactList(self):
  39. self.assertEquals('default', self.gd_client.contact_list)
  40. def testCustomContactList(self):
  41. if not conf.options.get_value('runlive') == 'true':
  42. return
  43. conf.configure_service_cache(self.gd_client, 'testCustomContactList')
  44. self.gd_client.contact_list = conf.options.get_value('username')
  45. feed = self.gd_client.GetContactsFeed()
  46. self.assert_(isinstance(feed, gdata.contacts.ContactsFeed))
  47. def testGetFeedUriDefault(self):
  48. self.gd_client.contact_list = 'domain.com'
  49. self.assertEquals('/m8/feeds/contacts/domain.com/full',
  50. self.gd_client.GetFeedUri())
  51. def testGetFeedUriCustom(self):
  52. uri = self.gd_client.GetFeedUri(kind='groups',
  53. contact_list='example.com',
  54. projection='base/batch',
  55. scheme='https')
  56. self.assertEquals(
  57. 'https://www.google.com/m8/feeds/groups/example.com/base/batch', uri)
  58. def testCreateUpdateDeleteContactAndUpdatePhoto(self):
  59. if not conf.options.get_value('runlive') == 'true':
  60. return
  61. conf.configure_service_cache(self.gd_client, 'testCreateUpdateDeleteContactAndUpdatePhoto')
  62. DeleteTestContact(self.gd_client)
  63. # Create a new entry
  64. new_entry = gdata.contacts.ContactEntry()
  65. new_entry.title = atom.Title(text='Elizabeth Bennet')
  66. new_entry.content = atom.Content(text='Test Notes')
  67. new_entry.email.append(gdata.contacts.Email(
  68. rel='http://schemas.google.com/g/2005#work',
  69. primary='true',
  70. address='liz@gmail.com'))
  71. new_entry.phone_number.append(gdata.contacts.PhoneNumber(
  72. rel='http://schemas.google.com/g/2005#work', text='(206)555-1212'))
  73. new_entry.organization = gdata.contacts.Organization(
  74. org_name=gdata.contacts.OrgName(text='TestCo.'),
  75. rel='http://schemas.google.com/g/2005#work')
  76. entry = self.gd_client.CreateContact(new_entry)
  77. # Generate and parse the XML for the new entry.
  78. self.assertEquals(entry.title.text, new_entry.title.text)
  79. self.assertEquals(entry.content.text, 'Test Notes')
  80. self.assertEquals(len(entry.email), 1)
  81. self.assertEquals(entry.email[0].rel, new_entry.email[0].rel)
  82. self.assertEquals(entry.email[0].address, 'liz@gmail.com')
  83. self.assertEquals(len(entry.phone_number), 1)
  84. self.assertEquals(entry.phone_number[0].rel,
  85. new_entry.phone_number[0].rel)
  86. self.assertEquals(entry.phone_number[0].text, '(206)555-1212')
  87. self.assertEquals(entry.organization.org_name.text, 'TestCo.')
  88. # Edit the entry.
  89. entry.phone_number[0].text = '(555)555-1212'
  90. updated = self.gd_client.UpdateContact(entry.GetEditLink().href, entry)
  91. self.assertEquals(updated.content.text, 'Test Notes')
  92. self.assertEquals(len(updated.phone_number), 1)
  93. self.assertEquals(updated.phone_number[0].rel,
  94. entry.phone_number[0].rel)
  95. self.assertEquals(updated.phone_number[0].text, '(555)555-1212')
  96. # Change the contact's photo.
  97. updated_photo = self.gd_client.ChangePhoto(
  98. conf.options.get_value('imgpath'), updated,
  99. content_type='image/jpeg')
  100. # Refetch the contact so that it has the new photo link
  101. updated = self.gd_client.GetContact(updated.GetSelfLink().href)
  102. self.assert_(updated.GetPhotoLink() is not None)
  103. # Fetch the photo data.
  104. hosted_image = self.gd_client.GetPhoto(updated)
  105. self.assert_(hosted_image is not None)
  106. # Delete the entry.
  107. self.gd_client.DeleteContact(updated.GetEditLink().href)
  108. def testCreateAndDeleteContactUsingBatch(self):
  109. if not conf.options.get_value('runlive') == 'true':
  110. return
  111. conf.configure_service_cache(self.gd_client, 'testCreateAndDeleteContactUsingBatch')
  112. # Get random data for creating contact
  113. random_contact_number = 'notRandom12'
  114. random_contact_title = 'Random Contact %s' % (
  115. random_contact_number)
  116. # Set contact data
  117. contact = gdata.contacts.ContactEntry()
  118. contact.title = atom.Title(text=random_contact_title)
  119. contact.email = gdata.contacts.Email(
  120. address='user%s@example.com' % random_contact_number,
  121. primary='true',
  122. rel=gdata.contacts.REL_WORK)
  123. contact.content = atom.Content(text='Contact created by '
  124. 'gdata-python-client automated test '
  125. 'suite.')
  126. # Form a batch request
  127. batch_request = gdata.contacts.ContactsFeed()
  128. batch_request.AddInsert(entry=contact)
  129. # Execute the batch request to insert the contact.
  130. default_batch_url = gdata.contacts.service.DEFAULT_BATCH_URL
  131. batch_result = self.gd_client.ExecuteBatch(batch_request,
  132. default_batch_url)
  133. self.assertEquals(len(batch_result.entry), 1)
  134. self.assertEquals(batch_result.entry[0].title.text,
  135. random_contact_title)
  136. self.assertEquals(batch_result.entry[0].batch_operation.type,
  137. gdata.BATCH_INSERT)
  138. self.assertEquals(batch_result.entry[0].batch_status.code,
  139. '201')
  140. expected_batch_url = re.compile('default').sub(
  141. urllib.quote(self.gd_client.email),
  142. gdata.contacts.service.DEFAULT_BATCH_URL)
  143. self.failUnless(batch_result.GetBatchLink().href,
  144. expected_batch_url)
  145. # Create a batch request to delete the newly created entry.
  146. batch_delete_request = gdata.contacts.ContactsFeed()
  147. batch_delete_request.AddDelete(entry=batch_result.entry[0])
  148. batch_delete_result = self.gd_client.ExecuteBatch(
  149. batch_delete_request,
  150. batch_result.GetBatchLink().href)
  151. self.assertEquals(len(batch_delete_result.entry), 1)
  152. self.assertEquals(batch_delete_result.entry[0].batch_operation.type,
  153. gdata.BATCH_DELETE)
  154. self.assertEquals(batch_result.entry[0].batch_status.code,
  155. '201')
  156. def testCleanUriNeedsCleaning(self):
  157. self.assertEquals('/relative/uri', self.gd_client._CleanUri(
  158. 'http://www.google.com/relative/uri'))
  159. def testCleanUriDoesNotNeedCleaning(self):
  160. self.assertEquals('/relative/uri', self.gd_client._CleanUri(
  161. '/relative/uri'))
  162. class ContactsQueryTest(unittest.TestCase):
  163. def testConvertToStringDefaultFeed(self):
  164. query = gdata.contacts.service.ContactsQuery()
  165. self.assertEquals(str(query), '/m8/feeds/contacts/default/full')
  166. query.max_results = 10
  167. self.assertEquals(query.ToUri(),
  168. '/m8/feeds/contacts/default/full?max-results=10')
  169. def testConvertToStringCustomFeed(self):
  170. query = gdata.contacts.service.ContactsQuery('/custom/feed/uri')
  171. self.assertEquals(str(query), '/custom/feed/uri')
  172. query.max_results = '10'
  173. self.assertEquals(query.ToUri(), '/custom/feed/uri?max-results=10')
  174. def testGroupQueryParameter(self):
  175. query = gdata.contacts.service.ContactsQuery()
  176. query.group = 'http://google.com/m8/feeds/groups/liz%40gmail.com/full/270f'
  177. self.assertEquals(query.ToUri(), '/m8/feeds/contacts/default/full'
  178. '?group=http%3A%2F%2Fgoogle.com%2Fm8%2Ffeeds%2Fgroups'
  179. '%2Fliz%2540gmail.com%2Ffull%2F270f')
  180. class ContactsGroupsTest(unittest.TestCase):
  181. def setUp(self):
  182. self.gd_client = gdata.contacts.service.ContactsService()
  183. conf.configure_service(self.gd_client, 'ContactsServiceTest', 'cp')
  184. def tearDown(self):
  185. conf.close_service(self.gd_client)
  186. def testCreateUpdateDeleteGroup(self):
  187. if not conf.options.get_value('runlive') == 'true':
  188. return
  189. conf.configure_service_cache(self.gd_client,
  190. 'testCreateUpdateDeleteGroup')
  191. test_group = gdata.contacts.GroupEntry(title=atom.Title(
  192. text='test group py'))
  193. new_group = self.gd_client.CreateGroup(test_group)
  194. self.assert_(isinstance(new_group, gdata.contacts.GroupEntry))
  195. self.assertEquals(new_group.title.text, 'test group py')
  196. # Change the group's title
  197. new_group.title.text = 'new group name py'
  198. updated_group = self.gd_client.UpdateGroup(new_group.GetEditLink().href,
  199. new_group)
  200. self.assertEquals(updated_group.title.text, new_group.title.text)
  201. # Remove the group
  202. self.gd_client.DeleteGroup(updated_group.GetEditLink().href)
  203. # Utility methods.
  204. def DeleteTestContact(client):
  205. # Get test contact
  206. feed = client.GetContactsFeed()
  207. for entry in feed.entry:
  208. if (entry.title.text == 'Elizabeth Bennet' and
  209. entry.content.text == 'Test Notes' and
  210. entry.email[0].address == 'liz@gmail.com'):
  211. client.DeleteContact(entry.GetEditLink().href)
  212. def suite():
  213. return unittest.TestSuite((unittest.makeSuite(ContactsServiceTest, 'test'),
  214. unittest.makeSuite(ContactsQueryTest, 'test'),
  215. unittest.makeSuite(ContactsGroupsTest, 'test'),))
  216. if __name__ == '__main__':
  217. print ('Contacts Tests\nNOTE: Please run these tests only with a test '
  218. 'account. The tests may delete or update your data.')
  219. unittest.TextTestRunner().run(suite())