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