Skip to content

Commit

Permalink
add zupassUserRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Mar 2, 2024
1 parent 2d219a2 commit 945deaa
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/contracts/userRegistry/ZupassUserRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.6.12;
pragma solidity ^0.8.10;

import '@openzeppelin/contracts/access/Ownable.sol';

Expand Down
110 changes: 110 additions & 0 deletions contracts/tests/userRegistryZupass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { ethers } from 'hardhat'
import { expect } from 'chai'
import { Contract, Wallet } from 'ethers'

import { ZERO_ADDRESS } from '../utils/constants'
import { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers'

describe('Zupass User Registry', () => {
let registry: Contract
let user: HardhatEthersSigner

beforeEach(async () => {
let deployer: HardhatEthersSigner
;[, deployer, user] = await ethers.getSigners()

const ZupassUserRegistry = await ethers.getContractFactory(
'ZupassUserRegistry',
deployer
)
registry = await ZupassUserRegistry.deploy()
})

describe('managing verified users', () => {
it('allows owner to add user to the registry', async () => {
const semaphoreId = 1
expect(await registry.isVerifiedUser(user.address)).to.equal(false)
await expect(registry.addUser(user.address, semaphoreId))
.to.emit(registry, 'UserAdded')
.withArgs(user.address, semaphoreId)
expect(await registry.isVerifiedUser(user.address)).to.equal(true)
})

it('rejects zero-address', async () => {
const semaphoreId = 1
await expect(
registry.addUser(ZERO_ADDRESS, semaphoreId)
).to.be.revertedWith('UserRegistry: User address is zero')
})

it('rejects zero semphoreId', async () => {
const semaphoreId = 0
await expect(
registry.addUser(user.address, semaphoreId)
).to.be.revertedWith('UserRegistry: Semaphore Id is zero')
})

it('rejects user who is already in the registry', async () => {
const semaphoreId = 1
await registry.addUser(user.address, semaphoreId)
await expect(
registry.addUser(user.address, semaphoreId)
).to.be.revertedWith('UserRegistry: User already verified')
})

it('rejects semphoreId that is already in the registry', async () => {
const semaphoreId = 1
const anotherUser = Wallet.createRandom()
await registry.addUser(user.address, semaphoreId)
await expect(
registry.addUser(anotherUser.address, semaphoreId)
).to.be.revertedWith('UserRegistry: Semaphore Id already registered')
})

it('allows only owner to add users', async () => {
const semaphoreId = 1
const registryAsUser = registry.connect(user) as Contract
await expect(
registryAsUser.addUser(user.address, semaphoreId)
).to.be.revertedWith('Ownable: caller is not the owner')
})

it('allows owner to remove user', async () => {
const semaphoreId = 1
await registry.addUser(user.address, semaphoreId)
await expect(registry.removeUser(user.address))
.to.emit(registry, 'UserRemoved')
.withArgs(user.address)
expect(await registry.isVerifiedUser(user.address)).to.equal(false)
})

it('reverts when trying to remove user who is not in the registry', async () => {
await expect(registry.removeUser(user.address)).to.be.revertedWith(
'UserRegistry: User is not in the registry'
)
})

it('allows only owner to remove users', async () => {
const semaphoreId = 1
await registry.addUser(user.address, semaphoreId)
const registryAsUser = registry.connect(user) as Contract
await expect(registryAsUser.removeUser(user.address)).to.be.revertedWith(
'Ownable: caller is not the owner'
)
})

it('checks valid semaphoreId', async () => {
const semaphoreId = 1
await registry.addUser(user.address, semaphoreId)
const isVerified = await registry.isVerifiedSemaphoreId(semaphoreId)
expect(isVerified).to.be.true
})

it('checks invalid semaphoreId', async () => {
const semaphoreId = 1
await registry.addUser(user.address, semaphoreId)
const isVerified = await registry.isVerifiedSemaphoreId(3)
expect(isVerified).to.be.false
})
})
})

0 comments on commit 945deaa

Please sign in to comment.