From d62a192c3d538e13c34037504f3cf6e22c641c2e Mon Sep 17 00:00:00 2001 From: TheresNoTime Date: Mon, 29 Jun 2026 14:01:45 +0100 Subject: [PATCH] Tests: Use .copy() to prevent mutation of shared module-level test data --- TWLight/users/tests.py | 65 ++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/TWLight/users/tests.py b/TWLight/users/tests.py index 98cb7745c3..ec96d579dd 100644 --- a/TWLight/users/tests.py +++ b/TWLight/users/tests.py @@ -1692,8 +1692,8 @@ def test_editor_email_not_null(self): platform, we shouldn't be overwriting it with a blank string. """ new_editor = EditorFactory(wp_registered=None) - new_identity = FAKE_IDENTITY - new_global_userinfo = FAKE_GLOBAL_USERINFO + new_identity = copy.copy(FAKE_IDENTITY) + new_global_userinfo = copy.copy(FAKE_GLOBAL_USERINFO) new_identity["sub"] = new_editor.wp_sub new_global_userinfo["id"] = new_identity["sub"] @@ -1715,8 +1715,8 @@ def test_editor_email_not_overwritten_with_blank(self): platform, we shouldn't be overwriting it with a blank string. """ new_editor = EditorFactory(wp_registered=None) - new_identity = FAKE_IDENTITY - new_global_userinfo = FAKE_GLOBAL_USERINFO + new_identity = copy.copy(FAKE_IDENTITY) + new_global_userinfo = copy.copy(FAKE_GLOBAL_USERINFO) new_identity["sub"] = new_editor.wp_sub new_global_userinfo["id"] = new_identity["sub"] @@ -1741,36 +1741,33 @@ def setUpTestData(cls): for editor in Editor.objects.all(): editor.delete() - @patch("urllib.request.urlopen") - def test_create_user_and_editor(self, mock_urlopen): - """ - OAuthBackend._create_user_and_editor() should: - * create a user - * with a suitable username and email - * without a password - * And a matching editor - """ - # HOTFIX: this test was failing in CI, but passing locally, just skipping it for now - # see: https://phabricator.wikimedia.org/T352896 - self.skipTest("See: https://phabricator.wikimedia.org/T352896") - oauth_backend = OAuthBackend() - oauth_data = FAKE_IDENTITY_DATA - identity = FAKE_IDENTITY - - mock_response = Mock() - mock_response.read.side_effect = [json.dumps(oauth_data)] * 7 - mock_urlopen.return_value = mock_response - - user, editor = oauth_backend._create_user_and_editor(identity) - - self.assertEqual(user.email, "alice@example.com") - self.assertEqual(user.username, "567823") - self.assertFalse(user.has_usable_password()) - - self.assertEqual(editor.user, user) - self.assertEqual(editor.wp_sub, 567823) - # We won't test the fields set by update_from_wikipedia, as they are - # tested elsewhere. + @patch("urllib.request.urlopen") + def test_create_user_and_editor(self, mock_urlopen): + """ + OAuthBackend._create_user_and_editor() should: + * create a user + * with a suitable username and email + * without a password + * And a matching editor + """ + oauth_backend = OAuthBackend() + oauth_data = copy.copy(FAKE_IDENTITY_DATA) + identity = copy.copy(FAKE_IDENTITY) + + mock_response = Mock() + mock_response.read.side_effect = [json.dumps(oauth_data)] * 7 + mock_urlopen.return_value = mock_response + + user, editor = oauth_backend._create_user_and_editor(identity) + + self.assertEqual(user.email, "alice@example.com") + self.assertEqual(user.username, "567823") + self.assertFalse(user.has_usable_password()) + + self.assertEqual(editor.user, user) + self.assertEqual(editor.wp_sub, 567823) + # We won't test the fields set by update_from_wikipedia, as they are + # tested elsewhere. # We mock out this function for two reasons: # 1) To prevent its call to an external API, which we would have otherwise