GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Pull Request — master (#29)
by
unknown
01:23
created

tests.TestB2.test_delete_file_version()   A

Complexity

Conditions 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 14
nop 1
dl 0
loc 24
ccs 0
cts 13
cp 0
crap 2
rs 9.7
c 0
b 0
f 0
1
"""
2
Copyright George Sibble 2018
3
"""
4 1
import b2blaze.b2lib
5
from datetime import datetime
6
import pytest
7
from b2blaze.b2_exceptions import B2Exception, B2RequestError, B2FileNotFoundError
8
9
10
class TestB2(object):
11
    """ Tests for the b2blaze library """
12
13
    @classmethod
14
    def setup_class(cls):
15
        """
16
17
        :return: None
18
        """
19
        cls.b2 = b2blaze.b2lib.B2()
20
        timestamp = datetime.now().strftime("%Y-%m-%d-%H%M%S")
21
        cls.bucket_name = "testbucket-" + timestamp
22
        print("test bucket: ", cls.bucket_name)
23
24
    # Helper methods
25
    def test_create_b2_instance(self):
26
        """Create a B2 instance """
27
        b2 = b2blaze.b2lib.B2()
28
29
    @classmethod
30
    def create_bucket(cls):
31
        return cls.b2.buckets.create(cls.bucket_name, security=cls.b2.buckets.public)
32
33
    @classmethod
34
    def getbucket(cls):
35
        return cls.b2.buckets.get(bucket_name=cls.bucket_name) or cls.create_bucket()
36
37
    @classmethod
38
    def upload_textfile(cls, contents="hello there", file_name="test/hello.txt"):
39
        """ Upload text file with name 'test/hello.txt' """
40
        contents = contents.encode("utf-8")  # These fail unless encoded to UTF8
41
        bucket = cls.getbucket()
42
        return bucket.files.upload(contents=contents, file_name=file_name)
43
44
    @classmethod
45
    def is_b2_file(cls, obj):
46
        """ hacky method for checking object class/type is B2File"""
47
        if "B2File" in str(type(obj)):
48
            return True
49
        return False
50
51
    ##   Tests    ##
52
    @pytest.mark.bucket
53
    @pytest.mark.files
54
    @pytest.mark.versions
55
    def test_create_bucket(self):
56
        """ Create a bucket by name. """
57
        self.bucket = self.b2.buckets.create(
58
            self.bucket_name, security=self.b2.buckets.public
59
        )
60
        assert self.bucket
61
62
    @pytest.mark.bucket
63
    def test_get_bucket(self):
64
        """ Get a bucket by name """
65
        bucket = self.getbucket()
66
        assert bucket
67
68
    @pytest.mark.bucket
69
    def test_get_all_buckets(self):
70
        """ Get buckets. Number of buckets returned should be >1 """
71
        buckets = self.b2.buckets.all()
72
        assert len(buckets) > 1, "Number of buckets returned should be >1"
73
74
    @pytest.mark.bucket
75
    def test_get_nonexistent_bucket(self):
76
        """ Get a bucket which doesn't exist should return None """
77
        bucket = self.b2.buckets.get(bucket_name="this doesnt exist")
78
        assert not bucket
79
80
    @pytest.mark.files
81
    def test_create_file_and_retrieve_by_id(self):
82
        """ Create a file and retrieve by ID """
83
        bucket = self.getbucket()
84
        contents = "Hello World!".encode("utf-8")  # These fail unless encoded to UTF8
85
        file = bucket.files.upload(contents=contents, file_name="test/hello.txt")
86
        file2 = bucket.files.get(file_id=file.file_id)
87
88
        # It should be a B2File
89
        assert self.is_b2_file(file2), "Should be a B2File object"
90
91
    @pytest.mark.files
92
    def test_direct_upload_file(self):
93
        """ Upload binary file """
94
        bucket = self.getbucket()
95
        binary_file = open("b2blaze/test_pic.jpg", "rb")
96
        uploaded_file = bucket.files.upload(
97
            contents=binary_file, file_name="test_pic2.jpg"
98
        )
99
        binary_file.close()
100
        assert self.is_b2_file(uploaded_file)
101
102
    @pytest.mark.files
103
    def test_get_all_files(self):
104
        """ Get all files from a bucket. Returned objects are B2Files """
105
        bucket = self.getbucket()
106
        files = bucket.files.all()
107
        print("test_get_files: all files: ", len(files))
108
109
        # check type
110
        assert self.is_b2_file(files[0]), "Should be a B2File object"
111
112
    @pytest.mark.versions
113
    @pytest.mark.files
114
    def test_get_all_file_versions(self):
115
        """ Get all file versions from a bucket """
116
        bucket = self.getbucket()
117
        file = self.upload_textfile()
118
        files = bucket.files.all_file_versions()
119
        print("test_get_all_file_versions: all versions: ", len(files["file_versions"]))
120
        assert len(files["file_versions"]) > 0, "File versions should exist"
121
122
    @pytest.mark.files
123
    def test_get_file_by_name(self):
124
        """ Get file by name """
125
        bucket = self.getbucket()
126
        file = self.upload_textfile()
127
128
        # check type
129
        assert self.is_b2_file(file), "Should be a B2File object"
130
131
    @pytest.mark.files
132
    def test_get_file_by_id(self):
133
        """ Get file by id """
134
        bucket = self.getbucket()
135
        file = self.upload_textfile()
136
137
        # check type
138
        assert self.is_b2_file(file), "Should be a B2File object"
139
140
    @pytest.mark.versions
141
    @pytest.mark.files
142
    def test_get_file_versions(self):
143
        """ Get all versions of a file via the file.get_versions method. 
144
            Returned data should be a list, and items should be of type B2File
145
        """
146
        bucket = self.getbucket()
147
        file = bucket.files.get(file_name="test/hello.txt")
148
        versions = file.get_versions()
149
        assert len(versions) > 0, "File should have multiple versions"
150
151
        # check type
152
        assert self.is_b2_file(versions[0]), "Should be a B2File object"
153
154
    @pytest.mark.versions
155
    @pytest.mark.files
156
    def test_bucket_get_file_versions_by_name(self):
157
        """ Get all versions of a file by name file_list.get_versions method. 
158
            Returned data should be a list, and items should be of type B2File
159
        """
160
        bucket = self.getbucket()
161
        versions = bucket.files.get_versions(file_name="test/hello.txt")
162
        assert len(versions) > 0, "File should have multiple versions"
163
        assert self.is_b2_file(versions[0]), "Should be a B2File object"
164
165
    @pytest.mark.versions
166
    @pytest.mark.files
167
    def test_bucket_get_file_versions_by_id(self):
168
        """ Get all versions of a file by id file_list.get_versions method. 
169
            Returned data should be a list, and items should be of type B2File
170
        """
171
        bucket = self.getbucket()
172
        file = bucket.files.get(file_name="test/hello.txt")
173
        versions = bucket.files.get_versions(file_id=file.file_id)
174
        assert len(versions) > 0, "File should have multiple versions"
175
        assert self.is_b2_file(versions[0]), "Should be a B2File object"
176
177
    @pytest.mark.files
178
    @pytest.mark.b2errors
179
    def test_get_file_doesnt_exist(self):
180
        """ Get file which doesn't exist should raise B2FileNotFoundError, get by ID should raise B2RequestError """
181
        bucket = self.getbucket()
182
        with pytest.raises(B2FileNotFoundError):
183
            file = bucket.files.get(file_name="nope.txt")
184
        with pytest.raises(B2RequestError):
185
            file2 = bucket.files.get(file_id="abcd")
186
187
    @pytest.mark.files
188
    def test_download_file(self):
189
        """ Get file by id """
190
        bucket = self.getbucket()
191
        file = self.upload_textfile()
192
        data = file.download()
193
        assert len(data.read()) > 0
194
195
    @pytest.mark.files
196
    def test_download_url(self):
197
        """ Download file url should be publicly GET accessible """
198
        import requests
199
200
        bucket = self.getbucket()
201
        file = self.upload_textfile()
202
        url = file.url
203
        downloaded_file = requests.get(url)
204
        if downloaded_file.status_code != 200:
205
            print(downloaded_file.json())
206
            raise ValueError
207
208
    @pytest.mark.files
209
    @pytest.mark.b2errors
210
    def test_hide_file(self):
211
        """ Should create + upload, then hide / soft-delete a file by name.
212
            File should no longer be returned when searched by name in bucket.
213
        """
214
        bucket = self.getbucket()
215
        upload = self.upload_textfile(
216
            contents="Delete this", file_name="test/deleteme.txt"
217
        )
218
219
        # Delete
220
        print("test_delete_file: upload.file_id", upload.file_id)
221
        print("test_delete_file: upload.file_name", upload.file_name)
222
        upload.hide()
223
224
        # Refresh bucket; getting the the file should fail
225
        with pytest.raises(B2FileNotFoundError):
226
            bucket = self.getbucket()
227
            file = bucket.files.get(file_name=upload.file_name)
228
            assert not file, "Deleted file should not be in files list"
229
230
    @pytest.mark.files
231
    @pytest.mark.versions
232
    def test_delete_file_version(self):
233
        """ Delete a file version by name. It should still exist when searched."""
234
        bucket = self.getbucket()
235
236
        # Upload file & delete
237
        file = self.upload_textfile()
238
        file2 = self.upload_textfile()
239
240
        # Update versions
241
        versions = file.get_versions()
242
243
        assert len(versions) > 1, "File should should have multiple version"
244
245
        # Delete version
246
        print("test_delete_file_version: file_name", file.file_name)
247
        print("test_delete_file_version: file_id", file.file_id)
248
        file.delete()
249
250
        # Refresh bucket; getting the the file should fail
251
        file2 = bucket.files.get(file_name=file.file_name)
252
        assert file2, "Deleted file version only, file should still exist"
253
        assert self.is_b2_file(file2), "Should be a B2File object"
254
255
    # @pytest.mark.versions
256
    @pytest.mark.files
257
    def test_delete_all_file_versions(self):
258
        """ Delete all versions of a file. It should be gone completely from bucket."""
259
        bucket = self.getbucket()
260
261
        # Create file, make sure we have multiple versions
262
        contents = "Hello World!".encode("utf-8")  # These fail unless encoded to UTF8
263
        upload = bucket.files.upload(contents=contents, file_name="test/hello.txt")
264
265
        # Get
266
        # versions = bucket.files.get_versions(file_name='test/hello.txt')
267
        file = bucket.files.get(file_name="test/hello.txt")
268
        versions = file.get_versions()
269
        assert len(versions) > 0, "File should should have multiple version"
270
271
        # Delete
272
        print("test_delete_all_file_versions: file_name", file.file_name)
273
        print("test_delete_all_file_versions: file_id", file.file_id)
274
        file.delete_all_versions(confirm=True)
275
276
        # Refresh bucket; getting the the file should fail
277
        with pytest.raises(B2FileNotFoundError):
278
            bucket = self.getbucket()
279
            file2 = bucket.files.get(file_name=file.file_name)
280
            assert not file2, "Deleted all file versions, file should not exist"
281
282
    @pytest.mark.bucket
283
    def test_delete_non_empty_bucket(self):
284
        """ Delete bucket should fail on bucket non-empty """
285
        bucket = self.getbucket()
286
287
        # Upload file
288
        self.upload_textfile()
289
        assert len(bucket.files.all()) > 0, "Bucket should still contain files"
290
291
        # Should raise exception on non-empty without confirm
292
        with pytest.raises(B2RequestError):
293
            bucket.delete()  # Try to delete without confirmation
294
295
        # Bucket should still exist
296
        assert self.b2.buckets.get(
297
            bucket_name=bucket.bucket_name
298
        ), "bucket should still exist"
299
300
        # # Delete with confirmation
301
        # bucket.delete(delete_files=True, confirm_non_empty=True)
302
303
        # # Bucket should be gone
304
        # assert self.b2.buckets.get(bucket_name=bucket.bucket_name), 'bucket should not exist'
305
306
    @pytest.mark.bucket
307
    @pytest.mark.files
308
    @pytest.mark.versions
309
    def test_bucket_delete_all_files(self):
310
        """ Delete all files from bucket. """
311
        bucket = self.getbucket()
312
        self.upload_textfile()
313
314
        files = bucket.files.all()
315
        assert len(files) > 0, "Bucket should still contain files"
316
317
        # Delete all files
318
        bucket.files.delete_all(confirm=True)
319
        assert len(bucket.files.all()) == 0, "Bucket should be empty"
320
321
    @pytest.mark.bucket
322
    def test_delete_bucket(self):
323
        """ Delete empty bucket"""
324
        bucket = self.getbucket()
325
326
        # Ascertain it's empty
327
        files_new = bucket.files.all(include_hidden=True)
328
        assert (
329
            len(files_new) == 0
330
        ), "Bucket should contain no files but contains {}".format(len(files_new))
331
332
        # Delete
333
        bucket.delete()
334
335
        # Confirm bucket is gone. bucket.get() nonexistent should return None.
336
        assert not self.b2.buckets.get(
337
            bucket_name=bucket.bucket_name
338
        ), "Deleted bucket still exists"
339
340
341
def main():
342
    import pytest
343
344
    pytest_args = [__file__, "--verbose"]
345
    pytest.main(pytest_args)
346
347
348
if __name__ == "__main__":
349
    main()
350