File: service_test.py

package info (click to toggle)
python-gdata 2.0.8-1.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 17,816 kB
  • ctags: 29,744
  • sloc: python: 50,599; ansic: 150; makefile: 5
file content (272 lines) | stat: -rwxr-xr-x 10,567 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/python
#
# Copyright (C) 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = 'api.jscudder (Jeff Scudder)'

import getpass
import re
import unittest
import urllib
import atom
import gdata.contacts.service
import gdata.test_config as conf


conf.options.register_option(conf.TEST_IMAGE_LOCATION_OPTION)


class ContactsServiceTest(unittest.TestCase):

  def setUp(self):
    self.gd_client = gdata.contacts.service.ContactsService()
    conf.configure_service(self.gd_client, 'ContactsServiceTest', 'cp')
    self.gd_client.email = conf.options.get_value('username')

  def tearDown(self):
    conf.close_service(self.gd_client)

  def testGetContactsFeed(self):
    if not conf.options.get_value('runlive') == 'true':
      return
    conf.configure_service_cache(self.gd_client, 'testGetContactsFeed')
    feed = self.gd_client.GetContactsFeed()
    self.assert_(isinstance(feed, gdata.contacts.ContactsFeed))

  def testDefaultContactList(self):
    self.assertEquals('default', self.gd_client.contact_list)

  def testCustomContactList(self):
    if not conf.options.get_value('runlive') == 'true':
      return
    conf.configure_service_cache(self.gd_client, 'testCustomContactList')

    self.gd_client.contact_list = conf.options.get_value('username')
    feed = self.gd_client.GetContactsFeed()
    self.assert_(isinstance(feed, gdata.contacts.ContactsFeed))

  def testGetFeedUriDefault(self):
    self.gd_client.contact_list = 'domain.com'
    self.assertEquals('/m8/feeds/contacts/domain.com/full',
                      self.gd_client.GetFeedUri())

  def testGetFeedUriCustom(self):
    uri = self.gd_client.GetFeedUri(kind='groups',
                                    contact_list='example.com',
                                    projection='base/batch',
                                    scheme='https')
    self.assertEquals(
        'https://www.google.com/m8/feeds/groups/example.com/base/batch', uri)

  def testCreateUpdateDeleteContactAndUpdatePhoto(self):
    if not conf.options.get_value('runlive') == 'true':
      return
    conf.configure_service_cache(self.gd_client, 'testCreateUpdateDeleteContactAndUpdatePhoto')

    DeleteTestContact(self.gd_client)

    # Create a new entry
    new_entry = gdata.contacts.ContactEntry()
    new_entry.title = atom.Title(text='Elizabeth Bennet')
    new_entry.content = atom.Content(text='Test Notes')
    new_entry.email.append(gdata.contacts.Email(
        rel='http://schemas.google.com/g/2005#work',
        primary='true',
        address='liz@gmail.com'))
    new_entry.phone_number.append(gdata.contacts.PhoneNumber(
        rel='http://schemas.google.com/g/2005#work', text='(206)555-1212'))
    new_entry.organization = gdata.contacts.Organization(
        org_name=gdata.contacts.OrgName(text='TestCo.'), 
        rel='http://schemas.google.com/g/2005#work')

    entry = self.gd_client.CreateContact(new_entry)

    # Generate and parse the XML for the new entry.
    self.assertEquals(entry.title.text, new_entry.title.text)
    self.assertEquals(entry.content.text, 'Test Notes')
    self.assertEquals(len(entry.email), 1)
    self.assertEquals(entry.email[0].rel, new_entry.email[0].rel)
    self.assertEquals(entry.email[0].address, 'liz@gmail.com')
    self.assertEquals(len(entry.phone_number), 1)
    self.assertEquals(entry.phone_number[0].rel,
        new_entry.phone_number[0].rel)
    self.assertEquals(entry.phone_number[0].text, '(206)555-1212')
    self.assertEquals(entry.organization.org_name.text, 'TestCo.')

    # Edit the entry.
    entry.phone_number[0].text = '(555)555-1212'
    updated = self.gd_client.UpdateContact(entry.GetEditLink().href, entry)
    self.assertEquals(updated.content.text, 'Test Notes')
    self.assertEquals(len(updated.phone_number), 1)
    self.assertEquals(updated.phone_number[0].rel,
        entry.phone_number[0].rel)
    self.assertEquals(updated.phone_number[0].text, '(555)555-1212')

    # Change the contact's photo.
    updated_photo = self.gd_client.ChangePhoto(
        conf.options.get_value('imgpath'), updated,
        content_type='image/jpeg')

    # Refetch the contact so that it has the new photo link
    updated = self.gd_client.GetContact(updated.GetSelfLink().href)
    self.assert_(updated.GetPhotoLink() is not None)

    # Fetch the photo data.
    hosted_image = self.gd_client.GetPhoto(updated)
    self.assert_(hosted_image is not None)

    # Delete the entry.
    self.gd_client.DeleteContact(updated.GetEditLink().href)

  def testCreateAndDeleteContactUsingBatch(self):
    if not conf.options.get_value('runlive') == 'true':
      return
    conf.configure_service_cache(self.gd_client, 'testCreateAndDeleteContactUsingBatch')

    # Get random data for creating contact
    random_contact_number = 'notRandom12'
    random_contact_title = 'Random Contact %s' % (
        random_contact_number)
    
    # Set contact data
    contact = gdata.contacts.ContactEntry()
    contact.title = atom.Title(text=random_contact_title)
    contact.email = gdata.contacts.Email(
        address='user%s@example.com' % random_contact_number,
        primary='true',
        rel=gdata.contacts.REL_WORK)
    contact.content = atom.Content(text='Contact created by '
                                   'gdata-python-client automated test '
                                   'suite.')
    
    # Form a batch request
    batch_request = gdata.contacts.ContactsFeed()
    batch_request.AddInsert(entry=contact)
    
    # Execute the batch request to insert the contact.
    default_batch_url = gdata.contacts.service.DEFAULT_BATCH_URL
    batch_result = self.gd_client.ExecuteBatch(batch_request,
                                               default_batch_url)
    
    self.assertEquals(len(batch_result.entry), 1)
    self.assertEquals(batch_result.entry[0].title.text,
                      random_contact_title)
    self.assertEquals(batch_result.entry[0].batch_operation.type,
                      gdata.BATCH_INSERT)
    self.assertEquals(batch_result.entry[0].batch_status.code,
                      '201')
    expected_batch_url = re.compile('default').sub(
        urllib.quote(self.gd_client.email),
        gdata.contacts.service.DEFAULT_BATCH_URL)
    self.failUnless(batch_result.GetBatchLink().href,
                    expected_batch_url)
    
    # Create a batch request to delete the newly created entry.
    batch_delete_request = gdata.contacts.ContactsFeed()
    batch_delete_request.AddDelete(entry=batch_result.entry[0])
    
    batch_delete_result = self.gd_client.ExecuteBatch(
        batch_delete_request,
        batch_result.GetBatchLink().href)
    self.assertEquals(len(batch_delete_result.entry), 1)
    self.assertEquals(batch_delete_result.entry[0].batch_operation.type,
                      gdata.BATCH_DELETE)
    self.assertEquals(batch_result.entry[0].batch_status.code,
                      '201')

  def testCleanUriNeedsCleaning(self):
    self.assertEquals('/relative/uri', self.gd_client._CleanUri(
        'http://www.google.com/relative/uri'))

  def testCleanUriDoesNotNeedCleaning(self):
    self.assertEquals('/relative/uri', self.gd_client._CleanUri(
        '/relative/uri'))


