From fb41a847d184843bf50a6856933809ae726628bb Mon Sep 17 00:00:00 2001
From: Peter Hutterer <peter.hutterer@who-t.net>
Date: Wed, 22 Mar 2023 11:12:51 +1000
Subject: [PATCH] tests: parametrize the tests with the readonly setting

After having to deal with a runway bot thanks to a copy-paste error,
let's make sure all our tests do the right thing with readonly
true/false.
---
 tests/test_damspam.py | 88 ++++++++++++++++++++-----------------------
 1 file changed, 41 insertions(+), 47 deletions(-)

diff --git a/tests/test_damspam.py b/tests/test_damspam.py
index 5434c11..33cec14 100644
--- a/tests/test_damspam.py
+++ b/tests/test_damspam.py
@@ -79,8 +79,9 @@ def test_webhook_abstractions(issue_event: dict):
     assert event.removed_labels[0].title == "API"
 
 
+@pytest.mark.parametrize("readonly", (True, False))
 @patch("gitlab.Gitlab")
-def test_builder(gitlab):
+def test_builder(gitlab, readonly):
     @attr.s
     class Status:
         auth_called: bool = attr.ib(init=False, default=False)
@@ -129,11 +130,8 @@ def test_builder(gitlab):
     builder.set_issue_iid(123)
     assert builder.issue == status.issue
 
-    builder.set_readonly(True)
-    assert builder.readonly is True
-
-    builder.set_readonly(False)
-    assert builder.readonly is False
+    builder.set_readonly(readonly)
+    assert builder.readonly == readonly
 
     def projects_get_tracker(**kwargs):
         assert "id" in kwargs
@@ -159,10 +157,11 @@ def test_builder(gitlab):
     assert damspam.project == status.project
     assert damspam.issue == status.issue
     assert damspam.tracker_issue == status.tracker_issue
-    assert damspam.readonly is False
+    assert damspam.readonly == readonly
 
 
-def test_link_to_tracker():
+@pytest.mark.parametrize("readonly", (True, False))
+def test_link_to_tracker(readonly):
     gl, project, issue, tracker_issue = (
         MagicMock(),
         MagicMock(),
@@ -183,26 +182,24 @@ def test_link_to_tracker():
     issue.links = MagicMock()
     issue.links.create = lambda args: links.append(args)
 
-    damspam = DamSpamIssue(gl, project, issue, tracker_issue)
-    # expect nothing to happen
-    damspam.readonly = True
+    damspam = DamSpamIssue(gl, project, issue, tracker_issue, readonly)
     damspam.link_to_tracker()
-    assert links == []
+    if readonly:
+        # expect nothing to happen
+        assert links == []
+    else:
+        # epxect the links to be called with the tracker issue data
+        assert links == [
+            {
+                "target_project_id": tracker_issue.project_id,
+                "target_issue_iid": tracker_issue.iid,
+            }
+        ]
 
-    # epxect the links to be called with the tracker issue data
-    damspam.readonly = False
-    damspam.link_to_tracker()
-    assert links == [
-        {
-            "target_project_id": tracker_issue.project_id,
-            "target_issue_iid": tracker_issue.iid,
-        }
-    ]
-
-    # expect nothing to happen if we don't have a tracker issue
+    # expect nothing to happen if we don't have a tracker issue,
+    # regardless of readonly setting
     links = []
     damspam.tracker_issue = None
-    damspam.readonly = True
     damspam.link_to_tracker()
     assert links == []
 
@@ -211,7 +208,8 @@ def test_link_to_tracker():
         damspam.link_to_tracker()
 
 
-def test_mark_as_spam():
+@pytest.mark.parametrize("readonly", (True, False))
+def test_mark_as_spam(readonly):
     gl, project, issue, tracker_issue = (
         MagicMock(),
         MagicMock(),
@@ -242,27 +240,25 @@ def test_mark_as_spam():
     issue.confidential = False
     issue.save = issue_save
 
-    damspam = DamSpamIssue(gl, project, issue, tracker_issue)
-    # expect nothing to happen
-    damspam.readonly = True
+    damspam = DamSpamIssue(gl, project, issue, tracker_issue, readonly=readonly)
     damspam.mark_as_spam()
-    assert issue.labels == ["existing-label"]
-    assert issue.confidential is False
-    assert status.saved is False
-
-    # now for realz
-    damspam.readonly = False
-    damspam.mark_as_spam()
-    assert sorted(issue.labels) == sorted(["existing-label", "spam"])
-    assert issue.confidential is True
-    assert status.saved is True
+    if readonly:
+        # expect nothing to happen
+        assert issue.labels == ["existing-label"]
+        assert issue.confidential is False
+        assert status.saved is False
+    else:
+        assert sorted(issue.labels) == sorted(["existing-label", "spam"])
+        assert issue.confidential is True
+        assert status.saved is True
 
     damspam.tracker_issue = issue
     with pytest.raises(AssertionError):
         damspam.mark_as_spam()
 
 
-def test_block_issue_creator():
+@pytest.mark.parametrize("readonly", (True, False))
+def test_block_issue_creator(readonly):
     gl, project, issue, tracker_issue = (
         MagicMock(),
         MagicMock(),
@@ -303,19 +299,17 @@ def test_block_issue_creator():
     gl.users = MagicMock()
     gl.users.get = users_get
 
-    damspam = DamSpamIssue(gl, project, issue, tracker_issue)
-    damspam.readonly = True
-    damspam.block_issue_creator()
-    assert status.blocked is False
-
-    damspam.readonly = False
+    damspam = DamSpamIssue(gl, project, issue, tracker_issue, readonly)
     damspam.block_issue_creator()
-    assert status.blocked is True
+    if readonly:
+        assert status.blocked is False
+    else:
+        assert status.blocked is True
 
     # check the 6 months time barrier
     status.blocked = False
     dt = timedelta(weeks=26)
     spammer.created_at = datetime.isoformat(datetime.now(tz=timezone.utc) - dt)
-    damspam.readonly = False
+    # never blocks, regardless of readonly setting
     damspam.block_issue_creator()
     assert status.blocked is False
-- 
GitLab