test-org-protocol.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ;;; test-org-protocol.el --- tests for org-protocol.el -*- lexical-binding: t; -*-
  2. ;; Copyright (c) Sacha Chua
  3. ;; Authors: Sacha Chua
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'cl-lib)
  17. (require 'org-protocol)
  18. (require 'org-capture)
  19. (ert-deftest test-org-protocol/org-protocol-parse-parameters ()
  20. "Test `org-protocol-parse-parameters' specifications."
  21. ;; Ignore lists
  22. (let ((data (org-protocol-parse-parameters '(:url "abc" :title "def") nil)))
  23. (should (string= (plist-get data :url) "abc"))
  24. (should (string= (plist-get data :title) "def")))
  25. ;; Parse new-style links
  26. (let ((data (org-protocol-parse-parameters "url=abc&title=def" t)))
  27. (should (string= (plist-get data :url) "abc"))
  28. (should (string= (plist-get data :title) "def")))
  29. ;; Parse new-style complex links
  30. (let* ((url (concat "template=p&"
  31. "url=https%3A%2F%2Forgmode.org%2Forg.html%23capture-protocol&"
  32. "title=The%20Org%20Manual&"
  33. "body=9.4.2%20capture%20protocol"))
  34. (data (org-protocol-parse-parameters url t)))
  35. (should (string= (plist-get data :template) "p"))
  36. (should (string= (plist-get data :url) "https://orgmode.org/org.html#capture-protocol"))
  37. (should (string= (plist-get data :title) "The Org Manual"))
  38. (should (string= (plist-get data :body) "9.4.2 capture protocol")))
  39. ;; Parse old-style links
  40. (let ((data (org-protocol-parse-parameters "abc/def" nil '(:url :title))))
  41. (should (string= (plist-get data :url) "abc"))
  42. (should (string= (plist-get data :title) "def")))
  43. ;; Parse old-style links even without keys
  44. (let ((data (org-protocol-parse-parameters "b/abc/def" nil)))
  45. (should (equal data '("b" "abc" "def"))))
  46. ;; Parse old-style links with key/val pairs
  47. (let ((data (org-protocol-parse-parameters "b/abc/extrakey/extraval" nil '(:param1 :param2))))
  48. (should (string= (plist-get data :param1) "b"))
  49. (should (string= (plist-get data :param2) "abc"))
  50. (should (string= (plist-get data :extrakey) "extraval"))))
  51. (ert-deftest test-org-protocol/org-protocol-store-link ()
  52. "Test `org-protocol-store-link' specifications."
  53. ;; Old link style
  54. (let ((uri "/some/directory/org-protocol:/store-link:/URL/TITLE"))
  55. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  56. (should (equal (car org-stored-links) '("URL" "TITLE"))))
  57. ;; URL encoded
  58. (let ((uri (format "/some/directory/org-protocol:/store-link:/%s/TITLE"
  59. (url-hexify-string "http://example.com"))))
  60. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  61. (should (equal (car org-stored-links) '("http://example.com" "TITLE"))))
  62. ;; Handle multiple slashes, old link style
  63. (let ((uri "/some/directory/org-protocol://store-link://URL2//TITLE2"))
  64. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  65. (should (equal (car org-stored-links) '("URL2" "TITLE2"))))
  66. ;; New link style
  67. (let ((uri "/some/directory/org-protocol://store-link?url=URL3&title=TITLE3"))
  68. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  69. (should (equal (car org-stored-links) '("URL3" "TITLE3"))))
  70. ;; Do not decode "+" in old-style link
  71. (let ((uri "/org-protocol:/store-link:/one+one/plus+preserved"))
  72. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  73. (should (equal (car org-stored-links) '("one+one" "plus+preserved"))))
  74. ;; Decode "+" to space in new-style link
  75. (let ((uri "/org-protocol:/store-link/?url=one+two&title=plus+is+space"))
  76. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  77. (should (equal (car org-stored-links) '("one two" "plus is space")))))
  78. (ert-deftest test-org-protocol/org-protocol-store-link-file ()
  79. "store-link: `org-protocol-sanitize-uri' could distort URL."
  80. :expected-result :failed
  81. (let ((uri "/org-protocol:/store-link:/file%3A%2F%2F%2Fetc%2Fmailcap/Triple%20Slash"))
  82. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  83. (should (equal (car org-stored-links) '("file:///etc/mailcap" "Triple Slash"))))
  84. (let ((uri "/org-protocol:/store-link?url=file%3A%2F%2F%2Fetc%2Fmailcap&title=Triple%20Slash"))
  85. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  86. (should (equal (car org-stored-links) '("file:///etc/mailcap" "Triple Slash")))))
  87. (ert-deftest test-org-protocol/org-protocol-capture ()
  88. "Test `org-protocol-capture' specifications."
  89. (let* ((org-protocol-default-template-key "t")
  90. (temp-file-name (make-temp-file "org-protocol-test"))
  91. (org-capture-templates
  92. `(("t" "Test" entry (file ,temp-file-name) "** TODO\n\n%i\n\n%a\n" :kill-buffer t)
  93. ("x" "With params" entry (file ,temp-file-name) "** SOMEDAY\n\n%i\n\n%a\n" :kill-buffer t)
  94. ("X" "Just the template" entry (file ,temp-file-name) "** Hello World\n\n%i\n\nGoodbye World\n" :kill-buffer t)))
  95. (test-urls
  96. '(
  97. ;; Old style:
  98. ;; - multiple slashes
  99. ("/some/directory/org-protocol:/capture:/URL/TITLE"
  100. . "** TODO\n\n\n\n[[URL][TITLE]]\n")
  101. ;; - body specification
  102. ("/some/directory/org-protocol:/capture:/URL/TITLE/BODY"
  103. . "** TODO\n\nBODY\n\n[[URL][TITLE]]\n")
  104. ;; - template
  105. ("/some/directory/org-protocol:/capture:/x/URL/TITLE/BODY"
  106. . "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
  107. ;; - query parameters, not sure how to include them in template
  108. ("/some/directory/org-protocol:/capture:/x/URL/TITLE/BODY/from/example"
  109. . "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
  110. ;; New style:
  111. ;; - multiple slashes
  112. ("/some/directory/org-protocol:/capture?url=NEWURL&title=TITLE"
  113. . "** TODO\n\n\n\n[[NEWURL][TITLE]]\n")
  114. ;; - body specification
  115. ("/some/directory/org-protocol:/capture?url=NEWURL&title=TITLE&body=BODY"
  116. . "** TODO\n\nBODY\n\n[[NEWURL][TITLE]]\n")
  117. ;; - template
  118. ("/some/directory/org-protocol:/capture?template=x&url=NEWURL&title=TITLE&body=BODY"
  119. . "** SOMEDAY\n\nBODY\n\n[[NEWURL][TITLE]]\n")
  120. ;; - no url specified
  121. ("/some/directory/org-protocol:/capture?template=x&title=TITLE&body=BODY"
  122. . "** SOMEDAY\n\nBODY\n\nTITLE\n")
  123. ;; - no title specified
  124. ("/some/directory/org-protocol:/capture?template=x&url=NEWURL&body=BODY"
  125. . "** SOMEDAY\n\nBODY\n\n[[NEWURL][NEWURL]]\n")
  126. ;; - just the template
  127. ("/some/directory/org-protocol:/capture?template=X"
  128. . "** Hello World\n\n\n\nGoodbye World\n")
  129. ;; - query parameters, not sure how to include them in template
  130. ("/some/directory/org-protocol:/capture?template=x&url=URL&title=TITLE&body=BODY&from=example"
  131. . "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
  132. ;; - "+" is not decoded to space in old-style URIs
  133. ("/org-protocol:/capture:/t/https%3A%2F%2Forgmode.org%2Fsome+thing/org+mode/Body+plus"
  134. . "** TODO\n\nBody+plus\n\n[[https://orgmode.org/some+thing][org+mode]]\n")
  135. ;; - decode "+" to space
  136. ("/org-protocol:/capture?template=t&url=URL&title=Mailing+list&body=Body+no+plus"
  137. . "** TODO\n\nBody no plus\n\n[[URL][Mailing list]]\n")
  138. )))
  139. ;; Old link style
  140. (mapc
  141. (lambda (test-case)
  142. (let ((uri (car test-case)))
  143. (org-protocol-check-filename-for-protocol uri (list uri) nil)
  144. (should (string= (buffer-string) (cdr test-case)))
  145. (org-capture-kill)))
  146. test-urls)
  147. (delete-file temp-file-name)))
  148. (ert-deftest test-org-protocol/org-protocol-capture-file ()
  149. "capture: `org-protocol-sanitize-uri' could distort URL."
  150. :expected-result :failed
  151. (let* ((org-protocol-default-template-key "t")
  152. (temp-file-name (make-temp-file "org-protocol-test"))
  153. (org-capture-templates
  154. `(("t" "Test" plain (file ,temp-file-name) "%a\n%i\n" :kill-buffer t))))
  155. (let ((uri "/org-protocol:/capture:/t/file%3A%2F%2F%2Fetc%2Fmailcap/Triple%20Slash/Body"))
  156. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  157. (should (string= (buffer-string) "[[file:///etc/mailcap][Triple Slash]]\nBody")))
  158. (let ((uri "/org-protocol:/capture?template=t&url=file%3A%2F%2F%2Fetc%2Fmailcap&title=Triple%20Slash&body=Body"))
  159. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  160. (should (string= (buffer-string) "[[file:///etc/mailcap][Triple Slash]]\nBody")))))
  161. (ert-deftest test-org-protocol/org-protocol-open-source ()
  162. "Test org-protocol://open-source links."
  163. (let* ((temp-file-name1 (make-temp-file "org-protocol-test1"))
  164. (temp-file-name2 (make-temp-file "org-protocol-test2"))
  165. (org-protocol-project-alist
  166. `((test1
  167. :base-url "http://example.com/"
  168. :online-suffix ".html"
  169. :working-directory ,(file-name-directory temp-file-name1))
  170. (test2
  171. :base-url "http://another.example.com/"
  172. :online-suffix ".js"
  173. :working-directory ,(file-name-directory temp-file-name2))
  174. (test3
  175. :base-url "https://blog-example.com/"
  176. :working-directory ,(file-name-directory temp-file-name2)
  177. :online-suffix ".html"
  178. :working-suffix ".md"
  179. :rewrites (("\\(https://blog-example.com/[0-9]+/[0-9]+/[0-9]+/\\)" . ".md")))))
  180. (test-cases
  181. (list
  182. ;; Old-style URLs
  183. (cons
  184. (concat "/some/directory/org-protocol:/open-source:/"
  185. (url-hexify-string
  186. (concat "http://example.com/" (file-name-nondirectory temp-file-name1) ".html")))
  187. temp-file-name1)
  188. (cons
  189. (concat "/some/directory/org-protocol:/open-source:/"
  190. (url-hexify-string
  191. (concat "http://another.example.com/" (file-name-nondirectory temp-file-name2) ".js")))
  192. temp-file-name2)
  193. ;; New-style URLs
  194. (cons
  195. (concat "/some/directory/org-protocol:/open-source?url="
  196. (url-hexify-string
  197. (concat "http://example.com/" (file-name-nondirectory temp-file-name1) ".html")))
  198. temp-file-name1)
  199. (cons
  200. (concat "/some/directory/org-protocol:/open-source?url="
  201. (url-hexify-string
  202. (concat "http://another.example.com/" (file-name-nondirectory temp-file-name2) ".js")))
  203. temp-file-name2))))
  204. (mapc (lambda (test-case)
  205. (should (string=
  206. (org-protocol-check-filename-for-protocol
  207. (car test-case)
  208. (list (car test-case)) nil)
  209. (cdr test-case))))
  210. test-cases)
  211. (delete-file temp-file-name1)
  212. (delete-file temp-file-name2)))
  213. (defun test-org-protocol/org-protocol-greedy-handler (fname)
  214. ;; fname should be a list of parsed items
  215. (should (listp fname))
  216. nil)
  217. (ert-deftest test-org-protocol/org-protocol-with-greedy-handler ()
  218. "Check that greedy handlers are called with all the filenames."
  219. (let ((org-protocol-protocol-alist
  220. '(("protocol-a" :protocol "greedy" :function test-org-protocol/org-protocol-greedy-handler :kill-client t :greedy t))))
  221. ;; Neither of these should signal errors
  222. (let ((uri "/some/dir/org-protocol://greedy?a=b&c=d")
  223. (uri2 "/some/dir/org-protocol://greedy?e=f&g=h"))
  224. (org-protocol-check-filename-for-protocol uri (list uri uri2) nil))))
  225. ;; TODO: Verify greedy protocol handling
  226. (provide 'test-org-protocol)
  227. ;;; test-org-protocol.el ends here