test_models.py 7.53 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
# pylint: disable=line-too-long,invalid-name
Lukas Burgey's avatar
Lukas Burgey committed
2

Lukas Burgey's avatar
Lukas Burgey committed
3
import logging
Lukas Burgey's avatar
Lukas Burgey committed
4
from django.contrib import auth
Lukas Burgey's avatar
Lukas Burgey committed
5
from django.contrib.auth.models import Group
Lukas Burgey's avatar
Lukas Burgey committed
6
7
from django.test import TestCase

Lukas Burgey's avatar
Lukas Burgey committed
8
from django_backend.backend.models import models
Lukas Burgey's avatar
Lukas Burgey committed
9
10
from django_backend.backend.auth.v1 import models as auth_models

Lukas Burgey's avatar
Lukas Burgey committed
11
12
LOGGER = logging.getLogger(__name__)

Lukas Burgey's avatar
Lukas Burgey committed
13
14
15
TEST_NAME = 'test_name'
TEST_EMAIL = 'test@hdf.org'
TEST_SUB = 'fb0fa558-cfa2-49f9-b847-5c651d1f6135'
Lukas Burgey's avatar
Lukas Burgey committed
16
TEST_KEY = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC/7NWc1pxCxi1f/+DtgCenIC9DDiT6l1cQSz4vJc1ecJA/gEUkd6CF/uR1Zl+yalWZej9UII5902Dm8WkIsIrHdRXevyt0QSzMbzZvaUAdJWcmWpqEKxJT6KGkd50O+qAJBXd5WZ2jUstmbHjoXio9eBY4hDcOofLD8vJT73DrZAEurd3F2S4s04oSt+onUmQ0inqeN38oqYOZGJhe/zZsDj65ctiTpj85rjglndNq+K7IHETGWXg9ht13FH9trjtEpLmAmR9UZGFkof+P+LiIUSYa05sVh6/MPFCgz7gzMCXw99lxoMz4lX2/wV/jnJ0+itn1WByWFWuqbDXUnH4pjk7K+YalFi2lA7F4ckWSn51IQRnh8Qp3CMuvid/Y3Q0qYa4Mtq5zhDnj+uGAUltoENqQCIKLp2KaCRQSbiDudYHXq9dufGRuAsls3rXnRhRJ26wOW+sggjfhB6LhGD20vSdJbuQQwUxZtRLB+y6uP6HXq/eqq+N+f8lfsw64b16xTOYFSpDniV/OE+PuDSNUFlO1V91FWhhiFWCyi+q6dTW5PTWBnnksXhR/LRaivA+UuNxPPbEfefBWmu7/y3jcqAn+CXSzbnvOrVXCU3E7DKBCR3d8TjBQj2No1qTNz6ZOD+dXEQvvjhl2lXkdGazG18hpaWN2hApzBpUulukg9Q== TEST_KEY'
Lukas Burgey's avatar
Lukas Burgey committed
17
18
19
20
21
22
TEST_PASSWORD = 'asnoteuhsn'
TEST_USERINFO = {
    'email': TEST_EMAIL,
    'name': TEST_NAME,
    'sub': TEST_SUB,
}
23
24
TEST_SERVICE = 'test_service'
TEST_SITE = 'test_site'
Lukas Burgey's avatar
Lukas Burgey committed
25
26
27
28
# Has no service
TEST_GROUP = 'test_group'
# Has a service
TEST_GROUP_2 = 'test_group_2'
Lukas Burgey's avatar
Lukas Burgey committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45


def setup_fixture():
    idp = auth_models.OIDCConfig(
        name=TEST_NAME,
        client_id='',
        client_secret='',
        redirect_uri='',
        issuer_uri='',
    )
    idp.save()

    user = models.User(
        username=TEST_NAME,
        sub=TEST_NAME,
        user_type='oidcuser',
        userinfo={
Lukas Burgey's avatar
Lukas Burgey committed
46
            'email': TEST_EMAIL,
Lukas Burgey's avatar
Lukas Burgey committed
47
48
49
50
51
52
53
            'sub': TEST_NAME,
        },
        idp=idp,
    )
    user.set_password(TEST_PASSWORD)
    user.save()

Lukas Burgey's avatar
Lukas Burgey committed
54
55
56
57
58
59
60
61
    # TEST_GROUP has no services
    group = Group(name=TEST_GROUP)
    group.save()

    # TEST_GROUP_2 has services
    group2 = Group(name=TEST_GROUP_2)
    group2.save()

Lukas Burgey's avatar
Lukas Burgey committed
62
    site = models.Site(
63
64
        name=TEST_SITE,
        description=TEST_SITE,
Lukas Burgey's avatar
Lukas Burgey committed
65
66
67
68
    )
    site.save()

    service = models.Service(
69
70
        name=TEST_SERVICE,
        description=TEST_SERVICE,
Lukas Burgey's avatar
Lukas Burgey committed
71
72
73
    )
    service.save()
    service.site.add(site)
Lukas Burgey's avatar
Lukas Burgey committed
74
    service.groups.add(group2)
Lukas Burgey's avatar
Lukas Burgey committed
75
76
77
78
79
80
81
82
83
84
85

    models.SSHPublicKey(
        name=TEST_NAME,
        key=TEST_KEY,
        user=user,
    ).save()


def teardown_fixture():
    models.SSHPublicKey.objects.get(name=TEST_NAME).delete()
    models.User.objects.get(username=TEST_NAME).delete()
Lukas Burgey's avatar
Lukas Burgey committed
86
87
88
89
    models.Site.objects.get(name=TEST_SITE).delete()
    models.Service.objects.get(name=TEST_SERVICE).delete()
    Group.objects.get(name=TEST_GROUP).delete()
    Group.objects.get(name=TEST_GROUP_2).delete()
