Skip to content

Commit

Permalink
feat(storage): check if bucket name is not empty
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Feb 27, 2023
1 parent 068f775 commit eb193db
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
26 changes: 22 additions & 4 deletions contracts/cw-storage/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ pub fn instantiate(
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let bucket = Bucket {
name: msg.bucket,
limits: msg.limits.into(),
};
let bucket = Bucket::new(msg.bucket, msg.limits.into())?;

set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
BUCKET.save(deps.storage, &bucket)?;
Expand Down Expand Up @@ -69,6 +66,7 @@ pub mod query {
#[cfg(test)]
mod tests {
use super::*;
use crate::error::BucketError;
use crate::msg::{BucketLimits, BucketResponse};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{from_binary, Uint128};
Expand Down Expand Up @@ -124,4 +122,24 @@ mod tests {
assert_eq!(Uint128::new(2000), value.limits.max_object_size.unwrap());
assert_eq!(Uint128::new(1), value.limits.max_object_pins.unwrap());
}

#[test]
fn empty_name_initialization() {
let mut deps = mock_dependencies();

let msg = InstantiateMsg {
bucket: "".to_string(),
limits: BucketLimits {
max_total_size: None,
max_objects: None,
max_object_size: None,
max_object_pins: None,
},
};
let info = mock_info("creator", &[]);

let err = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap_err();

assert_eq!(err, ContractError::Bucket(BucketError::EmptyName));
}
}
11 changes: 10 additions & 1 deletion contracts/cw-storage/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
use cosmwasm_std::StdError;
use thiserror::Error;

#[derive(Error, Debug)]
#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

#[error("Not implemented")]
NotImplemented {},

#[error("{0}")]
Bucket(#[from] BucketError),
}

#[derive(Error, Debug, Eq, PartialEq)]
pub enum BucketError {
#[error("Name of bucket could not be empty")]
EmptyName,
}
12 changes: 12 additions & 0 deletions contracts/cw-storage/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::error::BucketError;
use crate::error::BucketError::EmptyName;
use crate::msg::BucketLimits;
use cosmwasm_std::Uint128;
use cw_storage_plus::Item;
Expand All @@ -12,6 +14,16 @@ pub struct Bucket {
pub limits: Limits,
}

impl Bucket {
pub fn new(name: String, limits: Limits) -> Result<Self, BucketError> {
if name.is_empty() {
return Err(EmptyName);
}

Ok(Self { name, limits })
}
}

/// Limits is the type of the limits of a bucket.
///
/// The limits are optional and if not set, there is no limit.
Expand Down

0 comments on commit eb193db

Please sign in to comment.