payroll.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var Payroll = artifacts.require("./Payroll.sol");
  2. // TestCase for addEmployee function
  3. contract('Payroll_addEmployee', function(accounts) {
  4. it("Test other user want to add employee", function() {
  5. return Payroll.deployed().then(function(instance) {
  6. payrollInstance = instance;
  7. return payrollInstance.addEmployee(accounts[1], 2, {from: accounts[2]});
  8. }).catch( function(error) {
  9. assert.include(error.toString(),"revert", "Error!!: other user can add employee");
  10. });
  11. });
  12. it("Test add an exist employee", function() {
  13. return Payroll.deployed().then(function(instance) {
  14. payrollInstance = instance;
  15. return payrollInstance.addEmployee(accounts[1], 1, {from: accounts[0]});
  16. }).then( function() {
  17. return payrollInstance.addEmployee(accounts[1], 1, {from: accounts[0]});
  18. }).catch( function(error) {
  19. assert.include(error.toString(),"invalid opcode", "Error!!: Can add same employee");
  20. });
  21. });
  22. it("Test add an employee with 2 eter salary", function() {
  23. return Payroll.deployed().then(function(instance) {
  24. payrollInstance = instance;
  25. return payrollInstance.addEmployee(accounts[2], 2, {from: accounts[0]});
  26. }).then(function() {
  27. return payrollInstance.employees(accounts[2]);
  28. }).then(function(employee) {
  29. assert.equal(employee[1].toNumber(), web3.toWei(2, 'ether'), "Error!!: The salary was wrong!");
  30. });
  31. });
  32. });
  33. // TestCase for removeEmployee function
  34. contract('Payroll_removeEmployee', function(accounts) {
  35. it("Test other user want to remove employee", function() {
  36. return Payroll.deployed().then(function(instance) {
  37. payrollInstance = instance;
  38. return payrollInstance.removeEmployee(accounts[1], {from: accounts[2]});
  39. }).catch( function(error) {
  40. assert.include(error.toString(),"revert", "Error!!: other user can remove employee");
  41. });
  42. });
  43. it("Test remove an not exist employee", function() {
  44. return Payroll.deployed().then(function(instance) {
  45. payrollInstance = instance;
  46. return payrollInstance.removeEmployee(accounts[5], {from: accounts[0]});
  47. }).catch( function(error) {
  48. assert.include(error.toString(),"invalid opcode", "Error!!: can remove not exist employee");
  49. });
  50. });
  51. it("Test remove an existing employee", function() {
  52. return Payroll.deployed().then( function(instance) {
  53. payrollInstance = instance;
  54. return payrollInstance.addFund({from: accounts[0],value: web3.toWei('3', 'ether')});
  55. }).then(function(){
  56. return payrollInstance.addEmployee(accounts[1], 1, {from: accounts[0]});
  57. }).then(function() {
  58. return payrollInstance.employees(accounts[1]);
  59. }).then(function(employee) {
  60. assert.equal(employee[1].toNumber(), web3.toWei(1, 'ether'), "Error!!: add employee failed!");
  61. }).then(function() {
  62. return payrollInstance.addFund({from: accounts[0],value: web3.toWei('10', 'ether')});
  63. }).then(function() {
  64. web3.currentProvider.send({jsonrpc: "2.0", method: "evm_increaseTime", params: [11], id: 0});
  65. return payrollInstance.removeEmployee(accounts[1]);
  66. }).then(function() {
  67. return payrollInstance.employees(accounts[1]);
  68. }).then(function(employeeTwo) {
  69. assert.equal(employeeTwo[1].toNumber(), 0, "Error!!: remove employee failed!");
  70. });
  71. });
  72. });
  73. // TestCase for removeEmployee function
  74. contract('Payroll_getPaid', function(accounts) {
  75. it("Test get paid", function(){
  76. var oldBalance;
  77. Payroll.deployed().then(function(instance) {
  78. payrollInstance = instance;
  79. oldBalance = web3.eth.getBalance(accounts[2]).toNumber();
  80. return payrollInstance.addEmployee(accounts[2], 1, {from:accounts[0]});
  81. }).then(function() {
  82. return payrollInstance.addFund({from: accounts[0],value: web3.toWei('10', 'ether')});
  83. }).then(function() {
  84. web3.currentProvider.send({jsonrpc: "2.0", method: "evm_increaseTime", params: [11], id: 0});
  85. return payrollInstance.getPaid({from:accounts[2]})
  86. }).then(function() {
  87. var diff = web3.eth.getBalance(accounts[2]).toNumber() - oldBalance;
  88. assert(diff > 0, 'Error!!!: get paid failed!');
  89. });
  90. });
  91. it("Test not employee get paid", function(){
  92. Payroll.deployed().then(function(instance) {
  93. payrollInstance = instance;
  94. return payrollInstance.addFund({from: accounts[0],value: web3.toWei('10', 'ether')});
  95. }).then(function() {
  96. web3.currentProvider.send({jsonrpc: "2.0", method: "evm_increaseTime", params: [11], id: 0});
  97. return payrollInstance.getPaid({from: accounts[2]})
  98. }).catch(function(error) {
  99. assert.include(error.toString(),"revert", "Error!!!: Not employee get paid success!!!!!");
  100. });
  101. });
  102. });