Skip to content

Commit

Permalink
fix(submit_social): fix social submit bug and adding additional tests
Browse files Browse the repository at this point in the history
fix a bug where the contract state only store the latest social info and discard all previous entries.
now performs overwriting only when the given address already possesses social information on the specific platform
  • Loading branch information
TropicalDog17 committed Oct 22, 2023
1 parent 1b7a85c commit a81da72
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
73 changes: 68 additions & 5 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ pub fn submit_social_link(
if info.sender != OWNER.load(deps.storage)? {
return Err(ContractError::Unauthorized {});
}
let (platform_id, profile_id) = social_info;

let user_info = UserInfo {
address: address.clone(),
platform_id: social_info.0,
profile_id: social_info.1,
platform_id: platform_id.clone(),
profile_id: profile_id.clone(),
};
USER_INFOS.save(deps.storage, address.as_ref(), &user_info)?;
USER_INFOS.save(
deps.storage,
[address.to_string(), platform_id].join("_").as_str(),
&user_info,
)?;

Ok(Response::new().add_attribute("method", "submit_social_link"))
}
Expand Down Expand Up @@ -146,7 +152,7 @@ fn query_by_address(deps: Deps, address: Addr) -> StdResult<GetSocialsByAddressR
let user_infos: Vec<_> = USER_INFOS
.idx
.address
.prefix(address)
.prefix(address.to_string())
.range(deps.storage, None, None, Order::Ascending)
.flatten()
.collect();
Expand Down Expand Up @@ -249,7 +255,7 @@ mod tests {
// query again
let resp = query(
deps.as_ref(),
env,
env.clone(),
QueryMsg::GetAddressesBySocial {
platform: "twitter".to_string(),
profile_id: "123".to_string(),
Expand All @@ -263,6 +269,33 @@ mod tests {
address: vec![Addr::unchecked("abc")]
}
);
// execute
let resp = execute(
deps.as_mut(),
env.clone(),
mock_info("sender", &[]),
ExecuteMsg::SubmitSocial {
social_info: ("facebook".to_string(), "456".to_string()),
address: Addr::unchecked("abc"),
},
);
assert!(resp.is_ok());
let resp = query(
deps.as_ref(),
env.clone(),
QueryMsg::GetAddressesBySocial {
platform: "facebook".to_string(),
profile_id: "456".to_string(),
},
)
.unwrap();
let resp: GetAddressesBySocialResponse = from_binary(&resp).unwrap();
assert_eq!(
resp,
GetAddressesBySocialResponse {
address: vec![Addr::unchecked("abc")]
}
);
}
#[test]
fn query_by_address() {
Expand Down Expand Up @@ -321,6 +354,36 @@ mod tests {
social_infos: vec![("twitter".to_string(), "123".to_string())]
}
);
// execute
let resp = execute(
deps.as_mut(),
env.clone(),
mock_info("sender", &[]),
ExecuteMsg::SubmitSocial {
social_info: ("facebook".to_string(), "456".to_string()),
address: Addr::unchecked("user"),
},
);
assert!(resp.is_ok());
// query again
let resp = query(
deps.as_ref(),
env.clone(),
QueryMsg::GetSocialsByAddress {
address: Addr::unchecked("user"),
},
)
.unwrap();
let resp: GetSocialsByAddressResponse = from_binary(&resp).unwrap();
assert_eq!(
resp,
GetSocialsByAddressResponse {
social_infos: vec![
("facebook".to_string(), "456".to_string()),
("twitter".to_string(), "123".to_string()),
]
}
);
}
#[test]
fn donations() {
Expand Down
4 changes: 2 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub type SocialInfo = (Platform, ProfileId);
pub type Platform = String;
pub type ProfileId = String;
pub struct InfoIndexes<'a> {
pub address: MultiIndex<'a, Addr, UserInfo, String>,
pub address: MultiIndex<'a, String, UserInfo, String>,
pub social_info: MultiIndex<'a, (String, String), UserInfo, String>,
}
impl<'a> IndexList<UserInfo> for InfoIndexes<'a> {
Expand All @@ -17,7 +17,7 @@ impl<'a> IndexList<UserInfo> for InfoIndexes<'a> {
pub const fn infos<'a>() -> IndexedMap<'a, &'a str, UserInfo, InfoIndexes<'a>> {
let indexes = InfoIndexes {
address: MultiIndex::new(
|_pk: &[u8], d: &UserInfo| d.address.clone(),
|_pk: &[u8], d: &UserInfo| d.address.to_string(),
"infos",
"infos__address",
),
Expand Down

0 comments on commit a81da72

Please sign in to comment.