t_webfinger.ml 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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:Rfc7565.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. |> Rfc7565.of_string |> Result.get_ok
  42. |> Rfc7565.to_string
  43. |> Assrt.equals_string __LOC__ "acct:ab/c@d.e";
  44. "acct:ab/c@d.e"
  45. |> Rfc7565.of_string |> Result.get_ok
  46. |> Webfinger.well_known_uri
  47. |> Uri.host
  48. |> Option.value ~default:"-"
  49. |> Assrt.equals_string __LOC__ "d.e";
  50. "acct:ab/c@d.e"
  51. |> Rfc7565.of_string |> Result.get_ok
  52. |> Webfinger.well_known_uri
  53. |> Uri.to_string
  54. |> Assrt.equals_string __LOC__ "https://d.e/.well-known/webfinger?resource=acct:ab/c@d.e";
  55. "acct:demo@d.seppo.social"
  56. |> Rfc7565.of_string |> Result.get_ok
  57. |> Webfinger.well_known_uri
  58. |> Uri.host
  59. |> Option.value ~default:"-"
  60. |> Assrt.equals_string __LOC__ "d.seppo.social";
  61. "@a@b.c"
  62. |> Rfc7565.of_string |> Result.get_ok
  63. |> Rfc7565.to_string
  64. |> Assrt.equals_string __LOC__ "acct:a@b.c";
  65. "a@b.c"
  66. |> Rfc7565.of_string |> Result.get_ok
  67. |> Rfc7565.to_string
  68. |> Assrt.equals_string __LOC__ "acct:a@b.c";
  69. (*
  70. "http://a@b.c"
  71. |> Webfinger.Rfc7565.of_string_
  72. |> Webfinger.Rfc7565.to_string
  73. |> Assrt.equals_string __LOC__ "acct:a@b.c"
  74. *)
  75. ()
  76. let test_handle () =
  77. Logr.debug (fun m -> m "%s.%s" "webfinger" "handle");
  78. (match "uh@ah.oh" |> Rfc7565.of_string with
  79. | Ok acct -> acct |> Rfc7565.to_string |> Assrt.equals_string __LOC__ "acct:uh@ah.oh"
  80. | Error _ -> failwith __LOC__
  81. );
  82. "ok@example.com"
  83. |> Rfc7565.of_string
  84. |> Result.get_ok
  85. |> Rfc7565.to_string
  86. |> Assrt.equals_string __LOC__ "acct:ok@example.com"
  87. let test_webfinger () =
  88. Logr.debug (fun m -> m "webfinger");
  89. Webfinger.jsonm (Auth.Uid "usr", Uri.of_string "scheme://example.com/p/a/t/h/")
  90. |> Result.get_ok
  91. |> Ezjsonm.value_to_string ~minify:false
  92. |> Assrt.equals_string __LOC__
  93. {|{
  94. "subject": "acct:usr@example.com",
  95. "links": [
  96. {
  97. "href": "scheme://example.com/p/a/t/h/activitypub/actor.jsa",
  98. "rel": "self",
  99. "type": "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
  100. },
  101. {
  102. "href": "scheme://example.com/p/a/t/h/",
  103. "rel": "http://webfinger.net/rel/profile-page",
  104. "type": "text/html"
  105. },
  106. {
  107. "href": "scheme://example.com/p/a/t/h/o/p/",
  108. "rel": "alternate",
  109. "type": "application/atom+xml"
  110. },
  111. {
  112. "rel": "http://ostatus.org/schema/1.0/subscribe",
  113. "template": "scheme://example.com/p/a/t/h/seppo.cgi/activitypub/actor.xml?id={uri}"
  114. }
  115. ]
  116. }|};
  117. assert true
  118. let test_decode () =
  119. Logr.debug (fun m -> m "webfinger_decode");
  120. let j = "data/webfinger/gnusocial.json" |> File.in_channel Ezjsonm.value_from_channel in
  121. let q = As2_vocab.Decode.Webfinger.query_result j |> Result.get_ok in
  122. q.subject |> Assrt.equals_string __LOC__ "acct:administrator@gnusocial.net";
  123. As2_vocab.Encode.Webfinger.query_result ~base:Uri.empty q
  124. |> Ezjsonm.value_to_string ~minify:false
  125. |> Assrt.equals_string __LOC__ {|{
  126. "subject": "acct:administrator@gnusocial.net",
  127. "aliases": [
  128. "https://gnusocial.net/index.php/user/1",
  129. "https://gnusocial.net/administrator",
  130. "https://gnusocial.net/user/1",
  131. "https://gnusocial.net/index.php/administrator"
  132. ],
  133. "links": [
  134. {
  135. "href": "https://gnusocial.net/administrator",
  136. "rel": "http://webfinger.net/rel/profile-page",
  137. "type": "text/html"
  138. },
  139. {
  140. "rel": "http://ostatus.org/schema/1.0/subscribe",
  141. "template": "https://gnusocial.net/main/remotefollowsub?profile={uri}"
  142. },
  143. {
  144. "href": "https://gnusocial.net/index.php/user/1",
  145. "rel": "self",
  146. "type": "application/activity+json"
  147. }
  148. ]
  149. }|};
  150. let j = "data/webfinger/pleroma.json" |> File.in_channel Ezjsonm.value_from_channel in
  151. let q = j |> As2_vocab.Decode.Webfinger.query_result |> Result.get_ok in
  152. q.subject |> Assrt.equals_string __LOC__ "acct:gabek@social.gabekangas.com";
  153. assert true
  154. let test_sift () =
  155. Logr.debug (fun m -> m "webfinger_test.test_webfinger_sift");
  156. let base = Uri.of_string "https://example.com/sub/" in
  157. let p = Webfinger.make (Auth.Uid "usr", base) in
  158. p.links
  159. |> As2_vocab.Types.Webfinger.profile_page
  160. |> Option.get
  161. |> Uri.to_string
  162. |> Assrt.equals_string __LOC__ ".";
  163. p.links
  164. |> As2_vocab.Types.Webfinger.self_link
  165. |> Option.get
  166. |> Uri.to_string
  167. |> Assrt.equals_string __LOC__ "activitypub/actor.jsa";
  168. p
  169. |> As2_vocab.Encode.Webfinger.query_result ~base
  170. |> Ezjsonm.value_to_string ~minify:false
  171. |> Assrt.equals_string __LOC__ {|{
  172. "subject": "acct:usr@example.com",
  173. "links": [
  174. {
  175. "href": "https://example.com/sub/activitypub/actor.jsa",
  176. "rel": "self",
  177. "type": "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
  178. },
  179. {
  180. "href": "https://example.com/sub/",
  181. "rel": "http://webfinger.net/rel/profile-page",
  182. "type": "text/html"
  183. },
  184. {
  185. "href": "https://example.com/sub/o/p/",
  186. "rel": "alternate",
  187. "type": "application/atom+xml"
  188. },
  189. {
  190. "rel": "http://ostatus.org/schema/1.0/subscribe",
  191. "template": "https://example.com/sub/seppo.cgi/activitypub/actor.xml?id={uri}"
  192. }
  193. ]
  194. }|}
  195. let test_many () =
  196. let ex = [
  197. ("@actapopuli@fediverse.blog.json" ,"https://fediverse.blog/@/actapopuli/");
  198. ("@administrator@gnusocial.net.json","https://gnusocial.net/index.php/user/1");
  199. ("@btsavage@threads.net.json" ,"https://threads.net/ap/users/17841401679436667/");
  200. ("@dansup@pixelfed.social.json" ,"https://pixelfed.social/users/dansup");
  201. ("@demo@seppo.social.json" ,"https://seppo.social/demo/activitypub/actor.jsa");
  202. ("@framasoft@mobilizon.fr.json" ,"https://mobilizon.fr/@framasoft");
  203. ("@gargron@mastodon.social.json" ,"https://mastodon.social/users/Gargron");
  204. ("@Greensky@open.audio.json" ,"https://open.audio/federation/actors/Greensky");
  205. ("@kainoa@calckey.social.json" ,"https://calckey.social/users/9aprgabaeb");
  206. ("@karolat@stereophonic.space.json" ,"https://stereophonic.space/users/karolat");
  207. ("@lemmy_support@lemmy.ml.json" ,"https://lemmy.ml/c/lemmy_support");
  208. ("@manton@manton.org.json" ,"https://manton.org/activitypub/manton");
  209. ("@matt@write.as.json" ,"https://write.as/api/collections/matt");
  210. ("@mike@macgirvin.com.json" ,"https://macgirvin.com/channel/mike");
  211. ("@peertube@framapiaf.org.json" ,"https://framapiaf.org/users/peertube");
  212. ("@syuilo@misskey.io.json" ,"https://misskey.io/users/7rkrarq81i");
  213. ("@tobias@friendi.ca.json" ,"https://friendi.ca/author/tobias/");
  214. ("atom.json" ,"https://example.com/activitypub/");
  215. ("bonfire.json" ,"https://campground.bonfire.cafe/pub/actors/stpaultim");
  216. ("gnusocial.json" ,"https://gnusocial.net/index.php/user/1");
  217. ("mini.json" ,"https://example.com/activitypub/");
  218. ("misskey.json" ,"https://misskey.io/users/7rkrarq81i");
  219. ("pleroma.json" ,"https://social.gabekangas.com/users/gabek");
  220. ("zap.json" ,"https://macgirvin.com/channel/mike");
  221. ] in
  222. let dir = "data/webfinger/" in
  223. dir |> File.fold_dir
  224. (fun init f ->
  225. if match f with
  226. | "@contribute@hubzilla.org.json" -> false
  227. | f -> f |> St.is_suffix ~affix:".json"
  228. then (
  229. let q = (dir ^ f)
  230. |> File.in_channel Ezjsonm.value_from_channel
  231. |> As2_vocab.Decode.Webfinger.query_result
  232. |> Result.get_ok in
  233. let a = q.links
  234. |> As2_vocab.Types.Webfinger.self_link
  235. |> Option.get in
  236. (* Logr.debug (fun m -> m {|%s.%s ("%s","%a");|} "Webfinger" "test_many" f Uri.pp a); *)
  237. a |> Uri.to_string |> Assrt.equals_string __LOC__ (List.assoc f ex);
  238. 1 + init , true
  239. ) else
  240. init , true)
  241. 0
  242. |> Assrt.equals_int __LOC__ (ex |> List.length)
  243. let () =
  244. Unix.chdir "../../../test/";
  245. Printexc.record_backtrace true;
  246. test_uri_from_string ();
  247. test_handle ();
  248. test_webfinger ();
  249. test_decode ();
  250. test_sift ();
  251. test_many ();
  252. assert true