tests.py 4.05 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
3
4
5
# pylint: disable=line-too-long,invalid-name

import logging

from django.contrib.auth import authenticate
Lukas Burgey's avatar
Lukas Burgey committed
6
from django.test import Client, TestCase
Lukas Burgey's avatar
Lukas Burgey committed
7

8
9
10
11
from feudal.backend.models import Site, Service
from feudal.backend.models.users import User, SSHPublicKey
from feudal.backend.models.auth import OIDCConfig
from feudal.backend.models.auth.vos import Group, Entitlement
Lukas Burgey's avatar
Lukas Burgey committed
12
13
14

LOGGER = logging.getLogger(__name__)

Lukas Burgey's avatar
Lukas Burgey committed
15

Lukas Burgey's avatar
Lukas Burgey committed
16
17
# base test
class BaseTestCase(TestCase):
Lukas Burgey's avatar
Lukas Burgey committed
18
19
20

    @classmethod
    def setUpTestData(cls):
Lukas Burgey's avatar
Lukas Burgey committed
21
22
        cls.TEST_NAME = 'Gustav Holst'
        cls.TEST_EMAIL = 'gustav@test-domain.de'
Lukas Burgey's avatar
Lukas Burgey committed
23
24
        cls.TEST_SUB = 'fb0fa558-cfa2-49f9-b847-5c651d1f6135'
        cls.TEST_KEY = 'ssh-rsa AAAAB3NzaC1yhApzBpUulukg9Q== TEST_KEY'
Lukas Burgey's avatar
Lukas Burgey committed
25
        cls.TEST_ISSUER = 'https://unity.test-federation.de/oauth2'
Lukas Burgey's avatar
Lukas Burgey committed
26
27

        cls.TEST_USERINFO = {
Lukas Burgey's avatar
Lukas Burgey committed
28
29
            'sub': cls.TEST_SUB,
            'iss': cls.TEST_ISSUER,
Lukas Burgey's avatar
Lukas Burgey committed
30
31
32
33
34
35
36
37
38
            'email': cls.TEST_EMAIL,
            'name': cls.TEST_NAME,
            'ssh_key': cls.TEST_KEY,
        }

        cls.idp = OIDCConfig(
            name='test_idp_name',
            client_id='test_idp_client_id',
            client_secret='test_idp_secret',
Lukas Burgey's avatar
Lukas Burgey committed
39
40
41
42
            issuer_uri=cls.TEST_ISSUER,
            scopes=['test_scope'],
            userinfo_field_entitlements='',
            userinfo_field_groups='',
Lukas Burgey's avatar
Lukas Burgey committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        )
        cls.idp.save()

        cls.entitlement = Entitlement.get_entitlement(name='test_entitlement', idp=cls.idp)
        cls.entitlement.save()

        # Has no service
        cls.GROUP_NO_SERVICES = 'test_group_no'
        # Has a service
        cls.GROUP_ONE_SERVICE = 'test_group_one'
        # Has two services
        cls.GROUP_TWO_SERVICES = 'test_group_two'
        cls.group_none = Group.get_group(name=cls.GROUP_NO_SERVICES, idp=cls.idp)
        cls.group_none.save()
        cls.group_one = Group.get_group(name=cls.GROUP_ONE_SERVICE, idp=cls.idp)
        cls.group_one.save()
        cls.group_two = Group.get_group(name=cls.GROUP_TWO_SERVICES, idp=cls.idp)
        cls.group_two.save()

62
        cls.user = User.construct_from_userinfo(cls.TEST_USERINFO, cls.idp)
Lukas Burgey's avatar
Lukas Burgey committed
63
64
65
66

        cls.USER_NAME = cls.user.username
        cls.USER_PASSWORD = 'asdf1234foo'
        cls.user.set_password(cls.USER_PASSWORD)
Lukas Burgey's avatar
Lukas Burgey committed
67
68
69
70
71
        cls.user.save()
        cls.user.vos.add(cls.group_none)
        cls.user.vos.add(cls.group_one)
        cls.user.vos.add(cls.group_two)

Lukas Burgey's avatar
Lukas Burgey committed
72
73
        cls.site = Site.objects.create(name='test_site')
        cls.site2 = Site.objects.create(name='test_site_2')
Lukas Burgey's avatar
Lukas Burgey committed
74

75
        cls.service_one = Service.get_service(
Lukas Burgey's avatar
Lukas Burgey committed
76
77
            'test_service_one',
            cls.site,
Lukas Burgey's avatar
Lukas Burgey committed
78
            vos=[cls.group_one],
Lukas Burgey's avatar
Lukas Burgey committed
79
        )
80
        cls.service_two_a = Service.get_service(
Lukas Burgey's avatar
Lukas Burgey committed
81
82
            'test_service_two_a',
            cls.site,
Lukas Burgey's avatar
Lukas Burgey committed
83
            vos=[cls.group_two],
Lukas Burgey's avatar
Lukas Burgey committed
84
        )
85
        cls.service_two_b = Service.get_service(
Lukas Burgey's avatar
Lukas Burgey committed
86
87
            'test_service_two_b',
            cls.site,
Lukas Burgey's avatar
Lukas Burgey committed
88
            vos=[cls.group_two],
Lukas Burgey's avatar
Lukas Burgey committed
89
90
        )

91
        cls.key = SSHPublicKey(
Lukas Burgey's avatar
Lukas Burgey committed
92
93
94
95
96
            name=cls.TEST_NAME,
            key=cls.TEST_KEY,
            user=cls.user,
        ).save()

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    # only needed to manually tear down the test data
    @classmethod
    def manualTearDownTestData(cls):
        # delete in reverse order
        for member in [
                'key',
                'service_one',
                'service_two_a',
                'service_two_b',
                'site',
                'site2',
                'user',
                'group_none',
                'group_one',
                'group_two',
                'idp',
        ]:
            if hasattr(cls, member):
                try:
                    getattr(cls, member).delete()
                # pylint: disable=bare-except
                except:
                    pass

Lukas Burgey's avatar
Lukas Burgey committed
121
122
123
124
125
126
127

# the user is logged in using session authentication
class LoggedInTest(BaseTestCase):

    client = None

    def setUp(self):
Lukas Burgey's avatar
Lukas Burgey committed
128
129
130
131
132
133
134
135
136
        self.client = Client()
        user = authenticate(
            username=self.USER_NAME,
            password=self.USER_PASSWORD,
        )
        self.assertIsNotNone(user)
        self.client.force_login(
            user=user,
        )