~diegosarmentero/ubuntuone-control-panel/tab-shares-functions

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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
# -*- coding: utf-8 -*-
#
# Copyright 2010-2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Tests for the control panel backend."""

import operator
import os

from collections import defaultdict

import simplejson

from twisted.internet import defer
from twisted.internet.defer import inlineCallbacks, returnValue
from ubuntuone.devtools.handlers import MementoHandler

from ubuntuone.controlpanel import backend, replication_client
from ubuntuone.controlpanel.backend import (
    ACCOUNT_API, DEVICES_API, DEVICE_REMOVE_API, QUOTA_API,
    DEVICE_TYPE_COMPUTER,
    FILE_SYNC_DISABLED,
    FILE_SYNC_DISCONNECTED,
    FILE_SYNC_ERROR,
    FILE_SYNC_IDLE,
    FILE_SYNC_STARTING,
    FILE_SYNC_STOPPED,
    FILE_SYNC_SYNCING,
    FILE_SYNC_UNKNOWN,
    MSG_KEY, STATUS_KEY,
    UBUNTUONE_FROM_OAUTH,
    UBUNTUONE_LINK,
)
from ubuntuone.controlpanel.tests import (TestCase,
    EMPTY_DESCRIPTION_JSON,
    EXPECTED_ACCOUNT_INFO,
    EXPECTED_ACCOUNT_INFO_WITH_CURRENT_PLAN,
    EXPECTED_DEVICE_NAMES_INFO,
    EXPECTED_DEVICES_INFO,
    LOCAL_DEVICE,
    ROOT_PATH,
    SAMPLE_ACCOUNT_NO_CURRENT_PLAN,
    SAMPLE_ACCOUNT_WITH_CURRENT_PLAN,
    SAMPLE_DEVICES_JSON,
    SAMPLE_FOLDERS,
    SAMPLE_QUOTA_JSON,
    SAMPLE_SHARED,
    SAMPLE_SHARES,
    SHARES_PATH,
    SHARES_PATH_LINK,
    TOKEN,
    USER_HOME,
)

SAMPLE_SIGNED = UBUNTUONE_FROM_OAUTH + '?oauth_nonce=' \
    '36886134&oauth_timestamp=1310671062&oauth_consumer_key=consumer_key&' \
    'oauth_signature_method=HMAC-SHA1&next=%2Fblah&oauth_version=1.0&' \
    'oauth_token=GkInOfSMGwTXAUoVQwLUoPxElEEUdhsLVNTPhxHJDUIeHCPNEo&' \
    'oauth_signature=s6h0LRBiWchTADrTJWaJUSuaGpo%3D'


# pylint: disable=E1101, W0201, W0212


class CallRecorder(object):
    """A class that records every call clients made to it."""

    def __init__(self):
        self._called = defaultdict(int)

    def __getattribute__(self, attr_name):
        """Override to we can record calls to members."""
        result = super(CallRecorder, self).__getattribute__(attr_name)
        if attr_name != '_called':
            called = super(CallRecorder, self).__getattribute__('_called')
            called[attr_name] += 1
        return result


class MockWebClient(CallRecorder):
    """A mock webclient."""

    def __init__(self, get_credentials):
        """Initialize this mock instance."""
        super(MockWebClient, self).__init__()
        self.get_credentials = get_credentials
        self.failure = False
        self.results = {}

    def call_api(self, method):
        """Get a given url from the webservice."""
        if self.failure == 401:
            return defer.fail(backend.UnauthorizedError(self.failure))
        elif self.failure:
            return defer.fail(backend.WebClientError(self.failure))
        else:
            result = simplejson.loads(self.results[method])
            return defer.succeed(result)

    @defer.inlineCallbacks
    def build_signed_iri(self, iri, params):
        """Fake the IRI signing."""
        creds = yield self.get_credentials()
        result = u'%s-%s-%s' % (iri, unicode(creds), unicode(params))
        defer.returnValue(result)


class MockLoginClient(CallRecorder):
    """A mock login_client module."""

    def __init__(self):
        """Initialize this mock instance."""
        super(MockLoginClient, self).__init__()
        self.kwargs = None
        self.creds = TOKEN

    def find_credentials(self):
        """Return the mock credentials."""
        return defer.succeed(self.creds)

    def clear_credentials(self):
        """Clear the mock credentials."""
        self.creds = {}
        return defer.succeed(None)

    def login_email_password(self, **kwargs):
        """Simulate a login."""
        self.kwargs = kwargs
        self.creds = TOKEN
        return defer.succeed(self.creds)


class MockSDClient(CallRecorder):
    """A mock sd_client module."""

    def __init__(self):
        """Initialize this mock instance."""
        super(MockSDClient, self).__init__()
        self.throttling = False
        self.show_all_notifications = True
        self.autoconnect = True
        self.udf_autosubscribe = False
        self.share_autosubscribe = False
        self.limits = {"download": -1, "upload": -1}
        self.file_sync = True
        self.status = {
            'name': 'TEST', 'queues': 'GORGEOUS', 'connection': '',
            'description': 'Some test state, nothing else.',
            'is_error': '', 'is_connected': 'True', 'is_online': '',
        }
        self.status_changed_handler = None
        self.public_files_list_handler = None
        self.public_access_changed_handler = None
        self.subscribed_folders = []
        self.subscribed_shares = []
        self.actions = []
        self.shares = []
        self.folders = []
        self.volumes_refreshed = False
        self.menu_data = {'recent-transfers': (), 'uploading': ()}
        self.publicfiles = [
            {'path': '/home/file1', 'public_url': 'http:ubuntuone.com/asd123'},
            {'path': '/home/file2', 'public_url': 'http:ubuntuone.com/qwe456'},
        ]
        self.public_access_info = {
            'public_url': 'http:ubuntuone.com/zxc789'
        }

    def get_throttling_limits(self):
        """Return the sample speed limits."""
        return self.limits

    def set_throttling_limits(self, limits):
        """Set the sample speed limits."""
        self.limits["download"] = int(limits["download"])
        self.limits["upload"] = int(limits["upload"])

    def bandwidth_throttling_enabled(self):
        """Return the state of throttling."""
        return self.throttling

    def enable_bandwidth_throttling(self):
        """Enable bw throttling."""
        self.throttling = True

    def disable_bandwidth_throttling(self):
        """Disable bw throttling."""
        self.throttling = False

    def show_all_notifications_enabled(self):
        """Return the state of show_all_notifications."""
        return self.show_all_notifications

    def enable_show_all_notifications(self):
        """Enable show_all_notifications."""
        self.show_all_notifications = True

    def disable_show_all_notifications(self):
        """Disable show_all_notifications."""
        self.show_all_notifications = False

    def autoconnect_enabled(self):
        """Return the state of autoconnect."""
        return self.autoconnect

    def enable_autoconnect(self):
        """Enable autoconnect."""
        self.autoconnect = True

    def disable_autoconnect(self):
        """Disable autoconnect."""
        self.autoconnect = False

    def udf_autosubscribe_enabled(self):
        """Return the state of udf_autosubscribe."""
        return self.udf_autosubscribe

    def enable_udf_autosubscribe(self):
        """Enable udf_autosubscribe."""
        self.udf_autosubscribe = True

    def disable_udf_autosubscribe(self):
        """Disable udf_autosubscribe."""
        self.udf_autosubscribe = False

    def share_autosubscribe_enabled(self):
        """Return the state of share_autosubscribe."""
        return self.share_autosubscribe

    def enable_share_autosubscribe(self):
        """Enable share_autosubscribe."""
        self.share_autosubscribe = True

    def disable_share_autosubscribe(self):
        """Disable share_autosubscribe."""
        self.share_autosubscribe = False

    def files_sync_enabled(self):
        """Get if file sync service is enabled."""
        return self.file_sync

    def set_files_sync_enabled(self, enabled):
        """Set the file sync service to be 'enabled'."""
        self.file_sync = enabled

    def connect_file_sync(self):
        """Connect files service."""
        self.actions.append('connect')

    def disconnect_file_sync(self):
        """Disconnect file_sync service."""
        self.actions.append('disconnect')

    def start_file_sync(self):
        """Start the file_sync service."""
        self.actions.append('start')

    def stop_file_sync(self):
        """Stop the file_sync service."""
        self.actions.append('stop')

    def get_home_dir(self):
        """Grab the home dir."""
        return USER_HOME

    def get_root_dir(self):
        """Grab the root dir."""
        return ROOT_PATH

    def get_shares_dir(self):
        """Grab the shares dir."""
        return SHARES_PATH

    def get_shares_dir_link(self):
        """Grab the shares dir."""
        return SHARES_PATH_LINK

    def get_folders(self):
        """Grab list of folders."""
        return self.folders

    def subscribe_folder(self, volume_id):
        """Subcribe to 'volume_id'."""
        self.subscribed_folders.append(volume_id)

    def unsubscribe_folder(self, volume_id):
        """Unsubcribe from 'volume_id'."""
        self.subscribed_folders.remove(volume_id)

    def validate_path(self, path):
        """Validate a path for folder creation."""
        return path != USER_HOME

    def create_folder(self, path):
        """Grab list of folders."""
        self.folders.append(path)

    def get_shares(self):
        """Grab list of shares."""
        return self.shares

    def subscribe_share(self, volume_id):
        """Subcribe to 'volume_id'."""
        self.subscribed_shares.append(volume_id)

    def unsubscribe_share(self, volume_id):
        """Unsubcribe from 'volume_id'."""
        self.subscribed_shares.remove(volume_id)

    def get_current_status(self):
        """Grab syncdaemon status."""
        return self.status

    def set_status_changed_handler(self, handler):
        """Connect a handler for tracking syncdaemon status changes."""
        self.status_changed_handler = handler

    def get_shared(self):
        """Grab list of shared (shares from the user to others)."""
        return SAMPLE_SHARED

    def refresh_volumes(self):
        """Refresh the volume list."""
        self.volumes_refreshed = True
        return defer.succeed(None)

    def sync_menu(self):
        """Return the sync menu data."""
        return self.menu_data

    def get_public_files(self):
        """Trigger the action to get the public files."""
        if self.public_files_list_handler is not None:
            self.public_files_list_handler(self.publicfiles)

    def set_public_files_list_handler(self, handler):
        """Return the handler to be called for the public files list."""
        self.public_files_list_handler = handler

    def change_public_access(self, path, is_public):
        """Change the type access of a file."""
        if self.public_access_changed_handler is not None and is_public:
            self.public_access_changed_handler(self.public_access_info)

    def set_public_access_changed_handler(self, handler):
        """Return the handler to be called when a access type change."""
        self.public_access_changed_handler = handler


