Skip to content

Commit

Permalink
Prevents people to kick other authorMapping associations (#1113)
Browse files Browse the repository at this point in the history
* Prevents people to kick other authorMapping associations

* Adds test

* Fixes rust trest

* rust fmt
  • Loading branch information
crystalin authored Dec 22, 2021
1 parent 1744280 commit a427c2b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
4 changes: 4 additions & 0 deletions pallets/author-mapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ pub mod pallet {
account_id == stored_info.account,
Error::<T>::NotYourAssociation
);
ensure!(
MappingWithDeposit::<T>::get(&new_author_id).is_none(),
Error::<T>::AlreadyAssociated
);

MappingWithDeposit::<T>::remove(&old_author_id);
MappingWithDeposit::<T>::insert(&new_author_id, &stored_info);
Expand Down
15 changes: 7 additions & 8 deletions pallets/author-mapping/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,13 @@ fn rotating_to_the_same_author_id_leaves_registration_in_tact() {
.with_mappings(vec![(TestAuthor::Alice.into(), 1)])
.build()
.execute_with(|| {
assert_ok!(AuthorMapping::update_association(
Origin::signed(1),
TestAuthor::Alice.into(),
TestAuthor::Alice.into()
));
assert_eq!(
AuthorMapping::account_id_of(&TestAuthor::Alice.into()),
Some(1)
assert_noop!(
AuthorMapping::update_association(
Origin::signed(1),
TestAuthor::Alice.into(),
TestAuthor::Alice.into()
),
Error::<Runtime>::AlreadyAssociated
);
})
}
26 changes: 26 additions & 0 deletions tests/tests/test-author-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describeDevMoonbeam("Author Mapping - Fail to reassociate alice", (context) => {
baltathar,
context.polkadotApi.tx.authorMapping.addAssociation(aliceAuthorId)
);

// should check events for failure
expect(events.length === 6);
expect(context.polkadotApi.events.system.NewAccount.is(events[2])).to.be.true;
Expand All @@ -98,6 +99,31 @@ describeDevMoonbeam("Author Mapping - Fail to reassociate alice", (context) => {
).to.eq(0n);
expect((await getMappingInfo(context, aliceAuthorId)).account).to.eq(ALITH);
});

it("should fail to take someone else association", async function () {
const keyring = new Keyring({ type: "ethereum" });
const baltathar = await keyring.addFromUri(BALTATHAR_PRIV_KEY, null, "ethereum");

await createBlockWithExtrinsic(
context,
baltathar,
context.polkadotApi.tx.authorMapping.addAssociation(charlieAuthorId)
);
const { events } = await createBlockWithExtrinsic(
context,
baltathar,
context.polkadotApi.tx.authorMapping.updateAssociation(charlieAuthorId, aliceAuthorId)
);

// should check events for failure
expect(
events.find((e) => e.section == "system" && e.method == "ExtrinsicFailed"),
"ExtrinsicFailed is missing"
).to.not.be.undefined;

//check state
expect((await getMappingInfo(context, aliceAuthorId)).account).to.eq(ALITH);
});
});

describeDevMoonbeam("Author Mapping - Fail without deposit", (context) => {
Expand Down

0 comments on commit a427c2b

Please sign in to comment.