Mise à jour de GitLab prévue ce samedi 23 octobre 2021 à partir de 9h00 CET

Commit ad25ab39 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

More tests on Amendments

parent f04364b8
......@@ -87,78 +87,78 @@ AmendmentSchema.methods = {
else callback("Bad document structure: no new line character at the end of the document.");
},
verify: function(currency, callback){
verify: function(currency, done){
var obj = this;
async.waterfall([
function(callback, err){
// Version
if(!obj.version || !obj.version.match(/^1$/))
err = "Version unknown";
err = {code: 100, message: "Version unknown"};
callback(err);
},
function(callback, err){
// Currency
if(!obj.currency || !obj.currency.match("^"+ currency.name + "$"))
err = "Currency not managed";
err = {code: 101, message: "Currency '"+ obj.currency +"' not managed"};
callback(err);
},
function(callback, err){
// Number
if(!obj.number || !obj.number.match(/^\d+$/))
err = "Incorrect Number field";
err = {code: 102, message: "Incorrect Number field"};
callback(err);
},
function(callback, err){
// Previous hash
var isRoot = parseInt(obj.number, 10) === 0;
if(!isRoot && (!obj.previousHash || !obj.previousHash.match(/^[A-Z\d]{40}$/)))
err = "PreviousHash must be provided for non-root amendment and match an uppercase SHA1 hash";
err = {code: 103, message: "PreviousHash must be provided for non-root amendment and match an uppercase SHA1 hash"};
else if(isRoot && obj.previousHash)
err = "PreviousHash must not be provided for root amendment";
err = {code: 104, message: "PreviousHash must not be provided for root amendment"};
callback(err);
},
function(callback, err){
// Universal Dividend
if(obj.dividend && !obj.dividend.match(/^\d+$/))
err = "UniversalDividend must be a decimal number";
err = {code: 105, message: "UniversalDividend must be a decimal number"};
callback(err);
},
function(callback, err){
// Coin Minimal Power
if(obj.coinMinPower && !obj.dividend)
err = "CoinMinimalPower requires a valued UniversalDividend field";
err = {code: 106, message: "CoinMinimalPower requires a valued UniversalDividend field"};
else if(obj.coinMinPower && !obj.coinMinPower.match(/^\d+$/))
err = "CoinMinimalPower must be a decimal number";
err = {code: 107, message: "CoinMinimalPower must be a decimal number"};
else if(obj.coinMinPower && obj.dividend.length < parseInt(obj.coinMinPower, 10) + 1)
err = "No coin can be created with this value of CoinMinimalPower and UniversalDividend";
err = {code: 108, message: "No coin can be created with this value of CoinMinimalPower and UniversalDividend"};
callback(err);
},
function(callback, err){
// VotersRoot
if(!obj.votersRoot || !obj.votersRoot.match(/^[A-Z\d]{40}$/))
err = "VotersRoot must be provided and match an uppercase SHA1 hash";
err = {code: 109, message: "VotersRoot must be provided and match an uppercase SHA1 hash"};
callback(err);
},
function(callback, err){
// VotersCount
if(!obj.votersCount || !obj.votersCount.match(/^\d+$/))
err = "VotersCount must be a positive or null decimal number";
err = {code: 110, message: "VotersCount must be a positive or null decimal number"};
callback(err);
},
function(callback, err){
// MembersRoot
if(!obj.membersRoot || !obj.membersRoot.match(/^[A-Z\d]{40}$/))
err = "MembersRoot must be provided and match an uppercase SHA1 hash";
err = {code: 111, message: "MembersRoot must be provided and match an uppercase SHA1 hash"};
callback(err);
},
function(callback, err){
// MembersCount
if(!obj.membersCount || !obj.membersCount.match(/^\d+$/))
err = "MembersCount must be a positive or null decimal number";
err = {code: 112, message: "MembersCount must be a positive or null decimal number"};
callback(err);
}
], function (err, result) {
callback(err);
done(err.message, err.code);
});
},
......@@ -171,6 +171,39 @@ AmendmentSchema.methods = {
}
}
return members;
},
getLeavingMembers: function() {
var members = [];
for (var i = 0; i < this.membersChanges.length; i++) {
var matches = this.membersChanges[i].match(/^\-([\w\d]{40})$/);
if(matches){
members.push(matches[1]);
}
}
return members;
},
getNewVoters: function() {
var voters = [];
for (var i = 0; i < this.votersChanges.length; i++) {
var matches = this.votersChanges[i].match(/^\+([\w\d]{40})$/);
if(matches){
voters.push(matches[1]);
}
}
return voters;
},
getLeavingVoters: function() {
var voters = [];
for (var i = 0; i < this.votersChanges.length; i++) {
var matches = this.votersChanges[i].match(/^\-([\w\d]{40})$/);
if(matches){
voters.push(matches[1]);
}
}
return voters;
}
};
......
......@@ -8,13 +8,15 @@ var nodecoin = require('../app/lib/nodecoin');
nodecoin.database.init();
var Amendment = mongoose.model('Amendment');
var amTest = new Amendment();
var amTest;
describe('Amendments', function(){
describe('structure test', function(){
describe('Amendment', function(){
describe('0 of beta_brousouf currency', function(){
// Loads amTest with its data
beforeEach(function(done) {
before(function(done) {
amTest = new Amendment();
fs.readFile(__dirname + "/data/amendments/BB-AM0-OK", {encoding: "utf8"}, function (err, data) {
amTest.parse(data, function(err) {
done(err);
......@@ -26,14 +28,204 @@ describe('Amendments', function(){
assert.equal(amTest.version, 1);
});
it('should have 3 new members', function(){
var members = amTest.getNewMembers();
assert.equal(members.length, 3);
assert.equal(members[0], "2E69197FAB029D8669EF85E82457A1587CA0ED9C");
assert.equal(members[1], "33BBFC0C67078D72AF128B5BA296CC530126F372");
assert.equal(members[2], "C73882B64B7E72237A2F460CE9CAB76D19A8651E");
it('should have beta_brousouf currency name', function(){
assert.equal(amTest.currency, 'beta_brousouf');
});
it('should be number 0', function(){
assert.equal(amTest.number, 0);
});
it('should have no Universal Dividend', function(){
should.not.exist(amTest.dividend);
});
it('should have no Minimal Coin Power', function(){
should.not.exist(amTest.coinMinPower);
});
it('should have no previous hash', function(){
should.not.exist(amTest.previousHash);
});
it('should have F5ACFD67FC908D28C0CFDAD886249AC260515C90 members hash', function(){
assert.equal('F5ACFD67FC908D28C0CFDAD886249AC260515C90', amTest.membersRoot);
});
it('should have F5ACFD67FC908D28C0CFDAD886249AC260515C90 voters hash', function(){
assert.equal('F5ACFD67FC908D28C0CFDAD886249AC260515C90', amTest.votersRoot);
});
it('should have the following 3 new members', function(){
var newMembers = amTest.getNewMembers();
assert.equal(newMembers.length, 3);
assert.equal(amTest.membersCount, 3);
assert.equal(newMembers[0], "2E69197FAB029D8669EF85E82457A1587CA0ED9C"); // Obito Uchiwa
assert.equal(newMembers[1], "33BBFC0C67078D72AF128B5BA296CC530126F372"); // John Snow
assert.equal(newMembers[2], "C73882B64B7E72237A2F460CE9CAB76D19A8651E"); // LoL Cat
});
it('should have the following 3 new voters', function(){
var newVoters = amTest.getNewVoters();
assert.equal(newVoters.length, 3);
assert.equal(amTest.votersCount, 3);
assert.equal(newVoters[0], "2E69197FAB029D8669EF85E82457A1587CA0ED9C");
assert.equal(newVoters[1], "33BBFC0C67078D72AF128B5BA296CC530126F372");
assert.equal(newVoters[2], "C73882B64B7E72237A2F460CE9CAB76D19A8651E");
});
});
describe('1 of beta_brousouf currency', function(){
// Loads amTest with its data
before(function(done) {
amTest = new Amendment();
fs.readFile(__dirname + "/data/amendments/BB-AM1-OK", {encoding: "utf8"}, function (err, data) {
amTest.parse(data, function(err) {
done(err);
});
});
});
it('should be version 1', function(){
assert.equal(amTest.version, 1);
});
it('should have beta_brousouf currency name', function(){
assert.equal(amTest.currency, 'beta_brousouf');
});
it('should be number 1', function(){
assert.equal(amTest.number, 1);
});
it('should have no Universal Dividend', function(){
should.not.exist(amTest.dividend);
});
it('should have no Minimal Coin Power', function(){
should.not.exist(amTest.coinMinPower);
});
it('should have 6F4ACBC7A25A0AAB9B58778EAD5A297EF3E51D00 previous hash', function(){
assert.equal(amTest.previousHash, '6F4ACBC7A25A0AAB9B58778EAD5A297EF3E51D00');
});
it('should have 0 new members', function(){
var newMembers = amTest.getNewMembers();
assert.equal(newMembers.length, 0);
assert.equal(amTest.membersCount, 3);
});
it('should have 0 new voters', function(){
var newVoters = amTest.getNewVoters();
assert.equal(newVoters.length, 0);
assert.equal(amTest.votersCount, 3);
});
});
describe('2 of beta_brousouf currency', function(){
// Loads amTest with its data
before(function(done) {
amTest = new Amendment();
fs.readFile(__dirname + "/data/amendments/BB-AM2-OK", {encoding: "utf8"}, function (err, data) {
amTest.parse(data, function(err) {
done(err);
});
});
});
it('should be version 1', function(){
assert.equal(amTest.version, 1);
});
it('should have beta_brousouf currency name', function(){
assert.equal(amTest.currency, 'beta_brousouf');
});
// More tests here ...
it('should be number 2', function(){
assert.equal(amTest.number, 2);
});
it('should have a niversal Dividend of value 100', function(){
assert.equal(amTest.dividend, 100);
});
it('should have a Minimal Coin Power of 0', function(){
assert.equal(amTest.coinMinPower, 0);
});
it('should have 3E6EDD8CF1391AFADB3E3619B3A131E9300B963F previous hash', function(){
assert.equal(amTest.previousHash, '3E6EDD8CF1391AFADB3E3619B3A131E9300B963F');
});
it('should have D092366E448D18C0E72D7C7976A647A122B55225 members hash', function(){
assert.equal(amTest.membersRoot, 'D092366E448D18C0E72D7C7976A647A122B55225');
});
it('should have DC7A9229DFDABFB9769789B7BFAE08048BCB856F voters hash', function(){
assert.equal(amTest.votersRoot, 'DC7A9229DFDABFB9769789B7BFAE08048BCB856F');
});
it('should have the following new member', function(){
var newMembers = amTest.getNewMembers();
assert.equal(newMembers.length, 1);
assert.equal(amTest.membersCount, 4);
assert.equal(newMembers[0], "31A6302161AC8F5938969E85399EB3415C237F93"); // cgeek
});
it('should have 0 new voters', function(){
var voters = amTest.getNewVoters();
assert.equal(voters.length, 0);
assert.equal(amTest.votersCount, 2);
});
it('should have one voter leaving', function(){
var leavingVoters = amTest.getLeavingVoters();
assert.equal(leavingVoters.length, 1);
assert.equal(amTest.votersCount, 2);
});
});
describe('2 (WRONG-UD ONE) of beta_brousouf currency', function(){
var errCode = 0;
// Loads amTest with its data
before(function(done) {
amTest = new Amendment();
fs.readFile(__dirname + "/data/amendments/BB-AM2-WRONG-UD", {encoding: "utf8"}, function (err, data) {
amTest.parse(data, function(err) {
amTest.verify({name: "beta_brousouf"}, function(err, code) {
errCode = code;
done();
});
});
});
});
it('should be version 1', function(){
assert.equal(amTest.version, 1);
});
it('should have beta_brousouf currency name', function(){
assert.equal(amTest.currency, 'beta_brousouf');
});
it('should be number 2', function(){
assert.equal(amTest.number, 2);
});
it('should have a niversal Dividend of value 122', function(){
assert.equal(amTest.dividend, 122);
});
it('should have a Minimal Coin Power of 3', function(){
assert.equal(amTest.coinMinPower, 3);
});
it('should have verification error code 108', function(){
assert.equal(errCode, 108);
});
});
});
\ No newline at end of file
Version: 1
Currency: beta_brousouf
Number: 1
PreviousHash: 6F4ACBC7A25A0AAB9B58778EAD5A297EF3E51D00
VotersRoot: F5ACFD67FC908D28C0CFDAD886249AC260515C90
VotersCount: 3
VotersChanges:
MembersRoot: F5ACFD67FC908D28C0CFDAD886249AC260515C90
MembersCount: 3
MembersChanges:
Version: 1
Currency: beta_brousouf
Number: 2
PreviousHash: 3E6EDD8CF1391AFADB3E3619B3A131E9300B963F
UniversalDividend: 100
CoinMinimalPower: 0
VotersRoot: DC7A9229DFDABFB9769789B7BFAE08048BCB856F
VotersCount: 2
VotersChanges:
-C73882B64B7E72237A2F460CE9CAB76D19A8651E
MembersRoot: D092366E448D18C0E72D7C7976A647A122B55225
MembersCount: 4
MembersChanges:
+31A6302161AC8F5938969E85399EB3415C237F93
Version: 1
Currency: beta_brousouf
Number: 2
PreviousHash: 3E6EDD8CF1391AFADB3E3619B3A131E9300B963F
UniversalDividend: 122
CoinMinimalPower: 3
VotersRoot: DC7A9229DFDABFB9769789B7BFAE08048BCB856F
VotersCount: 2
VotersChanges:
-C73882B64B7E72237A2F460CE9CAB76D19A8651E
MembersRoot: D092366E448D18C0E72D7C7976A647A122B55225
MembersCount: 4
MembersChanges:
+31A6302161AC8F5938969E85399EB3415C237F93
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment