-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_api.py
196 lines (151 loc) · 7.68 KB
/
test_api.py
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
import logging
from onedrive import auth
from onedrive import api
from onedrive.api import AsyncOperationStatus
import unittest
import requests
class TestApi(unittest.TestCase):
def setUp(self):
self.header = auth.login()
api.mkdir("/api_test", self.header)
def tearDown(self):
api.delete("/api_test", self.header)
api.delete("/api_test2", self.header)
api.delete("/api_test_src", self.header)
api.delete("/api_test_dst", self.header)
def test_exists_mkdir(self):
api.delete("/api_test", self.header)
self.assertEqual(api.mkdir("/api_test", self.header).status_code, 201)
self.assertTrue(api.exists("/api_test", self.header))
self.assertEqual(api.delete("/api_test", self.header).status_code, 204)
def test_get_meta(self):
res = api.get_metadata("/", self.header)
self.assertEqual(res.status_code, 200)
self.assertIsNotNone(res.json_body()["folder"])
def test_mkdir_parents(self):
res = api.mkdir("/api_test/foo/bar/foobar", self.header, parents=False)
self.assertEqual(res.status_code, 400)
res = api.mkdir("/api_test/foo/bar/foobar", self.header, parents=True)
self.assertEqual(res.status_code, 201)
self.assertEqual(api.delete("/api_test", self.header).status_code, 204)
def test_delete(self):
self.assertEqual(api.delete("/api_test", self.header).status_code, 204)
def test_copy_dir(self):
self.assertTrue(api.exists("/api_test", self.header))
res = api.copy("/api_test", "/api_test2", self.header)
self.assertEqual(res.status_code, 202)
self.assertIsNotNone(res.headers.get('Location', None))
api.delete("/api_test", self.header)
api.delete("/api_test2", self.header)
def test_copy_file(self):
api.mkdir("/api_test/x", self.header, parents=True)
self.assertTrue(api.exists("/api_test", self.header))
self.assertTrue(api.exists("/api_test/x", self.header))
content = "1".encode("utf-8")
api.upload_simple(data=content, dst="/api_test/foo.tmp", auth=self.header)
self.assertTrue(api.exists("/api_test/foo.tmp", auth=self.header))
res = api.copy("/api_test/foo.tmp", "/api_test/x/foo2.tmp", self.header)
self.assertEqual(res.status_code, 202)
location = res.headers.get('Location', None)
self.assertIsNotNone(location)
AsyncOperationStatus(location, self.header).block()
self.assertTrue(api.exists("/api_test/foo.tmp", auth=self.header))
self.assertTrue(api.exists("/api_test/x/foo2.tmp", auth=self.header))
api.delete("/api_test", self.header)
def test_upload_success(self):
dst_file = "/api_test/upload.tmp"
api.delete(dst_file, self.header)
self.assertFalse(api.exists(dst_file, self.header))
content = "test content".encode("utf-8")
res = api.upload_simple(data=content, dst=dst_file, auth=self.header)
self.assertEqual(res.status_code, 201)
self.assertTrue(api.exists(dst_file, self.header))
api.delete("/api_test", self.header)
def test_upload_conflict_replace(self):
dst_file = "/api_test/upload.tmp"
api.delete(dst_file, self.header) # just to be sure
content = "1".encode("utf-8")
res1 = api.upload_simple(data=content, dst=dst_file, auth=self.header)
self.assertEqual(res1.status_code, 201)
content = ("x"*1000).encode("utf-8")
res2 = api.upload_simple(data=content, dst=dst_file, auth=self.header)
self.assertEqual(res2.status_code, 200)
size = api.get_metadata(dst_file, auth=self.header).json_body().get('size')
self.assertEqual(size, 1000)
api.delete("/api_test", self.header)
def test_upload_conflict_rename(self):
dst_file = "/api_test/upload.tmp"
api.delete(dst_file, self.header) # just to be sure
content = "1".encode("utf-8")
res1 = api.upload_simple(data=content, dst=dst_file, auth=self.header)
self.assertEqual(res1.status_code, 201)
content = "2".encode("utf-8")
res2 = api.upload_simple(data=content, dst=dst_file, auth=self.header, conflict='rename')
self.assertEqual(res2.status_code, 201) # renamed to upload 1.tmp
self.assertTrue(api.exists("/api_test/upload 1.tmp", auth=self.header))
api.delete("/api_test", auth=self.header)
def test_upload_conflict_fail(self):
dst_file = "/api_test/upload.tmp"
api.delete(dst_file, self.header) # just to be sure
content = "1".encode("utf-8")
res1 = api.upload_simple(data=content, dst=dst_file, auth=self.header)
self.assertEqual(res1.status_code, 201)
content = "2".encode("utf-8")
res2 = api.upload_simple(data=content, dst=dst_file, auth=self.header, conflict='fail')
self.assertEqual(res2.status_code, 409)
api.delete("/api_test", auth=self.header)
def test_download(self):
dst_file = "/api_test/upload.tmp"
content = "12345".encode("utf-8")
upload_result = api.upload_simple(data=content, dst=dst_file, auth=self.header)
self.assertEqual(upload_result.status_code, 201)
res = api.download(path=dst_file, auth=self.header)
self.assertEqual(res.status_code, 200)
self.assertEqual(content, res.text.encode("utf-8"))
api.delete("/api_test", auth=self.header)
def test_move(self):
api.mkdir("/api_test_src", self.header)
api.mkdir("/api_test_dst", self.header)
src_file = "/api_test_src/upload.tmp"
api.upload_simple(data="1".encode("utf-8"), dst=src_file, auth=self.header)
res = api.move(src_file, "/api_test_dst", auth=self.header)
self.assertEqual(res.status_code, 200)
self.assertFalse(api.exists(src_file, auth=self.header))
self.assertTrue(api.exists("/api_test_dst/upload.tmp", auth=self.header))
def test_rename(self):
src_file = "/api_test/upload.tmp"
api.upload_simple(data="1".encode("utf-8"), dst=src_file, auth=self.header)
res = api.rename(src_file, "renamed.tmp", auth=self.header)
self.assertEqual(res.status_code, 200)
self.assertFalse(api.exists(src_file, auth=self.header))
self.assertTrue(api.exists("/api_test/renamed.tmp", auth=self.header))
def test_list_children(self):
# 2 files, 1 dir
api.upload_simple(data="1".encode("utf-8"), dst="/api_test/upload1.tmp", auth=self.header)
api.upload_simple(data="2".encode("utf-8"), dst="/api_test/upload2.tmp", auth=self.header)
api.mkdir("/api_test/foo", auth=self.header)
res = api.list_children("/api_test", self.header)
self.assertEqual(res.status_code, 200)
jb = res.json_body()
entities = jb['value']
self.assertEqual(len(entities), 3)
files = [f for f in entities if 'file' in f]
folders = [f for f in entities if 'folder' in f]
self.assertEqual(len(files), 2)
self.assertEqual(len(folders), 1)
def test_list_children_paging(self):
# 4 dirs
api.mkdir("/api_test/foo1", auth=self.header)
api.mkdir("/api_test/foo2", auth=self.header)
api.mkdir("/api_test/foo3", auth=self.header)
api.mkdir("/api_test/foo4", auth=self.header)
# this is a lack: we have to rely on max_results being interpreted ...
res = api.list_children("/api_test", self.header, max_results=2)
self.assertEqual(res.status_code, 200)
jb = res.json_body()
self.assertEqual(len(jb['value']), 4)
if __name__ == '__main__':
logger = logging.getLogger('onedrive')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
unittest.main()