forge
Explore
Log in

Don't you dare go Hollow

Verify-squared: drop dead test assignment; end-to-end profile-frame tests

Browse files at this commit

3d752619a08d74a174a3370a8098fee7211b73d0Ahmet Kaan GümüşParent: 339b198
- im-client: the split-frame parser test initialized 'events' before
  the loop reassigned it — a value-assigned-never-read warning in the
  test profile. The initialization is gone.
- im-web: two router-level tests pin the emit path end to end. They
  open GET /directory/live through the router, read the body's data
  stream frame by frame (topcoat Body::into_data_stream, Box-pinned),
  drive POST /api/profile_photo and POST /api/delete_profile_photo
  through the router with the session cookie, and assert the
  event:profile frame arrives carrying sub and photo_version:2 —
  upload and removal halves both.
crates/im-client/src/directory.rsmodified+1 -1
@@ -379,7 +379,7 @@ mod tests {
379 fn a_frame_split_across_reads_still_parses() {379 fn a_frame_split_across_reads_still_parses() {
380 let whole = frame("two", 7);380 let whole = frame("two", 7);
381 let mut parser = SseParser::default();381 let mut parser = SseParser::default();
382- let mut events = Vec::new();382+ let mut events;
383 // Cut at every byte boundary in turn; no split may lose the event.383 // Cut at every byte boundary in turn; no split may lose the event.
384 for cut in 1..whole.len() {384 for cut in 1..whole.len() {
385 events = parser.feed(&whole.as_bytes()[..cut]);385 events = parser.feed(&whole.as_bytes()[..cut]);
crates/im-web/src/photo.rsmodified+148 -1
@@ -225,6 +225,7 @@ mod tests {
225 use std::path::Path;225 use std::path::Path;
226 use std::sync::Arc;226 use std::sync::Arc;
227 227
228+ use futures_util::StreamExt as _;
228 use im_core::accounts::{create_invite, create_user_from_invite};229 use im_core::accounts::{create_invite, create_user_from_invite};
229 use im_core::model::UserId;230 use im_core::model::UserId;
230 use im_core::oidc::create_client;231 use im_core::oidc::create_client;
@@ -232,7 +233,9 @@ mod tests {
232 use im_core::sessions::{SessionMeta, create_session};233 use im_core::sessions::{SessionMeta, create_session};
233 use im_core::store::Store;234 use im_core::store::Store;
234 use topcoat::cookie::RouterBuilderCookieExt as _;235 use topcoat::cookie::RouterBuilderCookieExt as _;
235- use topcoat::router::{Body, HeaderMap, Router, RouterBuilderDiscoverExt as _, StatusCode};236+ use topcoat::router::{
237+ Body, BodyDataStream, HeaderMap, Router, RouterBuilderDiscoverExt as _, StatusCode,
238+ };
236 use topcoat::router::{header, to_bytes};239 use topcoat::router::{header, to_bytes};
237 240
238 use crate::config::Config;241 use crate::config::Config;
@@ -612,4 +615,148 @@ mod tests {
612 assert_eq!(status, StatusCode::NOT_MODIFIED);615 assert_eq!(status, StatusCode::NOT_MODIFIED);
613 assert!(bytes.is_empty());616 assert!(bytes.is_empty());
614 }617 }
618+
619+ /// POSTs a photo the way the avatar script's autosubmit does — a
620+ /// multipart form with one named file — through the router, with the
621+ /// session cookie carrying who it is for.
622+ async fn post_upload(
623+ router: &Router,
624+ cookie: &str,
625+ bytes: &[u8],
626+ ) -> (StatusCode, Option<String>) {
627+ const BOUNDARY: &str = "imwebtestboundary";
628+ let mut body = Vec::new();
629+ body.extend_from_slice(
630+ format!(
631+ "--{BOUNDARY}\r\n\
632+ Content-Disposition: form-data; name=\"file\"; filename=\"face.png\"\r\n\
633+ Content-Type: image/png\r\n\r\n"
634+ )
635+ .as_bytes(),
636+ );
637+ body.extend_from_slice(bytes);
638+ body.extend_from_slice(format!("\r\n--{BOUNDARY}--\r\n").as_bytes());
639+ let response = router
640+ .handle(
641+ http::Request::builder()
642+ .method(http::Method::POST)
643+ .uri("/api/profile_photo")
644+ .header(
645+ header::CONTENT_TYPE,
646+ format!("multipart/form-data; boundary={BOUNDARY}"),
647+ )
648+ .header(header::COOKIE, cookie)
649+ .body(Body::from(body))
650+ .unwrap(),
651+ )
652+ .await;
653+ let (parts, body) = response.into_parts();
654+ let location = parts
655+ .headers
656+ .get(header::LOCATION)
657+ .and_then(|v| v.to_str().ok())
658+ .map(str::to_string);
659+ let _ = to_bytes(body, usize::MAX).await.unwrap();
660+ (parts.status, location)
661+ }
662+
663+ /// Opens `/directory/live` as an app would and pins the body's data
664+ /// stream. A single SSE frame may straddle two body frames, so
665+ /// callers accumulate text and match on substrings, never on chunk
666+ /// boundaries.
667+ async fn open_live_stream(
668+ router: &Router,
669+ authorization: &str,
670+ ) -> std::pin::Pin<Box<BodyDataStream>> {
671+ let response = router
672+ .handle(
673+ http::Request::builder()
674+ .uri("/directory/live")
675+ .header(header::AUTHORIZATION, authorization)
676+ .body(Body::empty())
677+ .unwrap(),
678+ )
679+ .await;
680+ let (parts, body) = response.into_parts();
681+ assert_eq!(parts.status, StatusCode::OK);
682+ Box::pin(body.into_data_stream())
683+ }
684+
685+ /// The next body frame off the stream, as text.
686+ async fn next_wire_chunk(stream: &mut std::pin::Pin<Box<BodyDataStream>>) -> String {
687+ let chunk = stream
688+ .as_mut()
689+ .next()
690+ .await
691+ .expect("stream stays open")
692+ .expect("frames succeed");
693+ String::from_utf8_lossy(&chunk).into_owned()
694+ }
695+
696+ /// The whole emit path, end to end: a signed-in upload through the
697+ /// router, the committed row's new version on the live bus, and an
698+ /// `event: profile` frame off `/directory/live` carrying it.
699+ #[tokio::test]
700+ async fn a_photo_upload_through_the_router_emits_a_profile_frame() {
701+ let setup = setup().await;
702+ // Open the app stream first, the way a sibling sits on it, and
703+ // read through the opening reconnection hint.
704+ let mut stream =
705+ open_live_stream(&setup.router, &basic(&setup.client_id, &setup.secret)).await;
706+ let mut wire = String::new();
707+ while !wire.contains("retry: 5000") {
708+ wire.push_str(&next_wire_chunk(&mut stream).await);
709+ }
710+
711+ // Setup's upload was version 1; this one must announce 2.
712+ let (status, location) = post_upload(&setup.router, &setup.session_cookie, PHOTO).await;
713+ assert_eq!(status, StatusCode::SEE_OTHER, "upload answers a 303");
714+ assert_eq!(location.as_deref(), Some("/?ok=photo_saved"));
715+
716+ wire.clear();
717+ while !wire.contains("event: profile") {
718+ wire.push_str(&next_wire_chunk(&mut stream).await);
719+ }
720+ assert!(wire.contains(&format!("\"sub\":\"{}\"", setup.user_id)));
721+ assert!(
722+ wire.contains("\"photo_version\":2"),
723+ "the frame carries the bumped version: {wire}"
724+ );
725+ }
726+
727+ /// Same path, the removal half: a delete announces too, and its frame
728+ /// moves the version forward even though the mime is gone.
729+ #[tokio::test]
730+ async fn a_photo_delete_through_the_router_emits_a_profile_frame() {
731+ let setup = setup().await;
732+ let mut stream =
733+ open_live_stream(&setup.router, &basic(&setup.client_id, &setup.secret)).await;
734+ let mut wire = String::new();
735+ while !wire.contains("retry: 5000") {
736+ wire.push_str(&next_wire_chunk(&mut stream).await);
737+ }
738+
739+ let response = setup
740+ .router
741+ .handle(
742+ http::Request::builder()
743+ .method(http::Method::POST)
744+ .uri("/api/delete_profile_photo")
745+ .header(header::COOKIE, &setup.session_cookie)
746+ .body(Body::empty())
747+ .unwrap(),
748+ )
749+ .await;
750+ let (parts, _) = response.into_parts();
751+ assert_eq!(parts.status, StatusCode::SEE_OTHER);
752+
753+ wire.clear();
754+ while !wire.contains("event: profile") {
755+ wire.push_str(&next_wire_chunk(&mut stream).await);
756+ }
757+ assert!(
758+ wire.contains("\"photo_version\":2"),
759+ "the removal bumps and announces the version: {wire}"
760+ );
761+ }
615 }762 }