From 0fedcdec17cba262ba57258e76dd19b3962f4c9f Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Thu, 20 Apr 2023 13:45:53 +0200 Subject: add basic http params and array to param alist tests --- test/mastodon-http-tests.el | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/mastodon-http-tests.el b/test/mastodon-http-tests.el index 57b52a4..b3a02bc 100644 --- a/test/mastodon-http-tests.el +++ b/test/mastodon-http-tests.el @@ -76,10 +76,25 @@ Strict-Transport-Security: max-age=31536000 (let ((response-buffer (get-buffer-create "mastodon-http--triage-buffer"))) (with-current-buffer response-buffer - (erase-buffer) + (erase-buffer) (insert mastodon-http--example-400)) (should (equal (mastodon-http--triage response-buffer (lambda () (message "success call"))) "Error 444: some unhappy complaint")))) + +(ert-deftest mastodon-http-params-build () + "Should correctly format parameters from an alist." + (let ((params '(("q" . "test") + ("foo" . "bar")))) + (should (string= (mastodon-http--build-params-string params) + "q=test&foo=bar")))) + +(ert-deftest mastodon-http-params-array-build () + "Should correctly format parameters from an alist." + (let ((array '("option" "option2")) + (param-str "poll[x][]")) + (should (equal (mastodon-http--build-array-params-alist param-str array) + '(("poll[x][]" . "option") + ("poll[x][]" . "option2")))))) -- cgit v1.2.3 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 --- lisp/mastodon-tl.el | 79 ++++++++++++++++-------------- test/mastodon-tl-tests.el | 122 +++++++++++++++++++++++++++++----------------- 2 files changed, 120 insertions(+), 81 deletions(-) (limited to 'test') diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 3ebcec1..0355a27 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -2102,46 +2102,53 @@ Prefix is sent to `mastodon-tl--show-tag-timeline', which see." (let ((url (mastodon-http--api "instance/rules"))) (mastodon-http--get-json url nil :silent))) +(defun mastodon-tl--report-params () + "Query user and return report params alist." + (let* ((url (mastodon-http--api "reports")) + (toot (mastodon-tl--toot-or-base + (mastodon-tl--property 'toot-json :no-move))) + (account (alist-get 'account toot)) + (handle (alist-get 'acct account)) + (account-id (mastodon-profile--account-field account 'id)) + (comment (read-string "Add comment [optional]: ")) + (toot-id (when (y-or-n-p "Also report status at point? ") + (mastodon-tl--toot-id toot))) ; base toot if poss + (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))))) + (when rules + (let ((alist + (mastodon-http--build-array-params-alist "rule_ids[]" rules))) + (mapc (lambda (x) + (push x params)) + alist))) + ;; FIXME: the above approach adds nils to your params. + (setq params (delete nil params)) + params)) + (defun mastodon-tl--report-to-mods () "Report the author of the toot at point to your instance moderators. -Optionally report the toot at point, optionally add a comment, optionally cite rules that have been broken, optionally forward the report to the remove admin, optionally report the account for spam." +Optionally report the toot at point, add a comment, cite rules +that have been broken, forward the report to the remove admin, +report the account for spam." (interactive) - (when (y-or-n-p (format "report author of toot at point?")) + (when (y-or-n-p "Report author of toot at point?") (let* ((url (mastodon-http--api "reports")) - (toot (mastodon-tl--toot-or-base - (mastodon-tl--property 'toot-json))) - (account (alist-get 'account toot)) - (handle (alist-get 'acct account)) - (account-id (mastodon-profile--account-field account 'id)) - (comment (read-string "Add comment [optional]: ")) - (toot-id (when (y-or-n-p "Also report status at point?") - (mastodon-tl--toot-id toot))) ; base toot if poss - (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))))) - (when rules - (let ((alist - (mastodon-http--build-array-params-alist "rule_ids[]" rules))) - (mapc (lambda (x) - (push x params)) - alist))) - ;; FIXME: the above approach adds nils to your params. - (setq params (delete nil params)) - ;; (message "%s" (prin1-to-string params)) - (let ((response (mastodon-http--post-async url params))) - (mastodon-http--triage response - (lambda (response) - (message "User %s reported!" handle))))))) + (params (mastodon-tl--report-params)) + (response (mastodon-http--post-async url params))) + (mastodon-http--triage response + (lambda (response) + (message "User %s reported!" handle)))))) (defun mastodon-tl--read-rules-ids () "Prompt for a list of instance rules and return a list of selected ids." 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') 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') 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') 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