From 97f63c424b6ab95dd89a243ab9f2adb71bdaca4b Mon Sep 17 00:00:00 2001 From: John McCarthy Date: Tue, 20 Mar 2018 11:04:16 -0600 Subject: [PATCH] add tests for Authentication.sol, helper contract for error testing --- contracts/Authentication.sol | 2 -- test/TestAuthentication.sol | 55 +++++++++++++++++++++++++++++++++++- test/authentication.js | 2 +- test/helpers/ErrorProxy.sol | 20 +++++++++++++ 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 test/helpers/ErrorProxy.sol diff --git a/contracts/Authentication.sol b/contracts/Authentication.sol index 3001647..8c4761f 100644 --- a/contracts/Authentication.sol +++ b/contracts/Authentication.sol @@ -9,8 +9,6 @@ contract Authentication is Killable { mapping (address => User) private users; - uint private id; // Stores user id temporarily - modifier onlyExistingUser { // Check if user exists or terminate diff --git a/test/TestAuthentication.sol b/test/TestAuthentication.sol index 00c3923..cdddfb4 100644 --- a/test/TestAuthentication.sol +++ b/test/TestAuthentication.sol @@ -3,6 +3,7 @@ pragma solidity ^0.4.2; import "truffle/Assert.sol"; import "truffle/DeployedAddresses.sol"; import "../contracts/Authentication.sol"; +import "./helpers/ErrorProxy.sol"; contract TestAuthentication { @@ -13,7 +14,59 @@ contract TestAuthentication { bytes32 expected = 'testuser'; - Assert.equal(authentication.login(), expected, "It should sign up and log in a user."); + Assert.equal(authentication.login(), expected, "It signs up and logs in a user."); + } + + function testUserCanUpdateName() { + Authentication authentication = Authentication(DeployedAddresses.Authentication()); + + bytes32 expected = 'new_name'; + + Assert.equal(authentication.update('new_name'), expected, "It updates a user."); + } + + function testLoginFailure() { + Authentication authentication = new Authentication(); + ErrorProxy errorProxy = new ErrorProxy(address(authentication)); + + Authentication(address(errorProxy)).login(); + + bool r = errorProxy.execute.gas(200000)(); + + Assert.isFalse(r, "Only existings users can log in."); + } + + function testSignUpFailure() { + Authentication authentication = new Authentication(); + ErrorProxy errorProxy = new ErrorProxy(address(authentication)); + + Authentication(address(errorProxy)).signup(''); + + bool r = errorProxy.execute.gas(200000)(); + + Assert.isFalse(r, "Cannot sign up with invalid name."); + } + + function testUpdateWithInvalidName() { + Authentication authentication = new Authentication(); + ErrorProxy errorProxy = new ErrorProxy(address(authentication)); + + Authentication(address(errorProxy)).update(''); + + bool r = errorProxy.execute.gas(200000)(); + + Assert.isFalse(r, "Cannot update user with invalid name."); + } + + function testUpdateWithInvalidUser() { + Authentication authentication = new Authentication(); + ErrorProxy errorProxy = new ErrorProxy(address(authentication)); + + Authentication(address(errorProxy)).update('new_name'); + + bool r = errorProxy.execute.gas(200000)(); + + Assert.isFalse(r, "Cannot update non-existent user."); } } diff --git a/test/authentication.js b/test/authentication.js index dc8926c..e73c688 100644 --- a/test/authentication.js +++ b/test/authentication.js @@ -2,7 +2,7 @@ var Authentication = artifacts.require("./Authentication.sol"); contract('Authentication', function(accounts) { - it("...should sign up and log in a user.", function() { + it("...signs up and logs in a user.", function() { return Authentication.deployed().then(function(instance) { authenticationInstance = instance; diff --git a/test/helpers/ErrorProxy.sol b/test/helpers/ErrorProxy.sol new file mode 100644 index 0000000..90a6111 --- /dev/null +++ b/test/helpers/ErrorProxy.sol @@ -0,0 +1,20 @@ +// Proxy contract for testing errors +// http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests + +contract ErrorProxy { + address public target; + bytes data; + + function ErrorProxy(address _target) { + target = _target; + } + + //prime the data using the fallback function. + function() { + data = msg.data; + } + + function execute() returns (bool) { + return target.call(data); + } +} \ No newline at end of file