Toggle registrations

This commit is contained in:
Andrea Maria Piana 2019-04-02 12:04:05 +02:00
parent c944f458bb
commit 25021f788d
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
2 changed files with 47 additions and 13 deletions

View File

@ -3,6 +3,7 @@ pragma solidity >=0.4.21 <0.6.0;
contract Nodes
{
address owner;
bool allowRegistration;
string[] public nodes;
mapping(string => uint) nodeIndex;
@ -15,6 +16,7 @@ contract Nodes
public
{
owner = msg.sender;
allowRegistration = false;
}
function nodeCount()
@ -28,8 +30,8 @@ contract Nodes
function addNode(string memory _node)
public
onlyOwner
{
require(allowRegistration || msg.sender == owner);
nodeIndex[_node] = nodes.length;
nodes.push(_node);
}
@ -49,6 +51,13 @@ contract Nodes
delete nodes;
}
function toggleRegistration(bool value)
public
onlyOwner
{
allowRegistration = value;
}
function _deleteNode(uint index) internal {
require(index < nodes.length);
nodes[index] = nodes[nodes.length-1];

View File

@ -9,21 +9,23 @@ contract('Nodes', async (accounts) => {
beforeEach(async () => {
instance = await Nodes.deployed();
await instance.deleteAll();
await instance.toggleRegistration(false);
});
describe('addNode', async () => {
describe('called by the owner', async () => {
describe('registering is enabled', async () => {
beforeEach(async () => {
await instance.toggleRegistration(true);
await instance.addNode(node1);
await instance.addNode(node2);
await instance.addNode(node2, { from: accounts[1] });
});
it('adds the first node', async () => {
it('adds the first node from the owner', async () => {
const actualNode1 = await instance.nodes(0);
assert.equal(actualNode1, node1);
});
it('adds the second node', async () => {
it('adds the second node from someone else', async () => {
const actualNode2 = await instance.nodes(1);
assert.equal(actualNode2, node2);
});
@ -33,14 +35,37 @@ contract('Nodes', async (accounts) => {
assert.equal(2, actualNodeCount);
});
});
describe('called by someone else', async () => {
it('throws an exception', async () => {
try {
await instance.addNode(node1, { from: accounts[1] });
} catch (error) {
return;
}
assert.fail('it should throw an exception');
describe('registering is disabled', async () => {
describe('called by the owner', async () => {
beforeEach(async () => {
await instance.addNode(node1);
await instance.addNode(node2);
});
it('adds the first node', async () => {
const actualNode1 = await instance.nodes(0);
assert.equal(actualNode1, node1);
});
it('adds the second node', async () => {
const actualNode2 = await instance.nodes(1);
assert.equal(actualNode2, node2);
});
it('sets the count', async () => {
const actualNodeCount = await instance.nodeCount();
assert.equal(2, actualNodeCount);
});
});
describe('called by someone else', async () => {
it('throws an exception', async () => {
try {
await instance.addNode(node1, { from: accounts[1] });
} catch (error) {
return;
}
assert.fail('it should throw an exception');
});
});
});
});