class MockReplicationClient(CallRecorder):
    """A mock replication_client module."""

    CONTACTS = 'legendary'

    replications = set([CONTACTS, 'other'])
    exclusions = set([CONTACTS])

    def get_replications(self):
        """Grab the list of replications in this machine."""
        return MockReplicationClient.replications

    def get_exclusions(self):
        """Grab the list of exclusions in this machine."""
        return MockReplicationClient.exclusions

    def replicate(self, replication_id):
        """Remove replication_id from the exclusions list."""
        if replication_id not in MockReplicationClient.replications:
            raise replication_client.ReplicationError(replication_id)
        MockReplicationClient.exclusions.remove(replication_id)

    def exclude(self, replication_id):
        """Add replication_id to the exclusions list."""
        if replication_id not in MockReplicationClient.replications:
            raise replication_client.ReplicationError(replication_id)
        MockReplicationClient.exclusions.add(replication_id)


def fail(*args, **kwargs):
    """Helper to raise any error."""
    raise ValueError(args)


class BackendBasicTestCase(TestCase):
    """Simple tests for the backend."""

    timeout = 3

    @defer.inlineCallbacks
    def setUp(self):
        yield super(BackendBasicTestCase, self).setUp()
        self.patch(backend, "WebClient", MockWebClient)
        self.patch(backend, "CredentialsManagementTool", MockLoginClient)
        self.patch(backend.sd_client, "SyncDaemonClient", MockSDClient)
        self.patch(backend, "replication_client", MockReplicationClient())

        self.local_token = DEVICE_TYPE_COMPUTER + TOKEN["token"]
        self.be = backend.ControlBackend()
        self.be.wc.failure = False

        self.memento = MementoHandler()
        backend.logger.addHandler(self.memento)

    def test_backend_creation(self):
        """The backend instance is successfully created."""
        self.assertIsInstance(self.be.wc, MockWebClient)
        self.assertIsInstance(self.be.login_client, MockLoginClient)
        self.assertIsInstance(self.be.sd_client, MockSDClient)

    @inlineCallbacks
    def test_get_token(self):
        """The get_token method returns the right token."""
        self.patch(self.be, 'get_credentials', lambda: defer.succeed(TOKEN))
        token = yield self.be.get_token()
        self.assertEqual(token, TOKEN["token"])

    @inlineCallbacks
    def test_device_is_local(self):
        """The device_is_local returns the right result."""
        self.patch(self.be, 'get_credentials', lambda: defer.succeed(TOKEN))
        result = yield self.be.device_is_local(self.local_token)
        self.assertTrue(result)

        did = EXPECTED_DEVICES_INFO[-1]['device_id']
        result = yield self.be.device_is_local(did)
        self.assertFalse(result)

    def test_shutdown_func(self):
        """A shutdown_func can be passed as creation parameter."""
        f = lambda: None
        be = backend.ControlBackend(shutdown_func=f)
        self.assertEqual(be.shutdown_func, f)

    def test_shutdown_func_is_called_on_shutdown(self):
        """The shutdown_func is called on shutdown."""
        self.be.shutdown_func = self._set_called
        self.be.shutdown()
        self.assertEqual(self._called, ((), {}))

    def test_shutdown_func_when_none(self):
        """The shutdown_func can be None."""
        self.be.shutdown_func = None
        self.be.shutdown()
        # nothing explodes

    def test_login_client_is_cached(self):
        """The login_client instance is cached."""
        self.assertIs(self.be.login_client, self.be.login_client)

    def test_sd_client_is_cached(self):
        """The sd_client instance is cached."""
        self.assertIs(self.be.sd_client, self.be.sd_client)


class SignIriTestCase(BackendBasicTestCase):
    """Test cases for the IRI signing function."""

    @defer.inlineCallbacks
    def setUp(self):
        yield super(SignIriTestCase, self).setUp()
        self.patch(self.be, 'get_credentials', lambda: defer.succeed(TOKEN))

    @inlineCallbacks
    def test_without_ubuntuone_prefix(self):
        """If given url is not an ubuntuone url, don't sign it."""
        iri = 'bad_prefix' + UBUNTUONE_LINK
        result = yield self.be.build_signed_iri(iri)

        self.assertEqual(result, iri)

    @inlineCallbacks
    def test_with_ubuntuone_prefix(self):
        """If given url is an ubuntuone url, sign it."""
        iri = UBUNTUONE_LINK + 'foo'
        result = yield self.be.build_signed_iri(iri)

        expected = yield self.be.wc.build_signed_iri(UBUNTUONE_FROM_OAUTH,
                                                     {'next': iri})
        self.assertEqual(expected, result)


class SignIriNoCredsTestCase(SignIriTestCase):
    """The test suite for the sign url management when there are no creds."""

    @defer.inlineCallbacks
    def setUp(self):
        yield super(SignIriNoCredsTestCase, self).setUp()
        self.patch(self.be, 'get_credentials', lambda: defer.succeed({}))

    @inlineCallbacks
    def test_with_ubuntuone_prefix(self):
        """If given url is an ubuntuone url, don't sign it.

        Since we have no credentials, the url should not be signed.

        """
        iri = UBUNTUONE_LINK + 'foo'
        result = yield self.be.build_signed_iri(iri)

        self.assertEqual(result, iri)


