apla-blockchain-tools
Version:
Module contains a number of tools to work with Apla Blockchain
355 lines (317 loc) • 13.8 kB
JavaScript
// MIT License
//
// Copyright (c) 2016-2018 AplaProject
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/*
* Package: apla-blockchain-tools
* Author: Anton Zuev
* mail: a.zuev@apla.io
* Company: apla.io
*/
"use strict";
let should = require('chai').should();
let {describe, it} = require("mocha");
let path = require("path");
let Session = require("../session/index.js");
let fs = require("fs");
let errors = require("../session/errors");
let apiUrl = process.env.API_URL || "https://testapla0.apla.io:7079/api/v2";
let networkId = process.env.NETWORK_ID || 1;
let testEcosystemNumber = process.env.TEST_ECOSYSTEM_NUMBER || 41;
let privateKey = process.env.PRIVATE_KEY || "4e304ae2492d633217eb2f161d94ba023d7f07a10b73370dd669a225bc08ba80";
describe('Session', function() {
describe('#constructor', function() {
it('Should create a new Session with passed private key', function() {
let privateKey = "3bce27b5e277382a84225217fd8a58bc2b3d4501a630ca0bc14c56c86b6b779f";
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
session.keys.should.have.property("private");
session.keys.should.have.property("public");
session.apiUrl.should.equal(apiUrl);
session.networkId.should.equal(networkId);
});
});
describe('#generateKeys', function() {
it('Should generate a new public and private key', function() {
let session = new Session(apiUrl, networkId);
session.generateKeys();
session.keys.should.have.property("private");
session.keys.should.have.property("public");
});
});
describe('#login', function() {
it('Should login to the system with new key. Ecosystem not passed. Default value is used', async () => {
let session = new Session(apiUrl, networkId);
session.generateKeys();
await session.login();
session.should.have.property("token");
});
it('Should login to the system with existing key. Ecosystem not passed. Default value is used', async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
}); await session.login();
session.should.have.property("token");
});
it('Should login to the system with new key. Ecosystem passed. Ecosystem exists', async () => {
let session = new Session(apiUrl, networkId);
session.generateKeys();
await session.login({
ecosystem: 1
});
session.should.have.property("token");
});
it('Should login to the system with existent key. Ecosystem passed. Ecosystem exists', async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: 1
});
session.should.have.property("token");
});
/*
it('Shouldn\'t login to the system. Ecosystem passed. Ecosystem doesn\'t exist', async () => {
let session = new Session(apiUrl, networkId);
session.generateKeys();
await session.login({
ecosystem: 548934
});
session.should.not.have.property("token");
}).timeout(5000);
it('Shouldn\'t login to the system. Ecosystem passed. Ecosystem exists. I am not a member', async () => {
let session = new Session(apiUrl, networkId);
session.generateKeys();
await session.login({
ecosystem: 2
});
session.should.not.have.property("token");
}).timeout(5000);
*/
});
describe("#callContract", function () {
it("Contract doesn't exist. Should throw error E_CONTRACT", async () => {
let session = new Session(apiUrl, networkId);
session.generateKeys();
await session.login({
ecosystem: testEcosystemNumber
});
try {
await session.callContract("@ProfilemkdfmkdmfEdit", {
member_name: "Anton",
information: "823984893489384"
});
throw new Error("ContractNotFoundError should be thrown")
}catch (err){
err.type.should.equal("E_CONTRACT")
}
}).timeout(5000);
it("Contract exists. Not Authorized. Should throw Error", async () => {
let session = new Session(apiUrl, networkId);
session.generateKeys();
try {
await session.callContract("@ProfileEdit", {
member_name: "Anton",
information: "823984893489384"
});
throw new Error("Error should be thrown")
}catch (err){
// TODO: change of type may be needed
err.type.should.equal("E_SERVER")
}
}).timeout(5000);
describe("Parameters serialization tests + contract execution", function () {
it("Passing int", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
let {result, blockid} = await session.callContract("TestIntPassing", {
param: 2
});
result.should.equal("2");
blockid.should.be.not.empty;
}).timeout(10000);
it("Passing float", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
let {result, blockid} = await session.callContract("TestFloatPassing", {
param: 2.3
});
result.should.equal("2.3");
blockid.should.be.not.empty;
}).timeout(10000);
it("Passing string", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
let {result, blockid} = await session.callContract("TestStringPassing", {
param: "testString"
});
result.should.equal("testString");
blockid.should.be.not.empty;
}).timeout(10000);
it("Passing bool", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
let {result, blockid} = await session.callContract("TestBoolPassing", {
param: true
});
result.should.equal("true");
blockid.should.be.not.empty;
}).timeout(10000);
it("Passing bytes", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
let param = new ArrayBuffer(2);
param[0] = 12;
param[1] = 42;
let {result, blockid} = await session.callContract("TestBytesPassing", {
param: param
});
result.should.be.not.empty;
blockid.should.be.not.empty;
}).timeout(10000);
it("Passing file", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
let filepath = path.resolve(__dirname, "./APLA.png");
let file = fs.readFileSync(filepath, null).buffer;
let {result, blockid} = await session.callContract("TestFilePassing", {
param: {
filename: "apla.png",
mimeType: "images/png",
body: file
}
});
result.should.be.not.empty;
blockid.should.be.not.empty;
}).timeout(10000);
it("Passing array", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
let {result, blockid} = await session.callContract("TestArrayPassing", {
param: [1, 2, 3]
});
result.should.be.not.empty;
blockid.should.be.not.empty;
}).timeout(10000);
it("Passing money", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
let {result, blockid} = await session.callContract("TestMoneyPassing", {
param: "12.3"
});
result.should.be.not.empty;
blockid.should.be.not.empty;
}).timeout(10000);
});
describe("Parameters checks + contract execution", function () {
it("Passing all required parameters. Only required parameters exist", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
let {result, blockid} = await session.callContract("TestParamsPassingOnlyRequired", {
number: 2,
b: true
});
result.should.equal("2");
blockid.should.be.not.empty;
}).timeout(10000);
it("Passing all required parameters. Required and optional params exist", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
let {result, blockid} = await session.callContract("TestParamsPassingRequiredAndOptional", {
number: 2
});
result.should.equal("2");
blockid.should.be.not.empty;
}).timeout(10000);
it("Passing only optional parameters", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
try{
await session.callContract("TestParamsPassingRequiredAndOptional", {
b: true
});
}catch(err){
err.name.should.equal("RequiredParamNotPassedError")
}
}).timeout(10000);
it("Passing redundant param. Should throw RedundantParamPassedError", async () => {
let session = new Session(apiUrl, networkId, {
privateKey: privateKey
});
await session.login({
ecosystem: testEcosystemNumber
});
try{
await session.callContract("TestParamsPassingRequiredAndOptional", {
b: true,
nonExistingParam: "some value"
});
throw new Error("Error RedundantParamPassedError should be thrown")
}catch(err){
err.name.should.equal("RedundantParamPassedError")
}
}).timeout(10000);
})
})
});