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

View File

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