~vpzom/lotide

ref: e87ca796126c958242fcc7302fa1d5a202c90033 lotide/tests/apub.rs -rw-r--r-- 4.9 KiB
e87ca796Colin Reeder Add test for following community a month ago
                                                                                
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use serde_derive::Deserialize;
use std::ops::Deref;

struct TestServer {
    host_url: String,
    process: std::process::Child,
}

impl TestServer {
    pub fn start(idx: u16) -> Self {
        let db_url =
            std::env::var(format!("DATABASE_URL_{}", idx)).expect("Missing DATABASE_URL_#");
        let port = 8330 + idx;
        let host_url = format!("http://localhost:{}", port);

        let child = std::process::Command::new(env!("CARGO_BIN_EXE_lotide"))
            .env("DATABASE_URL", db_url)
            .env("PORT", port.to_string())
            .env("HOST_URL_ACTIVITYPUB", format!("{}/apub", host_url))
            .env("HOST_URL_API", format!("{}/api", host_url))
            .spawn()
            .unwrap();

        let res = Self {
            host_url,
            process: child,
        };

        std::thread::sleep(std::time::Duration::from_secs(1));

        res
    }
}

impl std::ops::Drop for TestServer {
    fn drop(&mut self) {
        self.process.kill().unwrap();
    }
}

fn random_string() -> String {
    use rand::distributions::Distribution;

    rand::distributions::Alphanumeric
        .sample_iter(rand::thread_rng())
        .take(16)
        .collect()
}

fn create_account(client: &reqwest::blocking::Client, server: &TestServer) -> String {
    let resp = client
        .post(format!("{}/api/unstable/users", server.host_url).deref())
        .json(&serde_json::json!({
            "username": random_string(),
            "password": random_string(),
            "login": true
        }))
        .send()
        .unwrap()
        .error_for_status()
        .unwrap();

    #[derive(Deserialize)]
    struct JustToken {
        token: String,
    }

    let resp: JustToken = resp.json().unwrap();

    resp.token
}

struct CommunityInfo {
    id: i64,
    name: String,
}

fn create_community(
    client: &reqwest::blocking::Client,
    server: &TestServer,
    token: &str,
) -> CommunityInfo {
    let community_name = random_string();

    let resp = client
        .post(format!("{}/api/unstable/communities", server.host_url).deref())
        .bearer_auth(token)
        .json(&serde_json::json!({ "name": community_name }))
        .send()
        .unwrap()
        .error_for_status()
        .unwrap();

    let resp: serde_json::Value = resp.json().unwrap();

    CommunityInfo {
        id: resp["community"]["id"].as_i64().unwrap(),
        name: community_name,
    }
}

fn lookup_community(client: &reqwest::blocking::Client, server: &TestServer, ap_id: &str) -> i64 {
    let resp = client
        .get(
            format!(
                "{}/api/unstable/actors:lookup/{}",
                server.host_url,
                percent_encoding::utf8_percent_encode(&ap_id, percent_encoding::NON_ALPHANUMERIC)
            )
            .deref(),
        )
        .send()
        .unwrap()
        .error_for_status()
        .unwrap();

    let resp: (serde_json::Value,) = resp.json().unwrap();
    let (resp,) = resp;
    resp["id"].as_i64().unwrap()
}

#[test]
fn community_fetch() {
    let server1 = TestServer::start(1);
    let server2 = TestServer::start(2);

    let client = reqwest::blocking::Client::builder().build().unwrap();

    let token = create_account(&client, &server1);

    let community = create_community(&client, &server1, &token);

    let community_remote_id = lookup_community(
        &client,
        &server2,
        &format!("{}/apub/communities/{}", server1.host_url, community.id),
    );

    let resp = client
        .get(
            format!(
                "{}/api/unstable/communities/{}",
                server2.host_url, community_remote_id
            )
            .deref(),
        )
        .send()
        .unwrap()
        .error_for_status()
        .unwrap();
    let resp: serde_json::Value = resp.json().unwrap();

    assert_eq!(resp["name"].as_str(), Some(community.name.as_ref()));
    assert_eq!(resp["local"].as_bool(), Some(false));
}

#[test]
fn community_follow() {
    let server1 = TestServer::start(1);
    let server2 = TestServer::start(2);

    let client = reqwest::blocking::Client::builder().build().unwrap();

    let token1 = create_account(&client, &server1);

    let community = create_community(&client, &server1, &token1);

    let community_remote_id = lookup_community(
        &client,
        &server2,
        &format!("{}/apub/communities/{}", server1.host_url, community.id),
    );

    let token2 = create_account(&client, &server2);

    let resp = client
        .post(
            format!(
                "{}/api/unstable/communities/{}/follow",
                server2.host_url, community_remote_id,
            )
            .deref(),
        )
        .json(&serde_json::json!({
            "try_wait_for_accept": true
        }))
        .bearer_auth(token2)
        .send()
        .unwrap()
        .error_for_status()
        .unwrap();

    let resp: serde_json::Value = resp.json().unwrap();
    assert!(resp["accepted"].as_bool().unwrap());
}