Skip to content

Commit

Permalink
Move ResourcePreferences under ProfileNetwork (#209)
Browse files Browse the repository at this point in the history
* Move ResourcePreferences under ProfileNetwork

* missing tests

* Update kt extensions

---------

Co-authored-by: Sergiu Puhalschi <[email protected]>
  • Loading branch information
matiasbzurovski and sergiupuhalschi-rdx authored Sep 5, 2024
1 parent 284c16b commit 2d0cdb3
Show file tree
Hide file tree
Showing 20 changed files with 265 additions and 89 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/sargon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "1.1.9"
version = "1.1.10"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod tests {
accounts.clone().to_owned(),
Personas::new(),
AuthorizedDapps::new(),
ResourcePreferences::new(),
);
assert_eq!(
&profile_network.accounts_non_hidden().items(),
Expand All @@ -60,6 +61,7 @@ mod tests {
Accounts::new(),
personas,
AuthorizedDapps::new(),
ResourcePreferences::new(),
);
assert_eq!(
&sut.personas_non_hidden().items(),
Expand Down
38 changes: 2 additions & 36 deletions crates/sargon/src/profile/v100/app_preferences/app_preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ pub struct AppPreferences {

/// Default config related to making of transactions
pub transaction: TransactionPreferences,

/// Configuration related to resources
#[serde(default)]
pub resources: ResourcePreferences,
}

impl AppPreferences {
Expand All @@ -45,13 +41,8 @@ impl AppPreferences {
gateways: {}
security: {}
transaction: {}
resources: {:?}
"#,
self.display,
self.gateways,
self.security,
self.transaction,
self.resources
self.display, self.gateways, self.security, self.transaction
)
}
}
Expand All @@ -62,14 +53,12 @@ impl AppPreferences {
gateways: SavedGateways,
security: Security,
transaction: TransactionPreferences,
resources: ResourcePreferences,
) -> Self {
Self {
display,
gateways,
security,
transaction,
resources,
}
}
}
Expand All @@ -82,7 +71,6 @@ impl HasSampleValues for AppPreferences {
SavedGateways::sample(),
Security::sample(),
TransactionPreferences::sample(),
ResourcePreferences::sample(),
)
}

Expand All @@ -93,7 +81,6 @@ impl HasSampleValues for AppPreferences {
SavedGateways::sample_other(),
Security::sample_other(),
TransactionPreferences::sample_other(),
ResourcePreferences::sample_other(),
)
}
}
Expand Down Expand Up @@ -142,11 +129,6 @@ mod tests {
assert_eq!(SUT::sample().transaction, TransactionPreferences::sample())
}

#[test]
fn get_resources() {
assert_eq!(SUT::sample().resources, ResourcePreferences::sample())
}

#[test]
fn test_has_gateway_with_url() {
let sut = SUT::sample();
Expand Down Expand Up @@ -203,23 +185,7 @@ mod tests {
},
"transaction": {
"defaultDepositGuarantee": "0.975"
},
"resources": [
{
"resource": {
"kind": "fungible",
"value": "resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd"
},
"visibility": "hidden"
},
{
"resource": {
"kind": "nonFungible",
"value": "resource_rdx1t4dy69k6s0gv040xa64cyadyefwtett62ng6xfdnljyydnml7t6g3j"
},
"visibility": "visible"
}
]
}
}
"#,
)
Expand Down
2 changes: 0 additions & 2 deletions crates/sargon/src/profile/v100/app_preferences/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ mod app_display_settings;
mod app_preferences;
mod app_preferences_uniffi_fn;
mod gateways;
mod resource_preferences;
mod security;
mod transaction_preferences;

pub use app_display_settings::*;
pub use app_preferences::*;
pub use app_preferences_uniffi_fn::*;
pub use gateways::*;
pub use resource_preferences::*;
pub use security::*;
pub use transaction_preferences::*;
2 changes: 2 additions & 0 deletions crates/sargon/src/profile/v100/networks/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod network_id_uniffi_fn;
mod personas;
mod profile_network;
mod profile_network_uniffi_fn;
mod resource_preferences;

pub use accounts::*;
pub use authorized_dapp::*;
Expand All @@ -15,3 +16,4 @@ pub use network_id_uniffi_fn::*;
pub use personas::*;
pub use profile_network::*;
pub use profile_network_uniffi_fn::*;
pub use resource_preferences::*;
83 changes: 80 additions & 3 deletions crates/sargon/src/profile/v100/networks/network/profile_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub struct ProfileNetwork {
/// [`AuthorizedDapp`]s that the user has interacted with.
#[serde(rename = "authorizedDapps")]
pub authorized_dapps: AuthorizedDapps,

/// Configuration related to resources
#[serde(default)]
pub resource_preferences: ResourcePreferences,
}