class BackendCredentialsTestCase(BackendBasicTestCase):
    """Credentials tests for the backend."""

    @inlineCallbacks
    def test_credentials_are_cached(self):
        """The credentials are cached."""
        creds1 = yield self.be.get_credentials()
        creds2 = yield self.be.get_credentials()
        self.assertEqual(creds1, creds2)
        self.assertEqual(self.be.login_client._called['find_credentials'], 1)

    @inlineCallbacks
    def test_clear_credentials(self):
        """The credentials can be cleared."""
        # ensure we have creds
        result = yield self.be.login(email='yadda', password='doo')
        assert self.be.login_client.creds == result

        yield self.be.clear_credentials()
        self.assertEqual(self.be.login_client.creds, {})

    @inlineCallbacks
    def test_clear_credentials_invalidates_cached_credentials(self):
        """The cached credentials are cleared."""
        # ensure we have creds
        result = yield self.be.login(email='yadda', password='doo')
        assert self.be.login_client.creds == result

        yield self.be.clear_credentials()

        creds = yield self.be.get_credentials()
        self.assertEqual(creds, {})

    @inlineCallbacks
    def test_login(self):
        """Login can be called."""
        kwargs = dict(email='yadda', password='doo')
        result = yield self.be.login(**kwargs)

        self.assertEqual(self.be.login_client.kwargs, kwargs)
        self.assertEqual(result, TOKEN)

    @inlineCallbacks
    def test_login_caches_credentials(self):
        """Login catches credentials when available."""
        yield self.be.clear_credentials()

        result = yield self.be.login(email='yadda', password='doo')
        creds = yield self.be.get_credentials()

        self.assertEqual(result, TOKEN)
        self.assertEqual(result, creds)
        self.assertEqual(self.be.login_client._called['find_credentials'], 0)

    @inlineCallbacks
    def test_login_fails(self):
        """Login failure is emitted."""
        self.patch(self.be.login_client, 'login_email_password', fail)
        yield self.assertFailure(self.be.login('foo', 'bar'), ValueError)


class BackendAccountTestCase(BackendBasicTestCase):
    """Account tests for the backend."""

    @inlineCallbacks
    def test_account_info(self):
        """The account_info method exercises its callback."""
        self.be.wc.results[ACCOUNT_API] = SAMPLE_ACCOUNT_NO_CURRENT_PLAN
        self.be.wc.results[QUOTA_API] = SAMPLE_QUOTA_JSON
        result = yield self.be.account_info()
        self.assertEqual(result, EXPECTED_ACCOUNT_INFO)

    @inlineCallbacks
    def test_account_info_with_current_plan(self):
        """The account_info method exercises its callback."""
        self.be.wc.results[ACCOUNT_API] = SAMPLE_ACCOUNT_WITH_CURRENT_PLAN
        self.be.wc.results[QUOTA_API] = SAMPLE_QUOTA_JSON
        result = yield self.be.account_info()
        self.assertEqual(result, EXPECTED_ACCOUNT_INFO_WITH_CURRENT_PLAN)

    @inlineCallbacks
    def test_account_info_fails(self):
        """The account_info method exercises its errback."""
        self.be.wc.failure = 404
        yield self.assertFailure(self.be.account_info(),
                                 backend.WebClientError)

    @inlineCallbacks
    def test_account_info_fails_with_unauthorized(self):
        """The account_info clears the credentials on unauthorized."""
        self.be.wc.failure = 401
        d = defer.Deferred()
        self.patch(self.be, 'clear_credentials', lambda: d.callback('called'))
        yield self.assertFailure(self.be.account_info(),
                                 backend.UnauthorizedError)
        yield d


