File: live_client_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 (262 lines) | stat: -rwxr-xr-x 9,696 bytes parent folder | download
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
#!/usr/bin/python
#
# Copyright 2009 Google Inc. All Rights Reserved.
#
# 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.

# This module is used for version 2 of the Google Data APIs.
# These tests attempt to connect to Google servers.


__author__ = 'e.bidelman (Eric Bidelman)'


import os
import unittest
import gdata.client
import gdata.data
import gdata.gauth
import gdata.docs.client
import gdata.docs.data
import gdata.test_config as conf


class DocsTestCase(unittest.TestCase):

  def setUp(self):
    self.client = None
    if conf.options.get_value('runlive') == 'true':
      self.client = gdata.docs.client.DocsClient()
      if conf.options.get_value('ssl') == 'true':
        self.client.ssl = True
      conf.configure_client(self.client, 'DocsTest', self.client.auth_service)

  def tearDown(self):
    conf.close_client(self.client)


class DocsFetchingDataTest(DocsTestCase):

  def testGetDocList(self):
    if not conf.options.get_value('runlive') == 'true':
      return

    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'testGetDocList')

    # Query using GetDocList()
    feed = self.client.GetDocList(limit=1)
    self.assert_(isinstance(feed, gdata.docs.data.DocList))
    self.assertEqual(len(feed.entry), 1)

  def testGetDoc(self):
    if not conf.options.get_value('runlive') == 'true':
      return

    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'testGetDoc')

    uri = ('http://docs.google.com/feeds/default/private/full/'
           '-/spreadsheet?max-results=1')
    feed = self.client.GetDocList(uri, limit=1)
    self.assertEqual(len(feed.entry), 1)
    self.assertEqual(feed.entry[0].GetDocumentType(), 'spreadsheet')
    resource_id = feed.entry[0].resource_id.text
    entry = self.client.GetDoc(resource_id)
    self.assert_(isinstance(entry, gdata.docs.data.DocsEntry))
    self.assert_(entry.id.text is not None)
    self.assert_(entry.title.text is not None)
    self.assert_(entry.resource_id.text is not None)
    self.assert_(entry.title.text is not None)

  def testGetAclFeed(self):
    if not conf.options.get_value('runlive') == 'true':
      return

    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'testGetAclFeed')

    uri = ('http://docs.google.com/feeds/default/private/full/'
           '-/mine?max-results=1')
    feed = self.client.GetDocList(uri=uri)
    self.assertEqual(len(feed.entry), 1)
    acl_feed = self.client.GetAclPermissions(feed.entry[0].resource_id.text)
    self.assert_(isinstance(acl_feed, gdata.docs.data.AclFeed))
    self.assert_(isinstance(acl_feed.entry[0], gdata.docs.data.Acl))
    self.assert_(acl_feed.entry[0].scope is not None)
    self.assert_(acl_feed.entry[0].role is not None)

  def testGetRevisionFeed(self):
    if not conf.options.get_value('runlive') == 'true':
      return

    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'testGetRevisionFeed')

    uri = 'http://docs.google.com/feeds/default/private/full/-/document'
    feed = self.client.GetDocList(uri=uri, limit=1)
    self.assertEqual(len(feed.entry), 1)
    acl_feed = self.client.GetRevisions(feed.entry[0].resource_id.text)
    self.assert_(isinstance(acl_feed, gdata.docs.data.RevisionFeed))
    self.assert_(isinstance(acl_feed.entry[0], gdata.docs.data.Revision))


