Описание изменений
This commit is contained in:
45
backend/test/AccessToken.test.js
Normal file
45
backend/test/AccessToken.test.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const { expect } = require("chai");
|
||||
const { ethers } = require("hardhat");
|
||||
|
||||
describe("AccessToken", function () {
|
||||
let AccessToken;
|
||||
let accessToken;
|
||||
let owner;
|
||||
let addr1;
|
||||
let addr2;
|
||||
|
||||
beforeEach(async function () {
|
||||
[owner, addr1, addr2] = await ethers.getSigners();
|
||||
AccessToken = await ethers.getContractFactory("AccessToken");
|
||||
accessToken = await AccessToken.deploy();
|
||||
});
|
||||
|
||||
describe("Minting", function () {
|
||||
it("Should mint admin token", async function () {
|
||||
await accessToken.mintAccessToken(addr1.address, 0);
|
||||
expect(await accessToken.checkRole(addr1.address)).to.equal(0);
|
||||
});
|
||||
|
||||
it("Should mint moderator token", async function () {
|
||||
await accessToken.mintAccessToken(addr1.address, 1);
|
||||
expect(await accessToken.checkRole(addr1.address)).to.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Access Control", function () {
|
||||
it("Should fail for non-token holders", async function () {
|
||||
await expect(
|
||||
accessToken.checkRole(addr1.address)
|
||||
).to.be.revertedWith("No active token");
|
||||
});
|
||||
|
||||
it("Should revoke access", async function () {
|
||||
await accessToken.mintAccessToken(addr1.address, 0);
|
||||
const tokenId = await accessToken.activeTokens(addr1.address);
|
||||
await accessToken.revokeToken(tokenId);
|
||||
await expect(
|
||||
accessToken.checkRole(addr1.address)
|
||||
).to.be.revertedWith("No active token");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,42 +0,0 @@
|
||||
const { expect } = require("chai");
|
||||
const { ethers } = require("hardhat");
|
||||
|
||||
describe("MyContract", function () {
|
||||
let myContract;
|
||||
let owner;
|
||||
let addr1;
|
||||
let addr2;
|
||||
|
||||
beforeEach(async function () {
|
||||
// Получаем аккаунты из Hardhat
|
||||
[owner, addr1, addr2] = await ethers.getSigners();
|
||||
|
||||
// Деплоим контракт
|
||||
const MyContract = await ethers.getContractFactory("MyContract");
|
||||
myContract = await MyContract.deploy();
|
||||
await myContract.deployed();
|
||||
});
|
||||
|
||||
describe("Deployment", function () {
|
||||
it("Should set the right owner", async function () {
|
||||
expect(await myContract.owner()).to.equal(owner.address);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Transactions", function () {
|
||||
it("Should allow owner to set new owner", async function () {
|
||||
await myContract.setOwner(addr1.address);
|
||||
expect(await myContract.owner()).to.equal(addr1.address);
|
||||
});
|
||||
|
||||
it("Should fail if non-owner tries to set new owner", async function () {
|
||||
// Подключаемся к контракту от имени addr1
|
||||
const contractConnectedToAddr1 = myContract.connect(addr1);
|
||||
|
||||
// Ожидаем, что транзакция будет отменена
|
||||
await expect(
|
||||
contractConnectedToAddr1.setOwner(addr2.address)
|
||||
).to.be.revertedWith("Only owner can call this function");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user