Lukas Burgey's avatar
Lukas Burgey committed
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
    auth_models.OIDCConfig.objects.get(name=TEST_NAME).delete()


class UserTest(TestCase):
    def setUp(self):
        setup_fixture()

    def tearDown(self):
        teardown_fixture()

    def test_user(self):
        user = auth.authenticate(
            username=TEST_NAME,
            password=TEST_PASSWORD,
        )
        self.assertIsNotNone(user)
        self.assertTrue(user.is_active)

        user.deactivate()
        self.assertFalse(user.is_active)

        user.activate()
        self.assertTrue(user.is_active)

    def test_user_construction(self):
        idp = auth_models.OIDCConfig.objects.get(name=TEST_NAME)
        user = models.User.get_user(
            TEST_USERINFO,
            idp,
        )
        self.assertIsNotNone(user)


class DeploymentTest(TestCase):
    def setUp(self):
        setup_fixture()
Lukas Burgey's avatar
Lukas Burgey committed
126
        models.Service(name='NEW_SERVICE').save()
Lukas Burgey's avatar
Lukas Burgey committed
127
128
129

    def tearDown(self):
        teardown_fixture()
Lukas Burgey's avatar
Lukas Burgey committed
130
        models.Service.objects.get(name='NEW_SERVICE').delete()
Lukas Burgey's avatar
Lukas Burgey committed
131

Lukas Burgey's avatar
Lukas Burgey committed
132
133
    # Deployment with a service
    def test_deployment_service(self):
Lukas Burgey's avatar
Lukas Burgey committed
134
135
        user = models.User.objects.get(username=TEST_NAME)
        key = models.SSHPublicKey.objects.get(name=TEST_NAME)
136
        service = models.Service.objects.get(name=TEST_SERVICE)
Lukas Burgey's avatar
Lukas Burgey committed
137

138
139
        deployment = models.Deployment.get_deployment(
            user,
Lukas Burgey's avatar
Lukas Burgey committed
140
            service=service,
141
        )
Lukas Burgey's avatar
Lukas Burgey committed
142
        self.assertIsNotNone(deployment)
143

Lukas Burgey's avatar
Lukas Burgey committed
144
145
        deployment.deploy_key(key)

146
        state = models.DeploymentState.get_state(
Lukas Burgey's avatar
Lukas Burgey committed
147
148
149
            deployment=deployment,
            key=key,
        )
150
        self.assertIsNotNone(state)
151

Lukas Burgey's avatar
Lukas Burgey committed
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

        self.assertFalse(state.target_reached)

        # execute deployment
        for item in state.state_items.all():
            item.client_response({'state':'deployed'})

        self.assertTrue(state.target_reached)

        # start removal
        state.remove()

        self.assertFalse(state.target_reached)

        # execute removals
        for item in state.state_items.all():
            item.client_response({'state':'not_deployed'})

        self.assertTrue(state.target_reached)


    # Deployment with a group, which has one services (yet)
    def test_deployment_for_group_with_service(self):
        user = models.User.objects.get(username=TEST_NAME)
        key = models.SSHPublicKey.objects.get(name=TEST_NAME)
        group = Group.objects.get(name=TEST_GROUP_2)

        deployment = models.Deployment.get_deployment(
            user,
            group=group,
        )
        self.assertIsNotNone(deployment)

185
        deployment.deploy_key(key)
Lukas Burgey's avatar
Lukas Burgey committed
186

Lukas Burgey's avatar
Lukas Burgey committed
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
        state = models.DeploymentState.get_state(
            deployment=deployment,
            key=key,
        )

        self.assertIsNotNone(state)
        self.assertFalse(state.target_reached)

        # execute deployment
        for item in state.state_items.all():
            item.client_response({'state':'deployed'})

        self.assertTrue(state.target_reached)

        # start removal
        state.remove()

        self.assertFalse(state.target_reached)

        # execute removals
        for item in state.state_items.all():
            item.client_response({'state':'not_deployed'})

        self.assertTrue(state.target_reached)


    # Deployment with a group, which has no services at first
    def test_deployment_for_group_without_service(self):
        user = models.User.objects.get(username=TEST_NAME)
        key = models.SSHPublicKey.objects.get(name=TEST_NAME)
        group = Group.objects.get(name=TEST_GROUP)
        site = models.Site.objects.get(name=TEST_SITE)

        deployment = models.Deployment.get_deployment(
            user,
            group=group,
        )

        self.assertIsNotNone(deployment)

        # start deployment
        deployment.deploy_key(key)
        state = models.DeploymentState.get_state(
            deployment=deployment,
            key=key,
        )

        self.assertIsNotNone(state)
        self.assertFalse(state.state_items.exists())
        self.assertTrue(state.target_reached)

        # add service
        new_service = models.Service.objects.get(
            name='NEW_SERVICE',
        )
        new_service.site.add(site)
        new_service.groups.add(group)
        deployment.service_added(new_service)

        self.assertTrue(state.state_items.exists())
247
248
249
250
        self.assertFalse(state.target_reached)

        # execute deployment
        for item in state.state_items.all():
Lukas Burgey's avatar
Lukas Burgey committed
251
            item.client_response({'state':'deployed'})
252

Lukas Burgey's avatar
Lukas Burgey committed
253
        # the deployment was not done for new_service
254
255
256
257
258
259
        self.assertTrue(state.target_reached)

        # start removal
        state.remove()

        self.assertFalse(state.target_reached)
Lukas Burgey's avatar
Lukas Burgey committed
260

261
262
        # execute removals
        for item in state.state_items.all():
Lukas Burgey's avatar
Lukas Burgey committed
263
            item.client_response({'state':'not_deployed'})
264

265
        self.assertTrue(state.target_reached)