forked from fedify-dev/feder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote.rs
More file actions
106 lines (92 loc) · 3.24 KB
/
Copy pathnote.rs
File metadata and controls
106 lines (92 loc) · 3.24 KB
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
// Feder: A portable ActivityPub core for many runtimes.
// Copyright (C) 2026 Feder contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use axum::{
Json,
extract::{Path, State},
http::{StatusCode, header},
response::{IntoResponse, Response},
};
use crate::app::AppState;
pub async fn note(
State(app_state): State<AppState>,
Path(id): Path<String>,
) -> Result<Response, StatusCode> {
// TODO(#25): Replace this seeded preview note with durable runtime storage.
let note = app_state
.notes
.iter()
.find(|note| note.id.as_str() == format!("http://{}/notes/{id}", app_state.handle_host))
.cloned()
.ok_or(StatusCode::NOT_FOUND)?;
Ok((
[(header::CONTENT_TYPE, "application/activity+json")],
Json(note),
)
.into_response())
}
#[cfg(test)]
mod tests {
use axum::{
body::{Body, to_bytes},
http::{Request, StatusCode, header},
};
use serde_json::Value;
use tower::ServiceExt;
use crate::{app::build_router, config::RuntimeConfig};
#[tokio::test]
async fn returns_public_note() {
let app = build_router(&RuntimeConfig::default_local());
let response = app
.oneshot(
Request::builder()
.uri("/notes/1")
.body(Body::empty())
.expect("valid request"),
)
.await
.expect("response");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get(header::CONTENT_TYPE).unwrap(),
"application/activity+json"
);
let body = to_bytes(response.into_body(), 2048)
.await
.expect("read response body");
let json: Value = serde_json::from_slice(&body).expect("valid json");
assert_eq!(json["@context"], "https://www.w3.org/ns/activitystreams");
assert_eq!(json["type"], "Note");
assert_eq!(json["id"], "http://127.0.0.1:3000/notes/1");
assert_eq!(json["attributedTo"], "http://127.0.0.1:3000/users/alice");
assert_eq!(
json["content"],
"Hello, World! This is Feder, a portable AP core for many runtimes."
);
}
#[tokio::test]
async fn rejects_unknown_note() {
let app = build_router(&RuntimeConfig::default_local());
let response = app
.oneshot(
Request::builder()
.uri("/notes/missing")
.body(Body::empty())
.expect("valid request"),
)
.await
.expect("response");
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
}