tests.base.BaseTestCase.clear()   C
last analyzed

Complexity

Conditions 9

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 30
rs 6.6666
c 0
b 0
f 0
cc 9
nop 6
1
import os
2
import random
3
import string
4
from unittest import TestCase
5
6
from nextcloud import NextCloud
7
8
NEXTCLOUD_VERSION = os.environ.get('NEXTCLOUD_VERSION')
9
NEXTCLOUD_URL = "http://{}:80".format(os.environ.get('NEXTCLOUD_HOSTNAME', 'localhost'))
10
NEXTCLOUD_USERNAME = os.environ.get('NEXTCLOUD_ADMIN_USER', 'admin')
11
NEXTCLOUD_PASSWORD = os.environ.get('NEXTCLOUD_ADMIN_PASSWORD', 'admin')
12
13
14
class BaseTestCase(TestCase):
15
16
    SUCCESS_CODE = 100
17
    INVALID_INPUT_CODE = 101
18
    USERNAME_ALREADY_EXISTS_CODE = 102
19
    UNKNOWN_ERROR_CODE = 103
20
    NOT_FOUND_CODE = 404
21
22
    def setUp(self):
23
        self.username = NEXTCLOUD_USERNAME
24
        self.nxc = NextCloud(NEXTCLOUD_URL, NEXTCLOUD_USERNAME, NEXTCLOUD_PASSWORD, json_output=True)
25
26
    def create_new_user(self, username_prefix, password=None):
27
        """ Helper method to create new user """
28
        new_user_username = username_prefix + self.get_random_string(length=4)
29
        user_password = password or self.get_random_string(length=8)
30
        res = self.nxc.add_user(new_user_username, user_password)
31
        assert res.is_ok
32
        return new_user_username
33
34
    def delete_user(self, username):
35
        """ Helper method to delete user by username """
36
        res = self.nxc.delete_user(username)
37
        assert res.is_ok
38
39
    def clear(self, nxc=None, user_ids=None, group_ids=None, share_ids=None, group_folder_ids=None):
40
        """
41
        Delete created objects during tests
42
43
        Args:
44
            nxc (NextCloud object): (optional) Nextcloud instance, if not given - self.nxc is used
45
            user_ids (list): list of user ids
46
            group_ids (list): list of group ids
47
            share_ids (list): list of shares ids
48
49
        Returns:
50
51
        """
52
        nxc = nxc or self.nxc
53
        if share_ids:
54
            for share_id in share_ids:
55
                res = nxc.delete_share(share_id)
56
                assert res.is_ok
57
        if group_ids:
58
            for group_id in group_ids:
59
                res = nxc.delete_group(group_id)
60
                assert res.is_ok
61
        if user_ids:
62
            for user_id in user_ids:
63
                res = nxc.delete_user(user_id)
64
                assert res.is_ok
65
        if group_folder_ids:
66
            for group_folder_id in group_folder_ids:
67
                res = nxc.delete_group_folder(group_folder_id)
68
                assert res.is_ok
69
70
    def get_random_string(self, length=6):
71
        """ Helper method to get random string with set length  """
72
        return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))
73
74
75
class LocalNxcUserMixin(object):
76
    """Mixin to create new clean NextCloud admin user for tests and delete it on tear down"""
77
78
    def setUp(self):
79
        super(LocalNxcUserMixin, self).setUp()
80
        user_password = self.get_random_string(length=8)
81
        self.user_username = self.create_new_user('test_user_', password=user_password)
82
        self.nxc_local = self.nxc_local = NextCloud(NEXTCLOUD_URL, self.user_username, user_password, json_output=True)
83
        # make user admin
84
        self.nxc.add_to_group(self.user_username, 'admin')
85
86
    def tearDown(self):
87
        self.nxc.delete_user(self.user_username)
88