webfinger_test.ml 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * webfinger_test.ml
  12. *
  13. * Copyright (C) The #Seppo contributors. All rights reserved.
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *)
  28. open Seppo_lib
  29. let test_uri_from_string () =
  30. Logr.debug (fun m -> m "%s.%s" "webfinger" "uri_from_string");
  31. Uri.make ~scheme:Webfinger.scheme
  32. ~userinfo:"a:b/c" ~host:"d.e" ()
  33. |> Uri.to_string
  34. |> Assrt.equals_string __LOC__ "acct://a:b%2Fc@d.e";
  35. "mailto:demo@d.seppo.social"
  36. |> Uri.of_string
  37. |> Uri.host
  38. |> Option.value ~default:"-"
  39. |> Assrt.equals_string __LOC__ "-";
  40. "acct:ab/c@d.e"
  41. |> Webfinger.of_string
  42. |> Webfinger.to_string
  43. |> Assrt.equals_string __LOC__ "acct:ab%2Fc@d.e";
  44. "acct:ab/c@d.e"
  45. |> Webfinger.of_string
  46. |> Webfinger.well_known_uri
  47. |> Result.get_ok
  48. |> Uri.host
  49. |> Option.value ~default:"-"
  50. |> Assrt.equals_string __LOC__ "d.e";
  51. "acct:ab/c@d.e"
  52. |> Webfinger.of_string
  53. |> Webfinger.well_known_uri
  54. |> Result.get_ok
  55. |> Uri.to_string
  56. |> Assrt.equals_string __LOC__ "https://d.e/.well-known/webfinger?resource=acct:ab%252Fc@d.e";
  57. "acct:demo@d.seppo.social"
  58. |> Webfinger.of_string
  59. |> Webfinger.well_known_uri
  60. |> Result.get_ok
  61. |> Uri.host
  62. |> Option.value ~default:"-"
  63. |> Assrt.equals_string __LOC__ "d.seppo.social";
  64. "@a@b.c"
  65. |> Webfinger.of_string
  66. |> Webfinger.to_string
  67. |> Assrt.equals_string __LOC__ "acct:a@b.c";
  68. "a@b.c"
  69. |> Webfinger.of_string
  70. |> Webfinger.to_string
  71. |> Assrt.equals_string __LOC__ "acct:a@b.c";
  72. "http://a@b.c"
  73. |> Webfinger.of_string
  74. |> Webfinger.to_string
  75. |> Assrt.equals_string __LOC__ "http://a@b.c"
  76. let test_handle () =
  77. Logr.debug (fun m -> m "%s.%s" "webfinger" "handle");
  78. let open Webfinger.Client in
  79. (Localpart "uh", Domainpart "ah.oh")
  80. |> to_short
  81. |> Assrt.equals_string __LOC__ "uh@ah.oh";
  82. (match "uh@ah.oh" |> Webfinger.Client.from_string with
  83. | Ok (Localpart "uh", Domainpart "ah.oh") -> assert true
  84. | Ok _ -> Assrt.equals_string __LOC__ "h" "i"
  85. | Error e -> Assrt.equals_string __LOC__ "" e
  86. );
  87. let Rfc7565 u = "ok@example.com"
  88. |> Webfinger.Client.from_string
  89. |> Result.get_ok
  90. |> Webfinger.Client.to_rfc7565 in
  91. u |> Uri.to_string
  92. |> Assrt.equals_string __LOC__ "acct://ok@example.com";
  93. (match Webfinger.("@ok@example.com" |> of_string |> Client.from_uri) with
  94. | Ok (Localpart "ok", Domainpart "example.com") -> assert true
  95. | Ok _ -> Assrt.equals_string __LOC__ "h" "i"
  96. | Error e -> Assrt.equals_string __LOC__ "" e
  97. );
  98. let u = "@ok@example.com"
  99. |> Webfinger.of_string
  100. |> Webfinger.Client.from_uri
  101. |> Result.get_ok in
  102. u |> Webfinger.Client.to_string
  103. |> Assrt.equals_string __LOC__ "acct:ok@example.com"
  104. let test_webfinger () =
  105. Logr.debug (fun m -> m "webfinger");
  106. Webfinger.jsonm (Auth.Uid "usr", Uri.of_string "scheme://example.com/p/a/t/h/")
  107. |> Result.get_ok
  108. |> Ezjsonm.value_to_string ~minify:false
  109. |> Assrt.equals_string __LOC__
  110. {|{
  111. "subject": "acct:usr@example.com",
  112. "links": [
  113. {
  114. "href": "scheme://example.com/p/a/t/h/activitypub/actor.jsa",
  115. "rel": "self",
  116. "type": "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
  117. },
  118. {
  119. "href": "scheme://example.com/p/a/t/h/",
  120. "rel": "http://webfinger.net/rel/profile-page",
  121. "type": "text/html"
  122. },
  123. {
  124. "href": "scheme://example.com/p/a/t/h/o/p/",
  125. "rel": "alternate",
  126. "type": "application/atom+xml"
  127. },
  128. {
  129. "rel": "http://ostatus.org/schema/1.0/subscribe",
  130. "template": "scheme://example.com/p/a/t/h/seppo.cgi/activitypub/actor.xml?id={uri}"
  131. }
  132. ]
  133. }|};
  134. assert true
  135. let test_decode () =
  136. Logr.debug (fun m -> m "webfinger_decode");
  137. let j = File.in_channel "data/webfinger/gnusocial.json" Ezjsonm.value_from_channel in
  138. let q = As2_vocab.Decode.Webfinger.query_result j |> Result.get_ok in
  139. q.subject |> Assrt.equals_string __LOC__ "acct:administrator@gnusocial.net";
  140. As2_vocab.Encode.Webfinger.query_result ~base:Uri.empty q
  141. |> Ezjsonm.value_to_string ~minify:false
  142. |> Assrt.equals_string __LOC__ {|{
  143. "subject": "acct:administrator@gnusocial.net",
  144. "aliases": [
  145. "https://gnusocial.net/index.php/user/1",
  146. "https://gnusocial.net/administrator",
  147. "https://gnusocial.net/user/1",
  148. "https://gnusocial.net/index.php/administrator"
  149. ],
  150. "links": [
  151. {
  152. "href": "https://gnusocial.net/administrator",
  153. "rel": "http://webfinger.net/rel/profile-page",
  154. "type": "text/html"
  155. },
  156. {
  157. "rel": "http://ostatus.org/schema/1.0/subscribe",
  158. "template": "https://gnusocial.net/main/remotefollowsub?profile={uri}"
  159. },
  160. {
  161. "href": "https://gnusocial.net/index.php/user/1",
  162. "rel": "self",
  163. "type": "application/activity+json"
  164. }
  165. ]
  166. }|};
  167. let j = File.in_channel "data/webfinger/pleroma.json" Ezjsonm.value_from_channel in
  168. let q = j |> As2_vocab.Decode.Webfinger.query_result |> Result.get_ok in
  169. q.subject |> Assrt.equals_string __LOC__ "acct:gabek@social.gabekangas.com";
  170. assert true
  171. let test_sift () =
  172. Logr.debug (fun m -> m "webfinger_test.test_webfinger_sift");
  173. let base = Uri.of_string "https://example.com/sub/" in
  174. let p = Webfinger.make (Auth.Uid "usr", base) in
  175. p.links
  176. |> As2_vocab.Types.Webfinger.profile_page
  177. |> Option.get
  178. |> Uri.to_string
  179. |> Assrt.equals_string __LOC__ ".";
  180. p.links
  181. |> As2_vocab.Types.Webfinger.self_link
  182. |> Option.get
  183. |> Uri.to_string
  184. |> Assrt.equals_string __LOC__ "activitypub/actor.jsa";
  185. p
  186. |> As2_vocab.Encode.Webfinger.query_result ~base
  187. |> Ezjsonm.value_to_string ~minify:false
  188. |> Assrt.equals_string __LOC__ {|{
  189. "subject": "acct:usr@example.com",
  190. "links": [
  191. {
  192. "href": "https://example.com/sub/activitypub/actor.jsa",
  193. "rel": "self",
  194. "type": "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
  195. },
  196. {
  197. "href": "https://example.com/sub/",
  198. "rel": "http://webfinger.net/rel/profile-page",
  199. "type": "text/html"
  200. },
  201. {
  202. "href": "https://example.com/sub/o/p/",
  203. "rel": "alternate",
  204. "type": "application/atom+xml"
  205. },
  206. {
  207. "rel": "http://ostatus.org/schema/1.0/subscribe",
  208. "template": "https://example.com/sub/seppo.cgi/activitypub/actor.xml?id={uri}"
  209. }
  210. ]
  211. }|}
  212. let () =
  213. Unix.chdir "../../../test/";
  214. test_uri_from_string ();
  215. test_handle ();
  216. test_webfinger ();
  217. test_sift ();
  218. test_decode ();
  219. assert true