Skip to content

Commit

Permalink
#63 Adjust test for order sign command
Browse files Browse the repository at this point in the history
  • Loading branch information
ceres3idoo authored and andreafspeziale committed Mar 11, 2019
1 parent e506d86 commit 4c79449
Showing 1 changed file with 54 additions and 44 deletions.
98 changes: 54 additions & 44 deletions tests/unit/commands/signCommand.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-env node, jest */
const sandbox = require('sinon').createSandbox()

const MockCancelOrderDataToSign = require('../../factories/MockCancelOrderDataToSign')
const MockCreateOrderDataToSign = require('../../factories/MockCreateOrderDataToSign')
const logger = require('../../../src/logger')
const PrivateKeyServiceBuilder = require('../../../src/factories/PrivateKeyServiceBuilder')
const { InvalidKeystoreParams } = require('../../../src/services/PrivateKeyService')
const OrderSignerHelper = require('../../../src/helpers/OrderSignerHelper')
const PrivateKeyValidator = require('../../../src/validators/PrivateKeyValidator')
const OrderSignCommandValidator = require('../../../src/validators/commands/order/OrderSignCommandValidator')
Expand All @@ -16,74 +19,77 @@ const privateKeyService = PrivateKeyServiceBuilder.build()

const signCommand = new OrderSignCommand(logger, orderSignerHelper,
orderSignCommandValidator, privateKeyService, privateKeyValidator)
const keystorePassword = 'password'
const keystoreFilePath = 'tests/fixtures/keyStore/validKeystore'

