1
# Copyright 2008-2015 Canonical
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU Affero General Public License as
5
# published by the Free Software Foundation, either version 3 of the
6
# License, or (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Affero General Public License for more details.
13
# You should have received a copy of the GNU Affero General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
# For further info, check http://launchpad.net/filesync-server
18
"""Tests for the DAL entry point."""
22
from twisted.trial.unittest import TestCase
23
from twisted.internet import defer
24
from mocker import Mocker, expect, KWARGS
26
from backends.filesync.data import errors, model
27
from ubuntuone.storage.rpcdb import dal_backend
30
class DALTestCase(TestCase):
31
"""Tests for the DAL specifically."""
33
@defer.inlineCallbacks
36
yield super(DALTestCase, self).setUp()
37
self.dal = dal_backend.DAL()
39
def test_unlink_node(self):
43
# node, with a generation attribute
45
expect(node.generation).result(123)
46
expect(node.kind).result('File')
47
expect(node.name).result('foo')
48
expect(node.mimetype).result('mime')
50
# user, with the chained calls to the delete
52
expect(user.volume('vol_id').node('node_id').delete()).result(node)
53
self.dal._get_user = lambda *a: user
56
kwargs = dict(user_id='user_id', volume_id='vol_id',
57
node_id='node_id', session_id='session_id')
58
result = self.dal.unlink_node(**kwargs)
60
d = dict(generation=123, kind='File', name='foo', mimetype='mime')
61
self.assertEqual(result, d)
63
def test_list_volumes_root_and_quota(self):
64
"""List volumes, check root and quota."""
69
expect(root.generation).result(123)
70
expect(root.root_id).result('root_id')
74
expect(quota.free_bytes).result(4567890)
78
self.dal._get_user = lambda *a: user
79
expect(user.volume().get_volume()).result(root)
80
expect(user.get_shared_to(accepted=True)).result([])
81
expect(user.get_udfs()).result([])
82
expect(user.get_quota()).result(quota)
85
result = self.dal.list_volumes('user_id')
87
self.assertEqual(sorted(result),
88
['free_bytes', 'root', 'shares', 'udfs'])
89
self.assertEqual(result['root'],
90
dict(generation=123, root_id='root_id'))
91
self.assertEqual(result['free_bytes'], 4567890)
93
def test_list_volumes_shares(self):
94
"""List volumes, check shares."""
99
expect(root.generation).result(123)
100
expect(root.root_id).result('root_id')
101
quota = mocker.mock()
102
expect(quota.free_bytes).result(4567890)
105
sharedby1 = mocker.mock()
106
expect(sharedby1.username).result('byusername1')
107
expect(sharedby1.visible_name).result('byvisible1')
108
expect(sharedby1.id).result('byid1')
109
expect(sharedby1.shard_id).result('byshard1')
110
share1 = mocker.mock()
111
expect(share1.id).result('share1_id')
112
expect(share1.root_id).result('share1_root_id')
113
expect(share1.name).result('name1')
114
expect(share1.shared_by).result(sharedby1)
115
expect(share1.accepted).result(True)
116
expect(share1.access).result(1)
117
expect(share1.get_generation()).result(6)
120
sharedby2 = mocker.mock()
121
expect(sharedby2.username).result('byusername2')
122
expect(sharedby2.visible_name).result('byvisible2')
123
expect(sharedby2.id).result('byid2')
124
expect(sharedby2.shard_id).result('byshard2')
125
share2 = mocker.mock()
126
expect(share2.id).result('share2_id')
127
expect(share2.root_id).result('share2_root_id')
128
expect(share2.name).result('name2')
129
expect(share2.shared_by).result(sharedby2)
130
expect(share2.accepted).result(False)
131
expect(share2.access).result(0)
132
expect(share2.get_generation()).result(8)
136
expect(inf1.free_bytes).result(147)
138
expect(inf2.free_bytes).result(852)
139
shard_info = {('byid1', 'byshard1'): inf1, ('byid2', 'byshard2'): inf2}
140
self.patch(dal_backend.services, 'get_user_info_for_shard',
141
lambda *a: shard_info[a])
145
self.dal._get_user = lambda *a: user
146
expect(user.volume().get_volume()).result(root)
147
expect(user.get_shared_to(accepted=True)).result([share1, share2])
148
expect(user.get_udfs()).result([])
149
expect(user.get_quota()).result(quota)
152
result = self.dal.list_volumes('user_id')
153
share1, share2 = result['shares']
155
self.assertEqual(share1['id'], 'share1_id')
156
self.assertEqual(share1['root_id'], 'share1_root_id')
157
self.assertEqual(share1['name'], 'name1')
158
self.assertEqual(share1['shared_by_username'], 'byusername1')
159
self.assertEqual(share1['shared_by_visible_name'], 'byvisible1')
160
self.assertEqual(share1['accepted'], True)
161
self.assertEqual(share1['access'], 1)
162
self.assertEqual(share1['free_bytes'], 147)
163
self.assertEqual(share1['generation'], 6)
165
self.assertEqual(share2['id'], 'share2_id')
166
self.assertEqual(share2['root_id'], 'share2_root_id')
167
self.assertEqual(share2['name'], 'name2')
168
self.assertEqual(share2['shared_by_username'], 'byusername2')
169
self.assertEqual(share2['shared_by_visible_name'], 'byvisible2')
170
self.assertEqual(share2['accepted'], False)
171
self.assertEqual(share2['access'], 0)
172
self.assertEqual(share2['free_bytes'], 852)
173
self.assertEqual(share2['generation'], 8)
175
def test_list_volumes_udfs(self):
176
"""List volumes, check shares."""
181
expect(root.generation).result(123)
182
expect(root.root_id).result('root_id')
183
quota = mocker.mock()
184
expect(quota.free_bytes).result(4567890)
188
expect(udf1.id).result('udf1_id')
189
expect(udf1.root_id).result('udf1_root_id')
190
expect(udf1.path).result('path1')
191
expect(udf1.generation).result(6)
195
expect(udf2.id).result('udf2_id')
196
expect(udf2.root_id).result('udf2_root_id')
197
expect(udf2.path).result('path2')
198
expect(udf2.generation).result(8)
202
self.dal._get_user = lambda *a: user
203
expect(user.volume().get_volume()).result(root)
204
expect(user.get_shared_to(accepted=True)).result([])
205
expect(user.get_udfs()).result([udf1, udf2])
206
expect(user.get_quota()).result(quota)
209
result = self.dal.list_volumes('user_id')
210
udf1, udf2 = result['udfs']
212
self.assertEqual(udf1['id'], 'udf1_id')
213
self.assertEqual(udf1['root_id'], 'udf1_root_id')
214
self.assertEqual(udf1['path'], 'path1')
215
self.assertEqual(udf1['generation'], 6)
217
self.assertEqual(udf2['id'], 'udf2_id')
218
self.assertEqual(udf2['root_id'], 'udf2_root_id')
219
self.assertEqual(udf2['path'], 'path2')
220
self.assertEqual(udf2['generation'], 8)
226
# node, with a generation attribute
228
expect(node.generation).result(123)
229
expect(node.mimetype).result('mime')
231
# user, with the chained calls to the operation
233
new_parent_id = uuid.uuid4()
234
expect(user.volume('vol_id').node('node_id')
235
.move(new_parent_id, 'new_name')).result(node)
236
self.dal._get_user = lambda *a: user
239
kwargs = dict(user_id='user_id', volume_id='vol_id',
240
node_id='node_id', new_parent_id=new_parent_id,
241
new_name='new_name', session_id='session_id')
242
result = self.dal.move(**kwargs)
244
self.assertEqual(result, dict(generation=123, mimetype='mime'))
246
def test_move_with_new_parent_id_as_str(self):
247
"""Check that DAO.move() will cast new_parent_id into a UUID if it
250
This is necessary because StorageObject.move() only accepts UUIDs for
256
self.dal._get_user = lambda *a: user
257
parent_id = uuid.uuid4()
259
expect(node.generation).result(123)
260
expect(node.mimetype).result('mime')
261
expect(user.volume('vol_id').node('node_id').move(
262
parent_id, 'new_name')).result(node)
265
# Here we pass the new_parent_id as str but above we expect it to
267
self.dal.move('user_id', 'vol_id', 'node_id', str(parent_id),
268
'new_name', 'session_id')
270
def test_make_dir(self):
271
"""Make a directory."""
274
# node, with a generation attribute
276
expect(node.id).result('node_id')
277
expect(node.generation).result(123)
278
expect(node.mimetype).result('mime')
280
# user, with the chained calls to the operation
282
expect(user.volume('vol_id').dir('parent_id')
283
.make_subdirectory('name')).result(node)
284
self.dal._get_user = lambda *a: user
287
kwargs = dict(user_id='user_id', volume_id='vol_id',
288
parent_id='parent_id', name='name',
289
session_id='session_id')
290
result = self.dal.make_dir(**kwargs)
292
d = dict(generation=123, node_id='node_id', mimetype='mime')
293
self.assertEqual(result, d)
295
def test_make_file(self):
296
"""Make a file with no content."""
299
# node, with a generation attribute
301
expect(node.id).result('node_id')
302
expect(node.generation).result(123)
303
expect(node.mimetype).result('mime')
305
# user, with the chained calls to the operation
307
expect(user.volume('vol_id')
308
.dir('parent_id').make_file('name')).result(node)
309
self.dal._get_user = lambda *a: user
312
kwargs = dict(user_id='user_id', volume_id='vol_id',
313
parent_id='parent_id', name='name',
314
session_id='session_id')
315
result = self.dal.make_file(**kwargs)
317
d = dict(generation=123, node_id='node_id', mimetype='mime')
318
self.assertEqual(result, d)
320
def test_make_file_with_content(self):
321
"""Make a file with content associated."""
324
# node, with a generation attribute
326
expect(node.id).result('node_id')
327
expect(node.generation).result(123)
329
# user, with the chained calls to the operation
331
expect(user.volume('vol_id').dir('parent_id')
332
.make_file_with_content('name', 'hash', 'crc32', 'size',
333
'deflated_size', 'storage_key')
335
self.dal._get_user = lambda *a: user
338
kwargs = dict(user_id='user_id', volume_id='vol_id', name='name',
339
parent_id='parent_id', crc32='crc32', size='size',
340
node_hash='hash', deflated_size='deflated_size',
341
storage_key='storage_key', session_id='session_id')
342
result = self.dal.make_file_with_content(**kwargs)
344
self.assertEqual(result, dict(generation=123, node_id='node_id'))
346
def test_delete_share(self):
347
"""Delete a share."""
351
share = mocker.mock()
352
expect(share.delete())
354
# user, with the chained calls to the operation
356
expect(user.get_share('share_id')).result(share)
357
self.dal._get_user = lambda *a: user
360
result = self.dal.delete_share('user_id', 'share_id')
361
self.assertEqual(result, {})
363
def test_create_share(self):
364
"""Create a share."""
367
# patch the DAL method to get the other user id from the username
368
to_user = mocker.mock()
369
expect(to_user.id).result('to_user_id')
371
expect(fake(username='to_username')).result(to_user)
372
self.patch(dal_backend.services, 'get_storage_user', fake)
375
share = mocker.mock()
376
expect(share.id).result('share_id')
378
# user, with the chained calls to the operation
380
expect(user.volume().dir('node_id').share(
381
'to_user_id', 'name', True)).result(share)
382
self.dal._get_user = lambda *a: user
385
result = self.dal.create_share('user_id', 'node_id',
386
'to_username', 'name', True)
387
self.assertEqual(result, dict(share_id='share_id'))
389
def test_accept_share(self):
390
"""Accept a share."""
394
share = mocker.mock()
395
expect(share.accept())
397
# user, with the chained calls to the operation
399
expect(user.get_share('share_id')).result(share)
400
self.dal._get_user = lambda *a: user
403
result = self.dal.accept_share('user_id', 'share_id')
404
self.assertEqual(result, {})
406
def test_decline_share(self):
407
"""Decline a share."""
411
share = mocker.mock()
412
expect(share.decline())
414
# user, with the chained calls to the operation
416
expect(user.get_share('share_id')).result(share)
417
self.dal._get_user = lambda *a: user
420
result = self.dal.decline_share('user_id', 'share_id')
421
self.assertEqual(result, {})
423
def test_list_shares_shared_by(self):
424
"""List shares, the shared_by part."""
428
sharedto1 = mocker.mock()
429
expect(sharedto1.username).result('tousername1')
430
expect(sharedto1.visible_name).result('tovisible1')
431
share1 = mocker.mock()
432
expect(share1.id).result('share1_id')
433
expect(share1.root_id).result('share1_root_id')
434
expect(share1.name).result('name1')
435
expect(share1.shared_to).result(sharedto1)
436
expect(share1.accepted).result(True)
437
expect(share1.access).result(1)
439
# other share, without shared_to
440
share2 = mocker.mock()
441
expect(share2.id).result('share2_id')
442
expect(share2.root_id).result('share2_root_id')
443
expect(share2.name).result('name2')
444
expect(share2.shared_to).result(None)
445
expect(share2.accepted).result(False)
446
expect(share2.access).result(0)
450
self.dal._get_user = lambda *a: user
451
expect(user.get_shared_by()).result([share1, share2])
452
expect(user.get_shared_to(accepted=True)).result([])
455
result = self.dal.list_shares('user_id', accepted=True)
456
share1, share2 = result['shared_by']
458
self.assertEqual(share1['id'], 'share1_id')
459
self.assertEqual(share1['root_id'], 'share1_root_id')
460
self.assertEqual(share1['name'], 'name1')
461
self.assertEqual(share1['shared_to_username'], 'tousername1')
462
self.assertEqual(share1['shared_to_visible_name'], 'tovisible1')
463
self.assertEqual(share1['accepted'], True)
464
self.assertEqual(share1['access'], 1)
466
self.assertEqual(share2['id'], 'share2_id')
467
self.assertEqual(share2['root_id'], 'share2_root_id')
468
self.assertEqual(share2['name'], 'name2')
469
self.assertEqual(share2['shared_to_username'], None)
470
self.assertEqual(share2['shared_to_visible_name'], None)
471
self.assertEqual(share2['accepted'], False)
472
self.assertEqual(share2['access'], 0)
474
def test_list_shares_shared_to(self):
475
"""List shares, the shared_to part."""
479
sharedby1 = mocker.mock()
480
expect(sharedby1.username).result('byusername1')
481
expect(sharedby1.visible_name).result('byvisible1')
482
share1 = mocker.mock()
483
expect(share1.id).result('share1_id')
484
expect(share1.root_id).result('share1_root_id')
485
expect(share1.name).result('name1')
486
expect(share1.shared_by).result(sharedby1)
487
expect(share1.accepted).result(True)
488
expect(share1.access).result(1)
490
# other share, without shared_by
491
share2 = mocker.mock()
492
expect(share2.id).result('share2_id')
493
expect(share2.root_id).result('share2_root_id')
494
expect(share2.name).result('name2')
495
expect(share2.shared_by).result(None)
496
expect(share2.accepted).result(False)
497
expect(share2.access).result(0)
501
self.dal._get_user = lambda *a: user
502
expect(user.get_shared_by()).result([])
503
expect(user.get_shared_to(accepted=False)).result([share1, share2])
506
result = self.dal.list_shares('user_id', accepted=False)
507
share1, share2 = result['shared_to']
509
self.assertEqual(share1['id'], 'share1_id')
510
self.assertEqual(share1['root_id'], 'share1_root_id')
511
self.assertEqual(share1['name'], 'name1')
512
self.assertEqual(share1['shared_by_username'], 'byusername1')
513
self.assertEqual(share1['shared_by_visible_name'], 'byvisible1')
514
self.assertEqual(share1['accepted'], True)
515
self.assertEqual(share1['access'], 1)
517
self.assertEqual(share2['id'], 'share2_id')
518
self.assertEqual(share2['root_id'], 'share2_root_id')
519
self.assertEqual(share2['name'], 'name2')
520
self.assertEqual(share2['shared_by_username'], None)
521
self.assertEqual(share2['shared_by_visible_name'], None)
522
self.assertEqual(share2['accepted'], False)
523
self.assertEqual(share2['access'], 0)
525
def test_create_udf(self):
531
expect(udf.id).result('udf_id')
532
expect(udf.root_id).result('udf_root_id')
533
expect(udf.path).result('udf_path')
535
# user, with the chained calls to the operation
537
expect(user.make_udf('path')).result(udf)
538
self.dal._get_user = lambda *a: user
541
result = self.dal.create_udf('user_id', 'path', 'session_id')
542
should = dict(udf_id='udf_id', udf_root_id='udf_root_id',
544
self.assertEqual(result, should)
546
def test_delete_volume_share(self):
547
"""Delete a volume that was a share."""
551
share = mocker.mock()
552
expect(share.delete())
554
# user, getting a share when asked
556
expect(user.get_share('volume_id')).result(share)
557
self.dal._get_user = lambda *a: user
560
result = self.dal.delete_volume('user_id', 'volume_id',
562
self.assertEqual(result, {})
564
def test_delete_volume_udf(self):
565
"""Delete a volume that was a udf."""
568
# user, with an exception when asking for the share, and
571
expect(user.get_share('volume_id')).throw(errors.DoesNotExist)
572
expect(user.delete_udf('volume_id'))
573
self.dal._get_user = lambda *a: user
576
result = self.dal.delete_volume('user_id', 'volume_id',
578
self.assertEqual(result, {})
580
def test_delete_volume_none(self):
581
"""Delete a volume that was not there."""
584
# user, with an exception when asking for the share, and
587
expect(user.get_share('volume_id')).throw(errors.DoesNotExist)
588
expect(user.delete_udf('volume_id')).throw(errors.DoesNotExist)
589
self.dal._get_user = lambda *a: user
592
err = self.assertRaises(errors.DoesNotExist,
593
self.dal.delete_volume,
594
'user_id', 'volume_id', 'session_id')
595
self.assertEqual(str(err), "Volume 'volume_id' does not exist")
597
def test_get_user_quota(self):
598
"""Return the quota info for an user."""
603
expect(info.max_storage_bytes).result(100)
604
expect(info.used_storage_bytes).result(80)
605
expect(info.free_bytes).result(20)
609
expect(user.get_quota()).result(info)
610
self.dal._get_user = lambda *a: user
613
result = self.dal.get_user_quota('user_id')
614
should = dict(max_storage_bytes=100, used_storage_bytes=80,
616
self.assertEqual(result, should)
618
def test_get_share(self):
623
share = mocker.mock()
624
expect(share.id).result('share_id')
625
expect(share.root_id).result('share_root_id')
626
expect(share.name).result('name')
627
expect(share.shared_by_id).result('shared_by_id')
628
expect(share.shared_to_id).result('shared_to_id')
629
expect(share.accepted).result(True)
630
expect(share.access).result(1)
634
self.dal._get_user = lambda *a: user
635
expect(user.get_share('share_id')).result(share)
638
result = self.dal.get_share('user_id', 'share_id')
639
should = dict(share_id='share_id', share_root_id='share_root_id',
640
name='name', shared_by_id='shared_by_id', accepted=True,
641
shared_to_id='shared_to_id', access=1)
642
self.assertEqual(result, should)
644
def test_get_root(self):
645
"""Get the root id for an user."""
650
expect(node.load()).result(node)
651
expect(node.id).result('root_id')
652
expect(node.generation).result(123)
656
self.dal._get_user = lambda *a: user
657
expect(user.root).result(node)
660
result = self.dal.get_root('user_id')
661
self.assertEqual(result, dict(root_id='root_id', generation=123))
663
def test_get_node_ok(self):
669
expect(node.id).result('node_id')
670
expect(node.path).result('path')
671
expect(node.name).result('name')
672
expect(node.vol_id).result('volume_id')
673
expect(node.parent_id).result('parent_id')
674
expect(node.status).result(model.STATUS_LIVE)
675
expect(node.generation).result('generation')
676
expect(node.is_public).result(False)
677
expect(node.content_hash).result('content_hash')
678
expect(node.kind).result('File')
679
expect(node.when_last_modified).result('last_modified')
680
content = mocker.mock()
681
expect(content.size).result('size')
682
expect(content.crc32).result('crc32')
683
expect(content.deflated_size).result('deflated_size')
684
expect(content.storage_key).result('storage_key')
685
expect(node.content).count(1).result(content)
689
self.dal._get_user = lambda *a: user
690
expect(user.volume('volume_id').get_node('node_id', with_content=True)
694
result = self.dal.get_node(user_id='user_id', node_id='node_id',
695
volume_id='volume_id')
697
should = dict(id='node_id', name='name', parent_id='parent_id',
698
is_public=False, is_live=True, is_file=True, size='size',
699
last_modified='last_modified', crc32='crc32',
700
generation='generation', content_hash='content_hash',
701
deflated_size='deflated_size', storage_key='storage_key',
702
volume_id='volume_id', path='path', has_content=True)
703
self.assertEqual(result, should)
705
def test_get_node_no_content(self):
706
"""Get a node that has no content."""
711
expect(node.id).result('node_id')
712
expect(node.path).result('path')
713
expect(node.name).result('name')
714
expect(node.vol_id).result('volume_id')
715
expect(node.parent_id).result('parent_id')
716
expect(node.status).result(model.STATUS_LIVE)
717
expect(node.generation).result('generation')
718
expect(node.is_public).result(False)
719
expect(node.content_hash).result('content_hash')
720
expect(node.kind).result('File')
721
expect(node.when_last_modified).result('last_modified')
722
expect(node.content).result(None)
726
self.dal._get_user = lambda *a: user
727
expect(user.volume('volume_id').get_node('node_id', with_content=True)
731
result = self.dal.get_node(user_id='user_id', node_id='node_id',
732
volume_id='volume_id')
734
should = dict(id='node_id', name='name', parent_id='parent_id',
735
is_public=False, is_live=True, is_file=True, size=None,
736
last_modified='last_modified', crc32=None,
737
generation='generation', content_hash='content_hash',
738
deflated_size=None, storage_key=None,
739
volume_id='volume_id', path="path", has_content=False)
740
self.assertEqual(result, should)
742
def test_get_node_from_user(self):
743
"""Get a node just giving the user."""
748
expect(node.id).result('node_id')
749
expect(node.path).result('path')
750
expect(node.name).result('name')
751
expect(node.vol_id).result('volume_id')
752
expect(node.parent_id).result('parent_id')
753
expect(node.status).result(model.STATUS_LIVE)
754
expect(node.generation).result('generation')
755
expect(node.is_public).result(False)
756
expect(node.content_hash).result('content_hash')
757
expect(node.kind).result('File')
758
expect(node.when_last_modified).result('last_modified')
759
expect(node.content).count(1).result(None)
763
self.dal._get_user = lambda *a: user
764
expect(user.shard_id).result('shard_id')
766
# patch the DAL to return the node
768
expect(fake('node_id', 'shard_id')).result(node)
769
self.patch(dal_backend.services, 'get_node_for_shard', fake)
772
result = self.dal.get_node_from_user(user_id='user_id',
775
should = dict(id='node_id', name='name', parent_id='parent_id',
776
is_public=False, is_live=True, is_file=True, size=None,
777
last_modified='last_modified', crc32=None,
778
generation='generation', content_hash='content_hash',
779
deflated_size=None, storage_key=None,
780
volume_id='volume_id', path='path', has_content=False)
781
self.assertEqual(result, should)
783
def test_get_delta_and_from_scratch(self):
784
"""Get normal delta and from scratch."""
788
node1 = mocker.mock()
789
expect(node1.id).count(2).result('node_id1')
790
expect(node1.path).count(2).result('path1')
791
expect(node1.name).count(2).result('name1')
792
expect(node1.vol_id).count(2).result('volume_id1')
793
expect(node1.generation).count(2).result('generation1')
794
expect(node1.is_public).count(2).result(True)
795
expect(node1.parent_id).count(2).result('parent_id1')
796
expect(node1.status).count(2).result(model.STATUS_LIVE)
797
expect(node1.content_hash).count(2).result('content_hash1')
798
expect(node1.kind).count(2).result('File')
799
expect(node1.when_last_modified).count(2).result('last_modified1')
800
content1 = mocker.mock()
801
expect(content1.size).count(2).result('size1')
802
expect(content1.crc32).count(2).result('crc321')
803
expect(content1.deflated_size).count(2).result('deflated_size1')
804
expect(content1.storage_key).count(2).result('storage_key1')
805
expect(node1.content).count(2).result(content1)
808
node2 = mocker.mock()
809
expect(node2.id).count(2).result('node_id2')
810
expect(node2.path).count(2).result('path2')
811
expect(node2.name).count(2).result('name2')
812
expect(node2.vol_id).count(2).result('volume_id2')
813
expect(node2.generation).count(2).result('generation2')
814
expect(node2.is_public).count(2).result(False)
815
expect(node2.parent_id).count(2).result('parent_id2')
816
expect(node2.status).count(2).result(model.STATUS_DEAD)
817
expect(node2.content_hash).count(2).result('content_hash2')
818
expect(node2.kind).count(2).result('Directory')
819
expect(node2.when_last_modified).count(2).result('last_modified2')
820
content2 = mocker.mock()
821
expect(content2.size).count(2).result('size2')
822
expect(content2.crc32).count(2).result('crc322')
823
expect(content2.deflated_size).count(2).result('deflated_size2')
824
expect(content2.storage_key).count(2).result('storage_key2')
825
expect(node2.content).count(2).result(content2)
829
self.dal._get_user = lambda *a: user
830
expect(user.volume('volume_id').get_delta('from_gen', limit='limit')
831
).result(('vol_generation', 'free_bytes', [node1, node2]))
832
expect(user.volume('volume_id').get_from_scratch(KWARGS)
833
).result(('vol_generation', 'free_bytes', [node1, node2]))
836
result1 = self.dal.get_delta(user_id='user_id',
837
volume_id='volume_id',
838
from_generation='from_gen',
840
result2 = self.dal.get_from_scratch(user_id='user_id',
841
volume_id='volume_id')
842
self.assertEqual(result1, result2)
843
self.assertEqual(result1['vol_generation'], 'vol_generation')
844
self.assertEqual(result1['free_bytes'], 'free_bytes')
845
node1, node2 = result1['nodes']
847
self.assertEqual(node1['id'], 'node_id1')
848
self.assertEqual(node1['path'], 'path1')
849
self.assertEqual(node1['name'], 'name1')
850
self.assertEqual(node1['volume_id'], 'volume_id1')
851
self.assertEqual(node1['parent_id'], 'parent_id1')
852
self.assertEqual(node1['is_live'], True)
853
self.assertEqual(node1['generation'], 'generation1')
854
self.assertEqual(node1['is_public'], True)
855
self.assertEqual(node1['content_hash'], 'content_hash1')
856
self.assertEqual(node1['is_file'], True)
857
self.assertEqual(node1['size'], 'size1')
858
self.assertEqual(node1['crc32'], 'crc321')
859
self.assertEqual(node1['deflated_size'], 'deflated_size1')
860
self.assertEqual(node1['storage_key'], 'storage_key1')
861
self.assertEqual(node1['last_modified'], 'last_modified1')
863
self.assertEqual(node2['id'], 'node_id2')
864
self.assertEqual(node2['path'], 'path2')
865
self.assertEqual(node2['name'], 'name2')
866
self.assertEqual(node2['volume_id'], 'volume_id2')
867
self.assertEqual(node2['parent_id'], 'parent_id2')
868
self.assertEqual(node2['is_live'], False)
869
self.assertEqual(node2['generation'], 'generation2')
870
self.assertEqual(node2['is_public'], False)
871
self.assertEqual(node2['content_hash'], 'content_hash2')
872
self.assertEqual(node2['is_file'], False)
873
self.assertEqual(node2['size'], 'size2')
874
self.assertEqual(node2['crc32'], 'crc322')
875
self.assertEqual(node2['deflated_size'], 'deflated_size2')
876
self.assertEqual(node2['storage_key'], 'storage_key2')
877
self.assertEqual(node2['last_modified'], 'last_modified2')
879
def test_get_user(self):
880
"""Get accessable nodes and their hashes."""
885
expect(user.root_volume_id).result('root_volume_id')
886
expect(user.username).result('username')
887
expect(user.visible_name).result('visible_name')
888
self.dal._get_user = lambda *a: user
891
result = self.dal.get_user_data(user_id='user_id',
892
session_id='session_id')
893
should = dict(root_volume_id='root_volume_id', username='username',
894
visible_name='visible_name')
895
self.assertEqual(result, should)
897
def test_get_volume_id_normal(self):
898
"""Get the volume_id, normal case."""
903
expect(node.volume_id).result('volume_id')
907
expect(user.root_volume_id).result('root_volume_id')
908
expect(user.volume().get_node('node_id')).result(node)
909
self.dal._get_user = lambda *a: user
912
result = self.dal.get_volume_id(user_id='user_id',
914
self.assertEqual(result, dict(volume_id='volume_id'))
916
def test_get_volume_id_same_root(self):
917
"""Get the volume_id, special case where the subtree node is root."""
922
expect(node.volume_id).result('root_volume_id')
926
expect(user.root_volume_id).result('root_volume_id')
927
expect(user.volume().get_node('node_id')).result(node)
928
self.dal._get_user = lambda *a: user
931
result = self.dal.get_volume_id(user_id='user_id',
933
self.assertEqual(result, dict(volume_id=None))
935
def test_make_content(self):
941
expect(node.generation).result('new_generation')
942
expect(node.make_content('original_hash', 'hash_hint', 'crc32_hint',
943
'inflated_size_hint', 'deflated_size_hint',
944
'storage_key', 'magic_hash'))
948
expect(user.volume('volume_id').get_node('node_id')).result(node)
949
self.dal._get_user = lambda *a: user
952
d = dict(user_id='user_id', volume_id='volume_id',
953
node_id='node_id', original_hash='original_hash',
954
hash_hint='hash_hint', crc32_hint='crc32_hint',
955
inflated_size_hint='inflated_size_hint',
956
deflated_size_hint='deflated_size_hint',
957
storage_key='storage_key', magic_hash='magic_hash',
959
result = self.dal.make_content(**d)
960
self.assertEqual(result, dict(generation='new_generation'))
962
def test_get_upload_job(self):
963
"""Get an upload_job."""
968
expect(uj.id).result('uj_id')
969
expect(uj.uploaded_bytes).result('uploaded_bytes')
970
expect(uj.multipart_id).result('multipart_id')
971
expect(uj.multipart_key).result('multipart_key')
972
expect(uj.chunk_count).result('chunk_count')
973
expect(uj.hash_context).result('hash_context')
974
expect(uj.magic_hash_context).result('magic_hash_context')
975
expect(uj.decompress_context).result('decompress_context')
976
expect(uj.inflated_size).result('inflated_size')
977
expect(uj.crc32).result('crc32')
978
expect(uj.when_last_active).result('when_last_active')
982
self.dal._get_user = lambda *a: user
983
expect(user.volume('volume_id').get_node('node_id')
984
.get_multipart_uploadjob('uploadjob_id', 'hash_value', 'crc32',
985
'inflated_size', 'deflated_size')
989
d = dict(user_id='user_id', volume_id='volume_id',
990
node_id='node_id', uploadjob_id='uploadjob_id',
991
hash_value='hash_value', crc32='crc32',
992
inflated_size='inflated_size',
993
deflated_size='deflated_size')
994
result = self.dal.get_uploadjob(**d)
996
should = dict(uploadjob_id='uj_id', uploaded_bytes='uploaded_bytes',
997
multipart_id='multipart_id',
998
multipart_key='multipart_key', chunk_count='chunk_count',
999
hash_context='hash_context',
1000
magic_hash_context='magic_hash_context',
1001
decompress_context='decompress_context',
1002
inflated_size='inflated_size', crc32='crc32',
1003
when_last_active='when_last_active')
1004
self.assertEqual(result, should)
1006
def test_make_upload_job(self):
1007
"""Make an upload_job."""
1012
expect(uj.id).result('uj_id')
1013
expect(uj.uploaded_bytes).result('uploaded_bytes')
1014
expect(uj.multipart_id).result('multipart_id')
1015
expect(uj.multipart_key).result('multipart_key')
1016
expect(uj.chunk_count).result('chunk_count')
1017
expect(uj.hash_context).result('hash_context')
1018
expect(uj.magic_hash_context).result('magic_hash_context')
1019
expect(uj.decompress_context).result('decompress_context')
1020
expect(uj.inflated_size).result('inflated_size')
1021
expect(uj.crc32).result('crc32')
1022
expect(uj.when_last_active).result('when_last_active')
1025
user = mocker.mock()
1026
self.dal._get_user = lambda *a: user
1027
expect(user.volume('volume_id').get_node('node_id')
1028
.make_uploadjob('previous_hash', 'hash_value', 'crc32',
1029
'inflated_size', 'deflated_size',
1030
multipart_key='multipart_key')
1034
d = dict(user_id='user_id', volume_id='volume_id',
1035
node_id='node_id', previous_hash='previous_hash',
1036
hash_value='hash_value', crc32='crc32',
1037
inflated_size='inflated_size',
1038
deflated_size='deflated_size',
1039
multipart_key='multipart_key')
1040
result = self.dal.make_uploadjob(**d)
1042
should = dict(uploadjob_id='uj_id', uploaded_bytes='uploaded_bytes',
1043
multipart_id='multipart_id',
1044
multipart_key='multipart_key', chunk_count='chunk_count',
1045
hash_context='hash_context',
1046
magic_hash_context='magic_hash_context',
1047
decompress_context='decompress_context',
1048
inflated_size='inflated_size', crc32='crc32',
1049
when_last_active='when_last_active')
1050
self.assertEqual(result, should)
1052
def test_set_uploadjob_multipart_id(self):
1053
"""Set the multipart id to an uploadjob."""
1058
expect(uj.set_multipart_id('multipart_id'))
1061
user = mocker.mock()
1062
self.dal._get_user = lambda *a: user
1063
expect(user.get_uploadjob('uploadjob_id')).result(uj)
1066
d = dict(user_id='user_id', uploadjob_id='uploadjob_id',
1067
multipart_id='multipart_id')
1068
result = self.dal.set_uploadjob_multipart_id(**d)
1070
self.assertEqual(result, {})
1072
def test_delete_uploadjob(self):
1073
"""Delete an uploadjob."""
1081
user = mocker.mock()
1082
self.dal._get_user = lambda *a: user
1083
expect(user.get_uploadjob('uploadjob_id')).result(uj)
1086
d = dict(user_id='user_id', uploadjob_id='uploadjob_id')
1087
result = self.dal.delete_uploadjob(**d)
1089
self.assertEqual(result, {})
1091
def test_add_part_to_uploadjob(self):
1092
"""Delete an uploadjob."""
1097
expect(uj.add_part('chunk_size', 'inflated_size', 'crc32',
1098
'hash_context', 'magic_hash_context',
1099
'decompress_context'))
1102
user = mocker.mock()
1103
self.dal._get_user = lambda *a: user
1104
expect(user.get_uploadjob('uploadjob_id')).result(uj)
1107
d = dict(user_id='user_id', uploadjob_id='uploadjob_id',
1108
chunk_size='chunk_size', inflated_size='inflated_size',
1109
crc32='crc32', hash_context='hash_context',
1110
magic_hash_context='magic_hash_context',
1111
decompress_context='decompress_context')
1112
result = self.dal.add_part_to_uploadjob(**d)
1114
self.assertEqual(result, {})
1116
def test_touch_uploadjob(self):
1117
"""Delete an uploadjob."""
1123
expect(uj.when_last_active).result('when_last_active')
1126
user = mocker.mock()
1127
self.dal._get_user = lambda *a: user
1128
expect(user.get_uploadjob('uploadjob_id')).result(uj)
1131
d = dict(user_id='user_id', uploadjob_id='uploadjob_id')
1132
result = self.dal.touch_uploadjob(**d)
1134
self.assertEqual(result, dict(when_last_active='when_last_active'))
1136
def test_get_reusable_content(self):
1137
"""Get reusable content."""
1141
user = mocker.mock()
1142
self.dal._get_user = lambda *a: user
1143
expect(user.is_reusable_content('hash_value', 'magic_hash')
1144
).result(('blob_exists', 'storage_key'))
1147
result = self.dal.get_reusable_content(user_id='user_id',
1148
hash_value='hash_value',
1149
magic_hash='magic_hash')
1151
should = dict(blob_exists='blob_exists', storage_key='storage_key')
1152
self.assertEqual(result, should)