aboutsummaryrefslogtreecommitdiffstats
path: root/src/stream/actor.rs
blob: 4e2bbe4df040bb1ec65e8aae45cf43c938a18131 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 * Representation of Actors in the activitystream.
 *
 * SPDX-FileCopyrightText: 2023 Eilertsens Kodeknekkeri
 * SPDX-FileCopyrightText: 2023 Harald Eilertsen <haraldei@anduin.net>
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

use serde::Deserialize;
use url::Url;

#[derive(Debug, Deserialize, PartialEq)]
pub struct Actor {
    name: String,
    address: Option<String>,
    url: Option<Url>,
    id: String,
    id_sig: String,
    key: String,
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_parsing_actor_from_json() {
        let json = r#"
            {
              "name": "Benjamin Franklin",
              "address": "ben@example.com",
              "url": "https://example.com/channel/ben",
              "network": "zot6",
              "photo": {
                "mimetype": "image/jpeg",
                "src": "https://example.com/photo/profile/m/2"
              },
              "id": "rUpgk2qbvnWLoKIXOlZlwlqI5vk8C4NgudFNjbcmnOBjFSXU34TObkZEClaPSfKnpFZpg87tANtko7WGs7QRvA",
              "id_sig": "sha256.ZD8uwYmUEG_d02Y...",
              "key": "-----BEGIN PUBLIC KEY-----\n....\n-----END PUBLIC KEY-----\n"
            }"#;

        let actor: Actor = serde_json::from_str(&json).unwrap();

        assert_eq!("Benjamin Franklin", actor.name);
        assert_eq!("ben@example.com", &actor.address.unwrap());
        assert_eq!("https://example.com/channel/ben", actor.url.unwrap().to_string());
        assert_eq!("rUpgk2qbvnWLoKIXOlZlwlqI5vk8C4NgudFNjbcmnOBjFSXU34TObkZEClaPSfKnpFZpg87tANtko7WGs7QRvA", actor.id);
    }

    #[test]
    fn test_parsing_actor_without_address_from_json() {
        let json = r#"
            {
              "name": "Benjamin Franklin",
              "url": "https://example.com/channel/ben",
              "network": "zot6",
              "photo": {
                "mimetype": "image/jpeg",
                "src": "https://example.com/photo/profile/m/2"
              },
              "id": "rUpgk2qbvnWLoKIXOlZlwlqI5vk8C4NgudFNjbcmnOBjFSXU34TObkZEClaPSfKnpFZpg87tANtko7WGs7QRvA",
              "id_sig": "sha256.ZD8uwYmUEG_d02Y...",
              "key": "-----BEGIN PUBLIC KEY-----\n....\n-----END PUBLIC KEY-----\n"
            }"#;

        let actor: Actor = serde_json::from_str(&json).unwrap();

        assert_eq!("Benjamin Franklin", actor.name);
        assert_eq!(None, actor.address);
        assert_eq!("https://example.com/channel/ben", actor.url.unwrap().to_string());
        assert_eq!("rUpgk2qbvnWLoKIXOlZlwlqI5vk8C4NgudFNjbcmnOBjFSXU34TObkZEClaPSfKnpFZpg87tANtko7WGs7QRvA", actor.id);
    }

    #[test]
    fn test_parsing_actor_without_url_from_json() {
        let json = r#"
	        {
                "name":"Diaspora User",
                "address":"someone@diaspora.example.com",
                "network":"diaspora",
                "photo":{"mimetype":"image\/jpeg", "src":"https:\/\/example.com\/photo\/c992335849828915b5c963b985f68f46-5"},
                "id":"349ab82134669a3b",
                "id_sig":"",
                "key":"-----BEGIN PUBLIC KEY-----\n\n-----END PUBLIC KEY-----\n"
            }
        "#;

        let actor: Actor = serde_json::from_str(&json).unwrap();

        assert_eq!("Diaspora User", actor.name);
        assert_eq!("someone@diaspora.example.com", actor.address.unwrap());
        assert_eq!(None, actor.url);
    }
}