ERC721URIStorage.test.js 3.5 KB

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