diff --git a/custom-pallets/departments/src/lib.rs b/custom-pallets/departments/src/lib.rs index 07252e4..9f3882a 100644 --- a/custom-pallets/departments/src/lib.rs +++ b/custom-pallets/departments/src/lib.rs @@ -225,7 +225,8 @@ pub mod pallet { }, }, None => { - >::insert(department_id, vec![new_member]); + >::insert(department_id, vec![new_member.clone()]); + Self::deposit_event(Event::MemberAdded { new_member, department_id }); }, } diff --git a/custom-pallets/departments/src/tests.rs b/custom-pallets/departments/src/tests.rs index 12daec7..800043b 100644 --- a/custom-pallets/departments/src/tests.rs +++ b/custom-pallets/departments/src/tests.rs @@ -1,4 +1,4 @@ -use crate::{mock::*, Error, Event, Something}; +use crate::{mock::*, Error, Event}; use frame_support::{assert_noop, assert_ok}; use pallet_support::Content; @@ -31,3 +31,135 @@ fn create_department_works() { ); }); } + +#[test] +fn add_member_to_department_works() { + new_test_ext().execute_with(|| { + // Go past genesis block so events get deposited + System::set_block_number(1); + let admin_account_id = 1; + let new_member_account_id = 2; + let new_member_2_account_id = 3; + let content: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); + + // Create a department first + assert_ok!(Departments::create_department( + RuntimeOrigin::signed(admin_account_id), + content.clone() + )); + + // Get the department ID + let department_id = Departments::next_department_id() - 1; + + // Verify that the correct event was emitted for department creation + System::assert_last_event( + Event::DepartmentCreated { account: admin_account_id, department_id }.into(), + ); + + // Add a new member to the department + assert_ok!(Departments::add_member_to_department( + RuntimeOrigin::signed(admin_account_id), + department_id, + new_member_account_id + )); + + // Check that the member was added correctly + let stored_accounts = Departments::department_accounts(department_id).unwrap(); + assert!(stored_accounts.contains(&new_member_account_id)); + + // Verify that the correct event was emitted for adding a member + System::assert_last_event( + Event::MemberAdded { new_member: new_member_account_id, department_id }.into(), + ); + + // Add a new member to the department + assert_ok!(Departments::add_member_to_department( + RuntimeOrigin::signed(admin_account_id), + department_id, + new_member_2_account_id + )); + + // Check that the member was added correctly + let stored_accounts = Departments::department_accounts(department_id).unwrap(); + assert!(stored_accounts.contains(&new_member_2_account_id)); + + // Verify that the correct event was emitted for adding a member + System::assert_last_event( + Event::MemberAdded { new_member: new_member_2_account_id, department_id }.into(), + ); + }); +} + +#[test] +fn add_member_to_department_fails_if_not_admin() { + new_test_ext().execute_with(|| { + System::set_block_number(1); + let non_admin_account_id = 2; + let new_member_account_id = 3; + let content: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); + + // Create a department with account_id 1 as the admin + assert_ok!(Departments::create_department(RuntimeOrigin::signed(1), content.clone())); + + // Get the department ID + let department_id = Departments::next_department_id() - 1; + + // Try to add a member with a non-admin account, should fail + assert_noop!( + Departments::add_member_to_department( + RuntimeOrigin::signed(non_admin_account_id), + department_id, + new_member_account_id + ), + Error::::NotAdmin + ); + }); +} + +#[test] +fn add_member_to_department_fails_if_already_member() { + new_test_ext().execute_with(|| { + System::set_block_number(1); + let admin_account_id = 1; + let new_member_account_id = 2; + let content: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); + + // Create a department first + assert_ok!(Departments::create_department( + RuntimeOrigin::signed(admin_account_id), + content.clone() + )); + + // Get the department ID + let department_id = Departments::next_department_id() - 1; + + // Add the member for the first time + assert_ok!(Departments::add_member_to_department( + RuntimeOrigin::signed(admin_account_id), + department_id, + new_member_account_id + )); + + // Try to add the same member again, should fail + assert_noop!( + Departments::add_member_to_department( + RuntimeOrigin::signed(admin_account_id), + department_id, + new_member_account_id + ), + Error::::AlreadyMember + ); + }); +}