-
Notifications
You must be signed in to change notification settings - Fork 163
Tests pour le spam_detector #6723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Tests pour le spam_detector #6723
Conversation
philippemilink
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zds/utils/tests/tests_antispam.py
Outdated
| """class SpamDetectorTestCase(TestCase): | ||
|
|
||
| @patch('zds.utils.spam_detector.SpamDetector.send_alert') #To avoid sending the actual alert | ||
| def test_check_profile_no_bio(self, mock_send_alert): | ||
| # Create a user profile | ||
| self.user = User.objects.create_user(username="testuser1", password="password") | ||
| self.profile = Profile.objects.create(user=self.user) | ||
|
|
||
| self.spam_detector = SpamDetector() | ||
| #User without a biography | ||
| self.profile.biography = "" | ||
|
|
||
| # Appeler la méthode qui devrait vérifier la biographie de l'utilisateur | ||
| self.spam_detector.check_profile(self.profile) | ||
|
|
||
| # Vérifier que l'alerte n'a pas été envoyée | ||
| mock_send_alert.assert_not_called() | ||
|
|
||
| # Vérifier le message de log | ||
| with self.assertLogs(self.spam_detector.logger, level='INFO') as log: | ||
| self.spam_detector.check_profile(self.profile) | ||
| self.assertIn("∅ testuser has no biography", log.output) | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Que faire de ce code ?
| @patch("zds.utils.spam_detector.SpamDetector.send_alert") | ||
| def test_check_profile_no_bio(self, mock_send_alert): | ||
| """User with no biography should not trigger an alert.""" | ||
| self.spam_detector.check_profile(self.profile1) | ||
| mock_send_alert.assert_not_called() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dans les tests de zds-site, on utilise @patch() pour s'assurer que les signaux ont bien été émis, par exemple :
| @patch("zds.tutorialv2.signals.contributors_management") |
Pour tester ça, on ferait plutôt :
self.spam_detector.check_profile(self.profile1)
self.assertEqual(Alert.objects.all().count(), 0)
philippemilink
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ce qu'on a dit lors de notre dernière réunion, c'est que pour les tests il ne devrait pas y avoir besoin d'un fichier JSON avec des données, mais qu'on peut générer des fixtures avec du contenu qui est du spam, par exemple :
- Créer 10 profils avec une biographie qui contient du spam (phrase avec un préfixe, par exemple :
factory.fuzzy.FuzzyText(prefix="spam")). Bannir ces profils - Créer 10 profils avec une biographique qui ne contient pas de spam (
factory.fuzzy.FuzzyText(prefix="correct")) - Entraîner le modèle
- Créer un profil avec une biographie et spammeur et un autre profile non spammeur et tester que la prédiction du modèle est correcte.
Écrire les tests pour assurer le bon fonctionnement de spam_detector
Fix #6720Contrôle qualité