From 4162cc100f19bdd66e6775d631d430092c492725 Mon Sep 17 00:00:00 2001 From: Jingchao Zhong Date: Tue, 16 May 2023 03:49:18 -0700 Subject: [PATCH] Add tests for new plugin: Email Address --- tests/plugins/email_address_test.py | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/plugins/email_address_test.py diff --git a/tests/plugins/email_address_test.py b/tests/plugins/email_address_test.py new file mode 100644 index 00000000..cb3fccb6 --- /dev/null +++ b/tests/plugins/email_address_test.py @@ -0,0 +1,60 @@ +import pytest +from detect_secrets.plugins.email_address import EmailAddressDetector + + +class TestEmailAddressDetector: + """ + Testing strategy + + Cover the cartesian product of these partitions: + + 1. Partition on email address format: + a. Valid email addresses + b. Invalid email addresses + + 2. Partition on line content: + a. email address is the only content + b. email address is part of a larger string + + And cover these cases: + + 1. Partition on whitelist email addresses: + a. email address is in the whitelist + b. email address is not in the whitelist + """ + + @pytest.mark.parametrize( + 'payload, should_flag', + [ + # Valid email addresses, only content + ('user@example.com', True), + ('user.name@example.com', True), + ('user_name@example.com', True), + ('user-name@example.com', True), + ('user+name@example.com', True), + ('user@ex_ample.com', True), + ('user@-example.com', True), + ('user@example-.com', True), + ('user.name+category@example.com', True), + # Valid email addresses, part of larger string + ('This is an email address: user@example.com', True), + ('user@example.com is a valid email address', True), + # Invalid email addresses + ('user@com', False), + ('@example.com', False), + ('user@.com', False), + ('user@ex..com', False), + # Whitelist email addresses + ('noreply@github.com', False), + ('git@github.com', False), + # Non-whitelist email addresses + ('user@gmail.com', True), + ('user@yahoo.com', True), + ('user@hotmail.com', True), + ], + ) + def test_analyze_line(self, payload, should_flag): + logic = EmailAddressDetector() + + output = logic.analyze_line(filename='mock_filename', line=payload) + assert len(output) == int(should_flag)