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

Fix tests v0.0.10 #6

Merged
merged 5 commits into from
Aug 5, 2022
Merged
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
185 changes: 185 additions & 0 deletions test/SCPostStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { ERC721_ABI, LEDGER_ABI, emails, zeroAddress } from './utils'
import { ethers, waffle } from 'hardhat'
import { expect } from 'chai'

describe('SCPostStorage', () => {
before(async function () {
this.accounts = await ethers.getSigners()
this.owner = this.accounts[0]
this.user = this.accounts[1]
this.scPostStorageFactory = await ethers.getContractFactory('SCPostStorage')
this.txParams = {
post: 'gm',
original: emails[0],
}
this.maxPostLength = 280
this.infixLength = 3
})
beforeEach(async function () {
this.scLedger = await waffle.deployMockContract(this.owner, LEDGER_ABI)
this.scPostStorage = await this.scPostStorageFactory.deploy(
this.scLedger.address,
this.maxPostLength,
this.infixLength,
zeroAddress
)
await this.scPostStorage.connect(this.owner)
await this.scPostStorage.deployed()
this.derivativeContract = await waffle.deployMockContract(
this.owner,
ERC721_ABI
)

await this.derivativeContract.mock.symbol.returns('ME7-d')
})
describe('Constructor', function () {
beforeEach(async function () {
this.scPostStorage = await this.scPostStorageFactory.deploy(
this.scLedger.address,
this.maxPostLength,
this.infixLength,
zeroAddress
)

await this.scPostStorage.deployed()
})
it('should deploy the SCPostStorage contract with the correct fields', async function () {
expect(await this.scPostStorage.ledgerAddress()).to.equal(
this.scLedger.address
)
expect(await this.scPostStorage.maxPostLength()).to.equal(
this.maxPostLength
)
expect(await this.scPostStorage.infixLength()).to.equal(this.infixLength)
expect(await this.scPostStorage.getTrustedForwarder()).to.equal(
zeroAddress
)
})
it('should deploy SCPostStorage, derivative and SCEmailLedgerContract contracts', async function () {
expect(await this.scPostStorage.address).to.exist
expect(await this.derivativeContract.address).to.exist
expect(await this.scLedger.address).to.exist
})
})
describe('Owner-only calls from non-owner', function () {
before(function () {
this.contractWithIncorrectOwner = this.scPostStorage.connect(this.user)
})
it('should have the correct owner', async function () {
expect(await this.scPostStorage.owner()).to.equal(this.owner.address)
})
it('should not be able to call setMaxPostLength', async function () {
this.contractWithIncorrectOwner = this.scPostStorage.connect(this.user)
await expect(
this.contractWithIncorrectOwner.setMaxPostLength(281)
).to.be.revertedWith('Ownable: caller is not the owner')
})
it('should not be able to call setInfixLength', async function () {
this.contractWithIncorrectOwner = this.scPostStorage.connect(this.user)
await expect(
this.contractWithIncorrectOwner.setInfixLength(4)
).to.be.revertedWith('Ownable: caller is not the owner')
})
})
describe('Contract', function () {
it('should save post', async function () {
// Setup mocks
await this.scLedger.mock.getDerivative
.withArgs(emails[0])
.returns(this.derivativeContract.address)
await this.derivativeContract.mock.balanceOf
.withArgs(this.owner.address)
.returns(1)

await expect(
this.scPostStorage.savePost(this.txParams.post, this.txParams.original)
)
.to.emit(this.scPostStorage, 'PostSaved')
.withArgs(
0,
this.txParams.post,
this.derivativeContract.address,
this.owner.address,
(
await ethers.provider.getBlock('latest')
).timestamp
)

const savedPost = await this.scPostStorage.posts(0)
expect({
post: savedPost.post,
derivativeAddress: savedPost.derivativeAddress,
}).to.deep.eq({
post: this.txParams.post,
derivativeAddress: this.derivativeContract.address,
})
})
it('should not save post is derivative does not exist', async function () {
// Setup mocks
await this.scLedger.mock.getDerivative
.withArgs(emails[0])
.returns('0x0000000000000000000000000000000000000000')
await this.derivativeContract.mock.balanceOf
.withArgs(this.owner.address)
.returns(1)
await expect(
this.scPostStorage.savePost(this.txParams.post, this.txParams.original)
).to.be.revertedWith('Derivative contract not found')
})
it('should not save post if post exceeds max length', async function () {
// Setup mocks
await this.scLedger.mock.getDerivative
.withArgs(emails[0])
.returns(this.derivativeContract.address)
await this.derivativeContract.mock.balanceOf
.withArgs(this.owner.address)
.returns(1)

const post = 'a'

await expect(
this.scPostStorage.savePost(post.repeat(281), this.txParams.original)
).to.be.revertedWith('Post exceeds max post length')
})
it('should not save post if user does not own a derivative', async function () {
// Setup mocks
await this.scLedger.mock.getDerivative
.withArgs(emails[0])
.returns(this.derivativeContract.address)
await this.derivativeContract.mock.balanceOf
.withArgs(this.owner.address)
.returns(0)
await expect(
this.scPostStorage.savePost(this.txParams.post, this.txParams.original)
).to.be.revertedWith('You do not own this derivative')
})
it('should return all posts', async function () {
// Setup mocks
await this.derivativeContract.mock.balanceOf
.withArgs(this.owner.address)
.returns(1)
await this.scLedger.mock.getDerivative
.withArgs(emails[0])
.returns(this.derivativeContract.address)
const expectedPosts: { post: string; original: string }[] = []
// Saving posts and seting expectedPosts array
for (let i = 0; i < 5; i++) {
await this.scPostStorage.savePost(
this.txParams.post,
this.txParams.original
)
expectedPosts.push({
post: this.txParams.post,
original: this.derivativeContract.address,
})
}
const posts = await this.scPostStorage.getAllPosts()
// Serializing posts array from contract call
const serializedPosts = posts.map((post) => ({
post: post.post,
original: post.derivativeAddress,
}))
expect(serializedPosts).to.deep.eq(expectedPosts)
})
})
})
164 changes: 0 additions & 164 deletions test/SealCredTwitter.ts

This file was deleted.

17 changes: 17 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { MockContract } from 'ethereum-waffle'
import type { SignerWithAddress } from '@nomiclabs/hardhat-ethers/dist/src/signer-with-address'

import type { SCPostStorage, SCPostStorage__factory } from '../typechain'

declare module 'mocha' {
export interface Context {
// Facoriries for contracts
scPostStorage: SCPostStorage
scPostStorageFactory: SCPostStorage__factory
scLedger: MockContract
// Signers
accounts: SignerWithAddress[]
owner: SignerWithAddress
user: SignerWithAddress
}
}
Loading