class ContactsQueryTest(unittest.TestCase):

  def testConvertToStringDefaultFeed(self):
    query = gdata.contacts.service.ContactsQuery()
    self.assertEquals(str(query), '/m8/feeds/contacts/default/full')
    query.max_results = 10
    self.assertEquals(query.ToUri(),
        '/m8/feeds/contacts/default/full?max-results=10')

  def testConvertToStringCustomFeed(self):
    query = gdata.contacts.service.ContactsQuery('/custom/feed/uri')
    self.assertEquals(str(query), '/custom/feed/uri')
    query.max_results = '10'
    self.assertEquals(query.ToUri(), '/custom/feed/uri?max-results=10')

  def testGroupQueryParameter(self):
    query = gdata.contacts.service.ContactsQuery()
    query.group = 'http://google.com/m8/feeds/groups/liz%40gmail.com/full/270f'
    self.assertEquals(query.ToUri(), '/m8/feeds/contacts/default/full'
        '?group=http%3A%2F%2Fgoogle.com%2Fm8%2Ffeeds%2Fgroups'
        '%2Fliz%2540gmail.com%2Ffull%2F270f')


class ContactsGroupsTest(unittest.TestCase):

  def setUp(self):
    self.gd_client = gdata.contacts.service.ContactsService()
    conf.configure_service(self.gd_client, 'ContactsServiceTest', 'cp')

  def tearDown(self):
    conf.close_service(self.gd_client)

  def testCreateUpdateDeleteGroup(self):
    if not conf.options.get_value('runlive') == 'true':
      return
    conf.configure_service_cache(self.gd_client, 
                                 'testCreateUpdateDeleteGroup')

    test_group = gdata.contacts.GroupEntry(title=atom.Title(
        text='test group py'))
    new_group = self.gd_client.CreateGroup(test_group)
    self.assert_(isinstance(new_group, gdata.contacts.GroupEntry))
    self.assertEquals(new_group.title.text, 'test group py')

    # Change the group's title
    new_group.title.text = 'new group name py'
    updated_group = self.gd_client.UpdateGroup(new_group.GetEditLink().href, 
        new_group)
    self.assertEquals(updated_group.title.text, new_group.title.text)

    # Remove the group
    self.gd_client.DeleteGroup(updated_group.GetEditLink().href)


# Utility methods.
def DeleteTestContact(client):
  # Get test contact
  feed = client.GetContactsFeed()
  for entry in feed.entry:
    if (entry.title.text == 'Elizabeth Bennet' and 
          entry.content.text == 'Test Notes' and 
          entry.email[0].address == 'liz@gmail.com'):
      client.DeleteContact(entry.GetEditLink().href)


def suite():
  return unittest.TestSuite((unittest.makeSuite(ContactsServiceTest, 'test'),
                             unittest.makeSuite(ContactsQueryTest, 'test'),
                             unittest.makeSuite(ContactsGroupsTest, 'test'),))


if __name__ == '__main__':
  print ('Contacts Tests\nNOTE: Please run these tests only with a test '
         'account. The tests may delete or update your data.')
  unittest.TextTestRunner().run(suite())