ERC721URIStorage.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const { BN, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ERC721URIStorageMock = artifacts.require('ERC721URIStorageMock');
  4. contract('ERC721URIStorage', function (accounts) {
  5. const [ owner ] = accounts;
  6. const name = 'Non Fungible Token';
  7. const symbol = 'NFT';
  8. const firstTokenId = new BN('5042');
  9. const nonExistentTokenId = new BN('13');
  10. beforeEach(async function () {
  11. this.token = await ERC721URIStorageMock.new(name, symbol);
  12. });
  13. describe('token URI', function () {
  14. beforeEach(async function () {
  15. await this.token.mint(owner, firstTokenId);
  16. });
  17. const baseURI = 'https://api.example.com/v1/';
  18. const sampleUri = 'mock://mytoken';
  19. it('it is empty by default', async function () {
  20. expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
  21. });
  22. it('reverts when queried for non existent token id', async function () {
  23. await expectRevert(
  24. this.token.tokenURI(nonExistentTokenId), 'ERC721: invalid token ID',
  25. );
  26. });
  27. it('can be set for a token id', async function () {
  28. await this.token.setTokenURI(firstTokenId, sampleUri);
  29. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(sampleUri);
  30. });
  31. it('reverts when setting for non existent token id', async function () {
  32. await expectRevert(
  33. this.token.setTokenURI(nonExistentTokenId, sampleUri), 'ERC721URIStorage: URI set of nonexistent token',
  34. );
  35. });
  36. it('base URI can be set', async function () {
  37. await this.token.setBaseURI(baseURI);
  38. expect(await this.token.baseURI()).to.equal(baseURI);
  39. });
  40. it('base URI is added as a prefix to the token URI', async function () {
  41. await this.token.setBaseURI(baseURI);
  42. await this.token.setTokenURI(firstTokenId, sampleUri);
  43. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + sampleUri);
  44. });
  45. it('token URI can be changed by changing the base URI', async function () {
  46. await this.token.setBaseURI(baseURI);
  47. await this.token.setTokenURI(firstTokenId, sampleUri);
  48. const newBaseURI = 'https://api.example.com/v2/';
  49. await this.token.setBaseURI(newBaseURI);
  50. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + sampleUri);
  51. });
  52. it('tokenId is appended to base URI for tokens with no URI', async function () {
  53. await this.token.setBaseURI(baseURI);
  54. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + firstTokenId);
  55. });
  56. it('tokens without URI can be burnt ', async function () {
  57. await this.token.burn(firstTokenId, { from: owner });
  58. expect(await this.token.exists(firstTokenId)).to.equal(false);
  59. await expectRevert(
  60. this.token.tokenURI(firstTokenId), 'ERC721: invalid token ID',
  61. );
  62. });
  63. it('tokens with URI can be burnt ', async function () {
  64. await this.token.setTokenURI(firstTokenId, sampleUri);
  65. await this.token.burn(firstTokenId, { from: owner });
  66. expect(await this.token.exists(firstTokenId)).to.equal(false);
  67. await expectRevert(
  68. this.token.tokenURI(firstTokenId), 'ERC721: invalid token ID',
  69. );
  70. });
  71. });
  72. });