describe('os sign', () => {
describe('should raise validationError if', () => {
test('type is not creation or cancellation.', async() => {
const privateKeyPath = 'tests/fixtures/privateKeys/privateKey.key'
const cliInputJson = MockCancelOrderDataToSign.build({ type: 'wrongType' })
beforeEach(() => {
sandbox.stub(signCommand, 'promptKeyStorePasswordAsync')
.returns(keystorePassword)
})

const expectedResult = [{ code: 'ValidationError', field: 'type', message: 'type must be one of [creation, cancellation]' }]
afterEach(() => {
sandbox.restore()
})

const result = await signCommand
.executeAsync({ privateKeyPath, cliInputJson: JSON.stringify(cliInputJson) })
describe('os sign', () => {
test('should raise validationError if type is not creation or cancellation.', async() => {
const cliInputJson = MockCancelOrderDataToSign.build({ type: 'wrongType' })
const expectedResult = [{
code: 'ValidationError',
field: 'type',
message: 'type must be one of [creation, cancellation]',
}]

expect(result).toMatchObject(expectedResult)
})
const result = await signCommand
.executeAsync({ keystorePassword, keystoreFilePath, cliInputJson: JSON.stringify(cliInputJson) })

describe('private key was not defined as expected', () => {
const invalidPrivateKeyPaths = ['/notExistent/path', 1, [], {}]
test.each(invalidPrivateKeyPaths)(`should raise ValidationError if the private key path
refers to a file which does not exist: %o`, async(invalidPrivateKeyPath) => {
const expectedResult = [{ code: 'ValidationError', message: 'privateKeyFilePath file does not exist!', field: 'privateKeyFilePath' }]
expect(result).toMatchObject(expectedResult)
})

const result = await signCommand
.executeAsync({ privateKeyPath: invalidPrivateKeyPath })
const invalidKeystorePaths = ['/notExistent/path', 1, [], {}]
test.each(invalidKeystorePaths)(`should raise ValidationError if the keystore path
refers to a file which does not exist: %o`, async(invalidKeystorePath) => {
const cliInputJson = MockCancelOrderDataToSign.build()
const expectedResult = [{ code: 'ValidationError',
message: 'keystoreFilePath file does not exist!',
field: 'keystoreFilePath' }]

expect(result).toMatchObject(expectedResult)
const result = await signCommand
.executeAsync({
keystorePassword,
keystoreFilePath: invalidKeystorePath,
cliInputJson: JSON.stringify(cliInputJson),
})
})
})
describe('sign cancel order', () => {
test('should raise ValidationError if the private key is not a valid ethereum private key', async() => {
const invalidPrivateKey = 'tests/fixtures/privateKeys/invalidPrivateKey.key'
const cliInputJson = MockCancelOrderDataToSign.build()
const expectedResult = [{ code: 'ValidationError', field: 'privateKey', message: 'privateKey is an invalid ethereum private key.' }]

const result = await signCommand
.executeAsync({ privateKeyPath: invalidPrivateKey, cliInputJson: JSON.stringify(cliInputJson) })

expect(result).toMatchObject(expectedResult)
})
expect(result).toMatchObject(expectedResult)
})

describe('sign cancel order', () => {
test('confirmation field is not cancel_request.', async() => {
const privateKeyPath = 'tests/fixtures/privateKeys/privateKey.key'
const cliInputJson = MockCancelOrderDataToSign.build({ order: { confirmation: 'differentFrom cancel_request' } })

const expectedResult = [{ code: 'ValidationError',
field: 'confirmation',
message: 'confirmation must be one of [cancel_request]' }]

const result = await signCommand
.executeAsync({ privateKeyPath, cliInputJson: JSON.stringify(cliInputJson) })
.executeAsync({ keystoreFilePath, keystorePassword, cliInputJson: JSON.stringify(cliInputJson) })

expect(result).toMatchObject(expectedResult)
})
})

test('should return the expected signature.', async() => {
const privateKeyPath = 'tests/fixtures/privateKeys/privateKey.key'

const orderId = '0x9090909090909090909090909090909090909090909090909090909090909090'
const cliInputJson = MockCancelOrderDataToSign.build({ order: { id: orderId } })
const expectedEcSignature = {
r: '0xfdba9197cdb4c360f666fe66658812f50156655439f8845d0c12d199074d3905',
s: '0x660874341ed0ec11fe9b8f1fd9208d5b4a576884c442b7031f7d48fea4146d36',
r: '0x2c882baa96e7c38e2f38cdad9e2f4bd55934de27d52080e2782c3329cf7915f9',
s: '0x39ef0b364b6f947ba0acc27a3df3abd87f05be4ee293e6ae54152cd835e29572',
v: 27,
}

const result = await signCommand
.executeAsync({ privateKeyPath, cliInputJson: JSON.stringify(cliInputJson) })
.executeAsync({ keystoreFilePath, keystorePassword, cliInputJson: JSON.stringify(cliInputJson) })

expect(result).toMatchObject(expectedEcSignature)
})
Expand All @@ -93,13 +99,19 @@ describe('sign create order', () => {
test('should raise ValidationError if the private key is not a valid ethereum private key', async() => {
const invalidPrivateKey = 'tests/fixtures/privateKeys/invalidPrivateKey.key'
const cliInputJson = MockCreateOrderDataToSign.build()
const expectedResult = [{ code: 'ValidationError', field: 'privateKey', message: 'privateKey is an invalid ethereum private key.' }]

const expectedResult = [{
code: 'ValidationError',
field: 'privateKey',
message: 'privateKey is an invalid ethereum private key.',
}]

const result = await signCommand
.executeAsync({ privateKeyPath: invalidPrivateKey, cliInputJson: JSON.stringify(cliInputJson) })

expect(result).toMatchObject(expectedResult)
})

describe('should raise validationError if', () => {
const invalidEthereumAddressFields = ['exchangeAddress', 'maker', 'offerTokenAddress', 'wantTokenAddress']
test.each(invalidEthereumAddressFields)('should raise ValidationError if %o is not a valid ethereum address.', async(invalidEthereumAddressField) => {
Expand All @@ -116,8 +128,6 @@ describe('sign create order', () => {
expect(result).toMatchObject(expectedResult)
})
test('should return the expected signature.', async() => {
const privateKeyPath = 'tests/fixtures/privateKeys/privateKey.key'

const cliInputJson = MockCreateOrderDataToSign.build({ order: {
exchangeAddress: '0xb05042c4e649DA88f0d4A55aC33312f38F3aeaef',
maker: '0x062ca8979C5f30AA1bD23c4834eE048956ad02a4',
Expand All @@ -130,13 +140,13 @@ describe('sign create order', () => {
} })

const expectedEcSignature = {
r: '0x2572776ce20cb5f161416eaa05a0e1eaba5f1bcb02176da872868675689a1d16',
s: '0x52061b07ead176493d24bf382ec2506865587beb5dee717adea474f262dc0442',
v: 28,
r: '0xd002769436f9a0a7879d658077375a7b3506b00a5e51fb252edbd2f7ffec4cc3',
s: '0x2f783203d66556983991a0c40622ac875065866248dffa3f397b42bdd8498f7e',
v: 27,
}

const result = await signCommand
.executeAsync({ privateKeyPath, cliInputJson: JSON.stringify(cliInputJson) })
.executeAsync({ keystoreFilePath, keystorePassword, cliInputJson: JSON.stringify(cliInputJson) })

expect(result).toMatchObject(expectedEcSignature)
})
Expand Down

0 comments on commit 4c79449

Please sign in to comment.