-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_views.py
More file actions
212 lines (162 loc) · 7.83 KB
/
Copy pathtest_views.py
File metadata and controls
212 lines (162 loc) · 7.83 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
import pytest
import benefits.core.session
from benefits.core import views
from benefits.core.middleware import TEMPLATE_USER_ERROR
from benefits.core.models import EnrollmentFlow
from benefits.core.models.common import PemData
from benefits.core.models.enrollment import EligibilityApiVerificationRequest
from benefits.routes import routes
@pytest.fixture
def session_reset_spy(mocker):
return mocker.spy(benefits.core.session, "reset")
@pytest.mark.django_db
class TestIndexView:
@pytest.fixture
def view(self, app_request):
v = views.IndexView()
v.setup(app_request)
return v
def test_view(self, view):
assert view.template_name == "core/index.html"
def test_get(self, view, app_request, mocked_session_reset):
response = view.get(app_request)
assert response.status_code == 200
mocked_session_reset.assert_called_once()
def test_form_valid(self, view, model_TransitAgency):
form = view.form_class(data=dict(select_transit_agency=model_TransitAgency.slug))
assert form.is_valid()
view.form_valid(form)
assert view.success_url == model_TransitAgency.eligibility_index_url
@pytest.mark.django_db
class TestAgencyIndexView:
@pytest.fixture
def view(self, app_request, model_TransitAgency):
v = views.AgencyIndexView()
v.setup(app_request, agency=model_TransitAgency)
return v
def test_view(self, view):
assert view.template_name == "core/index--agency.html"
def test_get(self, view, app_request, mocked_session_reset, mocked_session_update):
response = view.get(app_request)
assert response.status_code == 200
mocked_session_reset.assert_called_once()
mocked_session_update.assert_called_once()
def test_get_context_data(self, view, model_TransitAgency):
context = view.get_context_data()
for key, value in model_TransitAgency.index_context.items():
assert context[key] == value
@pytest.mark.django_db
class TestAgencyCardView:
@pytest.fixture
def view(self, app_request, model_TransitAgency):
v = views.AgencyCardView()
v.setup(app_request, agency=model_TransitAgency)
return v
def test_view(self, view):
assert view.pattern_name == routes.ELIGIBILITY_CONFIRM
def test_get__with_one_eligibility_api_flow(
self, view, app_request, mocked_session_reset, mocked_session_update, model_EnrollmentFlow_with_eligibility_api
):
agency = view.kwargs["agency"]
# recreate the condition of the live view, where the agency kwarg is passed to the get() call
response = view.get(app_request, agency=agency)
assert response.status_code == 302
mocked_session_reset.assert_called_once()
update_calls = mocked_session_update.mock_calls
assert len(update_calls) == 2
assert update_calls[0].kwargs["agency"] == agency
assert update_calls[0].kwargs["origin"] == agency.index_url
assert update_calls[1].kwargs["flow"] == model_EnrollmentFlow_with_eligibility_api
def test_get__with_multiple_eligibility_api_flow(
self, view, app_request, mocked_session_reset, mocked_session_update, model_EnrollmentFlow_with_eligibility_api
):
agency = view.kwargs["agency"]
# fake multiple Eligibility API flows for the agency
new_flow = EnrollmentFlow.objects.get(pk=model_EnrollmentFlow_with_eligibility_api.id)
new_flow.label = "New flow"
new_flow.system_name = "senior"
new_flow.pk = None
new_flow.transit_agency = agency
new_flow.save()
# recreate the condition of the live view, where the agency kwarg is passed to the get() call
response = view.get(app_request, agency=agency)
assert response.status_code == 302
mocked_session_reset.assert_called_once()
assert mocked_session_update.mock_calls[1].kwargs["flow"] == new_flow
def test_get__without_eligibility_api_flow(
self, view, app_request, mocked_session_reset, mocked_session_update, model_EnrollmentFlow_with_scope_and_claim
):
agency = view.kwargs["agency"]
# we don't configure a flow with Eligibility API details
model_EnrollmentFlow_with_scope_and_claim.transit_agency = agency
model_EnrollmentFlow_with_scope_and_claim.save()
# recreate the condition of the live view, where the agency kwarg is passed to the get() call
response = view.get(app_request, agency=agency)
assert response.status_code == 200
assert response.template_name == TEMPLATE_USER_ERROR
mocked_session_reset.assert_called_once()
mocked_session_update.assert_called_once_with(app_request, agency=agency, origin=agency.index_url)
@pytest.mark.django_db
class TestAgencyEligibilityIndexView:
@pytest.fixture
def view(self, app_request, model_TransitAgency):
v = views.AgencyEligibilityIndexView()
v.setup(app_request, agency=model_TransitAgency)
return v
def test_view(self, view):
assert view.pattern_name == routes.ELIGIBILITY_INDEX
def test_get(self, view, app_request, mocked_session_reset, mocked_session_update):
agency = view.kwargs["agency"]
# recreate the condition of the live view, where the agency kwarg is passed to the get() call
response = view.get(app_request, agency=agency)
assert response.status_code == 302
mocked_session_reset.assert_called_once()
mocked_session_update.assert_called_once_with(app_request, agency=agency, origin=agency.index_url)
@pytest.mark.django_db
class TestAgencyPublicKeyView:
@pytest.fixture
def view(self, app_request, model_TransitAgency):
v = views.AgencyPublicKeyView()
v.setup(app_request, agency=model_TransitAgency)
return v
def test_get(self, view, app_request, model_EligibilityApiVerificationRequest):
agency = view.kwargs["agency"]
# recreate the condition of the live view, where the agency kwarg is passed to the get() call
response = view.get(app_request, agency=agency)
assert response.status_code == 200
assert response.headers["Content-Type"] == "text/plain"
assert response.content.decode("utf-8") == model_EligibilityApiVerificationRequest.client_public_key_data
def test_get_select_first_instance(self, view, app_request, model_EligibilityApiVerificationRequest):
"""
Ensures that if multiple EligibilityApiVerificationRequest objects exist,
the view returns the public key from the first one.
"""
# Create a second verification request instance with different data
public_key = PemData.objects.create(label="Test public key 2", text_secret_name="pem-secret-data-2")
EligibilityApiVerificationRequest.objects.create(client_public_key=public_key, api_public_key=public_key)
# Ensure we have more than one object in the DB
assert EligibilityApiVerificationRequest.objects.count() > 1
# The 'first' instance should be the one from the fixture
expected_key = model_EligibilityApiVerificationRequest.client_public_key_data
agency = view.kwargs["agency"]
response = view.get(app_request, agency=agency)
assert response.status_code == 200
assert response.content.decode("utf-8") == expected_key
@pytest.mark.django_db
class TestHelpView:
@pytest.fixture
def view(self, app_request):
v = views.HelpView()
v.setup(app_request)
return v
def test_view(self, view):
assert view.template_name == "core/help.html"
@pytest.mark.django_db
class TestLoggedOutView:
@pytest.fixture
def view(self, app_request):
v = views.LoggedOutView()
v.setup(app_request)
return v
def test_view(self, view):
assert view.template_name == "core/logged-out.html"