class BackendDevicesTestCase(BackendBasicTestCase):
    """Devices tests for the backend."""

    @inlineCallbacks
    def test_devices_info(self):
        """The devices_info method exercises its callback."""
        self.be.wc.results[DEVICES_API] = SAMPLE_DEVICES_JSON
        result = yield self.be.devices_info()
        self.assertEqual(result, EXPECTED_DEVICES_INFO)

    @inlineCallbacks
    def test_devices_info_fails(self):
        """The devices_info method exercises its errback."""
        self.patch(self.be.wc, 'call_api', fail)
        yield self.assertFailure(self.be.devices_info(), ValueError)

    @inlineCallbacks
    def test_devices_info_with_webclient_error(self):
        """The devices_info returns local info if webclient error."""
        self.be.wc.failure = 404
        result = yield self.be.devices_info()

        self.assertEqual(result, [LOCAL_DEVICE])
        self.assertTrue(self.memento.check_error('devices_info',
                                                 'web client failure'))

    @inlineCallbacks
    def test_devices_info_fails_with_unauthorized(self):
        """The devices_info clears the credentials on unauthorized."""
        self.be.wc.failure = 401
        d = defer.Deferred()
        self.patch(self.be, 'clear_credentials', lambda: d.callback('called'))
        yield self.assertFailure(self.be.devices_info(),
                                 backend.UnauthorizedError)
        yield d

    @inlineCallbacks
    def test_devices_info_if_files_disabled(self):
        """The devices_info returns device only info if files is disabled."""
        yield self.be.disable_files()
        status = yield self.be.file_sync_status()
        assert status['status'] == backend.FILE_SYNC_DISABLED

        self.be.wc.results[DEVICES_API] = SAMPLE_DEVICES_JSON
        result = yield self.be.devices_info()

        expected = []
        for device in EXPECTED_DEVICES_INFO:
            device = device.copy()
            device.pop('limit_bandwidth', None)
            device.pop(backend.DOWNLOAD_KEY, None)
            device.pop(backend.UPLOAD_KEY, None)
            device.pop(backend.AUTOCONNECT_KEY, None)
            device.pop(backend.SHOW_ALL_NOTIFICATIONS_KEY, None)
            device.pop(backend.SHARE_AUTOSUBSCRIBE_KEY, None)
            device.pop(backend.UDF_AUTOSUBSCRIBE_KEY, None)
            device['configurable'] = False
            expected.append(device)
        self.assertEqual(result, expected)

    @inlineCallbacks
    def test_devices_info_when_token_name_is_empty(self):
        """The devices_info can handle empty token names."""
        self.be.wc.results[DEVICES_API] = EMPTY_DESCRIPTION_JSON
        result = yield self.be.devices_info()
        expected = {'configurable': False,
                    'device_id': 'ComputerABCDEF01234token',
                    'is_local': False, 'name': self.be.NAME_NOT_SET,
                    'type': DEVICE_TYPE_COMPUTER}
        self.assertEqual(result, [expected])

    @inlineCallbacks
    def test_devices_info_does_not_log_device_id(self):
        """The devices_info does not log the device_id."""
        self.be.wc.results[DEVICES_API] = SAMPLE_DEVICES_JSON
        yield self.be.devices_info()

        dids = (d['device_id'] for d in EXPECTED_DEVICES_INFO)
        device_id_logged = all(all(did not in r.getMessage()
                                   for r in self.memento.records)
                               for did in dids)
        self.assertTrue(device_id_logged)

    @inlineCallbacks
    def test_device_names_info(self):
        """The device_names_info returns the correct info."""
        self.be.wc.results[DEVICES_API] = SAMPLE_DEVICES_JSON
        result = yield self.be.device_names_info()
        self.assertEqual(result, EXPECTED_DEVICE_NAMES_INFO)

    @inlineCallbacks
    def test_device_names_info_fails(self):
        """The device_names_info method exercises its errback."""
        self.patch(self.be.wc, 'call_api', fail)
        yield self.assertFailure(self.be.device_names_info(), ValueError)

    @inlineCallbacks
    def test_device_names_info_with_webclient_error(self):
        """The device_names_info returns local info if webclient error."""
        self.be.wc.failure = 404
        result = yield self.be.device_names_info()

        device = LOCAL_DEVICE.copy()
        device.pop('configurable', None)
        device.pop('limit_bandwidth', None)
        device.pop(backend.DOWNLOAD_KEY, None)
        device.pop(backend.UPLOAD_KEY, None)
        device.pop(backend.AUTOCONNECT_KEY, None)
        device.pop(backend.SHOW_ALL_NOTIFICATIONS_KEY, None)
        device.pop(backend.SHARE_AUTOSUBSCRIBE_KEY, None)
        device.pop(backend.UDF_AUTOSUBSCRIBE_KEY, None)
        self.assertEqual(result, [device])
        self.assertTrue(self.memento.check_error('device_names_info',
                                                 'web client failure'))

    @inlineCallbacks
    def test_device_names_info_fails_with_unauthorized(self):
        """The device_names_info clears the credentials on unauthorized."""
        self.be.wc.failure = 401
        d = defer.Deferred()
        self.patch(self.be, 'clear_credentials', lambda: d.callback('called'))
        yield self.assertFailure(self.be.device_names_info(),
                                 backend.UnauthorizedError)
        yield d

    @inlineCallbacks
    def test_device_names_info_when_token_name_is_empty(self):
        """The device_names_info can handle empty token names."""
        self.be.wc.results[DEVICES_API] = EMPTY_DESCRIPTION_JSON
        result = yield self.be.device_names_info()
        expected = {'device_id': 'ComputerABCDEF01234token',
                    'is_local': False, 'name': self.be.NAME_NOT_SET,
                    'type': DEVICE_TYPE_COMPUTER}
        self.assertEqual(result, [expected])

    @inlineCallbacks
    def test_device_names_info_does_not_log_device_id(self):
        """The device_names_info does not log the device_id."""
        self.be.wc.results[DEVICES_API] = SAMPLE_DEVICES_JSON
        yield self.be.device_names_info()

        dids = (d['device_id'] for d in EXPECTED_DEVICES_INFO)
        device_id_logged = all(all(did not in r.getMessage()
                                   for r in self.memento.records)
                               for did in dids)
        self.assertTrue(device_id_logged)

    @inlineCallbacks
    def test_remove_device(self):
        """The remove_device method calls the right api."""
        self.patch(self.be, 'clear_credentials', self._set_called)

        dtype, did = DEVICE_TYPE_COMPUTER, "SAMPLE-TOKEN"
        device_id = dtype + did
        apiurl = DEVICE_REMOVE_API % (dtype.lower(), did)
        self.be.wc.results[apiurl] = SAMPLE_DEVICES_JSON
        result = yield self.be.remove_device(device_id)
        self.assertEqual(result, device_id)

        # credentials were not cleared
        self.assertFalse(self._called)

    @inlineCallbacks
    def test_remove_device_clear_credentials_if_local_device(self):
        """The remove_device method clears the credentials if is local."""
        self.patch(self.be, 'clear_credentials', self._set_called)

        apiurl = DEVICE_REMOVE_API % ('computer', TOKEN['token'])
        self.be.wc.results[apiurl] = SAMPLE_DEVICES_JSON
        yield self.be.remove_device(self.local_token)

        # credentials were cleared
        self.assertEqual(self._called, ((), {}))

    @inlineCallbacks
    def test_remove_device_fails(self):
        """The remove_device method fails as expected."""
        self.be.wc.failure = 404
        yield self.assertFailure(self.be.remove_device(self.local_token),
                                 backend.WebClientError)

    @inlineCallbacks
    def test_remove_device_fails_with_unauthorized(self):
        """The remove_device clears the credentials on unauthorized."""
        self.be.wc.failure = 401
        d = defer.Deferred()
        self.patch(self.be, 'clear_credentials', lambda: d.callback('called'))
        yield self.assertFailure(self.be.remove_device(self.local_token),
                                 backend.UnauthorizedError)
        yield d

    @inlineCallbacks
    def test_remove_device_does_not_log_device_id(self):
        """The remove_device does not log the device_id."""
        device_id = DEVICE_TYPE_COMPUTER + TOKEN['token']
        apiurl = DEVICE_REMOVE_API % ('computer', TOKEN['token'])
        self.be.wc.results[apiurl] = SAMPLE_DEVICES_JSON
        yield self.be.remove_device(device_id)

        device_id_logged = all(device_id not in r.getMessage()
                               for r in self.memento.records)
        self.assertTrue(device_id_logged)

    @inlineCallbacks
    def test_change_show_all_notifications(self):
        """The device settings are updated."""
        self.be.sd_client.show_all_notifications = False
        yield self.be.change_device_settings(self.local_token,
                                    {backend.SHOW_ALL_NOTIFICATIONS_KEY: True})
        self.assertEqual(self.be.sd_client.show_all_notifications, True)
        yield self.be.change_device_settings(self.local_token,
                                   {backend.SHOW_ALL_NOTIFICATIONS_KEY: False})
        self.assertEqual(self.be.sd_client.show_all_notifications, False)

    @inlineCallbacks
    def test_change_limit_bandwidth(self):
        """The device settings are updated."""
        self.be.sd_client.throttling = False
        yield self.be.change_device_settings(self.local_token,
                                        {backend.LIMIT_BW_KEY: True})
        self.assertEqual(self.be.sd_client.throttling, True)
        yield self.be.change_device_settings(self.local_token,
                                        {backend.LIMIT_BW_KEY: False})
        self.assertEqual(self.be.sd_client.throttling, False)

    @inlineCallbacks
    def test_change_upload_speed_limit(self):
        """The device settings are updated."""
        self.be.sd_client.limits = {"download": -1, "upload": -1}
        yield self.be.change_device_settings(self.local_token,
                                        {backend.UPLOAD_KEY: 1111})
        self.assertEqual(self.be.sd_client.limits["upload"], 1111)
        self.assertEqual(self.be.sd_client.limits["download"], -1)

    @inlineCallbacks
    def test_change_download_speed_limit(self):
        """The device settings are updated."""
        self.be.sd_client.limits = {"download": -1, "upload": -1}
        yield self.be.change_device_settings(self.local_token,
                                        {backend.DOWNLOAD_KEY: 99})
        self.assertEqual(self.be.sd_client.limits["upload"], -1)
        self.assertEqual(self.be.sd_client.limits["download"], 99)

    @inlineCallbacks
    def test_changing_settings_for_wrong_id_has_no_effect(self):
        """If the id is wrong, no settings are changed."""
        self.be.sd_client.throttling = False
        self.be.sd_client.limits = {"download": -1, "upload": -1}
        new_settings = {
            backend.DOWNLOAD_KEY: 99,
            backend.UPLOAD_KEY: 99,
            backend.LIMIT_BW_KEY: True,
        }
        yield self.be.change_device_settings("wrong token!", new_settings)
        self.assertEqual(self.be.sd_client.throttling, False)
        self.assertEqual(self.be.sd_client.limits["upload"], -1)
        self.assertEqual(self.be.sd_client.limits["download"], -1)

    @inlineCallbacks
    def test_changing_settings_does_not_log_device_id(self):
        """The change_device_settings does not log the device_id."""
        device_id = 'yadda-yadda'
        yield self.be.change_device_settings(device_id, {})

        device_id_logged = all(device_id not in r.getMessage()
                               for r in self.memento.records)
        self.assertTrue(device_id_logged)