impl IsNetworkAware for ProfileNetwork {
Expand All @@ -49,8 +53,13 @@ impl ProfileNetwork {
accounts: {}
personas: {}
authorized_dapps: {}
resource_preferences: {:?}
"#,
self.id, self.accounts, self.personas, self.authorized_dapps,
self.id,
self.accounts,
self.personas,
self.authorized_dapps,
self.resource_preferences
)
}
}
Expand All @@ -76,11 +85,13 @@ impl ProfileNetwork {
accounts: impl Into<Accounts>,
personas: impl Into<Personas>,
authorized_dapps: impl Into<AuthorizedDapps>,
resource_preferences: impl Into<ResourcePreferences>,
) -> Self {
let network_id = network_id.into();
let accounts = accounts.into();
let personas = personas.into();
let authorized_dapps = authorized_dapps.into();
let resource_preferences = resource_preferences.into();
assert!(
accounts
.get_all()
Expand All @@ -102,11 +113,21 @@ impl ProfileNetwork {
.all(|d| d.network_id == network_id),
"Discrepancy, found an AuthorizedDapp on other network than {network_id}"
);

assert!(
resource_preferences
.get_all()
.into_iter()
.all(|d| d.network_id() == network_id),
"Discrepancy, found a ResourceAppPreference on other network than {network_id}"
);

Self {
id: network_id,
accounts,
personas,
authorized_dapps,
resource_preferences,
}
}

Expand All @@ -118,6 +139,7 @@ impl ProfileNetwork {
Accounts::new(),
Personas::new(),
AuthorizedDapps::new(),
ResourcePreferences::new(),
)
}
}
Expand Down Expand Up @@ -160,6 +182,7 @@ impl ProfileNetwork {
Accounts::sample_mainnet(),
Personas::sample_mainnet(),
AuthorizedDapps::sample_mainnet(),
ResourcePreferences::sample_mainnet(),
)
}

Expand All @@ -170,6 +193,7 @@ impl ProfileNetwork {
Accounts::sample_stokenet(),
Personas::sample_stokenet(),
AuthorizedDapps::sample_stokenet(),
ResourcePreferences::sample_stokenet(),
)
}
}
Expand Down Expand Up @@ -202,6 +226,14 @@ mod tests {
assert_eq!(sut.accounts, Accounts::sample());
}

#[test]
fn get_resources() {
assert_eq!(
SUT::sample().resource_preferences,
ResourcePreferences::sample()
)
}

#[test]
fn duplicate_accounts_are_filtered_out() {
assert_eq!(
Expand All @@ -212,6 +244,7 @@ mod tests {
),
Personas::default(),
AuthorizedDapps::default(),
ResourcePreferences::default(),
)
.accounts
.len(),
Expand All @@ -229,6 +262,7 @@ mod tests {
Accounts::just(Account::sample_stokenet()),
Personas::default(),
AuthorizedDapps::default(),
ResourcePreferences::default(),
);
}

Expand All @@ -242,6 +276,7 @@ mod tests {
Accounts::sample_mainnet(),
Personas::just(Persona::sample_stokenet()),
AuthorizedDapps::default(),
ResourcePreferences::default(),
);
}

Expand All @@ -255,6 +290,23 @@ mod tests {
Accounts::sample_mainnet(),
Personas::sample_mainnet(),
AuthorizedDapps::just(AuthorizedDapp::sample_stokenet()),
ResourcePreferences::default(),
);
}

#[test]
#[should_panic(
expected = "Discrepancy, found a ResourceAppPreference on other network than mainnet"
)]
fn panic_when_network_id_mismatch_between_resource_preferences_and_value() {
SUT::new(
NetworkID::Mainnet,
Accounts::sample_mainnet(),
Personas::sample_mainnet(),
AuthorizedDapps::sample_mainnet(),
ResourcePreferences::from_iter([
ResourceAppPreference::sample_non_fungible_stokenet(),
]),
);
}

Expand Down Expand Up @@ -604,7 +656,23 @@ mod tests {
}
]
}
]
],
"resource_preferences": [
{
"resource": {
"kind": "fungible",
"value": "resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd"
},
"visibility": "hidden"
},
{
"resource": {
"kind": "nonFungible",
"value": "resource_rdx1t4dy69k6s0gv040xa64cyadyefwtett62ng6xfdnljyydnml7t6g3j"
},
"visibility": "visible"
}
]
}
"#,
);
Expand Down Expand Up @@ -950,7 +1018,16 @@ mod tests {
}
]
}
]
],
"resource_preferences": [
{
"resource": {
"kind": "nonFungible",
"value": "resource_tdx_2_1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxtfd2jc"
},
"visibility": "visible"
}
]
}
"#,
)
Expand Down
Loading

0 comments on commit 2d0cdb3

Please sign in to comment.