Skip to content
Open
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
2 changes: 0 additions & 2 deletions contracts/Authentication.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
55 changes: 54 additions & 1 deletion test/TestAuthentication.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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.");
}

}
2 changes: 1 addition & 1 deletion test/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
20 changes: 20 additions & 0 deletions test/helpers/ErrorProxy.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}