Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(StudentRegistryV2): add registration test #48

Open
wants to merge 3 commits into
base: test-payFee
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions contracts/StudentRegistryV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -67,6 +67,7 @@ contract StudentRegistryV2 is Ownable {
emit registerStudent(_studentAddr, _name, _age);
}


// Function for authorizing registered Student
function authorizeStudentRegistration(
address _studentAddr
Expand All @@ -77,10 +78,10 @@ contract StudentRegistryV2 is Ownable {
);
require(
studentsMapping[_studentAddr].studentAddr == address(0),
"You're already registered"
"You're already authorized"
);
addStudent(_studentAddr);
emit authorizeStudentReg(_studentAddr);
emit AuthorizeStudentReg(_studentAddr);
}

// Function for Adding student, this function is called in the authorizeStudentRegistration() function
Expand All @@ -99,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
Expand Down
219 changes: 218 additions & 1 deletion test/StudentRegistryV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -121,5 +122,221 @@ describe("StudentRegistryV2 Test Suite", () => {
});
});
});
describe("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);
});
});
});
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("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);
});
});
});
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
});
});
});