Registry testing - part 4

This commit is contained in:
mdecimus
2026-03-14 20:39:25 +01:00
parent 65a5900e7b
commit 4b5688fd57
28 changed files with 1448 additions and 922 deletions

View File

@@ -45,6 +45,8 @@ pub struct JsonPointerPatch<'x> {
pos: usize,
validators: &'x [StringValidator],
is_create: bool,
can_set_tenant: bool,
can_set_account: bool,
}
pub trait RegistryJsonPatch: Debug + Default {

View File

@@ -27,6 +27,19 @@ impl<'x> JsonPointerPatch<'x> {
pos: 0,
validators: &[],
is_create: false,
can_set_tenant: false,
can_set_account: false,
}
}
pub fn cloned_with_ptr(&self, ptr: &'x JsonPointer<Property>) -> Self {
Self {
ptr,
pos: 0,
validators: &[],
is_create: self.is_create,
can_set_tenant: self.can_set_tenant,
can_set_account: self.can_set_account,
}
}
@@ -35,7 +48,9 @@ impl<'x> JsonPointerPatch<'x> {
ptr: self.ptr,
pos: 0,
validators: &[],
is_create: false,
is_create: self.is_create,
can_set_tenant: self.can_set_tenant,
can_set_account: self.can_set_account,
}
}
@@ -44,6 +59,16 @@ impl<'x> JsonPointerPatch<'x> {
self
}
pub fn with_can_set_tenant(mut self, can_set_tenant: bool) -> Self {
self.can_set_tenant = can_set_tenant;
self
}
pub fn with_can_set_account(mut self, can_set_account: bool) -> Self {
self.can_set_account = can_set_account;
self
}
pub fn with_validators(mut self, validators: &'x [StringValidator]) -> Self {
self.validators = validators;
self
@@ -98,9 +123,31 @@ impl<'x> JsonPointerPatch<'x> {
pub fn assert_server_set(self) -> PatchResult<'static> {
Err(PatchError::new(
self.cloned(),
"Cannot modify server-set property",
"Cannot modify server set property",
))
}
pub fn assert_can_set_tenant(self) -> Result<Self, PatchError> {
if self.can_set_tenant {
Ok(self)
} else {
Err(PatchError::new(
self.cloned(),
"Cannot modify memberTenantId property",
))
}
}
pub fn assert_can_set_account(self) -> Result<Self, PatchError> {
if self.can_set_account {
Ok(self)
} else {
Err(PatchError::new(
self.cloned(),
"Cannot modify accountId property",
))
}
}
}
impl<T: RegistryJsonPatch> RegistryJsonPatch for Option<T> {
@@ -326,7 +373,7 @@ impl<T: RegistryJsonPropertyPatch> RegistryJsonPatch for T {
if let Some(property) = key.as_property() {
if *property != Property::Type {
ptr.as_mut_slice()[0] = JsonPointerItem::Key(Key::Property(*property));
match self.patch_property(JsonPointerPatch::new(&ptr), value) {
match self.patch_property(pointer.cloned_with_ptr(&ptr), value) {
Ok(MaybeUnpatched::Patched) => {}
Ok(MaybeUnpatched::Unpatched { property, value }) => {
unpatched.append(property, value);
@@ -336,9 +383,20 @@ impl<T: RegistryJsonPropertyPatch> RegistryJsonPatch for T {
}
Err(mut e) => {
if !e.path.is_empty() {
e.path = format!("{}/{}", e.path, property.as_str());
if !pointer.ptr.as_slice().is_empty() {
e.path = format!("{}/{}", pointer.path(), e.path);
}
} else {
e.path = property.as_str().to_string();
e.path = JsonPointer::new(
pointer
.ptr
.as_slice()
.iter()
.cloned()
.chain([JsonPointerItem::Key(Key::Property(*property))])
.collect(),
)
.to_string();
}
return Err(e);
}

View File

@@ -129,3 +129,9 @@ impl Object {
Object { inner, revision }
}
}
impl From<Property> for String {
fn from(value: Property) -> Self {
value.as_str().to_string()
}
}

View File

@@ -11,7 +11,7 @@ use crate::{
};
use std::{borrow::Cow, fmt::Display};
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type")]
pub enum ValidationError {
Invalid { property: Property, value: String },

View File

@@ -20,7 +20,7 @@ use types::{
id::Id,
};
#[derive(Debug, PartialEq, Clone, Copy, Eq, Hash, serde::Serialize)]
#[derive(Debug, PartialEq, Clone, Copy, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct ObjectId {
object: ObjectType,
id: Id,