use super::harness::{Absent, Journal, MODEL, caller, other, two_tenant_catalogue_state};
use crate::desired_state::{
Credentials, ExpectedRevision, LoadedRevision, ModelOwner, Models, PolicyScope, PolicySet,
SecretOwner, fixtures,
};
async fn hydrated(journal: &Journal) -> LoadedRevision {
journal
.publish(
"two-catalogues",
ExpectedRevision::Empty,
two_tenant_catalogue_state(),
)
.await
.expect("two tenants with catalogues of their own publish");
journal.hydrated().await
}
#[tokio::test]
async fn a_tenants_credentials_are_never_another_tenants() {
let Some(journal) = Journal::open().await else {
return;
};
let revision = hydrated(&journal).await;
let credentials = Credentials::of(revision.state()).expect("the revision's credentials");
let mine: Vec<_> = credentials
.of_owner(SecretOwner::tenant(caller()))
.collect();
let theirs: Vec<_> = credentials.of_owner(SecretOwner::tenant(other())).collect();
assert_eq!(mine.len(), 1, "the caller's credentials: {mine:?}");
assert_eq!(
theirs.len(),
1,
"the other tenant's credentials: {theirs:?}"
);
assert_ne!(
mine[0].reference, theirs[0].reference,
"both tenants resolved to one credential"
);
Absent::of_the_other_tenant()
.assert_absent("the caller's own credentials", &format!("{mine:?}"));
assert_eq!(
credentials.owner_of(fixtures::secret_id(13)),
Some(SecretOwner::tenant(other())),
"the other tenant's secret is not attributed to it"
);
assert_eq!(
credentials.owner_of(fixtures::secret_id(3)),
Some(SecretOwner::tenant(caller())),
"the caller's own secret is not attributed to it"
);
}
#[tokio::test]
async fn the_same_offering_enabled_twice_is_two_tenants_models() {
let Some(journal) = Journal::open().await else {
return;
};
let revision = hydrated(&journal).await;
let models = Models::of(revision.state()).expect("the revision's models");
let offering = fixtures::offering_id(MODEL);
let mine = models
.default_for(caller(), offering)
.expect("the caller enabled the offering for itself");
let theirs = models
.default_for(other(), offering)
.expect("the other tenant enabled the same offering for itself");
assert_ne!(
mine.reference, theirs.reference,
"one enablement is answering for two tenants"
);
assert_eq!(mine.body.owner(), ModelOwner::tenant(caller()));
assert_eq!(theirs.body.owner(), ModelOwner::tenant(other()));
for (label, tenant, project, expected) in [
("its own project", caller(), fixtures::project_id(2), mine),
(
"the other tenant's project",
other(),
fixtures::project_id(12),
theirs,
),
] {
assert_eq!(
models
.effective_for(tenant, project, offering)
.map(|enablement| enablement.reference),
Some(expected.reference),
"{label} resolved to the wrong tenant's enablement"
);
}
let crossed = models.effective_for(caller(), fixtures::project_id(12), offering);
assert_ne!(
crossed.map(|enablement| enablement.reference),
Some(theirs.reference),
"naming another tenant's project reached that tenant's enablement"
);
let ours: Vec<_> = models.aliases_of(fixtures::project_id(2)).collect();
assert_eq!(ours.len(), 1, "the caller's aliases: {ours:?}");
assert_eq!(ours[0].slug.as_str(), "quick");
assert_eq!(ours[0].body.tenant(), caller());
assert_eq!(
ours[0]
.body
.targets()
.iter()
.map(|target| target.reference())
.collect::<Vec<_>>(),
vec![mine.reference],
"the caller's alias resolves somewhere other than its own enablement"
);
Absent::of_the_other_tenant().assert_absent("the caller's own aliases", &format!("{ours:?}"));
let theirs: Vec<_> = models.aliases_of(fixtures::project_id(12)).collect();
assert_eq!(theirs.len(), 1, "the other tenant's aliases: {theirs:?}");
assert_eq!(theirs[0].body.tenant(), other());
}
#[tokio::test]
async fn a_tenants_policy_governs_only_its_own_scopes() {
let Some(journal) = Journal::open().await else {
return;
};
let revision = hydrated(&journal).await;
let policies = PolicySet::of(revision.state()).expect("the revision's policies");
let mine = policies
.document(PolicyScope::Tenant(caller()))
.expect("the caller published a policy");
let theirs = policies
.document(PolicyScope::Tenant(other()))
.expect("the other tenant published one too");
assert_ne!(
mine.reference, theirs.reference,
"one document is governing two tenants"
);
for (label, scope, expected) in [
(
"the caller's own tenant",
PolicyScope::Tenant(caller()),
mine,
),
(
"the caller's own project",
PolicyScope::Project {
tenant: caller(),
project: fixtures::project_id(2),
},
mine,
),
(
"the other tenant's project",
PolicyScope::Project {
tenant: other(),
project: fixtures::project_id(12),
},
theirs,
),
] {
assert_eq!(
policies.effective(scope).map(|document| document.reference),
Some(expected.reference),
"{label} is governed by the wrong tenant's policy"
);
}
assert_eq!(
policies
.effective(PolicyScope::Project {
tenant: caller(),
project: fixtures::project_id(12),
})
.map(|document| document.reference),
Some(mine.reference),
"naming another tenant's project reached that tenant's policy"
);
Absent::of_the_other_tenant()
.assert_absent("the policy governing the caller", &format!("{mine:?}"));
}