From d8149443fafae6131a52443a0c5e333bf114aab2 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Fri, 21 Apr 2023 10:06:03 +0200 Subject: refactor and basic test for report to mods --- test/mastodon-tl-tests.el | 122 +++++++++++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 45 deletions(-) (limited to 'test/mastodon-tl-tests.el') diff --git a/test/mastodon-tl-tests.el b/test/mastodon-tl-tests.el index a3ac330..f1b4735 100644 --- a/test/mastodon-tl-tests.el +++ b/test/mastodon-tl-tests.el @@ -1073,53 +1073,85 @@ correct value for following, as well as notifications enabled or disabled." (let ((response-buffer-true (current-buffer))) (insert mastodon-tl--follow-notify-true-response) (with-mock - (mock (mastodon-http--post url-follow-only nil) - => response-buffer-true) - (should - (equal - (mastodon-tl--do-user-action-function url-follow-only - user-name - user-handle - "follow") - "User some-user (@some-user@instance.url) followed!")) - (mock (mastodon-http--post url-mute nil) - => response-buffer-true) - (should - (equal - (mastodon-tl--do-user-action-function url-mute - user-name - user-handle - "mute") - "User some-user (@some-user@instance.url) muted!")) - (mock (mastodon-http--post url-block nil) - => response-buffer-true) - (should - (equal - (mastodon-tl--do-user-action-function url-block - user-name - user-handle - "block") - "User some-user (@some-user@instance.url) blocked!"))) + (mock (mastodon-http--post url-follow-only nil) + => response-buffer-true) + (should + (equal + (mastodon-tl--do-user-action-function url-follow-only + user-name + user-handle + "follow") + "User some-user (@some-user@instance.url) followed!")) + (mock (mastodon-http--post url-mute nil) + => response-buffer-true) + (should + (equal + (mastodon-tl--do-user-action-function url-mute + user-name + user-handle + "mute") + "User some-user (@some-user@instance.url) muted!")) + (mock (mastodon-http--post url-block nil) + => response-buffer-true) + (should + (equal + (mastodon-tl--do-user-action-function url-block + user-name + user-handle + "block") + "User some-user (@some-user@instance.url) blocked!"))) (with-mock - (mock (mastodon-http--post url-true nil) => response-buffer-true) - (should - (equal - (mastodon-tl--do-user-action-function url-true - user-name - user-handle - "follow" - "true") - "Receiving notifications for user some-user (@some-user@instance.url)!"))))) + (mock (mastodon-http--post url-true nil) => response-buffer-true) + (should + (equal + (mastodon-tl--do-user-action-function url-true + user-name + user-handle + "follow" + "true") + "Receiving notifications for user some-user (@some-user@instance.url)!"))))) (with-temp-buffer (let ((response-buffer-false (current-buffer))) (insert mastodon-tl--follow-notify-false-response) (with-mock - (mock (mastodon-http--post url-false nil) => response-buffer-false) - (should - (equal - (mastodon-tl--do-user-action-function url-false - user-name - user-handle - "follow" - "false") - "Not receiving notifications for user some-user (@some-user@instance.url)!"))))))) + (mock (mastodon-http--post url-false nil) => response-buffer-false) + (should + (equal + (mastodon-tl--do-user-action-function url-false + user-name + user-handle + "follow" + "false") + "Not receiving notifications for user some-user (@some-user@instance.url)!"))))))) + +(ert-deftest mastodon-tl--report-to-mods-params-alist () + "" + (with-temp-buffer + (let ((toot mastodon-tl-test-base-toot)) + (with-mock + (mock (mastodon-http--api "reports") => "https://instance.url/api/v1/reports") + (mock (mastodon-tl--toot-or-base + (mastodon-tl--property 'toot-json :no-move)) + => mastodon-tl-test-base-toot) + (mock (read-string "Add comment [optional]: ") => "Dummy complaint") + + (stub y-or-n-p => nil) ; no to all + (should (equal (mastodon-tl--report-params) + '(("account_id" . 42) + ("comment" . "Dummy complaint") + ("category" . "other")))) + (with-mock + (stub y-or-n-p => t) ; yes to all + (mock (mastodon-tl--read-rules-ids) => '(1 2 3)) + ;; (mock (y-or-n-p "Also report status at point? ") => t) + ;; (mock (y-or-n-p "Forward to remote admin? ") => nil) + ;; (mock (y-or-n-p "Cite a rule broken? ") => nil) + ;; (mock (y-or-n-p "Spam? ") => nil) + (should (equal (mastodon-tl--report-params) + '(("rule_ids[]" . 3) + ("rule_ids[]" . 2) + ("rule_ids[]" . 1) + ("account_id" . 42) + ("comment" . "Dummy complaint") + ("status_ids[]" . 61208) + ("forward" . "true"))))))))) -- cgit v1.2.3 From 96fc842db36d138b3d6cbd8d109c61fb6aa2cf6f Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Fri, 21 Apr 2023 11:11:13 +0200 Subject: refactor tl--report-build-params and a hack test for it --- lisp/mastodon-tl.el | 28 ++++++++++++++++++---------- test/mastodon-tl-tests.el | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 10 deletions(-) (limited to 'test/mastodon-tl-tests.el') diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 0355a27..91cc989 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -2116,16 +2116,24 @@ Prefix is sent to `mastodon-tl--show-tag-timeline', which see." (forward-p (when (y-or-n-p "Forward to remote admin? ") "true")) (rules (when (y-or-n-p "Cite a rule broken? ") (mastodon-tl--read-rules-ids))) - (cat (unless rules (if (y-or-n-p "Spam? ") "spam" "other"))) - (params `(("account_id" . ,account-id) - ,(when comment - `("comment" . ,comment)) - ,(when toot-id - `("status_ids[]" . ,toot-id)) - ,(when forward-p - `("forward" . ,forward-p)) - ,(when cat - `("category" . ,cat))))) + (cat (unless rules (if (y-or-n-p "Spam? ") "spam" "other")))) + (mastodon-tl--report-build-params account-id comment toot-id + forward-p cat rules))) + +(defun mastodon-tl--report-build-params + (account-id comment toot-id forward-p cat &optional rules) + "Build the parameters alist based on user responses. +ACCOUNT-ID, COMMENT, TOOD-ID, FORWARD-P, CAT, and RULES are all from +`mastodon-tl--report-params', which see." + (let ((params `(("account_id" . ,account-id) + ,(when comment + `("comment" . ,comment)) + ,(when toot-id + `("status_ids[]" . ,toot-id)) + ,(when forward-p + `("forward" . ,forward-p)) + ,(when cat + `("category" . ,cat))))) (when rules (let ((alist (mastodon-http--build-array-params-alist "rule_ids[]" rules))) diff --git a/test/mastodon-tl-tests.el b/test/mastodon-tl-tests.el index f1b4735..5dd6a04 100644 --- a/test/mastodon-tl-tests.el +++ b/test/mastodon-tl-tests.el @@ -1155,3 +1155,45 @@ correct value for following, as well as notifications enabled or disabled." ("comment" . "Dummy complaint") ("status_ids[]" . 61208) ("forward" . "true"))))))))) + +(ert-deftest mastodon-tl--report-build-params () + "" + (should (equal + (mastodon-tl--report-build-params 42 "Dummy complaint" + 61208 "true" nil '(1 2 3)) + '(("rule_ids[]" . 3) + ("rule_ids[]" . 2) + ("rule_ids[]" . 1) + ("account_id" . 42) + ("comment" . "Dummy complaint") + ("status_ids[]" . 61208) + ("forward" . "true")))) + (should (equal + (mastodon-tl--report-build-params 42 "Dummy complaint" + nil "true" nil nil) + '(("account_id" . 42) + ("comment" . "Dummy complaint") + ("forward" . "true")))) + (should (equal + (mastodon-tl--report-build-params 42 "Dummy complaint" + 61208 "true" "spam" nil) + '(("account_id" . 42) + ("comment" . "Dummy complaint") + ("status_ids[]" . 61208) + ("forward" . "true") + ("category" . "spam")))) + (should (equal + (mastodon-tl--report-build-params 42 "Dummy complaint" + 61208 "true" "other" nil) + '(("account_id" . 42) + ("comment" . "Dummy complaint") + ("status_ids[]" . 61208) + ("forward" . "true") + ("category" . "other")))) + (should (equal + (mastodon-tl--report-build-params 42 "Dummy complaint" + 61208 nil "spam" nil) + '(("account_id" . 42) + ("comment" . "Dummy complaint") + ("status_ids[]" . 61208) + ("category" . "spam"))))) -- cgit v1.2.3 From d0b971aeafa71087f6b45cb78185eecd8faa8112 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Fri, 21 Apr 2023 15:33:59 +0200 Subject: add read-rules-ids test --- test/mastodon-tl-tests.el | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'test/mastodon-tl-tests.el') diff --git a/test/mastodon-tl-tests.el b/test/mastodon-tl-tests.el index 5dd6a04..0b8d3e3 100644 --- a/test/mastodon-tl-tests.el +++ b/test/mastodon-tl-tests.el @@ -4,6 +4,25 @@ (require 'cl-macs) (require 'el-mock) +(defconst mastodon-tl--test-instance-rules + ;; brief ones calqued off todon.nl + '(((id . "1") + (text . "We do not accept racism.")) + ((id . "2") + (text . "We do not accept homophobia.")) + ((id . "3") + (text . "We do not accept sexism.")) + ((id . "4") + (text . "We do not accept ableism.")) + ((id . "5") + (text . "We do not accept harassment.")) + ((id . "6") + (text . "We also do not accept hate speech.")) + ((id . "7") + (text . "We do not accept abuse of minors.")) + ((id . "8") + (text . "We do not accept glorification of violence.")))) + (defconst mastodon-tl-test-base-toot '((id . 61208) (created_at . "2017-04-24T19:01:02.000Z") @@ -1197,3 +1216,13 @@ correct value for following, as well as notifications enabled or disabled." ("comment" . "Dummy complaint") ("status_ids[]" . 61208) ("category" . "spam"))))) + +(ert-deftest mastodon-tl--read-rules () + "Should return a list of string numbers based on `mastodon-tl--test-instance-rules'" + (with-mock + (stub mastodon-tl--instance-rules => mastodon-tl--test-instance-rules) + (stub completing-read-multiple => '("We do not accept homophobia." + "We do not accept harassment." + "We also do not accept hate speech.")) + (should (equal '("2" "5" "6") + (mastodon-tl--read-rules-ids))))) -- cgit v1.2.3 From e13ffc1307e50f3b220a5e557dc6767bdc3d701a Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Fri, 21 Apr 2023 16:06:12 +0200 Subject: actually fix report to mods tests --- test/mastodon-tl-tests.el | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'test/mastodon-tl-tests.el') diff --git a/test/mastodon-tl-tests.el b/test/mastodon-tl-tests.el index 0b8d3e3..e029ba7 100644 --- a/test/mastodon-tl-tests.el +++ b/test/mastodon-tl-tests.el @@ -1146,27 +1146,24 @@ correct value for following, as well as notifications enabled or disabled." (ert-deftest mastodon-tl--report-to-mods-params-alist () "" (with-temp-buffer - (let ((toot mastodon-tl-test-base-toot)) + (let* ((toot mastodon-tl-test-base-toot) + (account (alist-get 'account toot))) (with-mock - (mock (mastodon-http--api "reports") => "https://instance.url/api/v1/reports") - (mock (mastodon-tl--toot-or-base - (mastodon-tl--property 'toot-json :no-move)) - => mastodon-tl-test-base-toot) + ;; no longer needed after our refactor + ;; (mock (mastodon-http--api "reports") => "https://instance.url/api/v1/reports") + ;; (mock (mastodon-tl--toot-or-base + ;; (mastodon-tl--property 'toot-json :no-move)) + ;; => mastodon-tl-test-base-toot) (mock (read-string "Add comment [optional]: ") => "Dummy complaint") - (stub y-or-n-p => nil) ; no to all - (should (equal (mastodon-tl--report-params) + (should (equal (mastodon-tl--report-params account toot) '(("account_id" . 42) ("comment" . "Dummy complaint") ("category" . "other")))) (with-mock (stub y-or-n-p => t) ; yes to all (mock (mastodon-tl--read-rules-ids) => '(1 2 3)) - ;; (mock (y-or-n-p "Also report status at point? ") => t) - ;; (mock (y-or-n-p "Forward to remote admin? ") => nil) - ;; (mock (y-or-n-p "Cite a rule broken? ") => nil) - ;; (mock (y-or-n-p "Spam? ") => nil) - (should (equal (mastodon-tl--report-params) + (should (equal (mastodon-tl--report-params account toot) '(("rule_ids[]" . 3) ("rule_ids[]" . 2) ("rule_ids[]" . 1) @@ -1219,10 +1216,11 @@ correct value for following, as well as notifications enabled or disabled." (ert-deftest mastodon-tl--read-rules () "Should return a list of string numbers based on `mastodon-tl--test-instance-rules'" - (with-mock - (stub mastodon-tl--instance-rules => mastodon-tl--test-instance-rules) - (stub completing-read-multiple => '("We do not accept homophobia." - "We do not accept harassment." - "We also do not accept hate speech.")) - (should (equal '("2" "5" "6") - (mastodon-tl--read-rules-ids))))) + (let ((crm-separator "[ ]*,[ ]*")) + (with-mock + (stub mastodon-tl--instance-rules => mastodon-tl--test-instance-rules) + (stub completing-read-multiple => '("We do not accept homophobia." + "We do not accept harassment." + "We also do not accept hate speech.")) + (should (equal '("2" "5" "6") + (mastodon-tl--read-rules-ids)))))) -- cgit v1.2.3