Skip to content

Commit b40ab99

Browse files
committed
Simplify test_login_errors
1 parent 02a65f3 commit b40ab99

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

components/tools/OmeroWeb/test/integration/test_api_login.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
"""
2323

2424
import pytest
25-
from omeroweb.testlib import IWebTest, get_json, post
25+
from omeroweb.testlib import IWebTest, get_json
2626
from django.urls import reverse, NoReverseMatch
2727
from omeroweb.api import api_settings
2828
from django.test import Client
2929
from omero_marshal import OME_SCHEMA_URL
30-
import json
3130

3231

3332
class TestLogin(IWebTest):
@@ -150,22 +149,28 @@ def test_login_invalid_csrf_token(self):
150149
"Server: This field is required.")],
151150
[{'username': 'nobody', 'password': 'fake', 'server': 1},
152151
("Connection not available, "
153-
"please check your user name and password.")]
152+
"please check your user name and password.")],
153+
[{'user': 1},
154+
("Username: This field is required. "
155+
"Password: This field is required. "
156+
"Server: This field is required.")]
154157
])
155158
def test_login_errors(self, credentials):
156159
"""
157160
Tests that we get expected form validation errors if try to login
158161
without required fields, as 'guest' or with invalid username/password.
159162
"""
160-
django_client = self.django_root_client
161-
# test the most recent version
162-
version = api_settings.API_VERSIONS[-1]
163-
request_url = reverse('api_login', kwargs={'api_version': version})
163+
client = Client(enforce_csrf_checks=True)
164+
client.get(reverse("weblogin"))
165+
csrf_token = client.cookies["csrftoken"].value
164166
data = credentials[0]
165167
message = credentials[1]
166-
rsp = post(django_client, request_url, data, status_code=403)
167-
rsp = json.loads(rsp.content)
168-
assert rsp['message'] == message
168+
rsp = client.post(
169+
self.get_login_url(),
170+
data,
171+
headers={"X-CSRFToken": csrf_token})
172+
assert rsp.status_code == 403
173+
assert rsp.json()['message'] == message
169174

170175
def test_login_example(self):
171176
"""

0 commit comments

Comments
 (0)