clients.py 4.44 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2

import logging
Lukas Burgey's avatar
Lukas Burgey committed
3
import base64
Lukas Burgey's avatar
Lukas Burgey committed
4

Lukas Burgey's avatar
Lukas Burgey committed
5
6
from django.test import TestCase, Client

Lukas Burgey's avatar
Lukas Burgey committed
7
8
from feudal.backend.models import Site
from feudal.backend.models.users import User
Lukas Burgey's avatar
Lukas Burgey committed
9
10
11
12

LOGGER = logging.getLogger(__name__)


Lukas Burgey's avatar
Lukas Burgey committed
13
14
15
16
17
18
19
20
21
class ClientViewTest(TestCase):

    client = None

    @classmethod
    def setUpTestData(cls):
        cls.CLIENT_NAME = 'TEST_DOWNSTREAM_CLIENT'
        cls.CLIENT_PASSWORD = 'test1234'

Lukas Burgey's avatar
Lukas Burgey committed
22
        cls.api_client = User.construct_client(
Lukas Burgey's avatar
Lukas Burgey committed
23
24
25
26
27
28
29
            cls.CLIENT_NAME,
            cls.CLIENT_PASSWORD,
        )
        cls.api_client.save()

        cls.SITE_NAME = 'TEST_SITE'

Lukas Burgey's avatar
Lukas Burgey committed
30
        cls.site = Site(
Lukas Burgey's avatar
Lukas Burgey committed
31
32
33
34
35
            client=cls.api_client,
            name=cls.SITE_NAME,
        )
        cls.site.save()

Lukas Burgey's avatar
Lukas Burgey committed
36
    def setUp(self):
Lukas Burgey's avatar
Lukas Burgey committed
37
38
39
40
41
42
43
44
45
46
        self.client = Client()

    def auth_headers(self):
        auth_str = '{}:{}'.format(
            self.CLIENT_NAME,
            self.CLIENT_PASSWORD,
        )
        return {
            'HTTP_AUTHORIZATION': 'Basic ' + base64.b64encode(auth_str.encode()).decode(),
        }
Lukas Burgey's avatar
Lukas Burgey committed
47
48
49

    def test_configuration_entitlement(self):
        response = self.client.put(
50
            '/client/config',
Lukas Burgey's avatar
Lukas Burgey committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
            {
                "services": {
                    "CVMFS": {
                        "name": "CERN VM FS",
                        "command": "xxxxxxxxxxxx",
                        "description": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                    }
                },
                "entitlement_to_service_ids": {
                    "urn:geant:h-df.de:group:myExampleColab#unity.helmholtz-data-federation.de": [
                        "CVMFS"
                    ]
                }
            },
            content_type='application/json',
            **self.auth_headers(),
        )
        LOGGER.debug('response: %s', response)
        LOGGER.debug('response json: %s', response.json())
        self.assertEqual(response.status_code, 200)

    def test_configuration_group(self):
        response = self.client.put(
74
            '/client/config',
Lukas Burgey's avatar
Lukas Burgey committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
            {
                "services": {
                    "CVMFS": {
                        "name": "CERN VM FS",
                        "command": "xxxxxxxxxxxx",
                        "description": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                    }
                },
                "group_to_service_ids": {
                    "/foobar": [
                        "CVMFS"
                    ]
                }
            },
            content_type='application/json',
            **self.auth_headers(),
        )
        LOGGER.debug('response: %s', response)
        LOGGER.debug('response json: %s', response.json())
        self.assertEqual(response.status_code, 200)
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

    def test_configuration_error(self):
        services = {
            "CVMFS": {
                "name": "CERN VM FS",
                "command": "xxxxxxxxxxxx",
                "description": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            }
        }
        invalid_configs = [
            {
                "services": services,
                "group_to_service_ids": None,
            },
            {
                "services": services,
                "group_to_service_ids": "",
            },
            {
                "services": services,
                "entitlement_to_service_ids": None,
            },
            {
                "services": services,
                "entitlement_to_service_ids": "",
            },
        ]

        for invalid_config in invalid_configs:
            response = self.client.put(
125
                '/client/config',
126
127
128
129
130
131
132
133
134
                invalid_config,
                content_type='application/json',
                **self.auth_headers(),
            )
            LOGGER.debug('response: %s', response)
            LOGGER.debug('response json: %s', response.json())
            self.assertEqual(response.status_code, 200)

    def test_deregister(self):
135
        response = self.client.get(
136
            '/client/deregister',
137
138
139
140
141
142
143
144
145
146
147
            {
            },
            content_type='application/json',
            **self.auth_headers(),
        )
        LOGGER.debug('response: %s', response)
        LOGGER.debug('response json: %s', response.json())
        self.assertEqual(response.status_code, 200)

    def test_get_states(self):
        response = self.client.get(
148
            '/client/dep-states',
149
150
151
152
153
            **self.auth_headers(),
        )
        LOGGER.debug('response: %s', response)
        LOGGER.debug('response json: %s', response.json())
        self.assertEqual(response.status_code, 200)