Skip to content

Commit

Permalink
subdao-timelocks unit tests implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
swelf committed Dec 29, 2022
1 parent d6fffde commit 17e41fc
Show file tree
Hide file tree
Showing 7 changed files with 580 additions and 9 deletions.
8 changes: 3 additions & 5 deletions contracts/subdaos/cwd-subdao-timelock-single/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,9 @@ pub fn execute_update_config(
.map(|new_owner| deps.api.addr_validate(&new_owner))
.transpose()?;

if Some(info.sender) != config.owner && new_owner != config.owner {
return Err(ContractError::OnlyOwnerCanChangeOwner {});
};

config.owner = new_owner;
if new_owner.is_some() {
config.owner = new_owner;
}

if let Some(timelock_duration) = new_timelock_duration {
config.timelock_duration = timelock_duration;
Expand Down
2 changes: 1 addition & 1 deletion contracts/subdaos/cwd-subdao-timelock-single/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ pub mod proposal;
pub mod state;

#[cfg(test)]
mod tests;
mod testing;

pub use crate::error::ContractError;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use neutron_bindings::bindings::msg::NeutronMsg;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, JsonSchema, Debug)]
#[derive(Serialize, Deserialize, Clone, JsonSchema, Debug, Eq, PartialEq)]
pub struct SingleChoiceProposal {
/// The ID of the proposal being returned.
pub id: u64,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::marker::PhantomData;

use cosmwasm_std::{
from_binary, from_slice,
testing::{MockApi, MockQuerier, MockStorage},
to_binary, ContractResult, Empty, OwnedDeps, Querier, QuerierResult, QueryRequest, SystemError,
SystemResult, WasmQuery,
};
use cwd_pre_propose_base::msg::QueryMsg as PreProposeQuery;

pub const MOCK_SUBDAO_CORE_ADDR: &str = "neutron1subdao_core_contract";
pub const MOCK_TIMELOCK_INITIALIZER: &str = "neutron1timelock_initializer";

pub fn mock_dependencies() -> OwnedDeps<MockStorage, MockApi, WasmMockQuerier> {
let custom_querier = WasmMockQuerier::new(MockQuerier::new(&[]));

OwnedDeps {
storage: MockStorage::default(),
api: MockApi::default(),
querier: custom_querier,
custom_query_type: PhantomData,
}
}

pub struct WasmMockQuerier {
base: MockQuerier,
}

impl Querier for WasmMockQuerier {
fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
let request: QueryRequest<Empty> = match from_slice(bin_request) {
Ok(v) => v,
Err(e) => {
return QuerierResult::Err(SystemError::InvalidRequest {
error: format!("Parsing query request: {}", e),
request: bin_request.into(),
});
}
};
self.handle_query(&request)
}
}

impl WasmMockQuerier {
pub fn handle_query(&self, request: &QueryRequest<Empty>) -> QuerierResult {
match &request {
QueryRequest::Wasm(WasmQuery::Smart { contract_addr, msg }) => {
if contract_addr == MOCK_TIMELOCK_INITIALIZER {
let q: PreProposeQuery = from_binary(msg).unwrap();
let addr = match q {
PreProposeQuery::ProposalModule {} => todo!(),
PreProposeQuery::Dao {} => MOCK_SUBDAO_CORE_ADDR,
PreProposeQuery::Config {} => todo!(),
PreProposeQuery::DepositInfo { proposal_id: _ } => todo!(),
};
return SystemResult::Ok(ContractResult::from(to_binary(addr)));
}
SystemResult::Err(SystemError::NoSuchContract {
addr: contract_addr.to_string(),
})
}
_ => self.base.handle_query(request),
}
}
}

impl WasmMockQuerier {
pub fn new(base: MockQuerier) -> Self {
WasmMockQuerier { base }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod mock_querier;
mod tests;
Loading

0 comments on commit 17e41fc

Please sign in to comment.