TestShares.test_get_path_shares()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 30
rs 9.45
c 0
b 0
f 0
cc 1
nop 1
1
import requests
2
from datetime import datetime, timedelta
3
4
from nextcloud.base import ShareType, Permission, datetime_to_expire_date
5
6
from .base import BaseTestCase, LocalNxcUserMixin
7
8
9
class TestShares(LocalNxcUserMixin, BaseTestCase):
10
11
    def test_share_create_retrieve_delete(self):
12
        """ shallow test for base retrieving single, list, creating and deleting share """
13
        # check no shares exists
14
        res = self.nxc_local.get_shares()
15
        assert res.is_ok
16
        assert len(res.data) == 0
17
18
        # create public share
19
        res = self.nxc_local.create_share('Documents', share_type=ShareType.PUBLIC_LINK.value)
20
        assert res.is_ok
21
        share_id = res.data['id']
22
23
        # get all shares
24
        all_shares = self.nxc_local.get_shares().data
25
        assert len(all_shares) == 1
26
        assert all_shares[0]['id'] == share_id
27
        assert all_shares[0]['share_type'] == ShareType.PUBLIC_LINK.value
28
29
        # get single share info
30
        created_share = self.nxc_local.get_share_info(share_id)
31
        assert res.is_ok
32
        created_share_data = created_share.data[0]
33
        assert created_share_data['id'] == share_id
34
        assert created_share_data['share_type'] == ShareType.PUBLIC_LINK.value
35
        assert created_share_data['uid_owner'] == self.user_username
36
37
        # delete share
38
        res = self.nxc_local.delete_share(share_id)
39
        assert res.is_ok
40
        all_shares = self.nxc_local.get_shares().data
41
        assert len(all_shares) == 0
42
43
    def test_create(self):
44
        """ creating share with different params """
45
        share_path = "Documents"
46
        user_to_share_with = self.create_new_user("test_shares_")
47
        group_to_share_with = 'group_to_share_with'
48
        self.nxc.add_group(group_to_share_with)
49
50
        # create for user, group
51
        for (share_type, share_with, permissions) in [(ShareType.USER.value, user_to_share_with, Permission.READ.value),
52
                                                      (ShareType.GROUP.value, group_to_share_with, Permission.READ.value + Permission.CREATE.value)]:
53
            # create share with user
54
            res = self.nxc_local.create_share(share_path,
55
                                              share_type=share_type,
56
                                              share_with=share_with,
57
                                              permissions=permissions)
58
            assert res.is_ok
59
            share_id = res.data['id']
60
61
            # check if shared with right user/group, permission
62
            created_share = self.nxc_local.get_share_info(share_id)
63
            assert res.is_ok
64
            created_share_data = created_share.data[0]
65
            assert created_share_data['id'] == share_id
66
            assert created_share_data['share_type'] == share_type
67
            assert created_share_data['share_with'] == share_with
68
            assert created_share_data['permissions'] == permissions
69
70
            # delete share, user
71
            self.nxc_local.delete_share(share_id)
72
            self.nxc.delete_user(user_to_share_with)
73
74
    def test_create_with_password(self):
75
        share_path = "Documents"
76
        res = self.nxc_local.create_share(path=share_path,
77
                                          share_type=ShareType.PUBLIC_LINK.value,
78
                                          password=self.get_random_string(length=8))
79
        assert res.is_ok
80
        share_url = res.data['url']
81
        share_resp = requests.get(share_url)
82
        assert "This share is password-protected" in share_resp.text
83
        self.nxc_local.delete_share(res.data['id'])
84
85
    def test_get_path_shares(self):
86
        share_path = "Documents"
87
        group_to_share_with_name = self.get_random_string(length=4) + "_test_add"
88
        self.nxc.add_group(group_to_share_with_name)
89
90
        # check that path has no shares yet
91
        res = self.nxc_local.get_shares_from_path(share_path)
92
        assert res.is_ok
93
        assert len(res.data) == 0
94
95
        # first path share
96
        first_share = self.nxc_local.create_share(path=share_path,
97
                                                  share_type=ShareType.PUBLIC_LINK.value)
98
99
        # create second path share
100
        second_share = self.nxc_local.create_share(path=share_path,
101
                                                   share_type=ShareType.GROUP.value,
102
                                                   share_with=group_to_share_with_name,
103
                                                   permissions=Permission.READ.value)
104
105
        all_shares_ids = [first_share.data['id'], second_share.data['id']]
106
107
        # check that path has two shares
108
        res = self.nxc_local.get_shares_from_path(share_path)
109
        assert res.is_ok
110
        assert len(res.data) == 2
111
        assert all([each['id'] in all_shares_ids for each in res.data])
112
113
        # clean shares, groups
114
        self.clear(self.nxc_local, share_ids=all_shares_ids, group_ids=[group_to_share_with_name])
115
116
    def test_update_share(self):
117
        share_path = "Documents"
118
        user_to_share_with = self.create_new_user("test_shares_")
119
120
        share_with = user_to_share_with
121
        share_type = ShareType.USER.value
122
        # create share with user
123
        res = self.nxc_local.create_share(share_path,
124
                                          share_type=ShareType.USER.value,
125
                                          share_with=user_to_share_with,
126
                                          permissions=Permission.READ.value)
127
        assert res.is_ok
128
        share_id = res.data['id']
129
130
        # update share permissions
131
        new_permissions = Permission.READ.value + Permission.CREATE.value
132
        res = self.nxc_local.update_share(share_id, permissions=new_permissions)
133
        assert res.is_ok
134
135
        updated_share_data = res.data
136
        assert updated_share_data['id'] == share_id
137
        assert updated_share_data['share_type'] == share_type
138
        assert updated_share_data['share_with'] == share_with
139
        assert updated_share_data['permissions'] == new_permissions
140
        assert updated_share_data['expiration'] is None
141
142
        # update share expire date
143
        expire_date = datetime_to_expire_date(datetime.now() + timedelta(days=5))
144
        res = self.nxc_local.update_share(share_id, expire_date=expire_date)
145
        assert res.is_ok
146
147
        updated_share_data = res.data
148
        assert updated_share_data['id'] == share_id
149
        assert updated_share_data['share_type'] == share_type
150
        assert updated_share_data['share_with'] == share_with
151
        assert updated_share_data['permissions'] == new_permissions
152
        assert updated_share_data['expiration'] == "{} 00:00:00".format(expire_date)
153
154
        self.clear(self.nxc_local, share_ids=[share_id], user_ids=[user_to_share_with])
155