aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/conftest.py
blob: 3c310c8b3419613897611da2037b352b80d4c88e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import os

import pytest
from django.contrib.auth.models import Group, Permission
from django.db.models import Q
from django.test import RequestFactory, Client
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

from ctrack.caf.models import CAF
from ctrack.caf.tests.factories import GradingFactory
from ctrack.core.utils import _create_caf_app_service
from ctrack.organisations.models import (
    Address,
    AddressType,
    Mode,
    Organisation,
    Stakeholder,
    Submode,
)
from ctrack.organisations.tests.factories import (
    AddressFactory,
    OrganisationFactory,
    PersonFactory,
    RoleFactory,
)
from ctrack.users.models import User
from ctrack.users.tests.factories import UserFactory


@pytest.fixture
def user() -> User:
    return UserFactory()


@pytest.fixture
def inspector1() -> User:
    return UserFactory(first_name="Cyril", last_name="Bloanssette_Ridgewell")


@pytest.fixture
def inspector2() -> User:
    return UserFactory(first_name="Ogilvie", last_name="Cathroyd-Marylls")


@pytest.fixture
def submode():
    return Submode.objects.create(
        descriptor="Light Rail", mode=Mode.objects.create(descriptor="Rail")
    )


@pytest.fixture
def mode():
    return Mode.objects.create(descriptor="Rail")


@pytest.fixture
def role():
    return RoleFactory.create(name="Test Role")


@pytest.fixture
def org_with_people(role):
    org = OrganisationFactory.create(
        submode=None,
        name="TEST ORGANISATION",
        designation_type=3,
        registered_company_name="Test PLC",
        comments="NA",
    )
    PersonFactory.create(
        role=role,
        job_title="Test Job Title",
        predecessor=None,
        organisation__submode=None,
        organisation=org,
    )
    return org


@pytest.fixture(autouse=True)
def media_storage(settings, tmpdir):
    settings.MEDIA_ROOT = tmpdir.strpath


@pytest.fixture
def cct_user_group() -> Group:
    """
    TODO: An inspector will not require this many permissions! Reduce.
    """
    group = Group.objects.create(name="cct_user")
    ctrack_permissions = Permission.objects.filter(
        Q(codename__contains="address")
        | Q(codename__contains="addresstype")
        | Q(codename__contains="mode")
        | Q(codename__contains="organisation")
        | Q(codename__contains="role")
        | Q(codename__contains="submode")
        | Q(codename__contains="person")
        | Q(codename__contains="applicablesystem")
        | Q(codename__contains="caf")
        | Q(codename__contains="documentfile")
        | Q(codename__contains="filestore")
        | Q(codename__contains="grading")
        | Q(codename__contains="engagementtype")
        | Q(codename__contains="engagementevent")
        | Q(codename__contains="cafassessment")
        | Q(codename__contains="cafobjective")
        | Q(codename__contains="cafprinciple")
        | Q(codename__contains="cafcontributingoutcome")
        | Q(codename__contains="cafassessmentoutcomescore")
        | Q(codename__contains="achievmentlevel")
        | Q(codename__contains="igp")
        | Q(codename__contains="stakeholder")
        | Q(codename__contains="incidentreport")
    )
    group.permissions.add(*ctrack_permissions)
    return group


@pytest.fixture
def cct_user(cct_user_group) -> User:
    # For testing views which require redirects to permission-controlled
    # pages, we have to ensure our test user is has the requisite permissions here
    return UserFactory(groups=[cct_user_group])


@pytest.fixture
def person(user, submode, org_with_people):
    org = org_with_people
    role = RoleFactory.create(name="Compliance Inspector")
    person = PersonFactory.create(
        first_name="Toss",
        last_name="McBride",
        role=role,
        predecessor=None,
        organisation__submode=submode,
        organisation=org,
    )
    return person


@pytest.fixture
def org() -> Organisation:
    return OrganisationFactory()


@pytest.fixture
def addr() -> Address:
    address_type = AddressType.objects.create(descriptor="Random Type")
    return AddressFactory(type=address_type)


@pytest.fixture
def stakeholder_user(person):
    user = User.objects.create_user(username="toss", password="knob")
    stakeholder = Stakeholder.objects.create(person=person)
    user.stakeholder = stakeholder
    user.save()
    return user


@pytest.fixture
def request_factory() -> RequestFactory:
    return RequestFactory()


@pytest.fixture
def caf(org) -> CAF:
    # Quality gradings
    q_descriptors = ["Q1", "Q2", "Q3", "Q4", "Q5"]
    for g in q_descriptors:
        GradingFactory.create(descriptor=g, type="QUALITY")

    # Confidence gradings
    c_descriptors = ["C1", "C2", "C3", "C4", "C5"]
    for g in c_descriptors:
        GradingFactory.create(descriptor=g, type="CONFIDENCE")
    caf = _create_caf_app_service(c_descriptors, org, q_descriptors)
    return caf


@pytest.fixture
def browser(request):
    "Provide selenium webdriver instance."
    os.environ["PATH"] += os.pathsep + os.getcwd()
    options = Options()
    options.headless = True
    browser_ = webdriver.Firefox(firefox_options=options)
    yield browser_
    browser_.quit()


@pytest.fixture
def client(user):
    client = Client()
    client.force_login(user)
    return client