|
| 1 | +import { loadFixture } from "@nomicfoundation/hardhat-network-helpers"; |
| 2 | +import { TradeTrustToken } from "@tradetrust/contracts"; |
| 3 | +import faker from "faker"; |
| 4 | +import { expect } from "."; |
| 5 | +import { getTestUsers, TestUsers, createDeployFixtureRunner, toAccessControlRevertMessage } from "./helpers"; |
| 6 | +import { deployTokenFixture, DeployTokenFixtureRunner } from "./fixtures"; |
| 7 | +import { roleHash } from "../src/constants"; |
| 8 | + |
| 9 | +describe("TradeTrustTokenBaseURI", async () => { |
| 10 | + let users: TestUsers; |
| 11 | + let registryContract: TradeTrustToken; |
| 12 | + |
| 13 | + let registryName: string; |
| 14 | + let registrySymbol: string; |
| 15 | + |
| 16 | + let registryContractAsAdmin: TradeTrustToken; |
| 17 | + |
| 18 | + let fakeBaseURI: string; |
| 19 | + |
| 20 | + let deployTokenFixtureRunner: DeployTokenFixtureRunner; |
| 21 | + |
| 22 | + // eslint-disable-next-line no-undef |
| 23 | + before(async () => { |
| 24 | + users = await getTestUsers(); |
| 25 | + |
| 26 | + registryName = "The Great Shipping Company"; |
| 27 | + registrySymbol = "GSC"; |
| 28 | + |
| 29 | + deployTokenFixtureRunner = async () => |
| 30 | + createDeployFixtureRunner( |
| 31 | + ...(await deployTokenFixture<TradeTrustToken>({ |
| 32 | + tokenContractName: "TradeTrustToken", |
| 33 | + tokenName: registryName, |
| 34 | + tokenInitials: registrySymbol, |
| 35 | + deployer: users.carrier, |
| 36 | + })) |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + beforeEach(async () => { |
| 41 | + fakeBaseURI = `${faker.internet.url()}/`; |
| 42 | + |
| 43 | + [, registryContract] = await loadFixture(deployTokenFixtureRunner); |
| 44 | + |
| 45 | + registryContractAsAdmin = registryContract.connect(users.carrier); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should revert set base URI when caller has no admin role", async () => { |
| 49 | + const nonAdminSigner = users.beneficiary; |
| 50 | + const tx = registryContract.connect(nonAdminSigner).setBaseURI(fakeBaseURI); |
| 51 | + |
| 52 | + await expect(tx).to.be.revertedWith(toAccessControlRevertMessage(nonAdminSigner.address, roleHash.DefaultAdmin)); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should set base URI when caller has admin role", async () => { |
| 56 | + const tx = registryContractAsAdmin.setBaseURI(fakeBaseURI); |
| 57 | + |
| 58 | + await expect(tx).to.not.be.reverted; |
| 59 | + }); |
| 60 | + |
| 61 | + describe("Behaviours of tokenURI and baseURI", () => { |
| 62 | + let tokenId: number; |
| 63 | + |
| 64 | + beforeEach(async () => { |
| 65 | + tokenId = faker.datatype.number(); |
| 66 | + await registryContractAsAdmin.mint(users.beneficiary.address, users.beneficiary.address, tokenId); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should return the correct tokenURI when baseURI is set", async () => { |
| 70 | + await registryContractAsAdmin.setBaseURI(fakeBaseURI); |
| 71 | + |
| 72 | + const tokenURI = await registryContract.tokenURI(tokenId); |
| 73 | + |
| 74 | + expect(tokenURI).to.equal(`${fakeBaseURI}${tokenId}`); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should return empty string as tokenURI when baseURI is not set", async () => { |
| 78 | + const tokenURI = await registryContract.tokenURI(tokenId); |
| 79 | + |
| 80 | + expect(tokenURI).to.be.empty; |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments