Skip to content
Snippets Groups Projects
Commit 8ba7799e authored by Éloïs's avatar Éloïs
Browse files

Use cucumber for integration tests

parent 77edd3fc
No related branches found
No related tags found
1 merge request!11Use cucumber for integration tests
This commit is part of merge request !11. Comments created here will be created in the context of that merge request.
This diff is collapsed.
......@@ -9,12 +9,16 @@ repository = 'https://git.duniter.org/nodes/rust/duniter-substrate'
version = '3.0.0'
[dev-dependencies]
async-trait = "0.1"
cucumber = "0.11"
env_logger = "0.9.0"
parity-scale-codec = "2.3.1"
portpicker = "0.1.1"
serde_json = "1.0.64"
sp-keyring = { git = "https://github.com/librelois/substrate.git", branch = "duniter-monthly-2022-01" }
subxt = { git = 'https://github.com/librelois/subxt.git', branch = 'duniter-monthly-2022-01' }
tokio = { version = "1.15.0", features = ["macros"] }
# substrate dev-dependencies
sp-keyring = { git = "https://github.com/librelois/substrate.git", branch = "duniter-monthly-2022-01" }
[[test]]
name = "cucumber_tests"
harness = false # allows Cucumber to print output instead of libtest
......@@ -13,8 +13,3 @@
//
// You should have received a copy of the GNU Affero General Public License
// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
#[cfg(test)]
mod balance_transfer;
#[cfg(test)]
pub mod common;
......@@ -14,39 +14,29 @@
// You should have received a copy of the GNU Affero General Public License
// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
use crate::common::*;
use super::*;
use sp_keyring::AccountKeyring;
use subxt::PairSigner;
#[tokio::test]
async fn test_balance_transfer() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
// Spawn a node
let (api, client, _process) = spawn_node().await;
let alice = PairSigner::new(AccountKeyring::Alice.pair());
let dave = AccountKeyring::Dave.to_account_id();
let events = create_block_with_extrinsic(
&client,
pub async fn transfer(
api: &Api,
client: &Client,
from: AccountKeyring,
amount: u64,
to: AccountKeyring,
) -> Result<()> {
let from = PairSigner::new(from.pair());
let to = to.to_account_id();
let _events = create_block_with_extrinsic(
client,
api.tx()
.balances()
.transfer(dave.clone().into(), 512)
.create_signed(&alice, ())
.transfer(to.clone().into(), amount)
.create_signed(&from, ())
.await?,
)
.await?;
println!(
"Balance transfer extrinsic written in blockchain, events: {:?}",
events
);
// verify that Bob's free Balance increased
let dave_post = api.storage().system().account(dave, None).await?;
println!("Bob's Free Balance is now {}\n", dave_post.data.free);
assert_eq!(dave_post.data.free, 512);
Ok(())
}
......@@ -14,15 +14,23 @@
// You should have received a copy of the GNU Affero General Public License
// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
#![allow(clippy::enum_variant_names)]
pub mod balances;
#[subxt::subxt(runtime_metadata_path = "../resources/metadata.scale")]
pub mod node_runtime {}
use serde_json::Value;
use sp_keyring::AccountKeyring;
use std::process::Command;
use subxt::{ClientBuilder, DefaultConfig, DefaultExtra};
pub type Api = node_runtime::RuntimeApi<DefaultConfig, DefaultExtra<DefaultConfig>>;
pub type Client = subxt::Client<DefaultConfig>;
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub const SUDO_ACCOUNT: AccountKeyring = AccountKeyring::Alice;
pub struct Process(std::process::Child);
......@@ -67,10 +75,24 @@ pub async fn spawn_node() -> (Api, Client, Process) {
(api, client, process)
}
/*pub async fn create_empty_block(client: &Client) -> Result<(), subxt::Error> {
// Create an empty block
let _: Value = client
.rpc()
.client
.request(
"engine_createBlock",
&[Value::Bool(true), Value::Bool(false), Value::Null],
)
.await?;
Ok(())
}*/
pub async fn create_block_with_extrinsic(
client: &Client,
extrinsic: subxt::UncheckedExtrinsic<DefaultConfig, DefaultExtra<DefaultConfig>>,
) -> Result<subxt::TransactionEvents<DefaultConfig>, subxt::Error> {
) -> Result<subxt::TransactionEvents<DefaultConfig>> {
// Get a hash of the extrinsic (we'll need this later).
use subxt::sp_runtime::traits::Hash as _;
let ext_hash = <DefaultConfig as subxt::Config>::Hashing::hash_of(&extrinsic);
......@@ -89,5 +111,10 @@ pub async fn create_block_with_extrinsic(
.await?;
// Get extrinsic events
watcher.wait_for_in_block().await?.fetch_events().await
watcher
.wait_for_in_block()
.await?
.fetch_events()
.await
.map_err(Into::into)
}
// Copyright 2021 Axiom-Team
//
// This file is part of Substrate-Libre-Currency.
//
// Substrate-Libre-Currency is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// Substrate-Libre-Currency is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
mod common;
use async_trait::async_trait;
use common::node_runtime::runtime_types::gdev_runtime;
use common::node_runtime::runtime_types::pallet_balances;
use common::*;
use cucumber::{given, then, when, World, WorldInit};
use sp_keyring::AccountKeyring;
use std::convert::Infallible;
use std::str::FromStr;
use subxt::{sp_runtime::MultiAddress, PairSigner};
#[derive(WorldInit)]
pub struct DuniterWorld {
api: Api,
client: Client,
_process: Process,
}
impl std::fmt::Debug for DuniterWorld {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
Ok(())
}
}
#[async_trait(?Send)]
impl World for DuniterWorld {
// We do require some error type.
type Error = Infallible;
async fn new() -> std::result::Result<Self, Infallible> {
let (api, client, _process) = spawn_node().await;
Ok(DuniterWorld {
api,
client,
_process,
})
}
}
#[given(regex = r"([a-zA-Z]+) have (\d+) ĞD")]
async fn who_have(world: &mut DuniterWorld, who: String, amount: u64) -> Result<()> {
// Parse inputs
let who = AccountKeyring::from_str(&who)
.expect("unknown to")
.to_account_id();
let amount = amount * 100;
// Create {amount} ĞD for {who}
let _events = create_block_with_extrinsic(
&world.client,
world
.api
.tx()
.sudo()
.sudo(gdev_runtime::Call::Balances(
pallet_balances::pallet::Call::set_balance {
who: MultiAddress::Id(who),
new_free: amount,
new_reserved: 0,
},
))
.create_signed(&PairSigner::new(SUDO_ACCOUNT.pair()), ())
.await?,
)
.await?;
Ok(())
}
#[when(regex = r"([a-zA-Z]+) send (\d+) ĞD to ([a-zA-Z]+)")]
async fn transfer(world: &mut DuniterWorld, from: String, amount: u64, to: String) -> Result<()> {
// Parse inputs
let from = AccountKeyring::from_str(&from).expect("unknown from");
let amount = amount * 100;
let to = AccountKeyring::from_str(&to).expect("unknown to");
common::balances::transfer(&world.api, &world.client, from, amount, to).await
}
#[when(regex = r"([a-zA-Z]+) sends all (?:his|her) ĞDs? to ([a-zA-Z]+)")]
async fn send_all_to(world: &mut DuniterWorld, from: String, to: String) -> Result<()> {
// Parse inputs
let from = PairSigner::new(
AccountKeyring::from_str(&from)
.expect("unknown from")
.pair(),
);
let to = AccountKeyring::from_str(&to)
.expect("unknown to")
.to_account_id();
let _events = create_block_with_extrinsic(
&world.client,
world
.api
.tx()
.balances()
.transfer_all(to.clone().into(), false)
.create_signed(&from, ())
.await?,
)
.await?;
Ok(())
}
#[then(regex = r"([a-zA-Z]+) have (\d+) ĞD")]
async fn assert_who_have(world: &mut DuniterWorld, who: String, amount: u64) -> Result<()> {
// Parse inputs
let who = AccountKeyring::from_str(&who)
.expect("unknown to")
.to_account_id();
let amount = amount * 100;
let who_account = world.api.storage().system().account(who, None).await?;
assert_eq!(who_account.data.free, amount);
Ok(())
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
//env_logger::init();
DuniterWorld::run("tests/features").await
}
Feature: Balance transfer
Scenario: If alice sends 5 ĞD to Dave, Dave will get 5 ĞD
Given alice have 10 ĞD
When alice send 5 ĞD to dave
Then dave have 5 ĞD
Feature: Balance transfer all
Scenario: If alice sends all her ĞDs to Dave, Dave will get 10 ĞD
Given alice have 10 ĞD
When alice sends all her ĞDs to dave
Then dave have 10 ĞD
......@@ -101,11 +101,7 @@ fn devnet_genesis(
code: wasm_binary.to_vec(),
},
balances: BalancesConfig {
balances: initial_identities
.values()
.cloned()
.map(|account_id| (account_id, 1_000))
.collect(),
balances: Default::default(),
},
grandpa: GrandpaConfig {
authorities: initial_authorities
......@@ -145,7 +141,7 @@ fn devnet_genesis(
},
universal_dividend: UniversalDividendConfig {
first_ud: 1_000,
initial_monetary_mass: initial_identities.len() as u64 * 1_000,
initial_monetary_mass: 0,
},
}
}
......@@ -70,7 +70,10 @@ fn test_two_identities() {
events[0],
EventRecord {
phase: Phase::Initialization,
event: Event::Identity(crate::Event::IdtyAcquireRight(IdtyName(vec![0]), Right::Right1)),
event: Event::Identity(crate::Event::IdtyAcquireRight(
IdtyName(vec![0]),
Right::Right1
)),
topics: vec![],
}
);
......@@ -92,7 +95,10 @@ fn test_two_identities() {
events[1],
EventRecord {
phase: Phase::Initialization,
event: Event::Identity(crate::Event::IdtyLostRight(IdtyName(vec![1]), Right::Right1)),
event: Event::Identity(crate::Event::IdtyLostRight(
IdtyName(vec![1]),
Right::Right1
)),
topics: vec![],
}
);
......
......@@ -64,6 +64,6 @@ pub type Signature = sp_runtime::MultiSignature;
pub struct IdtyNameValidatorImpl;
impl pallet_identity::traits::IdtyNameValidator for IdtyNameValidatorImpl {
fn validate(idty_name: &pallet_identity::IdtyName) -> bool {
idty_name.0.len() <= 64
idty_name.0.len() >= 3 && idty_name.0.len() <= 64
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment