Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions apps/auth-service/src/account/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async fn administrators_change_another_account_on_its_behalf() {
.iter()
.find(|stored| stored.event.known() == Some(&expected))
.unwrap_or_else(|| panic!("{expected:?} recorded"));
assert_eq!(stored.actor, actor, "{expected:?}");
assert_eq!(stored.actor.known(), Some(&actor), "{expected:?}");
assert_eq!(stored.reason.as_deref(), Some(REASON), "{expected:?}");
}

Expand Down Expand Up @@ -281,7 +281,7 @@ async fn owners_and_administrators_change_the_display_name() {
Some(DomainEvent::DisplayNameChanged { old, new }) => Some((
old.clone(),
new.clone(),
stored.actor.clone(),
stored.actor.known().cloned().expect("known actor"),
stored.reason.as_deref(),
)),
_ => None,
Expand Down Expand Up @@ -430,11 +430,11 @@ async fn administrators_reads_are_recorded_as_views() {
};
assert_ne!(other.actor, view.actor);
assert_eq!(
view.actor,
Actor::Admin {
view.actor.known(),
Some(&Actor::Admin {
user_id: fixture.admin.user_id,
session_id: fixture.admin.session_id,
}
})
);
assert_eq!(view.subject, Some(target.user_id));
}
Expand Down
8 changes: 5 additions & 3 deletions apps/auth-service/src/admin/service/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ impl AdminService {
Personal::Value(origin) => (origin, false),
Personal::Erased => (RequestOrigin::default(), true),
};
let administrator = match &stored.actor {
Actor::Admin { user_id, .. } => administrators.get(user_id).cloned().flatten(),
let administrator = match stored.actor.known() {
Some(Actor::Admin { user_id, .. }) => {
administrators.get(user_id).cloned().flatten()
}
_ => None,
};
Ok(Event {
Expand Down Expand Up @@ -76,7 +78,7 @@ impl AdminService {
) -> Result<HashMap<UserId, Option<Handle>>, AdminError> {
let mut handles = HashMap::new();
for event in events {
if let Actor::Admin { user_id, .. } = event.actor
if let Some(&Actor::Admin { user_id, .. }) = event.actor.known()
&& !handles.contains_key(&user_id)
{
let user = self.database.users().find_by_id(user_id).await?;
Expand Down
9 changes: 8 additions & 1 deletion apps/auth-service/src/admin/tests/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,14 @@ async fn events_a_newer_release_wrote_are_listed() {
harness
.database
.events()
.insert_written_elsewhere(user.user_id, "from_a_newer_release", 1, &data)
.insert_written_elsewhere(
user.user_id,
"from_a_newer_release",
1,
&data,
&json!({"type": "anonymous"}),
None,
)
.await
.expect("event");
let filter = EventSearch {
Expand Down
2 changes: 1 addition & 1 deletion apps/auth-service/src/admin/tests/announcements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ async fn announcements_change_until_a_confirmed_administrator_sends_them() {
})
})
.expect("recorded");
assert_eq!(recorded.actor, administrator.reading().actor);
assert_eq!(recorded.actor.known(), Some(&administrator.reading().actor));
// Nobody writes a reason for it: its text says why.
assert_eq!(recorded.reason.as_deref(), Some("See message contents"));
assert_refused(
Expand Down
2 changes: 1 addition & 1 deletion apps/auth-service/src/admin/tests/deliveries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ async fn delivery_changes_check_and_record_their_administrator() {
)
})
.unwrap_or_else(|| panic!("{kind} recorded"));
assert_eq!(recorded.actor, admin.reading().actor, "{kind}");
assert_eq!(recorded.actor.known(), Some(&admin.reading().actor));
}
}
2 changes: 1 addition & 1 deletion apps/auth-service/src/admin/tests/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async fn messages_reach_the_inbox_and_the_primary_address_until_purged() {
let events = harness.database.events().list(&filter, 10).await;
let [sent] = events.expect("events").try_into().expect("one event");
assert_eq!(sent.event.known(), Some(&DomainEvent::MessageSent {}));
assert_eq!(sent.actor, admin.reading().actor);
assert_eq!(sent.actor.known(), Some(&admin.reading().actor));
// Nobody writes a reason for it: its text says why.
assert_eq!(sent.reason.as_deref(), Some("See message contents"));

Expand Down
2 changes: 1 addition & 1 deletion apps/auth-service/src/alerts/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ async fn updates_acknowledge_an_alert_until_its_condition_changes() {
.await
.expect("events")
.remove(0);
assert!(matches!(recorded.actor, Actor::Admin { .. }));
assert!(matches!(recorded.actor.known(), Some(Actor::Admin { .. })));
assert_eq!(
recorded.event,
RecordedEvent::Known(DomainEvent::AlertUpdated {
Expand Down
6 changes: 3 additions & 3 deletions apps/auth-service/src/audit/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,10 @@ async fn the_verifier_verifies_when_no_verification_is_recent() {
.expect("newest")
.expect("recorded");
assert_eq!(
newest.actor,
Actor::System {
newest.actor.known(),
Some(&Actor::System {
job: JobName::AuditVerification
}
})
);
assert_eq!(verifier.pass(&mut lock).await.expect("pass"), None);
lock.release().await.expect("release");
Expand Down
10 changes: 5 additions & 5 deletions apps/auth-service/src/cli/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use clap::{Args, Subcommand};
use sourceorigin_common::{
ClientId, EmailId, EventId, NotificationId, PasskeyId, Provider, SessionId, UserId,
admin::KnownDevice,
events::HistoryActor,
notifications::NotificationPreference,
protocol::api::{Identity, Passkey},
};
Expand Down Expand Up @@ -593,7 +594,7 @@ report!(HistoryPage, self, out, {
Cell::new(item.id),
item.occurred_at.into(),
Cell::new(item.entry.kind()),
Cell::new(item.actor),
item.actor.map(HistoryActor::as_str).into(),
item.reason.as_deref().into(),
]
}),
Expand Down Expand Up @@ -652,10 +653,9 @@ report!(NotificationView, self, out, {
fields.push(("Offers", Cell::new(action.label())));
}
if let Some(entry) = &self.history {
fields.push((
"History entry",
Cell::new(format_args!("{} (by {})", entry.id, entry.actor)),
));
// An actor a newer release recorded is not named.
let by = (entry.actor).map_or_else(String::new, |actor| format!(" (by {actor})"));
fields.push(("History entry", Cell::new(format_args!("{}{by}", entry.id))));
}
out.fields(&fields)?;
out.blank()?;
Expand Down
8 changes: 5 additions & 3 deletions apps/auth-service/src/cli/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,9 @@ async fn user_disable_runs_the_admin_operation() {
matches!(event, DomainEvent::AccountDisabled { .. })
})
.await;
assert!(matches!(&disabled.actor, Actor::Cli { os_user, .. } if os_user == "test"));
assert!(
matches!(disabled.actor.known(), Some(Actor::Cli { os_user, .. }) if os_user == "test")
);
recorded(&database, browser.user_id, |event| {
matches!(event, DomainEvent::SessionRevoked { session_id, .. } if *session_id == browser.session_id)
})
Expand Down Expand Up @@ -560,7 +562,7 @@ async fn account_commands_run_the_shared_operation() {
matches!(event, DomainEvent::SessionRevoked { session_id, .. } if *session_id == browser.session_id)
})
.await;
assert!(matches!(&revoked.actor, Actor::Cli { os_user, .. } if os_user == "test"));
assert!(matches!(revoked.actor.known(), Some(Actor::Cli { os_user, .. }) if os_user == "test"));
assert_eq!(revoked.reason.as_deref(), Some("Lost phone"));
}

Expand Down Expand Up @@ -609,6 +611,6 @@ async fn account_apps_remove_withdraws_the_consent() {
matches!(event, DomainEvent::ConsentRevoked { client_id } if *client_id == client.id)
})
.await;
assert!(matches!(&removed.actor, Actor::Cli { os_user, .. } if os_user == "test"));
assert!(matches!(removed.actor.known(), Some(Actor::Cli { os_user, .. }) if os_user == "test"));
assert_eq!(removed.reason.as_deref(), Some("Impersonating app"));
}
6 changes: 3 additions & 3 deletions apps/auth-service/src/clients/tests/deletion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ async fn deleted_clients_stay_deleted() {
.await
.expect("history");
assert_eq!(
(history[0].event.known(), &history[0].actor),
(history[0].event.known(), history[0].actor.known()),
(
Some(&DomainEvent::ClientDeleted {
client_id: client_id.clone()
}),
&Actor::User {
Some(&Actor::User {
user_id: alice.user_id,
session_id: Some(alice.session_id),
}
})
)
);

Expand Down
2 changes: 1 addition & 1 deletion apps/auth-service/src/clients/tests/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async fn changes_apply_to_the_next_request_on_every_replica() {
assert!(
history
.iter()
.all(|event| event.actor == operator().reading().actor)
.all(|event| event.actor.known() == Some(&operator().reading().actor))
);
let client_id = client.id.clone();
assert_eq!(
Expand Down
92 changes: 85 additions & 7 deletions apps/auth-service/src/deletion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use super::*;
use crate::{
AdminError, AdminService,
account::Viewer,
jobs::AccountPurge,
security::SessionError,
tests::{
Expand All @@ -15,6 +16,7 @@ use axum::http::header;
use serde_json::json;
use sourceorigin_common::{
AnnouncementId, AuthenticationMethod, PictureId, Provider, RecoveryId, TokenId,
admin::EventSearch,
clients::{ClientKind, ClientOwner},
events::{Actor, JobName, Platform, RequestOrigin, SignInFailure, UserAgentFamily},
notifications::{EmailCategory, Notification, NotificationPreference},
Expand Down Expand Up @@ -55,7 +57,10 @@ impl Fixture {
.await
.expect("history")
.into_iter()
.map(|stored| (stored.event.known().cloned().expect("known"), stored.actor))
.map(|stored| {
let actor = stored.actor.known().cloned().expect("known actor");
(stored.event.known().cloned().expect("known"), actor)
})
.collect()
}

Expand Down Expand Up @@ -377,6 +382,16 @@ fn holds(value: &serde_json::Value, path: &[&str]) -> bool {
}
}

/// The document `export` streams.
async fn download(export: AccountExport) -> String {
let mut body = Vec::new();
let mut chunks = export.stream();
while let Some(chunk) = chunks.next().await {
body.extend_from_slice(&chunk.expect("chunk"));
}
String::from_utf8(body).expect("UTF-8")
}

/// `browser`'s user, acting from it.
const fn user(browser: &Browser) -> Actor {
Actor::User {
Expand Down Expand Up @@ -420,12 +435,7 @@ async fn exports_hold_every_category_and_no_secret() {
));
drop(second);
// Its file name and the sections' shapes are checked in the export's own tests.
let mut body = Vec::new();
let mut chunks = export.stream();
while let Some(chunk) = chunks.next().await {
body.extend_from_slice(&chunk.expect("chunk"));
}
let text = String::from_utf8(body).expect("UTF-8");
let text = download(export).await;
let export: serde_json::Value = serde_json::from_str(&text).expect("JSON");
assert_eq!(
export["profile"]["handle"],
Expand Down Expand Up @@ -489,6 +499,74 @@ async fn exports_hold_every_category_and_no_secret() {
);
}

/// Events whose actor a newer release recorded, of a type this one does not know
/// or a known type naming a job it does not know, fail none of the pages that
/// read them. The dashboard lists them with the actor as stored, which it shows as
/// nobody recorded and the CLI as JSON. The owner's history and export name no
/// actor, and show no IP address or device, as the actor may be an administrator.
#[tokio::test]
#[ignore = "requires TEST_DATABASE_URL"]
async fn actors_a_newer_release_wrote_are_read() {
let fixture = Fixture::new().await;
let browser = Browser::new_account(&fixture.database).await;
let actors = [
json!({"type": "from_a_newer_release"}),
json!({"type": "system", "job": "from_a_newer_release"}),
];
let origin = RequestOrigin {
client_ip: Some([192, 0, 2, 7].into()),
user_agent_family: Some(UserAgentFamily {
browser: sourceorigin_common::events::Browser::Firefox,
platform: Platform::Linux,
}),
};
// Personal data is only recorded in its event's transaction.
let mut tx = fixture.database.begin().await.expect("transaction");
for actor in &actors {
tx.events()
.insert_written_elsewhere(
browser.user_id,
"passkey_renamed",
DomainEvent::FIRST_VERSION,
&json!({"passkey_id": PasskeyId::new()}),
actor,
Some(&origin),
)
.await
.expect("event");
}
tx.commit().await.expect("commit");
let search = EventSearch {
user: Some(browser.user_id),
kind: Some("passkey_renamed".into()),
..EventSearch::default()
};
let page = (fixture.admin.events(&operator(), search))
.await
.expect("events");
let listed: Vec<_> = page.events.iter().map(|event| &event.actor).collect();
assert_eq!(listed, [&actors[1], &actors[0]]);

let owner = browser.owner(&fixture.database).await;
let history = (fixture.account.history(&Viewer::from(&owner), None))
.await
.expect("history");
let history = serde_json::to_value(history.entries).expect("history");
let export = fixture.account.export(&owner).await.expect("export");
let export: serde_json::Value = serde_json::from_str(&download(export).await).expect("JSON");
for (surface, entries, kind) in [
("history", &history, "passkey.renamed"),
("export", &export["events"], "passkey_renamed"),
] {
let renamed: Vec<_> = (entries.as_array().expect("entries").iter())
.filter(|entry| entry["kind"] == kind)
.map(|entry| (&entry["actor"], &entry["ip"], &entry["device"]))
.collect();
let unnamed = (&json!(null), &json!(null), &json!(null));
assert_eq!(renamed, [unnamed, unnamed], "{surface}: {entries}");
}
}

/// Deleting needs the account's handle and a recent passkey confirmation. It signs
/// every browser out, revokes every grant and leaves an account that can no longer
/// sign in.
Expand Down
2 changes: 1 addition & 1 deletion apps/auth-service/src/emails/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ async fn verification_checks_the_signed_in_account() {
Some(&DomainEvent::EmailVerified { email_id: second })
);
assert!(
matches!(verified.actor, Actor::User { user_id, .. } if user_id == owner.user_id),
matches!(verified.actor.known(), Some(&Actor::User { user_id, .. }) if user_id == owner.user_id),
"{:?}",
verified.actor
);
Expand Down
Loading