From dbf8c407a185c3a69cff179bd3fdb3192140de64 Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Sun, 1 Sep 2024 20:28:12 +0100 Subject: [PATCH 1/3] test(StudentRegistryV2): add registration test --- contracts/StudentRegistryV2.sol | 9 +-- test/StudentRegistryV2.js | 99 ++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/contracts/StudentRegistryV2.sol b/contracts/StudentRegistryV2.sol index dfc8eb34..62ebc811 100644 --- a/contracts/StudentRegistryV2.sol +++ b/contracts/StudentRegistryV2.sol @@ -46,9 +46,9 @@ contract StudentRegistryV2 is Ownable { tempstudentsMapping[_studentAddr].studentAddr == address(0), "You're already registered" ); - require(hasPaidMapping[msg.sender], "You must pay first"); - require(bytes(_name).length > 0, "No name has been inputed"); - require(_age >= 18, "name should be 18 or more"); + require(hasPaidMapping[_studentAddr], "You must pay first"); + require(bytes(_name).length > 0, "No name has been inputted"); + require(_age >= 18, "Age should be 18 or more"); uint256 _studentId = students.length + 1; // A variable is defined to match the array format and datatype @@ -67,6 +67,7 @@ contract StudentRegistryV2 is Ownable { emit registerStudent(_studentAddr, _name, _age); } + // Function for authorizing registered Student function authorizeStudentRegistration( address _studentAddr @@ -77,7 +78,7 @@ contract StudentRegistryV2 is Ownable { ); require( studentsMapping[_studentAddr].studentAddr == address(0), - "You're already registered" + "You're already authorized" ); addStudent(_studentAddr); emit authorizeStudentReg(_studentAddr); diff --git a/test/StudentRegistryV2.js b/test/StudentRegistryV2.js index 87f932c0..3e114069 100644 --- a/test/StudentRegistryV2.js +++ b/test/StudentRegistryV2.js @@ -35,6 +35,8 @@ describe("StudentRegistryV2 Test Suite", () => { const { deployedStudentRegistryV2, owner, ZERO_ADDRESS, addr1 } = await loadFixture(deployUtil); // check the owner of the StudentRegistry contract which auto inherits from Ownable let studentRegistryV2Owner = await deployedStudentRegistryV2.getOwner(); + console.log(studentRegistryV2Owner); + expect(studentRegistryV2Owner).to.eq(owner); // check that students array of Student struct is empty @@ -67,7 +69,6 @@ describe("StudentRegistryV2 Test Suite", () => { await expect( deployedStudentRegistryV2.connect(owner).payFee({ value: convertEther("1") }) ).to.be.revertedWith("Owner is excluded"); - }); it("should revert attempt to proceed without paying 1ETH as fee", async () => { @@ -121,5 +122,101 @@ describe("StudentRegistryV2 Test Suite", () => { }); }); }); + describe.only("Registration", function () { + describe("Validation", function () { + it("should revert if the address is already registered", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + // Make a payment first from addr1 + await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + + // Register addr1 + await deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20); + + // Attempt to register the same address again and expect it to revert + await expect( + deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20) + ).to.be.revertedWith("You're already registered"); + }); + + it("should revert if the student has not paid the fee", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + await expect( + deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20) + ).to.be.revertedWith("You must pay first"); + }); + + it("should revert if the name is empty", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + + await expect(deployedStudentRegistryV2.connect(owner).register(addr1.address, "", 20)).to.be.revertedWith( + "No name has been inputted" + ); + }); + + it("should revert if the age is below 18", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + + await expect( + deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 17) + ).to.be.revertedWith("Age should be 18 or more"); + }); + }); + describe("Registration Transcation", function () { + it("should allow the owner to successfully register a student", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + // Ensure the contract owner is correct + let studentRegistryV2Owner = await deployedStudentRegistryV2.getOwner(); + expect(studentRegistryV2Owner).to.eq(owner.address); + + // Make a payment first from addr1 + const payFeeTx = await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + await payFeeTx.wait(); // Ensure the transaction is mined before proceeding + + // Verify the payment status + const hasPaid = await deployedStudentRegistryV2.hasPaidMapping(addr1.address); + expect(hasPaid).to.be.true; + + // Perform registration as the owner + const registerTx = await deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20); + await registerTx.wait(); + + // Verify the student was added + const student = await deployedStudentRegistryV2.tempstudentsMapping(addr1.address); + expect(student.studentAddr).to.equal(addr1.address); + expect(student.name).to.equal("Alice"); + expect(student.age).to.equal(20); + expect(student.hasPaid).to.be.true; + expect(student.isAuthorized).to.be.false; + + // Check that the event was emitted + await expect(registerTx) + .to.emit(deployedStudentRegistryV2, "registerStudent") + .withArgs(addr1.address, "Alice", 20); + }); + }); + describe("Event", () => { + it("should emit registerStudent when registeration is successfull", async () => { + const { deployedStudentRegistryV2, addr1, owner } = await loadFixture(deployUtil); + + // Make a payment first from addr1 + const payFeeTx = await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + await payFeeTx.wait(); // Ensure the transaction is mined before proceeding + + const registerTx = await deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20); + await registerTx.wait(); + + await expect(registerTx) + .to.emit(deployedStudentRegistryV2, "registerStudent") + .withArgs(addr1.address, "Alice", 20); + }); + }); + }); }); }); From 057d1a96ac6b11479ade67c05347b943cfaac48b Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Sun, 1 Sep 2024 20:55:35 +0100 Subject: [PATCH 2/3] test(StudentRegistryV2): add authorization test --- test/StudentRegistryV2.js | 84 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/test/StudentRegistryV2.js b/test/StudentRegistryV2.js index 3e114069..d2618baa 100644 --- a/test/StudentRegistryV2.js +++ b/test/StudentRegistryV2.js @@ -122,7 +122,7 @@ describe("StudentRegistryV2 Test Suite", () => { }); }); }); - describe.only("Registration", function () { + describe("Registration", function () { describe("Validation", function () { it("should revert if the address is already registered", async () => { const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); @@ -218,5 +218,87 @@ describe("StudentRegistryV2 Test Suite", () => { }); }); }); + describe("Authorization", function () { + describe("Validation", function () { + it("should revert if the address is already authorized", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + // Make a payment first from addr1 + await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + + // Register addr1 as a temporary student + await deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20); + + // Authorize the registered student + await deployedStudentRegistryV2.connect(owner).authorizeStudentRegistration(addr1.address); + + // Attempt to authorize the same address again and expect it to revert + await expect( + deployedStudentRegistryV2.connect(owner).authorizeStudentRegistration(addr1.address) + ).to.be.revertedWith("You're already authorized"); + }); + it("should allow the owner to authorize a registered student", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + // Make a payment first from addr1 + await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + + // Register addr1 as a temporary student + await deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20); + + // Authorize the registered student + const authorizeTx = await deployedStudentRegistryV2 + .connect(owner) + .authorizeStudentRegistration(addr1.address); + await authorizeTx.wait(); + + it("should allow the owner to authorize a registered student", async () => { + const { deployedStudentRegistryV2, owner, addr1, addr2 } = await loadFixture(deployUtil); + + // Make a payment first from addr1 + await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + + // Register addr1 as a temporary student + await deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20); + + // Authorize the registered student + const authorizeTx = await deployedStudentRegistryV2 + .connect(owner) + .authorizeStudentRegistration(addr1.address); + await authorizeTx.wait(); + + await expect( + deployedStudentRegistryV2.connect(owner).authorizeStudentRegistration(addr2.address) + ).to.be.revertedWith("Invalid Address"); + }); + }); + }); + describe("Only owner Authorization", function () { + it("should authorize registered student", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + await deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20); + + expect(deployedStudentRegistryV2.connect(owner).authorizeStudentRegistration(addr1.address)); + }); + }); + describe("Event", function () { + it.only("should emit authorizeStudentReg", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + // Make a payment first from addr1 + await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + + // Register addr1 as a temporary student + await deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20); + + // Perform the authorization and expect the event + await expect(deployedStudentRegistryV2.connect(owner).authorizeStudentRegistration(addr1.address)) + .to.emit(deployedStudentRegistryV2, "authorizeStudentReg") + .withArgs(addr1.address); + }); + }); + }); }); }); From bb536bed33fd7a495c0d1fc807cbc26d355fb45b Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Sun, 1 Sep 2024 21:09:33 +0100 Subject: [PATCH 3/3] test(StudentRegistryV2): add student test --- contracts/StudentRegistryV2.sol | 8 +++---- test/StudentRegistryV2.js | 42 +++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/contracts/StudentRegistryV2.sol b/contracts/StudentRegistryV2.sol index 62ebc811..f10f5e94 100644 --- a/contracts/StudentRegistryV2.sol +++ b/contracts/StudentRegistryV2.sol @@ -22,8 +22,8 @@ contract StudentRegistryV2 is Ownable { string _StName, uint8 _stAge ); - event authorizeStudentReg(address _studentAddress); - event addStud(address _studentAddr); + event AuthorizeStudentReg(address _studentAddress); + event AddStudent(address _studentAddr); event PaidFee(address indexed payer, uint256 amount); // Function For Paying @@ -81,7 +81,7 @@ contract StudentRegistryV2 is Ownable { "You're already authorized" ); addStudent(_studentAddr); - emit authorizeStudentReg(_studentAddr); + emit AuthorizeStudentReg(_studentAddr); } // Function for Adding student, this function is called in the authorizeStudentRegistration() function @@ -100,7 +100,7 @@ contract StudentRegistryV2 is Ownable { // add student to Studentmapping studentsMapping[_studentAddr] = student; - emit addStud(_studentAddr); + emit AddStudent(_studentAddr); } // Function to get student by call the ID diff --git a/test/StudentRegistryV2.js b/test/StudentRegistryV2.js index d2618baa..b5d91e9a 100644 --- a/test/StudentRegistryV2.js +++ b/test/StudentRegistryV2.js @@ -284,7 +284,7 @@ describe("StudentRegistryV2 Test Suite", () => { }); }); describe("Event", function () { - it.only("should emit authorizeStudentReg", async () => { + it("should emit authorizeStudentReg", async () => { const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); // Make a payment first from addr1 @@ -295,10 +295,48 @@ describe("StudentRegistryV2 Test Suite", () => { // Perform the authorization and expect the event await expect(deployedStudentRegistryV2.connect(owner).authorizeStudentRegistration(addr1.address)) - .to.emit(deployedStudentRegistryV2, "authorizeStudentReg") + .to.emit(deployedStudentRegistryV2, "AuthorizeStudentReg") .withArgs(addr1.address); }); }); }); + describe("Add Student Functionality", function () { + describe("Add student to the struct", function () { + it("should add a student", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + + // Register addr1 as a temporary student + await deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20); + + // Authorize addr1 and add them as a student + await deployedStudentRegistryV2.connect(owner).authorizeStudentRegistration(addr1.address); + + // Verify the student was added + const student = await deployedStudentRegistryV2.studentsMapping(addr1.address); + expect(student.studentAddr).to.equal(addr1.address); + expect(student.name).to.equal("Alice"); + expect(student.age).to.equal(20); + expect(student.hasPaid).to.be.true; + expect(student.isAuthorized).to.be.false; + }); + }); + describe("Event", function () { + it("should emit", async () => { + const { deployedStudentRegistryV2, owner, addr1 } = await loadFixture(deployUtil); + + await deployedStudentRegistryV2.connect(addr1).payFee({ value: ethers.parseEther("1") }); + + // Register addr1 as a temporary student + await deployedStudentRegistryV2.connect(owner).register(addr1.address, "Alice", 20); + + await expect(deployedStudentRegistryV2.connect(owner).authorizeStudentRegistration(addr1.address)) + .to.emit(deployedStudentRegistryV2, "AddStudent") + .withArgs(addr1.address); + }); + }); + // Check that the event was emitted + }); }); });