Skip to main content

max / makenotwork

6.4 KB · 191 lines History Blame Raw
1 use crate::harness::TestHarness;
2
3 #[tokio::test]
4 async fn profile_page_shows_user_info() {
5 let mut h = TestHarness::new().await;
6 let user_id = h.login_as("profileuser").await;
7 let comm_id = h.create_community("TestCommunity", "test-comm").await;
8 h.create_category(comm_id, "General", "general").await;
9 h.add_membership(user_id, comm_id, "member").await;
10
11 let resp = h.client.get("/p/test-comm/u/profileuser").await;
12 assert_eq!(resp.status.as_u16(), 200, "profile page should load");
13 assert!(resp.text.contains("profileuser"), "should show username");
14 assert!(resp.text.contains("member"), "should show role badge");
15 assert!(resp.text.contains("Joined"), "should show join date");
16 }
17
18 #[tokio::test]
19 async fn profile_page_shows_activity() {
20 let mut h = TestHarness::new().await;
21 let user_id = h.login_as("activeuser").await;
22 let comm_id = h.create_community("ActivityComm", "activity-comm").await;
23 let cat_id = h.create_category(comm_id, "General", "general").await;
24 h.add_membership(user_id, comm_id, "member").await;
25
26 let thread_id = h
27 .create_thread_with_post(cat_id, user_id, "My Thread", "Hello world")
28 .await;
29
30 let thread_id_2 = h
31 .create_thread_with_post(cat_id, user_id, "Another Thread", "Second post")
32 .await;
33 mt_db::mutations::create_post(&h.db, thread_id_2, user_id, "A reply", "<p>A reply</p>")
34 .await
35 .unwrap();
36
37 let resp = h.client.get("/p/activity-comm/u/activeuser").await;
38 assert_eq!(resp.status.as_u16(), 200);
39 assert!(
40 resp.text.contains("My Thread"),
41 "should list thread activity"
42 );
43 assert!(
44 resp.text.contains("Another Thread"),
45 "should list reply activity"
46 );
47
48 assert!(
49 resp.text.contains(&thread_id.to_string()),
50 "should link to thread"
51 );
52 }
53
54 #[tokio::test]
55 async fn profile_nonmember_returns_404() {
56 let mut h = TestHarness::new().await;
57 h.login_as("outsider").await;
58 let _comm_id = h.create_community("ClosedComm", "closed-comm").await;
59
60 // User exists but is not a member
61 let resp = h.client.get("/p/closed-comm/u/outsider").await;
62 assert_eq!(resp.status.as_u16(), 404, "non-member should get 404");
63 }
64
65 #[tokio::test]
66 async fn profile_nonexistent_user_404() {
67 let mut h = TestHarness::new().await;
68 h.login_as("someuser").await;
69 let _comm_id = h.create_community("SomeComm", "some-comm").await;
70
71 let resp = h.client.get("/p/some-comm/u/nobodyhere").await;
72 assert_eq!(resp.status.as_u16(), 404, "nonexistent user should get 404");
73 }
74
75 #[tokio::test]
76 async fn profile_suspended_community_blocked() {
77 let mut h = TestHarness::new().await;
78 let user_id = h.login_as("suspendedfan").await;
79 let comm_id = h.create_community("SuspendedComm", "suspended-comm").await;
80 h.add_membership(user_id, comm_id, "member").await;
81
82 sqlx::query("UPDATE communities SET suspended_at = now() WHERE id = $1")
83 .bind(comm_id)
84 .execute(&h.db)
85 .await
86 .unwrap();
87
88 let resp = h.client.get("/p/suspended-comm/u/suspendedfan").await;
89 assert_eq!(
90 resp.status.as_u16(),
91 403,
92 "suspended community should return 403"
93 );
94 }
95
96 #[tokio::test]
97 async fn api_summary_requires_auth() {
98 let mut h = TestHarness::new().await;
99 // Don't log in, make unauthenticated request
100 h.client.get("/").await; // establish session
101
102 let user_id = uuid::Uuid::new_v4();
103 let resp = h.client.get(&format!("/api/user/{user_id}/summary")).await;
104 assert_eq!(resp.status.as_u16(), 401, "unauthenticated should get 401");
105 }
106
107 #[tokio::test]
108 async fn api_summary_returns_memberships() {
109 let mut h = TestHarness::new().await;
110 let user_id = h.login_as("summaryuser").await;
111 let comm_id = h.create_community("SummaryComm", "summary-comm").await;
112 h.add_membership(user_id, comm_id, "member").await;
113
114 let resp = h.client.get(&format!("/api/user/{user_id}/summary")).await;
115 assert_eq!(resp.status.as_u16(), 200, "should return 200");
116
117 let json: serde_json::Value = resp.json();
118 let memberships = json["memberships"].as_array().unwrap();
119 assert_eq!(memberships.len(), 1, "should have one membership");
120 assert_eq!(memberships[0]["community_name"], "SummaryComm");
121 assert_eq!(memberships[0]["community_slug"], "summary-comm");
122 assert_eq!(memberships[0]["role"], "member");
123 }
124
125 #[tokio::test]
126 async fn api_summary_only_own_data() {
127 let mut h = TestHarness::new().await;
128 let _user_id = h.login_as("snoop").await;
129
130 let other_id = uuid::Uuid::new_v4();
131 let resp = h.client.get(&format!("/api/user/{other_id}/summary")).await;
132 assert_eq!(
133 resp.status.as_u16(),
134 403,
135 "accessing other user's data should return 403"
136 );
137 }
138
139 #[tokio::test]
140 async fn profile_shows_endorsement_count() {
141 let mut h = TestHarness::new().await;
142 let author_id = h.login_as("endorsedauthor").await;
143 let comm_id = h.create_community("EndorseComm", "endorse-comm").await;
144 let cat_id = h.create_category(comm_id, "General", "general").await;
145 h.add_membership(author_id, comm_id, "member").await;
146
147 let thread_id = h
148 .create_thread_with_post(cat_id, author_id, "Great Thread", "Great content")
149 .await;
150
151 let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id)
152 .await
153 .unwrap();
154 let post_id = posts[0].id;
155
156 let endorser_id = h.login_as("endorser1").await;
157 h.add_membership(endorser_id, comm_id, "member").await;
158 mt_db::mutations::toggle_endorsement(&h.db, post_id, endorser_id)
159 .await
160 .unwrap();
161
162 let resp = h.client.get("/p/endorse-comm/u/endorsedauthor").await;
163 assert_eq!(resp.status.as_u16(), 200);
164 assert!(
165 resp.text.contains("1 endorsement received"),
166 "Profile should show endorsement count. Body: {}",
167 &resp.text[..500.min(resp.text.len())]
168 );
169 }
170
171 #[tokio::test]
172 async fn tracking_info_page_loads() {
173 let mut h = TestHarness::new().await;
174 h.login_as("infouser").await;
175
176 let resp = h.client.get("/about/tracking").await;
177 assert_eq!(resp.status.as_u16(), 200);
178 assert!(
179 resp.text.contains("How Tracking Works"),
180 "Should show tracking info heading"
181 );
182 assert!(
183 resp.text.contains("localStorage"),
184 "Should explain localStorage tracking"
185 );
186 assert!(
187 resp.text.contains("No third-party"),
188 "Should mention no third-party tracking"
189 );
190 }
191