class BackendVolumesTestCase(BackendBasicTestCase):
    """Volumes tests for the backend."""

    # Access to a protected member of a client class

    @defer.inlineCallbacks
    def setUp(self):
        yield super(BackendVolumesTestCase, self).setUp()
        # fake quota info and calculate free bytes
        self.be.wc.results[ACCOUNT_API] = SAMPLE_ACCOUNT_NO_CURRENT_PLAN
        self.be.wc.results[QUOTA_API] = SAMPLE_QUOTA_JSON

        # root dir info
        display_name = yield self.be._process_path(ROOT_PATH)
        self.root_volume = {
            u'volume_id': u'', u'path': ROOT_PATH, u'subscribed': True,
            u'type': self.be.ROOT_TYPE,
            u'display_name': display_name,
        }

        self.patch(self.be.sd_client, 'shares', SAMPLE_SHARES)
        self.patch(self.be.sd_client, 'folders', SAMPLE_FOLDERS)

    @inlineCallbacks
    def expected_volumes(self, sample_shares=None, sample_folders=None,
                         with_storage_info=True):
        """Get shares and group by sharing user, get folders and free space."""
        if sample_shares is None:
            sample_shares = self.be.sd_client.shares

        if sample_folders is None:
            sample_folders = self.be.sd_client.folders

        free_bytes = self.be.FREE_BYTES_NOT_AVAILABLE
        if with_storage_info:
            try:
                result = yield self.be.account_info()
            except Exception:  # pylint: disable=W0703
                pass
            else:
                free_bytes = result['quota_total'] - result['quota_used']

        # get shares and group by sharing user
        shares = defaultdict(list)
        for share in sample_shares:
            # filter out non accepted values
            if not bool(share['accepted']):
                continue

            share = share.copy()

            share[u'realpath'] = share[u'path']
            nicer_path = share[u'path'].replace(SHARES_PATH, SHARES_PATH_LINK)
            share[u'path'] = nicer_path
            share[u'type'] = self.be.SHARE_TYPE
            share[u'subscribed'] = bool(share[u'subscribed'])
            share[u'display_name'] = share[u'name']

            username = share['other_visible_name']
            if not username:
                username = share['other_username'] + \
                           ' (%s)' % self.be.NAME_NOT_SET

            shares[username].append(share)

        folders = []
        for folder in sample_folders:
            folder = folder.copy()
            folder[u'type'] = self.be.FOLDER_TYPE
            folder[u'subscribed'] = bool(folder[u'subscribed'])
            display_name = yield self.be._process_path(folder[u'path'])
            folder[u'display_name'] = display_name
            folders.append(folder)

        # sort folders by path
        folders.sort(key=operator.itemgetter('path'))
        expected = [(u'', free_bytes, [self.root_volume] + folders)]

        for other_user, data in shares.iteritems():
            send_freebytes = any(d['access_level'] == 'Modify' for d in data)
            if send_freebytes:
                free_bytes = int(data[0][u'free_bytes'])
            else:
                free_bytes = self.be.FREE_BYTES_NOT_AVAILABLE

            # sort data by path
            data.sort(key=operator.itemgetter('path'))

            expected.append((other_user, free_bytes, data))

        returnValue(expected)

    @inlineCallbacks
    def test_volumes_info(self):
        """The volumes_info method exercises its callback."""
        expected = yield self.expected_volumes()
        result = yield self.be.volumes_info()

        self.assertEqual(result, expected)
        self.assertFalse(self.be.sd_client.volumes_refreshed)

    @inlineCallbacks
    def test_volumes_info_can_refresh_volumes(self):
        """The volumes_info can be refreshed."""
        expected = yield self.expected_volumes()
        result = yield self.be.volumes_info(refresh=True)

        self.assertTrue(self.be.sd_client.volumes_refreshed)
        self.assertEqual(result, expected)

    @inlineCallbacks
    def test_volumes_info_process_path(self):
        """The volumes_info method exercises its callback."""
        root_path = USER_HOME + os.path.sep + 'My Ubuntu' + USER_HOME
        self.patch(self.be.sd_client, 'get_root_dir', lambda: root_path)
        for item in SAMPLE_FOLDERS:
            path = item[u'path']
            path = path[len(USER_HOME) + 1:]
            item[u'path'] = os.path.join(root_path, path)
        self.patch(self.be.sd_client, 'shares', SAMPLE_SHARES)

        yield self.be.volumes_info()
        for item in SAMPLE_FOLDERS:
            key = item[u'volume_id']
            folder = self.be._volumes[key]
            display_name = folder['display_name']
            prefix = 'My Ubuntu' + USER_HOME
            self.assertTrue(display_name.startswith(prefix))

    @inlineCallbacks
    def test_volumes_info_without_storage_info(self):
        """The volumes_info method exercises its callback."""
        expected = yield self.expected_volumes(with_storage_info=False)
        result = yield self.be.volumes_info(with_storage_info=False)
        self.assertEqual(result, expected)

    def test_cached_volumes_are_initially_empty(self):
        """The cached volume list is empty."""
        self.assertEqual(self.be._volumes, {})

    @inlineCallbacks
    def test_volumes_are_cached(self):
        """The volume list is cached."""
        expected = {}
        info = yield self.expected_volumes()
        for _, _, data in info:
            for volume in data:
                sid = volume['volume_id']
                assert sid not in expected
                expected[sid] = volume

        _ = yield self.be.volumes_info()

        self.assertEqual(len(self.be._volumes), len(expected))
        self.assertEqual(self.be._volumes, expected)

    @inlineCallbacks
    def test_cached_volumes_are_updated_with_volume_info(self):
        """The cached volume list is updated."""
        yield self.test_volumes_are_cached()
        yield self.test_volumes_are_cached()

    @inlineCallbacks
    def test_volumes_info_no_quota_for_read_onlys(self):
        """The volumes_info does not send qupota info for RO shares."""
        read_only_shares = [
            {u'accepted': u'True',
             u'subscribed': u'True',
             u'access_level': u'View',
             u'free_bytes': u'39892622746',
             u'generation': u'2704',
             u'name': u're',
             u'node_id': u'c483f419-ed28-490a-825d-a8c074e2d795',
             u'other_username': u'otheruser',
             u'other_visible_name': u'Other User',
             u'path': os.path.join(SHARES_PATH, 're from Other User'),
             u'type': u'Share',
             u'volume_id': u'4a1b263b-a2b3-4f66-9e66-4cd18050810d'},
            {u'accepted': u'True',
             u'subscribed': u'True',
             u'access_level': u'View',
             u'free_bytes': u'39892622746',
             u'generation': u'2704',
             u'name': u'do',
             u'node_id': u'84544ea4-aefe-4f91-9bb9-ed7b0a805baf',
             u'other_username': u'otheruser',
             u'other_visible_name': u'Other User',
             u'path': os.path.join(SHARES_PATH, 'do from Other User'),
             u'type': u'Share',
             u'volume_id': u'7d130dfe-98b2-4bd5-8708-9eeba9838ac0'},
        ]

        self.patch(self.be.sd_client, 'shares', read_only_shares)

        expected = yield self.expected_volumes()
        result = yield self.be.volumes_info()
        self.assertEqual(result, expected)

    @inlineCallbacks
    def test_volumes_info_no_quota_for_root(self):
        """The volumes_info returns info even if quota call fails."""
        self.be.wc.failure = 500

        expected = yield self.expected_volumes()
        result = yield self.be.volumes_info()

        self.assertEqual(len(result), len(expected))
        self.assertEqual(result, expected)

    @inlineCallbacks
    def test_subscribe_volume_folder(self):
        """The subscribe_volume method subscribes a folder."""
        fid = '0123-4567'
        self.be._volumes[fid] = {u'type': self.be.FOLDER_TYPE}

        yield self.be.subscribe_volume(volume_id=fid)
        self.addCleanup(self.be.sd_client.subscribed_folders.remove, fid)

        self.assertEqual(self.be.sd_client.subscribed_folders, [fid])

    @inlineCallbacks
    def test_unsubscribe_volume_folder(self):
        """The unsubscribe_volume method unsubscribes a folder."""
        fid = '0123-4567'
        self.be._volumes[fid] = {u'type': self.be.FOLDER_TYPE}

        yield self.be.subscribe_volume(volume_id=fid)
        self.assertEqual(self.be.sd_client.subscribed_folders, [fid])

        yield self.be.unsubscribe_volume(volume_id=fid)

        self.assertEqual(self.be.sd_client.subscribed_folders, [])

    @inlineCallbacks
    def test_subscribe_volume_share(self):
        """The subscribe_volume method subscribes a share."""
        sid = '0123-4567'
        self.be._volumes[sid] = {u'type': self.be.SHARE_TYPE}

        yield self.be.subscribe_volume(volume_id=sid)
        self.addCleanup(self.be.sd_client.subscribed_shares.remove, sid)

        self.assertEqual(self.be.sd_client.subscribed_shares, [sid])

    @inlineCallbacks
    def test_unsubscribe_volume_share(self):
        """The unsubscribe_volume method unsubscribes a share."""
        sid = '0123-4567'
        self.be._volumes[sid] = {u'type': self.be.SHARE_TYPE}

        yield self.be.subscribe_volume(volume_id=sid)
        self.assertEqual(self.be.sd_client.subscribed_shares, [sid])

        yield self.be.unsubscribe_volume(volume_id=sid)

        self.assertEqual(self.be.sd_client.subscribed_shares, [])

    @inlineCallbacks
    def test_change_volume_settings_folder(self):
        """The volume settings can be changed."""
        fid = '0123-4567'
        self.be._volumes[fid] = {u'type': self.be.FOLDER_TYPE}

        yield self.be.change_volume_settings(fid, {'subscribed': True})
        self.assertEqual(self.be.sd_client.subscribed_folders, [fid])

        yield self.be.change_volume_settings(fid, {'subscribed': False})
        self.assertEqual(self.be.sd_client.subscribed_folders, [])

    @inlineCallbacks
    def test_change_volume_settings_share(self):
        """The volume settings can be changed."""
        sid = '0123-4567'
        self.be._volumes[sid] = {u'type': self.be.SHARE_TYPE}

        yield self.be.change_volume_settings(sid, {'subscribed': True})
        self.assertEqual(self.be.sd_client.subscribed_shares, [sid])

        yield self.be.change_volume_settings(sid, {'subscribed': False})
        self.assertEqual(self.be.sd_client.subscribed_shares, [])

    @inlineCallbacks
    def test_change_volume_settings_no_setting(self):
        """The change volume settings does not fail on empty settings."""
        yield self.be.change_volume_settings('test', {})
        self.assertEqual(self.be.sd_client.subscribed_folders, [])
        self.assertEqual(self.be.sd_client.subscribed_shares, [])

    @inlineCallbacks
    def test_create_folder(self):
        """New folders can be created."""
        self.patch(self.be.sd_client, 'folders', [])

        folder_path = os.path.join(USER_HOME, 'Test Me')
        yield self.be.create_folder(folder_path=folder_path)

        self.assertEqual(self.be.sd_client.folders, [folder_path])

    @defer.inlineCallbacks
    def test_validate_path_for_folder(self):
        """Test proper validation of path for creating folders."""
        folder_path = os.path.join(USER_HOME, 'Test Me')

        result = yield self.be.validate_path_for_folder(folder_path)
        self.assertTrue(result,
            '%r must be a valid path for creating a folder.' % folder_path)

    @defer.inlineCallbacks
    def test_validate_path_for_folder_invalid(self):
        """Test proper validation of path for creating folders."""
        result = yield self.be.validate_path_for_folder(USER_HOME)
        self.assertFalse(result,
            '%r must not be a valid path for creating a folder.' % USER_HOME)


