1
// Copyright 2024 Moonbeam foundation
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonbeam is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! Unit testing
18
use {
19
	crate::{
20
		mock::{AccountId, ExtBuilder, LazyMigrations, RuntimeOrigin, Test},
21
		Error,
22
	},
23
	frame_support::{assert_noop, assert_ok},
24
	sp_core::{H160, H256},
25
	sp_io::hashing::keccak_256,
26
};
27

            
28
2
fn address_build(seed: u8) -> H160 {
29
2
	let address = H160::from(H256::from(keccak_256(&[seed; 32])));
30
2
	address
31
2
}
32

            
33
1
fn create_dummy_contract_without_metadata(seed: u8) -> H160 {
34
1
	let address = address_build(seed);
35
1
	let dummy_code = vec![1, 2, 3];
36
1
	pallet_evm::AccountCodes::<Test>::insert(address, dummy_code);
37
1
	address
38
1
}
39

            
40
#[test]
41
1
fn test_create_contract_metadata_contract_not_exist() {
42
1
	ExtBuilder::default().build().execute_with(|| {
43
1
		assert_noop!(
44
1
			LazyMigrations::create_contract_metadata(
45
1
				RuntimeOrigin::signed(AccountId::from([45; 20])),
46
1
				address_build(1),
47
1
			),
48
1
			Error::<Test>::ContractNotExist
49
1
		);
50
1
	});
51
1
}
52

            
53
#[test]
54
1
fn test_create_contract_metadata_success_path() {
55
1
	ExtBuilder::default().build().execute_with(|| {
56
1
		// Setup: create a dummy contract
57
1
		let address = create_dummy_contract_without_metadata(1);
58
1

            
59
1
		assert_ok!(LazyMigrations::create_contract_metadata(
60
1
			RuntimeOrigin::signed(AccountId::from([45; 20])),
61
1
			address,
62
1
		));
63

            
64
1
		assert!(pallet_evm::AccountCodesMetadata::<Test>::get(address).is_some());
65

            
66
		// Should not be able to set metadata again
67
1
		assert_noop!(
68
1
			LazyMigrations::create_contract_metadata(
69
1
				RuntimeOrigin::signed(AccountId::from([45; 20])),
70
1
				address,
71
1
			),
72
1
			Error::<Test>::ContractMetadataAlreadySet
73
1
		);
74
1
	});
75
1
}