replaced unittest assertions pytest assertions (#26547)

This commit is contained in:
Aarif
2021-02-19 12:36:28 +05:00
committed by GitHub
parent a332aac1f6
commit 87fbbc7f3b
18 changed files with 229 additions and 273 deletions

View File

@@ -4,7 +4,7 @@ Tests for the LTI user management functionality
import string
import pytest
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.core.exceptions import PermissionDenied
from django.test import TestCase
@@ -42,7 +42,7 @@ class UserManagementHelperTest(TestCase):
@patch('django.contrib.auth.authenticate', return_value=None)
def test_permission_denied_for_unknown_user(self, _authenticate_mock):
with self.assertRaises(PermissionDenied):
with pytest.raises(PermissionDenied):
users.switch_user(self.request, self.lti_user, self.lti_consumer)
@patch('lms.djangoapps.lti_provider.users.login')
@@ -64,13 +64,11 @@ class UserManagementHelperTest(TestCase):
def test_random_username_generator(self):
for _idx in range(1000):
username = users.generate_random_edx_username()
self.assertLessEqual(len(username), 30, 'Username too long')
assert len(username) <= 30, 'Username too long'
# Check that the username contains only allowable characters
for char in range(len(username)): # lint-amnesty, pylint: disable=consider-using-enumerate
self.assertIn(
username[char], string.ascii_letters + string.digits,
assert username[char] in (string.ascii_letters + string.digits), \
u"Username has forbidden character '{}'".format(username[char])
)
@patch('lms.djangoapps.lti_provider.users.switch_user', autospec=True)
@@ -121,8 +119,8 @@ class AuthenticateLtiUserTest(TestCase):
self.request.user = lti_user.edx_user
assert self.request.user.is_authenticated
users.authenticate_lti_user(self.request, self.lti_user_id, self.lti_consumer)
self.assertFalse(create_user.called)
self.assertFalse(switch_user.called)
assert not create_user.called
assert not switch_user.called
def test_authentication_with_unauthenticated_user(self, create_user, switch_user):
lti_user = self.create_lti_user_model()
@@ -130,7 +128,7 @@ class AuthenticateLtiUserTest(TestCase):
with patch('django.contrib.auth.models.User.is_authenticated', new_callable=PropertyMock) as mock_is_auth:
mock_is_auth.return_value = False
users.authenticate_lti_user(self.request, self.lti_user_id, self.lti_consumer)
self.assertFalse(create_user.called)
assert not create_user.called
switch_user.assert_called_with(self.request, lti_user, self.lti_consumer)
def test_authentication_with_wrong_user(self, create_user, switch_user):
@@ -138,7 +136,7 @@ class AuthenticateLtiUserTest(TestCase):
self.request.user = self.old_user
assert self.request.user.is_authenticated
users.authenticate_lti_user(self.request, self.lti_user_id, self.lti_consumer)
self.assertFalse(create_user.called)
assert not create_user.called
switch_user.assert_called_with(self.request, lti_user, self.lti_consumer)
@@ -158,25 +156,25 @@ class CreateLtiUserTest(TestCase):
def test_create_lti_user_creates_auth_user_model(self):
users.create_lti_user('lti_user_id', self.lti_consumer)
self.assertEqual(User.objects.count(), 1)
assert User.objects.count() == 1
@patch('uuid.uuid4', return_value='random_uuid')
@patch('lms.djangoapps.lti_provider.users.generate_random_edx_username', return_value='edx_id')
def test_create_lti_user_creates_correct_user(self, uuid_mock, _username_mock):
users.create_lti_user('lti_user_id', self.lti_consumer)
self.assertEqual(User.objects.count(), 1)
assert User.objects.count() == 1
user = User.objects.get(username='edx_id')
self.assertEqual(user.email, 'edx_id@lti.example.com')
assert user.email == 'edx_id@lti.example.com'
uuid_mock.assert_called_with()
@patch('lms.djangoapps.lti_provider.users.generate_random_edx_username', side_effect=['edx_id', 'new_edx_id'])
def test_unique_username_created(self, username_mock):
User(username='edx_id').save()
users.create_lti_user('lti_user_id', self.lti_consumer)
self.assertEqual(username_mock.call_count, 2)
self.assertEqual(User.objects.count(), 2)
assert username_mock.call_count == 2
assert User.objects.count() == 2
user = User.objects.get(username='new_edx_id')
self.assertEqual(user.email, 'new_edx_id@lti.example.com')
assert user.email == 'new_edx_id@lti.example.com'
class LtiBackendTest(TestCase):
@@ -210,7 +208,7 @@ class LtiBackendTest(TestCase):
lti_user_id=self.lti_user_id,
lti_consumer=self.lti_consumer
)
self.assertEqual(user, self.edx_user)
assert user == self.edx_user
def test_missing_user_returns_none(self):
user = users.LtiBackend().authenticate(
@@ -219,7 +217,7 @@ class LtiBackendTest(TestCase):
lti_user_id='Invalid Username',
lti_consumer=self.lti_consumer
)
self.assertIsNone(user)
assert user is None
def test_non_lti_user_returns_none(self):
non_edx_user = UserFactory.create()
@@ -228,7 +226,7 @@ class LtiBackendTest(TestCase):
self.request,
username=non_edx_user.username,
)
self.assertIsNone(user)
assert user is None
def test_missing_lti_id_returns_null(self):
user = users.LtiBackend().authenticate(
@@ -236,7 +234,7 @@ class LtiBackendTest(TestCase):
username=self.edx_user.username,
lti_consumer=self.lti_consumer
)
self.assertIsNone(user)
assert user is None
def test_missing_lti_consumer_returns_null(self):
user = users.LtiBackend().authenticate(
@@ -244,12 +242,12 @@ class LtiBackendTest(TestCase):
username=self.edx_user.username,
lti_user_id=self.lti_user_id,
)
self.assertIsNone(user)
assert user is None
def test_existing_user_returned_by_get_user(self):
user = users.LtiBackend().get_user(self.edx_user.id)
self.assertEqual(user, self.edx_user)
assert user == self.edx_user
def test_get_user_returns_none_for_invalid_user(self):
user = users.LtiBackend().get_user(-1)
self.assertIsNone(user)
assert user is None