Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #738 from LiskHQ/736-fix_delegate_asset_schema
Browse files Browse the repository at this point in the history
Fixing delegate asset schema and add delegate transaction tests - Closes #736 & #802
  • Loading branch information
Isabella Dell authored Sep 26, 2017
2 parents 5694df5 + c7db284 commit 249a3d3
Show file tree
Hide file tree
Showing 6 changed files with 582 additions and 46 deletions.
48 changes: 18 additions & 30 deletions logic/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Delegate.prototype.calculateFee = function (trs, sender) {
* @param {transaction} trs
* @param {account} sender
* @param {function} cb - Callback function.
* @returns {setImmediateCallback|Object} returns error if invalid parameter |
* @returns {setImmediateCallback|Object} returns error if invalid parameter |
* trs validated.
*/
Delegate.prototype.verify = function (trs, sender, cb) {
Expand Down Expand Up @@ -156,14 +156,11 @@ Delegate.prototype.apply = function (trs, block, sender, cb) {
address: sender.address,
u_isDelegate: 0,
isDelegate: 1,
vote: 0
vote: 0,
u_username: null,
username: trs.asset.delegate.username
};

if (trs.asset.delegate.username) {
data.u_username = null;
data.username = trs.asset.delegate.username;
}

modules.accounts.setAccountAndGet(data, cb);
};

Expand All @@ -180,14 +177,11 @@ Delegate.prototype.undo = function (trs, block, sender, cb) {
address: sender.address,
u_isDelegate: 1,
isDelegate: 0,
vote: 0
vote: 0,
username: null,
u_username: trs.asset.delegate.username
};

if (!sender.nameexist && trs.asset.delegate.username) {
data.username = null;
data.u_username = trs.asset.delegate.username;
}

modules.accounts.setAccountAndGet(data, cb);
};

Expand All @@ -202,19 +196,16 @@ Delegate.prototype.applyUnconfirmed = function (trs, sender, cb) {
var data = {
address: sender.address,
u_isDelegate: 1,
isDelegate: 0
isDelegate: 0,
username: null,
u_username: trs.asset.delegate.username
};

if (trs.asset.delegate.username) {
data.username = null;
data.u_username = trs.asset.delegate.username;
}

modules.accounts.setAccountAndGet(data, cb);
};