class BackendSyncStatusTestCase(BackendBasicTestCase):
    """Syncdaemon state for the backend."""

    was_disabled = False

    @defer.inlineCallbacks
    def setUp(self):
        yield super(BackendSyncStatusTestCase, self).setUp()
        self.be.file_sync_disabled = self.was_disabled

    def _build_msg(self):
        """Build expected message regarding file sync status."""
        return '%s (%s)' % (self.be.sd_client.status['description'],
                            self.be.sd_client.status['name'])

    @inlineCallbacks
    def assert_correct_status(self, status, msg=None):
        """Check that the resulting status is correct."""
        expected = {MSG_KEY: self._build_msg() if msg is None else msg,
                    STATUS_KEY: status}
        result = yield self.be.file_sync_status()
        self.assertEqual(expected, result)

    @inlineCallbacks
    def test_disabled(self):
        """The syncdaemon status is processed and emitted."""
        self.patch(self.be.sd_client, 'file_sync', False)
        yield self.assert_correct_status(FILE_SYNC_DISABLED, msg='')
        self.assertTrue(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_error(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': 'True',  # nothing else matters
            'is_online': '', 'is_connected': '',
            'name': 'AUTH_FAILED', 'connection': '', 'queues': '',
            'description': 'auth failed',
        }
        yield self.assert_correct_status(FILE_SYNC_ERROR)
        # self.be.file_sync_disabled does not change
        self.assertEqual(self.was_disabled, self.be.file_sync_disabled)

    @inlineCallbacks
    def test_starting_when_init_not_user(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': '', 'is_connected': '',
            'connection': 'Not User With Network', 'queues': '',
            'name': 'INIT', 'description': 'something new',
        }
        yield self.assert_correct_status(FILE_SYNC_STARTING)
        self.assertFalse(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_starting_when_init_with_user(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': '', 'is_connected': '',
            'connection': 'With User With Network', 'queues': '',
            'name': 'INIT', 'description': 'something new',
        }
        yield self.assert_correct_status(FILE_SYNC_STARTING)
        self.assertFalse(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_starting_when_local_rescan_not_user(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': '', 'is_connected': '',
            'connection': 'Not User With Network', 'queues': '',
            'name': 'LOCAL_RESCAN', 'description': 'something new',
        }
        yield self.assert_correct_status(FILE_SYNC_STARTING)
        self.assertFalse(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_starting_when_local_rescan_with_user(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': '', 'is_connected': '',
            'connection': 'With User With Network', 'queues': '',
            'name': 'LOCAL_RESCAN', 'description': 'something new',
        }
        yield self.assert_correct_status(FILE_SYNC_STARTING)
        self.assertFalse(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_starting_when_ready_with_user(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': '', 'is_connected': '',
            'connection': 'With User With Network', 'queues': '',
            'name': 'READY', 'description': 'something nicer',
        }
        yield self.assert_correct_status(FILE_SYNC_STARTING)
        self.assertFalse(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_disconnected(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': '', 'is_connected': '',
            'queues': '', 'description': 'something new',
            'connection': 'Not User With Network',  # user didn't connect
            'name': 'READY',  # must be READY, otherwise is STARTING
        }
        yield self.assert_correct_status(FILE_SYNC_DISCONNECTED)

        # self.be.file_sync_disabled does not change
        self.assertEqual(self.was_disabled, self.be.file_sync_disabled)

    @inlineCallbacks
    def test_disconnected_when_waiting(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': '', 'is_connected': '',
            'connection': 'With User With Network', 'queues': '',
            'name': 'WAITING', 'description': 'what a long wait!',
        }
        yield self.assert_correct_status(FILE_SYNC_DISCONNECTED)

        # self.be.file_sync_disabled does not change
        self.assertEqual(self.was_disabled, self.be.file_sync_disabled)

    @inlineCallbacks
    def test_syncing_if_online(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': 'True', 'is_connected': 'True',
            'name': 'QUEUE_MANAGER', 'connection': '',
            'queues': 'WORKING_ON_CONTENT',  # anything but IDLE
            'description': 'something nicer',
        }
        yield self.assert_correct_status(FILE_SYNC_SYNCING)

        # self.be.file_sync_disabled does not change
        self.assertEqual(self.was_disabled, self.be.file_sync_disabled)

    @inlineCallbacks
    def test_syncing_even_if_not_online(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': '', 'is_connected': 'True',
            'name': 'CHECK_VERSION', 'connection': '',
            'queues': 'WORKING_ON_CONTENT',
            'description': 'something nicer',
        }
        yield self.assert_correct_status(FILE_SYNC_SYNCING)

        # self.be.file_sync_disabled does not change
        self.assertEqual(self.was_disabled, self.be.file_sync_disabled)

    @inlineCallbacks
    def test_idle(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': 'True', 'is_connected': 'True',
            'name': 'QUEUE_MANAGER', 'connection': '',
            'queues': 'IDLE',
            'description': 'something nice',
        }
        yield self.assert_correct_status(FILE_SYNC_IDLE)

        # self.be.file_sync_disabled does not change
        self.assertEqual(self.was_disabled, self.be.file_sync_disabled)

    @inlineCallbacks
    def test_stopped(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = {
            'is_error': '', 'is_online': '', 'is_connected': '',
            'name': 'SHUTDOWN', 'connection': '',
            'queues': 'IDLE',
            'description': 'something nice',
        }
        yield self.assert_correct_status(FILE_SYNC_STOPPED)

        # self.be.file_sync_disabled does not change
        self.assertEqual(self.was_disabled, self.be.file_sync_disabled)

    @inlineCallbacks
    def test_unknown(self):
        """The syncdaemon status is processed and emitted."""
        self.be.sd_client.status = status = {
            'is_error': '', 'is_online': '', 'is_connected': '',
            'name': '', 'connection': '', 'queues': '',
            'description': '',
        }
        yield self.assert_correct_status(FILE_SYNC_UNKNOWN)

        has_warning = self.memento.check_warning('file_sync_status: unknown',
                                                 repr(status))
        self.assertTrue(has_warning)

        # self.be.file_sync_disabled does not change
        self.assertEqual(self.was_disabled, self.be.file_sync_disabled)

    def test_status_changed(self):
        """The file_sync_status is the status changed handler."""
        self.be.status_changed_handler = self._set_called
        status = {'name': 'foo', 'description': 'bar', 'is_error': '',
                  'is_connected': '', 'is_online': '', 'queues': ''}
        self.be.sd_client.status_changed_handler(status)

        expected_status = self.be._process_file_sync_status(status)
        self.assertEqual(self._called, ((expected_status,), {}))

    def test_invalid_status_type(self):
        """Check that the method return None with invalid types."""
        self.be.status_changed_handler = self._set_called
        status = 3
        self.be.sd_client.status_changed_handler(status)

        self.assertEqual(self._called, ((None,), {}))


class BackendSyncStatusIfDisabledTestCase(BackendSyncStatusTestCase):
    """Syncdaemon state for the backend when file sync is disabled."""

    was_disabled = True

    @inlineCallbacks
    def assert_correct_status(self, status, msg=None):
        """Check that the resulting status is correct."""
        sup = super(BackendSyncStatusIfDisabledTestCase, self)
        if status != FILE_SYNC_STARTING:
            yield sup.assert_correct_status(FILE_SYNC_DISABLED, msg='')
        else:
            yield sup.assert_correct_status(status, msg=msg)


class BackendFileSyncOpsTestCase(BackendBasicTestCase):
    """Syncdaemon operations for the backend."""

    @defer.inlineCallbacks
    def setUp(self):
        yield super(BackendFileSyncOpsTestCase, self).setUp()
        self.be.sd_client.actions = []

    @inlineCallbacks
    def test_enable_files(self):
        """Files service is enabled."""
        yield self.be.disable_files()

        yield self.be.enable_files()
        self.assertTrue(self.be.sd_client.file_sync)
        self.assertFalse(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_disable_files(self):
        """Files service is disabled."""
        yield self.be.enable_files()

        yield self.be.disable_files()
        self.assertFalse(self.be.sd_client.file_sync)
        self.assertTrue(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_connect_files(self):
        """Connect files service."""
        yield self.be.connect_files()

        self.assertEqual(self.be.sd_client.actions, ['connect'])
        self.assertFalse(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_disconnect_files(self):
        """Disconnect files service."""
        yield self.be.disconnect_files()

        self.assertEqual(self.be.sd_client.actions, ['disconnect'])
        self.assertFalse(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_restart_files(self):
        """Restart the files service."""
        yield self.be.restart_files()

        self.assertEqual(self.be.sd_client.actions, ['stop', 'start'])
        self.assertFalse(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_start_files(self):
        """Start the files service."""
        yield self.be.start_files()

        self.assertEqual(self.be.sd_client.actions, ['start'])
        self.assertFalse(self.be.file_sync_disabled)

    @inlineCallbacks
    def test_stop_files(self):
        """Stop the files service."""
        yield self.be.stop_files()

        self.assertEqual(self.be.sd_client.actions, ['stop'])
        self.assertFalse(self.be.file_sync_disabled)


class BackendReplicationsTestCase(BackendBasicTestCase):
    """Replications tests for the backend."""

    @inlineCallbacks
    def test_replications_info(self):
        """The replications_info method exercises its callback."""
        result = yield self.be.replications_info()

        # replications_info will use exclusions information
        expected = []
        for name in MockReplicationClient.replications:
            enabled = name not in MockReplicationClient.exclusions
            dependency = ''
            if name == MockReplicationClient.CONTACTS:
                dependency = backend.CONTACTS_PKG

            item = {'replication_id': name, 'name': name,
                    'enabled': enabled, 'dependency': dependency}
            expected.append(item)
        self.assertEqual(sorted(expected), sorted(result))

    @inlineCallbacks
    def test_change_replication_settings(self):
        """The replication settings can be changed."""
        rid = '0123-4567'
        MockReplicationClient.replications.add(rid)
        self.addCleanup(MockReplicationClient.replications.remove, rid)

        yield self.be.change_replication_settings(rid, {'enabled': False})
        self.assertIn(rid, MockReplicationClient.exclusions)

        yield self.be.change_replication_settings(rid, {'enabled': True})
        self.assertNotIn(rid, MockReplicationClient.exclusions)

    @inlineCallbacks
    def test_change_replication_settings_not_in_replications(self):
        """The settings can not be changed for an item not in replications."""
        rid = '0123-4567'
        assert rid not in MockReplicationClient.replications

        d = self.be.change_replication_settings(rid, {'enabled': True})
        yield self.assertFailure(d, replication_client.ReplicationError)

        d = self.be.change_replication_settings(rid, {'enabled': False})
        yield self.assertFailure(d, replication_client.ReplicationError)

    @inlineCallbacks
    def test_change_replication_settings_no_setting(self):
        """The change replication settings does not fail on empty settings."""
        rid = '0123-4567'
        MockReplicationClient.replications.add(rid)
        self.addCleanup(MockReplicationClient.replications.remove, rid)

        prior = MockReplicationClient.exclusions.copy()
        yield self.be.change_replication_settings(rid, {})

        self.assertEqual(MockReplicationClient.exclusions, prior)


class BackendFileSyncSettingsTestCase(BackendBasicTestCase):
    """File Sync Settings tests for the backend."""

    default_settings = backend.ControlBackend.DEFAULT_FILE_SYNC_SETTINGS

    @inlineCallbacks
    def setUp(self):
        yield super(BackendFileSyncSettingsTestCase, self).setUp()
        # restore default settings
        yield self.be.change_file_sync_settings(self.default_settings)

    @inlineCallbacks
    def assert_boolean_setting_is_correct(self, setting_name):
        """The 'setting_name' can be successfully changed."""
        new_value = not self.default_settings[setting_name]
        yield self.be.change_file_sync_settings({setting_name: new_value})

        self.assertEqual(getattr(self.be.sd_client, setting_name), new_value)

        actual = yield self.be.file_sync_settings_info()
        expected = self.default_settings.copy()
        expected[setting_name] = new_value
        self.assertEqual(expected, actual)

    @inlineCallbacks
    def test_file_sync_settings_info(self):
        """The settings_info method exercises its callback."""
        self.patch(self.be.sd_client, "throttling", True)
        self.be.sd_client.limits = {"download": 1000, "upload": 100}
        expected = {
            backend.AUTOCONNECT_KEY: self.be.sd_client.autoconnect,
            backend.SHOW_ALL_NOTIFICATIONS_KEY:
                self.be.sd_client.show_all_notifications,
            backend.SHARE_AUTOSUBSCRIBE_KEY:
                self.be.sd_client.share_autosubscribe,
            backend.UDF_AUTOSUBSCRIBE_KEY:
                self.be.sd_client.udf_autosubscribe,
            backend.DOWNLOAD_KEY: self.be.sd_client.limits['download'],
            backend.UPLOAD_KEY: self.be.sd_client.limits['upload'],
        }
        result = yield self.be.file_sync_settings_info()
        self.assertEqual(expected, result)

    @inlineCallbacks
    def test_file_sync_settings_info_with_limit(self):
        """The settings_info method exercises its callback."""
        self.patch(self.be.sd_client, "throttling", False)
        self.be.sd_client.limits = {"download": 987456, "upload": 125698}
        expected = {
            backend.AUTOCONNECT_KEY: self.be.sd_client.autoconnect,
            backend.SHOW_ALL_NOTIFICATIONS_KEY:
                self.be.sd_client.show_all_notifications,
            backend.SHARE_AUTOSUBSCRIBE_KEY:
                self.be.sd_client.share_autosubscribe,
            backend.UDF_AUTOSUBSCRIBE_KEY:
                self.be.sd_client.udf_autosubscribe,
            backend.DOWNLOAD_KEY: -1,
            backend.UPLOAD_KEY: -1,
        }
        result = yield self.be.file_sync_settings_info()
        self.assertEqual(expected, result)

    @inlineCallbacks
    def test_change_file_sync_setting_autoconnect(self):
        """The settings can be changed for autoconnect."""
        yield self.assert_boolean_setting_is_correct(backend.AUTOCONNECT_KEY)

    @inlineCallbacks
    def test_change_file_sync_setting_show_all_notifications(self):
        """The settings can be changed for show_all_notifications."""
        yield self.assert_boolean_setting_is_correct(
            backend.SHOW_ALL_NOTIFICATIONS_KEY)

    @inlineCallbacks
    def test_change_file_sync_setting_share_autosubscribe(self):
        """The settings can be changed for share_autosubscribe."""
        yield self.assert_boolean_setting_is_correct(
            backend.SHARE_AUTOSUBSCRIBE_KEY)

    @inlineCallbacks
    def test_change_file_sync_setting_udf_autosubscribe(self):
        """The settings can be changed for udf_autosubscribe."""
        yield self.assert_boolean_setting_is_correct(
            backend.UDF_AUTOSUBSCRIBE_KEY)

    @inlineCallbacks
    def test_change_file_sync_setting_download_bandwidth_limit(self):
        """The settings can be changed for download_bandwidth_limit."""
        new_value = 834
        setting_name = backend.DOWNLOAD_KEY
        yield self.be.change_file_sync_settings({setting_name: new_value})

        self.assertEqual(self.be.sd_client.throttling, True)
        self.assertEqual(self.be.sd_client.limits,
                         {'download': new_value, 'upload': -1})

    @inlineCallbacks
    def test_change_file_sync_setting_upload_bandwidth_limit(self):
        """The settings can be changed for upload_bandwidth_limit."""
        new_value = 932
        setting_name = backend.UPLOAD_KEY
        yield self.be.change_file_sync_settings({setting_name: new_value})

        self.assertEqual(self.be.sd_client.throttling, True)
        self.assertEqual(self.be.sd_client.limits,
                         {'download': -1, 'upload': new_value})

    @inlineCallbacks
    def test_no_download_limit_and_upload_change_to_no_limit(self):
        """The settings can be changed for download_bandwidth_limit.

        If the download limit was not set and the upload was set, when
        unsetting the upload limit the bandwidth_throttling_enabled is False.

        """
        only_one_limit_set = {
            backend.DOWNLOAD_KEY: -1,
            backend.UPLOAD_KEY: 52,
        }
        yield self.be.change_file_sync_settings(only_one_limit_set)

        # unset upload_bandwidth_limit
        setting_name = backend.UPLOAD_KEY
        yield self.be.change_file_sync_settings({setting_name: -1})

        self.assertEqual(self.be.sd_client.throttling, False)
        self.assertEqual(self.be.sd_client.limits,
                         {'download': -1, 'upload': -1})

    @inlineCallbacks
    def test_no_upload_limit_and_download_change_to_no_limit(self):
        """The settings can be changed for upload_bandwidth_limit.

        If the upload limit was not set and the download was set, when
        unsetting the download limit the bandwidth_throttling_enabled is False.

        """
        only_one_limit_set = {
            backend.DOWNLOAD_KEY: 33,
            backend.UPLOAD_KEY: -1,
        }
        yield self.be.change_file_sync_settings(only_one_limit_set)

        # unset download_bandwidth_limit
        setting_name = backend.DOWNLOAD_KEY
        yield self.be.change_file_sync_settings({setting_name: -1})

        self.assertEqual(self.be.sd_client.throttling, False)
        self.assertEqual(self.be.sd_client.limits,
                         {'download': -1, 'upload': -1})

    @inlineCallbacks
    def test_restore_defaults(self):
        """The defaults can be restored."""
        not_defaults = {
            backend.AUTOCONNECT_KEY: False,
            backend.SHOW_ALL_NOTIFICATIONS_KEY: False,
            backend.SHARE_AUTOSUBSCRIBE_KEY: True,
            backend.UDF_AUTOSUBSCRIBE_KEY: True,
            backend.DOWNLOAD_KEY: 204800,
            backend.UPLOAD_KEY: 2048,
        }
        yield self.be.change_file_sync_settings(not_defaults)

        # the call we want to test
        yield self.be.restore_file_sync_settings()

        self.assertEqual(self.be.sd_client.autoconnect, True)
        self.assertEqual(self.be.sd_client.show_all_notifications, True)
        self.assertEqual(self.be.sd_client.share_autosubscribe, False)
        self.assertEqual(self.be.sd_client.udf_autosubscribe, False)
        self.assertEqual(self.be.sd_client.throttling, False)
        self.assertEqual(self.be.sd_client.limits,
                         {'download': -1, 'upload': -1})

        result = yield self.be.file_sync_settings_info()
        self.assertEqual(self.default_settings, result)

    @inlineCallbacks
    def test_sync_menu(self):
        """Check that we get the right data to create the menu."""
        result = yield self.be.sync_menu()
        expected = {'recent-transfers': (), 'uploading': ()}
        self.assertEqual(result, expected)

    @inlineCallbacks
    def test_get_public_files(self):
        """Check that we get list of public files."""
        data = []

        def get_public_files_handler(publicfiles):
            """Callback for get_public_files."""
            data.append(publicfiles)

        self.be.set_public_files_list_handler(get_public_files_handler)
        yield self.be.get_public_files()
        expected = [[{'path': '/home/file1',
                      'public_url': 'http:ubuntuone.com/asd123'},
                     {'path': '/home/file2',
                      'public_url': 'http:ubuntuone.com/qwe456'}]]
        self.assertEqual(expected, data)

    @inlineCallbacks
    def test_get_shares(self):
        """Check that we get the list of shares."""
        result = yield self.be.get_shares()
        self.assertEqual(result, [])

    @inlineCallbacks
    def test_change_public_access_set_public(self):
        """Check that we get a notification when the access type change."""
        data = []

        def public_access_change_handler(info):
            """Callback for get_public_files."""
            data.append(info)

        self.be.set_public_access_changed_handler(public_access_change_handler)
        yield self.be.change_public_access('file1', True)
        expected = {'public_url': 'http:ubuntuone.com/zxc789'}
        self.assertEqual([expected], data)