|
| 1 | +from django.contrib.auth.models import User |
| 2 | +from django.test import TestCase |
| 3 | +from django.db.models import Q |
| 4 | + |
| 5 | +from kitsune.questions.tests import ( |
| 6 | + AnswerFactory, |
| 7 | + QuestionFactory, |
| 8 | + QuestionVoteFactory, |
| 9 | + AnswerVoteFactory, |
| 10 | +) |
| 11 | +from kitsune.users.tests import ProfileFactory |
| 12 | +from kitsune.wiki.tests import RevisionFactory |
| 13 | +from kitsune.messages.models import InboxMessage, OutboxMessage |
| 14 | +from kitsune.gallery.tests import ImageFactory, VideoFactory |
| 15 | +from kitsune.forums.tests import ThreadFactory, PostFactory |
| 16 | + |
| 17 | + |
| 18 | +class TestDeleteNonMigratedUsersMigrationQuery(TestCase): |
| 19 | + """Test the migration that deletes non-migrated users.""" |
| 20 | + |
| 21 | + def setUp(self): |
| 22 | + # Create users with different combinations of migration status and content creation |
| 23 | + |
| 24 | + # Case 1: Non-migrated user with no content (should be deleted) |
| 25 | + ProfileFactory(user__username="user1", is_fxa_migrated=False) |
| 26 | + |
| 27 | + # Case 2: Non-migrated user with a question (should be kept) |
| 28 | + user2 = ProfileFactory(user__username="user2", is_fxa_migrated=False).user |
| 29 | + QuestionFactory(creator=user2) |
| 30 | + |
| 31 | + # Case 3: Non-migrated user with an answer (should be kept) |
| 32 | + user3 = ProfileFactory(user__username="user3", is_fxa_migrated=False).user |
| 33 | + AnswerFactory(creator=user3) |
| 34 | + |
| 35 | + # Case 4: Non-migrated user with a revision (should be kept) |
| 36 | + user4 = ProfileFactory(user__username="user4", is_fxa_migrated=False).user |
| 37 | + RevisionFactory(creator=user4) |
| 38 | + |
| 39 | + # Case 5: Migrated user with no content (should be kept) |
| 40 | + ProfileFactory(user__username="user5", is_fxa_migrated=True) |
| 41 | + |
| 42 | + # Case 6: Non-migrated user with a question vote (should be kept) |
| 43 | + user6 = ProfileFactory(user__username="user6", is_fxa_migrated=False).user |
| 44 | + QuestionVoteFactory(creator=user6) |
| 45 | + |
| 46 | + # Case 7: Non-migrated user with an answer vote (should be kept) |
| 47 | + user7 = ProfileFactory(user__username="user7", is_fxa_migrated=False).user |
| 48 | + AnswerVoteFactory(creator=user7) |
| 49 | + |
| 50 | + # Case 8: Non-migrated user who is a sender of inbox messages (should be kept) |
| 51 | + user8 = ProfileFactory(user__username="user8", is_fxa_migrated=False).user |
| 52 | + InboxMessage.objects.create(to=user2, sender=user8, message="test") |
| 53 | + |
| 54 | + # Case 9: Non-migrated user with an outbox message (should be kept) |
| 55 | + user9 = ProfileFactory(user__username="user9", is_fxa_migrated=False).user |
| 56 | + outbox_msg = OutboxMessage.objects.create(sender=user9, message="test") |
| 57 | + outbox_msg.to.add(user2) |
| 58 | + |
| 59 | + # Case 10: Non-migrated user who is a sender of inbox messages (should be kept) |
| 60 | + user10 = ProfileFactory(user__username="user10", is_fxa_migrated=False).user |
| 61 | + InboxMessage.objects.create(to=user2, sender=user10, message="test") |
| 62 | + |
| 63 | + # Case 11: Non-migrated user with a gallery image (should be kept) |
| 64 | + user11 = ProfileFactory(user__username="user11", is_fxa_migrated=False).user |
| 65 | + ImageFactory(creator=user11) |
| 66 | + |
| 67 | + # Case 12: Non-migrated user with a gallery video (should be kept) |
| 68 | + user12 = ProfileFactory(user__username="user12", is_fxa_migrated=False).user |
| 69 | + VideoFactory(creator=user12) |
| 70 | + |
| 71 | + # Case 13: Non-migrated user with a forum thread (should be kept) |
| 72 | + user13 = ProfileFactory(user__username="user13", is_fxa_migrated=False).user |
| 73 | + ThreadFactory(creator=user13) |
| 74 | + |
| 75 | + # Case 14: Non-migrated user with a forum post (should be kept) |
| 76 | + user14 = ProfileFactory(user__username="user14", is_fxa_migrated=False).user |
| 77 | + thread = ThreadFactory() |
| 78 | + PostFactory(thread=thread, author=user14) |
| 79 | + |
| 80 | + # Case 15: Non-migrated user who reviewed a revision (should be kept) |
| 81 | + user15 = ProfileFactory(user__username="user15", is_fxa_migrated=False).user |
| 82 | + RevisionFactory(reviewer=user15) |
| 83 | + |
| 84 | + def test_direct_query_logic(self): |
| 85 | + """Test the query logic of the migration directly without going through apps.""" |
| 86 | + # Query using the same logic as the migration but with direct model references |
| 87 | + users_to_delete = User.objects.filter(profile__is_fxa_migrated=False).exclude( |
| 88 | + Q(answer_votes__isnull=False) |
| 89 | + | Q(answers__isnull=False) |
| 90 | + | Q(award_creator__isnull=False) |
| 91 | + | Q(badge__isnull=False) |
| 92 | + | Q(created_revisions__isnull=False) |
| 93 | + | Q(gallery_images__isnull=False) |
| 94 | + | Q(gallery_videos__isnull=False) |
| 95 | + | Q(inboxmessage__isnull=False) |
| 96 | + | Q(outbox__isnull=False) |
| 97 | + | Q(poll_votes__isnull=False) |
| 98 | + | Q(post__isnull=False) |
| 99 | + | Q(question_votes__isnull=False) |
| 100 | + | Q(questions__isnull=False) |
| 101 | + | Q(readied_for_l10n_revisions__isnull=False) |
| 102 | + | Q(reviewed_revisions__isnull=False) |
| 103 | + | Q(thread__isnull=False) |
| 104 | + | Q(wiki_post_set__isnull=False) |
| 105 | + | Q(wiki_thread_set__isnull=False) |
| 106 | + ) |
| 107 | + |
| 108 | + # Only user1 should be in this queryset |
| 109 | + self.assertEqual(users_to_delete.count(), 1) |
| 110 | + self.assertEqual(users_to_delete.first().username, "user1") |
0 commit comments