Skip to content

Commit 9b61526

Browse files
authored
tests: increase code coverage at consecutive (#293)
Adds missing tests to erc721 consecutive mostly associated with erc721 standard's operations.
1 parent d186818 commit 9b61526

File tree

2 files changed

+340
-11
lines changed

2 files changed

+340
-11
lines changed

contracts/src/token/erc721/extensions/consecutive.rs

Lines changed: 336 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,13 +801,16 @@ mod tests {
801801
ERC721ExceededMaxBatchMint, Erc721Consecutive, Error,
802802
},
803803
tests::random_token_id,
804-
ERC721InvalidReceiver, ERC721NonexistentToken, IErc721,
804+
ERC721IncorrectOwner, ERC721InvalidApprover,
805+
ERC721InvalidReceiver, ERC721InvalidSender,
806+
ERC721NonexistentToken, IErc721,
805807
},
806808
},
807809
utils::structs::checkpoints::U96,
808810
};
809811

810812
const BOB: Address = address!("F4EaCDAbEf3c8f1EdE91b6f2A6840bc2E4DD3526");
813+
const DAVE: Address = address!("0BB78F7e7132d1651B4Fd884B7624394e92156F1");
811814

812815
fn init(
813816
contract: &mut Erc721Consecutive,
@@ -858,6 +861,43 @@ mod tests {
858861
assert_eq!(balance2, balance1 + uint!(1_U256));
859862
}
860863

864+
#[motsu::test]
865+
fn error_when_minting_token_id_twice(contract: Erc721Consecutive) {
866+
let alice = msg::sender();
867+
let token_id = random_token_id();
868+
contract
869+
._mint(alice, token_id)
870+
.expect("should mint the token a first time");
871+
let err = contract
872+
._mint(alice, token_id)
873+
.expect_err("should not mint a token with `token_id` twice");
874+
875+
assert!(matches!(
876+
err,
877+
Error::Erc721(erc721::Error::InvalidSender(ERC721InvalidSender {
878+
sender: Address::ZERO
879+
}))
880+
));
881+
}
882+
883+
#[motsu::test]
884+
fn error_when_minting_token_invalid_receiver(contract: Erc721Consecutive) {
885+
let invalid_receiver = Address::ZERO;
886+
887+
let token_id = random_token_id();
888+
889+
let err = contract
890+
._mint(invalid_receiver, token_id)
891+
.expect_err("should not mint a token for invalid receiver");
892+
893+
assert!(matches!(
894+
err,
895+
Error::Erc721(erc721::Error::InvalidReceiver(ERC721InvalidReceiver {
896+
receiver
897+
})) if receiver == invalid_receiver
898+
));
899+
}
900+
861901
#[motsu::test]
862902
fn error_when_to_is_zero(contract: Erc721Consecutive) {
863903
let err = contract
@@ -994,5 +1034,300 @@ mod tests {
9941034
Error::Erc721(erc721::Error::NonexistentToken(ERC721NonexistentToken { token_id }))
9951035
if token_id == U256::from(non_consecutive_token_id)
9961036
));
1037+
1038+
// After being burnt the token should not be burnt again.
1039+
let non_existent_token = non_consecutive_token_id;
1040+
let err = contract
1041+
._burn(non_existent_token)
1042+
.expect_err("should return Error::NonexistentToken");
1043+
1044+
assert!(matches!(
1045+
err,
1046+
Error::Erc721(erc721::Error::NonexistentToken (ERC721NonexistentToken{
1047+
token_id: t_id
1048+
})) if t_id == non_existent_token
1049+
));
1050+
}
1051+
1052+
#[motsu::test]
1053+
fn safe_transfer_from(contract: Erc721Consecutive) {
1054+
let alice = msg::sender();
1055+
let token_id = random_token_id();
1056+
contract._mint(alice, token_id).expect("should mint a token to Alice");
1057+
1058+
contract
1059+
.safe_transfer_from(alice, BOB, token_id)
1060+
.expect("should transfer a token from Alice to Bob");
1061+
1062+
let owner = contract
1063+
.owner_of(token_id)
1064+
.expect("should return the owner of the token");
1065+
1066+
assert_eq!(owner, BOB);
1067+
}
1068+
1069+
#[motsu::test]
1070+
fn safe_transfers_from_approved_token(contract: Erc721Consecutive) {
1071+
let alice = msg::sender();
1072+
let token_id = random_token_id();
1073+
contract._mint(BOB, token_id).expect("should mint token to Bob");
1074+
contract.erc721._token_approvals.setter(token_id).set(alice);
1075+
contract
1076+
.safe_transfer_from(BOB, alice, token_id)
1077+
.expect("should transfer Bob's token to Alice");
1078+
let owner = contract
1079+
.owner_of(token_id)
1080+
.expect("should return the owner of the token");
1081+
assert_eq!(owner, alice);
1082+
}
1083+
1084+
#[motsu::test]
1085+
fn error_when_safe_transfer_from_incorrect_owner(
1086+
contract: Erc721Consecutive,
1087+
) {
1088+
let alice = msg::sender();
1089+
let token_id = random_token_id();
1090+
1091+
contract._mint(alice, token_id).expect("should mint a token to Alice");
1092+
1093+
let err = contract
1094+
.safe_transfer_from(DAVE, BOB, token_id)
1095+
.expect_err("should not transfer from incorrect owner");
1096+
1097+
assert!(matches!(
1098+
err,
1099+
Error::Erc721(erc721::Error::IncorrectOwner(ERC721IncorrectOwner {
1100+
sender,
1101+
token_id: t_id,
1102+
owner
1103+
})) if sender == DAVE && t_id == token_id && owner == alice
1104+
));
1105+
}
1106+
1107+
#[motsu::test]
1108+
fn error_when_internal_safe_transfer_nonexistent_token(
1109+
contract: Erc721Consecutive,
1110+
) {
1111+
let alice = msg::sender();
1112+
let token_id = random_token_id();
1113+
let err = contract
1114+
._safe_transfer(alice, BOB, token_id, vec![0, 1, 2, 3].into())
1115+
.expect_err("should not transfer a non-existent token");
1116+
1117+
assert!(matches!(
1118+
err,
1119+
Error::Erc721(erc721::Error::NonexistentToken(ERC721NonexistentToken {
1120+
token_id: t_id,
1121+
})) if t_id == token_id
1122+
));
1123+
}
1124+
1125+
#[motsu::test]
1126+
fn error_when_safe_transfer_to_invalid_receiver(
1127+
contract: Erc721Consecutive,
1128+
) {
1129+
let alice = msg::sender();
1130+
let token_id = random_token_id();
1131+
let invalid_receiver = Address::ZERO;
1132+
1133+
contract._mint(alice, token_id).expect("should mint a token to Alice");
1134+
1135+
let err = contract
1136+
.safe_transfer_from(alice, invalid_receiver, token_id)
1137+
.expect_err("should not transfer the token to invalid receiver");
1138+
1139+
assert!(matches!(
1140+
err,
1141+
Error::Erc721(erc721::Error::InvalidReceiver(ERC721InvalidReceiver {
1142+
receiver
1143+
})) if receiver == invalid_receiver
1144+
));
1145+
1146+
let owner = contract
1147+
.owner_of(token_id)
1148+
.expect("should return the owner of the token");
1149+
assert_eq!(alice, owner);
1150+
}
1151+
1152+
#[motsu::test]
1153+
fn safe_transfers_from_with_data(contract: Erc721Consecutive) {
1154+
let alice = msg::sender();
1155+
let token_id = random_token_id();
1156+
contract._mint(alice, token_id).expect("should mint a token to Alice");
1157+
1158+
contract
1159+
.safe_transfer_from_with_data(
1160+
alice,
1161+
BOB,
1162+
token_id,
1163+
vec![0, 1, 2, 3].into(),
1164+
)
1165+
.expect("should transfer a token from Alice to Bob");
1166+
1167+
let owner = contract
1168+
.owner_of(token_id)
1169+
.expect("should return the owner of the token");
1170+
1171+
assert_eq!(owner, BOB);
1172+
}
1173+
1174+
#[motsu::test]
1175+
fn error_when_internal_safe_transfer_to_invalid_receiver(
1176+
contract: Erc721Consecutive,
1177+
) {
1178+
let alice = msg::sender();
1179+
let token_id = random_token_id();
1180+
let invalid_receiver = Address::ZERO;
1181+
1182+
contract._mint(alice, token_id).expect("should mint a token to Alice");
1183+
1184+
let err = contract
1185+
._safe_transfer(
1186+
alice,
1187+
invalid_receiver,
1188+
token_id,
1189+
vec![0, 1, 2, 3].into(),
1190+
)
1191+
.expect_err("should not transfer the token to invalid receiver");
1192+
1193+
assert!(matches!(
1194+
err,
1195+
Error::Erc721(erc721::Error::InvalidReceiver(ERC721InvalidReceiver {
1196+
receiver
1197+
})) if receiver == invalid_receiver
1198+
));
1199+
1200+
let owner = contract
1201+
.owner_of(token_id)
1202+
.expect("should return the owner of the token");
1203+
assert_eq!(alice, owner);
1204+
}
1205+
1206+
#[motsu::test]
1207+
fn error_when_internal_safe_transfer_from_incorrect_owner(
1208+
contract: Erc721Consecutive,
1209+
) {
1210+
let alice = msg::sender();
1211+
let token_id = random_token_id();
1212+
1213+
contract._mint(alice, token_id).expect("should mint a token to Alice");
1214+
1215+
let err = contract
1216+
._safe_transfer(DAVE, BOB, token_id, vec![0, 1, 2, 3].into())
1217+
.expect_err("should not transfer the token from incorrect owner");
1218+
assert!(matches!(
1219+
err,
1220+
Error::Erc721(erc721::Error::IncorrectOwner(ERC721IncorrectOwner {
1221+
sender,
1222+
token_id: t_id,
1223+
owner
1224+
})) if sender == DAVE && t_id == token_id && owner == alice
1225+
));
1226+
}
1227+
1228+
#[motsu::test]
1229+
fn safe_mints(contract: Erc721Consecutive) {
1230+
let alice = msg::sender();
1231+
let token_id = random_token_id();
1232+
1233+
let initial_balance = contract
1234+
.balance_of(alice)
1235+
.expect("should return the balance of Alice");
1236+
1237+
contract
1238+
._safe_mint(alice, token_id, vec![0, 1, 2, 3].into())
1239+
.expect("should mint a token for Alice");
1240+
1241+
let owner = contract
1242+
.owner_of(token_id)
1243+
.expect("should return the owner of the token");
1244+
assert_eq!(owner, alice);
1245+
1246+
let balance = contract
1247+
.balance_of(alice)
1248+
.expect("should return the balance of Alice");
1249+
1250+
assert_eq!(initial_balance + uint!(1_U256), balance);
1251+
}
1252+
1253+
#[motsu::test]
1254+
fn approves(contract: Erc721Consecutive) {
1255+
let alice = msg::sender();
1256+
let token_id = random_token_id();
1257+
contract._mint(alice, token_id).expect("should mint a token");
1258+
contract
1259+
.approve(BOB, token_id)
1260+
.expect("should approve Bob for operations on token");
1261+
assert_eq!(contract.erc721._token_approvals.get(token_id), BOB);
1262+
}
1263+
1264+
#[motsu::test]
1265+
fn error_when_approve_for_nonexistent_token(contract: Erc721Consecutive) {
1266+
let token_id = random_token_id();
1267+
let err = contract
1268+
.approve(BOB, token_id)
1269+
.expect_err("should not approve for a non-existent token");
1270+
1271+
assert!(matches!(
1272+
err,
1273+
Error::Erc721(erc721::Error::NonexistentToken(ERC721NonexistentToken {
1274+
token_id: t_id
1275+
})) if token_id == t_id
1276+
));
1277+
}
1278+
1279+
#[motsu::test]
1280+
fn error_when_approve_by_invalid_approver(contract: Erc721Consecutive) {
1281+
let token_id = random_token_id();
1282+
contract._mint(BOB, token_id).expect("should mint a token");
1283+
1284+
let err = contract
1285+
.approve(DAVE, token_id)
1286+
.expect_err("should not approve when invalid approver");
1287+
1288+
assert!(matches!(
1289+
err,
1290+
Error::Erc721(erc721::Error::InvalidApprover(ERC721InvalidApprover {
1291+
approver
1292+
})) if approver == msg::sender()
1293+
));
1294+
}
1295+
1296+
#[motsu::test]
1297+
fn approval_for_all(contract: Erc721Consecutive) {
1298+
let alice = msg::sender();
1299+
contract
1300+
.erc721
1301+
._operator_approvals
1302+
.setter(alice)
1303+
.setter(BOB)
1304+
.set(false);
1305+
1306+
contract
1307+
.set_approval_for_all(BOB, true)
1308+
.expect("should approve Bob for operations on all Alice's tokens");
1309+
assert_eq!(contract.is_approved_for_all(alice, BOB), true);
1310+
1311+
contract.set_approval_for_all(BOB, false).expect(
1312+
"should disapprove Bob for operations on all Alice's tokens",
1313+
);
1314+
assert_eq!(contract.is_approved_for_all(alice, BOB), false);
1315+
}
1316+
1317+
#[motsu::test]
1318+
fn error_when_get_approved_of_nonexistent_token(
1319+
contract: Erc721Consecutive,
1320+
) {
1321+
let token_id = random_token_id();
1322+
let err = contract
1323+
.get_approved(token_id)
1324+
.expect_err("should not return approved for a non-existent token");
1325+
1326+
assert!(matches!(
1327+
err,
1328+
Error::Erc721(erc721::Error::NonexistentToken(ERC721NonexistentToken {
1329+
token_id: t_id
1330+
})) if token_id == t_id
1331+
));
9971332
}
9981333
}

0 commit comments

Comments
 (0)