class CreatingAndDeletionTest(DocsTestCase):

  def testCreateAndMoveDoc(self):
    if not conf.options.get_value('runlive') == 'true':
      return

    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'testCreateAndMoveDoc')

    new_folder = self.client.Create(gdata.docs.data.FOLDER_LABEL, 'My Folder')
    self.assertEqual(new_folder.title.text, 'My Folder')
    self.assertEqual(new_folder.GetDocumentType(), 'folder')

    new_doc = self.client.Create(gdata.docs.data.DOCUMENT_LABEL, 'My Doc',
                                 writers_can_invite=False)
    self.assertEqual(new_doc.GetDocumentType(), 'document')
    self.assertEqual(new_doc.title.text, 'My Doc')
    self.assertEqual(new_doc.writers_can_invite.value, 'false')

    # Move doc into folder
    new_entry = self.client.Move(new_doc, new_folder)
    self.assertEqual(len(new_entry.InFolders()), 1)
    self.assertEqual(new_entry.InFolders()[0].title, 'My Folder')

    # Create new spreadsheet inside the folder.
    new_spread = self.client.Create(
        gdata.docs.data.SPREADSHEET_LABEL, 'My Spread', folder_or_id=new_folder)
    self.assertEqual(new_spread.GetDocumentType(), 'spreadsheet')
    self.assertEqual(len(new_spread.InFolders()), 1)
    self.assertEqual(new_spread.InFolders()[0].title, 'My Folder')

    # Create new folder, and move spreadsheet into that folder too.
    new_folder2 = self.client.Create(gdata.docs.data.FOLDER_LABEL, 'My Folder2')
    self.assertEqual(new_folder2.title.text, 'My Folder2')
    self.assertEqual(new_folder2.GetDocumentType(), 'folder')
    moved_entry = self.client.Move(
        new_spread, new_folder2, keep_in_folders=True)
    self.assertEqual(len(moved_entry.InFolders()), 2)

    # Move spreadsheet to root level
    was_moved = self.client.Move(moved_entry)
    self.assert_(was_moved)
    spread_entry = self.client.GetDoc(moved_entry.resource_id.text)
    self.assertEqual(len(spread_entry.InFolders()), 0)
    
    # Clean up our mess.
    self.client.Delete(new_folder.GetEditLink().href, force=True)
    self.client.Delete(new_folder2.GetEditLink().href, force=True)
    self.client.Delete(new_doc.GetEditLink().href, force=True)
    self.client.Delete(spread_entry.GetEditLink().href, force=True)


class DocumentListUploadTest(DocsTestCase):

  def testUploadAndDeleteDocument(self):
    if not conf.options.get_value('runlive') == 'true':
      return

    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'testUploadAndDeleteDocument')

    ms = gdata.data.MediaSource(file_path='test.doc',
                                content_type='application/msword')

    entry = self.client.Upload(ms, 'test doc')
    self.assertEqual(entry.title.text, 'test doc')
    self.assertEqual(entry.GetDocumentType(), 'document')
    self.assert_(isinstance(entry, gdata.docs.data.DocsEntry))
    self.client.Delete(entry, force=True)

  def testUploadAndDeletePdf(self):
    if not conf.options.get_value('runlive') == 'true':
      return

    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'testUploadAndDeletePdf')

    # Try passing in filename isntead of MediaSource object on this upload.
    entry = self.client.Upload(
        'test.pdf', 'test pdf', content_type='application/pdf')
    self.assertEqual(entry.title.text, 'test pdf')
    self.assertEqual(entry.GetDocumentType(), 'pdf')
    self.assert_(isinstance(entry, gdata.docs.data.DocsEntry))
    self.client.Delete(entry, force=True)


class DocumentListExportTest(DocsTestCase):

  def testExportDocument(self):
    if not conf.options.get_value('runlive') == 'true':
      return

    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'testExportDocument')

    uri = 'http://docs.google.com/feeds/default/private/full/-/document'
    feed = self.client.GetDocList(uri=uri, limit=1)
    file_paths = ['./downloadedTest.doc', './downloadedTest.html',
                  './downloadedTest.odt', './downloadedTest.pdf',
                  './downloadedTest.png', './downloadedTest.rtf',
                  './downloadedTest.txt', './downloadedTest.zip']
    for path in file_paths:
      self.client.Export(feed.entry[0], path)
      self.assert_(os.path.exists(path))
      self.assert_(os.path.getsize(path))
      os.remove(path)

  def testExportNonExistentDocument(self):
    if not conf.options.get_value('runlive') == 'true':
      return

    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'testExportNonExistentDocument')

    path = './ned.txt'
    self.assert_(not os.path.exists(path))

    exception_raised = False
    try:
      self.client.Export('non_existent_doc', path)
    except Exception, e:  # expected
      exception_raised = True

    self.assert_(exception_raised)
    self.assert_(not os.path.exists(path))

  def testDownloadPdf(self):
    if not conf.options.get_value('runlive') == 'true':
      return

    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'testDownloadPdf')

    uri = 'http://docs.google.com/feeds/default/private/full/-/pdf'
    feed = self.client.GetDocList(uri=uri, limit=1)
    path = './downloadedTest.pdf'
    self.client.Download(feed.entry[0], path)
    self.assert_(os.path.exists(path))
    self.assert_(os.path.getsize(path))
    os.remove(path)


def suite():
  return conf.build_suite([DocsFetchingDataTest, CreatingAndDeletionTest,
                           DocumentListUploadTest, DocumentListExportTest])


if __name__ == '__main__':
  unittest.TextTestRunner().run(suite())