/**
* Checks trs delegate and calls modules.accounts.setAccountAndGet() with
* Checks trs delegate and calls modules.accounts.setAccountAndGet() with
* username and u_username both null.
* @implements module:accounts#Accounts~setAccountAndGet
* @param {transaction} trs
Expand All @@ -225,27 +216,24 @@ Delegate.prototype.undoUnconfirmed = function (trs, sender, cb) {
var data = {
address: sender.address,
u_isDelegate: 0,
isDelegate: 0
isDelegate: 0,
username: null,
u_username: null
};

if (trs.asset.delegate.username) {
data.username = null;
data.u_username = null;
}

modules.accounts.setAccountAndGet(data, cb);
};

Delegate.prototype.schema = {
id: 'Delegate',
type: 'object',
properties: {
publicKey: {
username: {
type: 'string',
format: 'publicKey'
format: 'username'
}
},
required: ['publicKey']
required: ['username']
};

/**
Expand All @@ -258,7 +246,7 @@ Delegate.prototype.objectNormalize = function (trs) {
var report = library.schema.validate(trs.asset.delegate, Delegate.prototype.schema);

if (!report) {
throw 'Failed to validate delegate schema: ' + this.scope.schema.getLastErrors().map(function (err) {
throw 'Failed to validate delegate schema: ' + library.schema.getLastErrors().map(function (err) {
return err.message;
}).join(', ');
}
Expand Down
27 changes: 13 additions & 14 deletions test/common/schemaDynamicTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function SchemaDynamicTest (config) {
}
}
},
shouldFailWithoutRequiredProperties: self.testRequired
shouldFailWithoutRequiredProperties: self.testRequired.bind(self)
};
}

Expand All @@ -113,30 +113,29 @@ SchemaDynamicTest.prototype.standardInvalidPropertyAssertion = function (input,
};

SchemaDynamicTest.prototype.testArgument = function (expectedType, invalidInputs, testedFunction) {
var assertion = self.customArgumentAssertion ? self.customArgumentAssertion.bind(self) : self.standardInvalidArgumentAssertion;
var assertion = this.customArgumentAssertion || this.standardInvalidArgumentAssertion;
var test = function (invalidInput, eachCb) {
self.testStyle(testedFunction, invalidInput.input, function (err) {
this.testStyle(testedFunction, invalidInput.input, function (err) {
assertion(invalidInput, expectedType, err);
eachCb();
});
};
self.carpetTesting(test, invalidInputs, 'should return an error when invoked with %s');
}.bind(this);
this.carpetTesting(test, invalidInputs, 'should return an error when invoked with %s');
};


SchemaDynamicTest.prototype.testProperty = function (expectedType, invalidInputs, testedFunction, validArgument, property) {
var assertion = self.customPropertyAssertion ? self.customPropertyAssertion.bind(self) : self.standardInvalidPropertyAssertion;
var assertion = this.customPropertyAssertion || this.standardInvalidPropertyAssertion;
var test = function (invalidInput, eachCb) {
var malformedPart = {};
set(malformedPart, property, invalidInput.input);

var invalidArgument = assign({}, validArgument, malformedPart);
self.testStyle(testedFunction, invalidArgument, function (err) {
this.testStyle(testedFunction, invalidArgument, function (err) {
assertion(invalidInput, expectedType, property, err);
eachCb();
});
};
self.carpetTesting(test, invalidInputs, 'should return an error when ' + property + ' is %s');
}.bind(this);
this.carpetTesting(test, invalidInputs, 'should return an error when ' + property + ' is %s');
};

SchemaDynamicTest.prototype.standardMissingRequiredPropertiesAssertion = function (property, err) {
Expand All @@ -145,19 +144,19 @@ SchemaDynamicTest.prototype.standardMissingRequiredPropertiesAssertion = functio
};

SchemaDynamicTest.prototype.testRequired = function (testedFunction, validArgument, properties) {
var assertion = self.customRequiredPropertiesAssertion ? self.customRequiredPropertiesAssertion.bind(self) : self.standardMissingRequiredPropertiesAssertion;
var assertion = this.customRequiredPropertiesAssertion || this.standardMissingRequiredPropertiesAssertion;
var test = function (missingProperty, eachCb) {
var invalidArgument = assign({}, validArgument);
delete invalidArgument[missingProperty.description];
self.testStyle(testedFunction, invalidArgument, function (err) {
this.testStyle(testedFunction, invalidArgument, function (err) {
assertion(missingProperty.description, err);
eachCb();
});
};
}.bind(this);
var missingFieldsDescriptions = properties.map(function (property) {
return {description: property};
});
self.carpetTesting(test, missingFieldsDescriptions, 'should return an error when invoked without %s');
this.carpetTesting(test, missingFieldsDescriptions, 'should return an error when invoked without %s');
};

module.exports = SchemaDynamicTest;
7 changes: 6 additions & 1 deletion test/unit/helpers/wsApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('handshake', function () {
height: 1
};

describe('schema', function () {
describe('schema dynamic tests: handshake wsApi', function () {

var schemaDynamicTests = new SchemaDynamicTests({
customArgumentAssertion: function (input, expectedType, err) {
Expand All @@ -110,22 +110,27 @@ describe('handshake', function () {
schemaDynamicTests.schema.shouldFailAgainst.nonObject.arguments(handshake);

describe('nonce', function () {

schemaDynamicTests.schema.shouldFailAgainst.nonString.property(handshake, validHeaders, 'nonce');
});

describe('height', function () {

schemaDynamicTests.schema.shouldFailAgainst.nonInteger.property(handshake, validHeaders, 'height');
});

describe('nethash', function () {

schemaDynamicTests.schema.shouldFailAgainst.nonString.property(handshake, validHeaders, 'nethash');
});

describe('version', function () {

schemaDynamicTests.schema.shouldFailAgainst.nonString.property(handshake, validHeaders, 'version');
});

describe('required properties', function () {

schemaDynamicTests.schema.shouldFailWithoutRequiredProperties(handshake, validHeaders, ['port', 'version', 'nonce', 'nethash', 'height']);
});
});
Expand Down
1 change: 1 addition & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require('./helpers/wsApi');
require('./helpers/z_schema');

require('./logic/account');
require('./logic/delegate');
require('./logic/peer');
require('./logic/peers');
require('./logic/multisignature');
Expand Down
Loading

0 comments on commit 249a3d3

Please sign in to comment.