|
1
|
|
|
import os |
|
2
|
|
|
import random |
|
3
|
|
|
import string |
|
4
|
|
|
from unittest import TestCase |
|
5
|
|
|
|
|
6
|
|
|
from NextCloud import NextCloud |
|
7
|
|
|
|
|
8
|
|
|
NEXTCLOUD_URL = "http://{}:80".format(os.environ['NEXTCLOUD_HOST']) |
|
9
|
|
|
NEXTCLOUD_USERNAME = os.environ.get('NEXTCLOUD_USERNAME') |
|
10
|
|
|
NEXTCLOUD_PASSWORD = os.environ.get('NEXTCLOUD_PASSWORD') |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class BaseTestCase(TestCase): |
|
14
|
|
|
|
|
15
|
|
|
SUCCESS_CODE = 100 |
|
16
|
|
|
INVALID_INPUT_CODE = 101 |
|
17
|
|
|
USERNAME_ALREADY_EXISTS_CODE = 102 |
|
18
|
|
|
UNKNOWN_ERROR_CODE = 103 |
|
19
|
|
|
NOT_FOUND_CODE = 404 |
|
20
|
|
|
|
|
21
|
|
|
SHARE_API_SUCCESS_CODE = 200 # share api has different code |
|
22
|
|
|
|
|
23
|
|
|
def setUp(self): |
|
24
|
|
|
self.username = NEXTCLOUD_USERNAME |
|
25
|
|
|
self.nxc = NextCloud(NEXTCLOUD_URL, NEXTCLOUD_USERNAME, NEXTCLOUD_PASSWORD, js=True) |
|
26
|
|
|
|
|
27
|
|
|
def create_new_user(self, username_prefix, password=None): |
|
28
|
|
|
""" Helper method to create new user """ |
|
29
|
|
|
new_user_username = username_prefix + self.get_random_string(length=4) |
|
30
|
|
|
user_password = password or self.get_random_string(length=8) |
|
31
|
|
|
res = self.nxc.add_user(new_user_username, user_password) |
|
32
|
|
|
assert res['ocs']['meta']['statuscode'] == self.SUCCESS_CODE |
|
33
|
|
|
return new_user_username |
|
34
|
|
|
|
|
35
|
|
|
def delete_user(self, username): |
|
36
|
|
|
""" Helper method to delete user by username """ |
|
37
|
|
|
res = self.nxc.delete_user(username) |
|
38
|
|
|
assert res['ocs']['meta']['statuscode'] == self.SUCCESS_CODE |
|
39
|
|
|
|
|
40
|
|
|
def clear(self, nxc=None, user_ids=None, group_ids=None, share_ids=None, group_folder_ids=None): |
|
41
|
|
|
""" |
|
42
|
|
|
Delete created objects during tests |
|
43
|
|
|
|
|
44
|
|
|
Args: |
|
45
|
|
|
nxc (NextCloud object): (optional) Nextcloud instance, if not given - self.nxc is used |
|
46
|
|
|
user_ids (list): list of user ids |
|
47
|
|
|
group_ids (list): list of group ids |
|
48
|
|
|
share_ids (list): list of shares ids |
|
49
|
|
|
|
|
50
|
|
|
Returns: |
|
51
|
|
|
|
|
52
|
|
|
""" |
|
53
|
|
|
nxc = nxc or self.nxc |
|
54
|
|
|
if share_ids: |
|
55
|
|
|
for share_id in share_ids: |
|
56
|
|
|
res = nxc.delete_share(share_id) |
|
57
|
|
|
assert res['ocs']['meta']['statuscode'] == self.SHARE_API_SUCCESS_CODE |
|
58
|
|
|
if group_ids: |
|
59
|
|
|
for group_id in group_ids: |
|
60
|
|
|
res = nxc.delete_group(group_id) |
|
61
|
|
|
assert res['ocs']['meta']['statuscode'] == self.SUCCESS_CODE |
|
62
|
|
|
if user_ids: |
|
63
|
|
|
for user_id in user_ids: |
|
64
|
|
|
res = nxc.delete_user(user_id) |
|
65
|
|
|
assert res['ocs']['meta']['statuscode'] == self.SUCCESS_CODE |
|
66
|
|
|
if group_folder_ids: |
|
67
|
|
|
for group_folder_id in group_folder_ids: |
|
68
|
|
|
res = nxc.delete_group_folder(group_folder_id) |
|
69
|
|
|
assert res['ocs']['meta']['statuscode'] == self.SUCCESS_CODE |
|
70
|
|
|
|
|
71
|
|
|
def get_random_string(self, length=6): |
|
72
|
|
|
""" Helper method to get random string with set length """ |
|
73
|
|
|
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length)) |
|
74
|
|
|
|