Skip to content

Commit

Permalink
processed comments
Browse files Browse the repository at this point in the history
Signed-off-by: DenisRybas <[email protected]>
  • Loading branch information
DenisRybas committed Feb 12, 2024
1 parent a945a5f commit b02ae42
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 95 deletions.
42 changes: 42 additions & 0 deletions vdr/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vdr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ web-sys = { version = "0.3.64", optional = true, features = ["Window"] }
web3-wasm = { package = "web3", version = "0.19.0", default-features = false, features = ["wasm", "http", "http-tls"], optional = true }

[dev-dependencies]
rstest = "0.18.0"
mockall = "0.12.0"
env_logger = "0.10.0"
rand = "0.8.5"
Expand Down
49 changes: 15 additions & 34 deletions vdr/src/contracts/cl/credential_definition_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub mod test {
},
contracts::{
cl::types::{
credential_definition::test::{credential_definition, CREDENTIAL_DEFINITION_TAG},
credential_definition::test::{credential_definition, CREDENTIAL_DEFINITION_TAG, credential_definition_value},
schema::test::SCHEMA_ID,
schema_id::SchemaId,
},
Expand All @@ -281,6 +281,7 @@ pub mod test {
use std::sync::RwLock;

mod build_create_credential_definition_transaction {
use rstest::rstest;
use super::*;
use serde_json::Value;

Expand Down Expand Up @@ -341,47 +342,27 @@ pub mod test {
assert_eq!(expected_transaction, transaction);
}

#[async_std::test]
async fn build_create_credential_definition_transaction_no_tag() {
#[rstest]
#[case("", credential_definition_value())]
#[case(CREDENTIAL_DEFINITION_TAG, Value::Null)]
async fn build_create_credential_definition_transaction_errors(
#[case] tag: &str,
#[case] value: Value,
) {
init_env_logger();
let client = mock_client();
let (id, mut cred_def) = credential_definition(
&DID::from(ISSUER_ID),
&SchemaId::from(SCHEMA_ID),
Some(CREDENTIAL_DEFINITION_TAG),
Some(tag),
);
cred_def.tag = "".to_string();
cred_def.tag = tag.to_string();
cred_def.value = value;

let err = build_create_credential_definition_transaction(
&client,
&TRUSTEE_ACC,
&id,
&cred_def,
)
.await
.unwrap_err();
assert!(matches!(err, VdrError::InvalidCredentialDefinition { .. }));
}

#[async_std::test]
async fn build_create_credential_definition_transaction_no_value() {
init_env_logger();
let client = mock_client();
let (id, mut cred_def) = credential_definition(
&DID::from(ISSUER_ID),
&SchemaId::from(SCHEMA_ID),
Some(CREDENTIAL_DEFINITION_TAG),
);
cred_def.value = Value::Null;
let err = build_create_credential_definition_transaction(&client, &TRUSTEE_ACC, &id, &cred_def)
.await
.unwrap_err();

let err = build_create_credential_definition_transaction(
&client,
&TRUSTEE_ACC,
&id,
&cred_def,
)
.await
.unwrap_err();
assert!(matches!(err, VdrError::InvalidCredentialDefinition { .. }));
}
}
Expand Down
48 changes: 17 additions & 31 deletions vdr/src/contracts/cl/schema_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,21 +259,23 @@ pub mod test {
mock_client, CHAIN_ID, DEFAULT_NONCE, SCHEMA_REGISTRY_ADDRESS, TRUSTEE_ACC,
},
contracts::{
cl::types::schema::test::{schema, SCHEMA_NAME},
cl::types::schema::test::{schema, SCHEMA_NAME, SCHEMA_VERSION, SCHEMA_ATTRIBUTES},
did::types::{did::DID, did_doc::test::ISSUER_ID},
},
utils::init_env_logger,
};
use std::sync::RwLock;

mod build_create_schema_transaction {
use std::collections::HashSet;
use rstest::rstest;
use super::*;

#[async_std::test]
async fn build_create_schema_transaction_test() {
init_env_logger();
let client = mock_client();
let (id, schema) = schema(&DID::from(ISSUER_ID), Some(SCHEMA_NAME));
let (_, schema) = schema(&DID::from(ISSUER_ID), Some(SCHEMA_NAME));
let transaction = build_create_schema_transaction(&client, &TRUSTEE_ACC, &schema)
.await
.unwrap();
Expand Down Expand Up @@ -306,42 +308,26 @@ pub mod test {
assert_eq!(expected_transaction, transaction);
}

#[async_std::test]
async fn build_create_schema_transaction_no_name() {
init_env_logger();
let client = mock_client();
let (id, mut schema) = schema(&DID::from(ISSUER_ID), Some(SCHEMA_NAME));
schema.name = "".to_string();

let err = build_create_schema_transaction(&client, &TRUSTEE_ACC, &schema)
.await
.unwrap_err();
assert!(matches!(err, VdrError::InvalidSchema { .. }));
}

#[async_std::test]
async fn build_create_schema_transaction_no_version() {
#[rstest]
#[case::name_not_provided("", SCHEMA_VERSION, &SCHEMA_ATTRIBUTES)]
#[case::version_not_provided(SCHEMA_NAME, "", &SCHEMA_ATTRIBUTES)]
#[case::attributes_not_provided(SCHEMA_NAME, SCHEMA_VERSION, &HashSet::new())]
async fn build_create_schema_transaction_errors(
#[case] name: &str,
#[case] version: &str,
#[case] attributes: &HashSet<String>,
) {
init_env_logger();
let client = mock_client();
let (id, mut schema) = schema(&DID::from(ISSUER_ID), Some(SCHEMA_NAME));
schema.version = "".to_string();
let (_, mut schema) = schema(&DID::from(ISSUER_ID), Some(name));
schema.name = name.to_string();
schema.version = version.to_string();
schema.attr_names = attributes.clone();

let err = build_create_schema_transaction(&client, &TRUSTEE_ACC, &schema)
.await
.unwrap_err();
assert!(matches!(err, VdrError::InvalidSchema { .. }));
}

#[async_std::test]
async fn build_create_schema_transaction_no_attributes() {
init_env_logger();
let client = mock_client();
let (id, mut schema) = schema(&DID::from(ISSUER_ID), Some(SCHEMA_NAME));
schema.attr_names = vec![];

let err = build_create_schema_transaction(&client, &TRUSTEE_ACC, &schema)
.await
.unwrap_err();
assert!(matches!(err, VdrError::InvalidSchema { .. }));
}
}
Expand Down
14 changes: 2 additions & 12 deletions vdr/src/contracts/cl/types/credential_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,12 @@ pub struct CredentialDefinition {
pub value: serde_json::Value,
}




impl CredentialDefinition {
pub fn id(&self) -> CredentialDefinitionId {
CredentialDefinitionId::build(&self.issuer_id, &self.schema_id.to_string(), &self.tag)
CredentialDefinitionId::build(&self.issuer_id, &self.schema_id, &self.tag)
}

pub fn validate(&self) -> VdrResult<()> {
if self.cred_def_type != CredentialDefinitionTypes::CL {
return Err(VdrError::InvalidCredentialDefinition(format!(
"Unsupported type: {}",
self.cred_def_type.as_ref()
)));
}

if self.tag.is_empty() {
return Err(VdrError::InvalidCredentialDefinition(
"Tag is not provided".to_string(),
Expand Down Expand Up @@ -116,7 +106,7 @@ pub mod test {
CredentialDefinitionId::build(issuer_id, schema_id, tag)
}

fn credential_definition_value() -> serde_json::Value {
pub fn credential_definition_value() -> serde_json::Value {
json!({
"n": "779...397",
"rctxt": "774...977",
Expand Down
19 changes: 6 additions & 13 deletions vdr/src/contracts/cl/types/schema.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use crate::{error::VdrError, types::{ContractOutput, ContractParam}, Address, VdrResult, SchemaId};
use crate::{
error::VdrError,
types::{ContractOutput, ContractParam},
Address,
};
use std::collections::HashSet;
use crate::{
error::VdrError,
types::{ContractOutput, ContractParam},
Address, SchemaId, VdrResult,
};

use crate::{contracts::did::types::did::DID, types::ContractEvent};
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -104,6 +94,7 @@ impl TryFrom<ContractEvent> for SchemaCreatedEvent {

#[cfg(test)]
pub mod test {
use once_cell::sync::Lazy;
use super::*;
use crate::{
contracts::{cl::types::schema_id::SchemaId, did::types::did_doc::test::ISSUER_ID},
Expand All @@ -116,6 +107,11 @@ pub mod test {
pub const SCHEMA_VERSION: &str = "1.0.0";
pub const SCHEMA_ATTRIBUTE_FIRST_NAME: &str = "First Name";

pub static SCHEMA_ATTRIBUTES: Lazy<HashSet<String>> = Lazy::new(|| {
let attributes = vec![SCHEMA_ATTRIBUTE_FIRST_NAME.to_string()];
attributes.into_iter().collect()
});

pub fn schema_id(issuer_id: &DID, name: &str) -> SchemaId {
SchemaId::build(issuer_id, name, SCHEMA_VERSION)
}
Expand All @@ -142,9 +138,6 @@ pub mod test {

mod convert_into_contract_param {
use super::*;
use crate::contracts::cl::types::credential_definition::test::{
credential_definition, CREDENTIAL_DEFINITION_TAG,
};

#[test]
fn convert_schema_into_contract_param_test() {
Expand Down
1 change: 0 additions & 1 deletion vdr/src/contracts/migration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod legacy_mapping_registry;
pub mod types;

pub use types::*;
4 changes: 0 additions & 4 deletions vdr/src/contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ pub mod did;
pub mod migration;
pub mod network;

pub use auth::{role_control, Role};
pub use cl::{credential_definition_registry, schema_registry, CredentialDefinition, Schema};
pub use did::*;
pub use migration::legacy_mapping_registry;
pub use network::validator_control;

0 comments on commit b02ae42

Please sign in to comment.