Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
dkijania committed Oct 23, 2020
1 parent 77cef08 commit 410e18c
Show file tree
Hide file tree
Showing 36 changed files with 1,071 additions and 154 deletions.
139 changes: 139 additions & 0 deletions testing/jormungandr-integration-tests/src/common/jcli/api/address.rs
@@ -0,0 +1,139 @@
use crate::common::jcli::command::{AddressCommand, GenesisCommand};
use assert_cmd::assert::OutputAssertExt;
use assert_fs::assert::PathAssert;
use assert_fs::fixture::ChildPath;
use chain_addr::Discrimination;
use jormungandr_lib::crypto::hash::Hash;
use jortestkit::prelude::ProcessOutput;
use std::str::FromStr;
use std::{collections::BTreeMap, path::Path};
pub struct JCliAddress {
address_command: AddressCommand,
}

impl JCliAddress {
pub fn new(address_command: AddressCommand) -> Self {
Self { address_command }
}

pub fn info<S: Into<String>>(self, public_key: S) -> BTreeMap<String, String> {
self.address_command
.info()
.address(public_key.into())
.build()
.assert()
.success()
.get_output()
.as_single_node_yaml()
}

pub fn account<S: Into<String>>(
self,
prefix: Option<S>,
public_key: S,
discrimination: Discrimination,
) -> String {
let mut address_command = self.address_command.account();

if let Some(prefix) = prefix {
address_command = address_command.prefix(prefix.into());
}

if discrimination == Discrimination::Test {
address_command = address_command.test_discrimination();
}

address_command
.public_key(public_key)
.build()
.assert()
.success()
.get_output()
.as_single_line()
}

pub fn account_expect_fail<S: Into<String>>(
self,
prefix: Option<S>,
public_key: S,
expected_msg: &str,
) {
let mut address_command = self.address_command.account();

if let Some(prefix) = prefix {
address_command = address_command.prefix(prefix.into());
}

if discrimination == Discrimination::Test {
address_command = address_command.test_discrimination();
}

address_command
.public_key(public_key)
.build()
.assert()
.failure()
.stderr(predicates::str::contains(expected_msg));
}

pub fn info_expect_fail<S: Into<String>>(self, public_key: S, expected_msg: &str) {
self.address_command
.info()
.address(public_key.into())
.build()
.assert()
.failure()
.stderr(predicates::str::contains(expected_msg));
}

pub fn single<S: Into<String>>(
self,
prefix: Option<S>,
public_key: S,
discrimination: Discrimination,
) -> String {
let mut address_command = self.address_command.single();

if let Some(prefix) = prefix {
address_command = address_command.prefix(prefix.into());
}

if discrimination == Discrimination::Test {
address_command = address_command.test_discrimination();
}

address_command
.public_key(public_key)
.build()
.assert()
.success()
.get_output()
.as_single_line()
}

pub fn delegation<S: Into<String>, P: Into<String>>(
mut self,
public_key: S,
delegation_key: P,
discrimination: Discrimination,
) -> String {
let mut address_command = self.address_command.single();

if let Some(prefix) = prefix {
address_command = address_command.prefix(prefix.into());
}

if discrimination == Discrimination::Test {
address_command = address_command.test_discrimination();
}

address_command
.public_key(public_key)
.delegation_key(delegation_key)
.build()
.assert()
.success()
.get_output()
.as_single_line()
}
}
@@ -0,0 +1,83 @@
use crate::common::jcli::command::GenesisCommand;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::assert::PathAssert;
use assert_fs::fixture::ChildPath;
use jormungandr_lib::crypto::hash::Hash;
use jortestkit::prelude::ProcessOutput;
use std::path::Path;
use std::str::FromStr;
pub struct JCliGenesis {
genesis_command: GenesisCommand,
}

impl JCliGenesis {
pub fn new(genesis_command: GenesisCommand) -> Self {
Self { genesis_command }
}

pub fn decode<P: AsRef<Path>>(self, input: P, output: &ChildPath) {
self.genesis_command
.decode()
.input(input)
.output(output.path())
.build()
.assert()
.success();
output.assert(crate::predicate::file_exists_and_not_empty());
}

pub fn encode<P: AsRef<Path>>(self, input: P, output: &ChildPath) {
self.genesis_command
.encode()
.input(input)
.output(output.path())
.build()
.assert()
.success();

output.assert(crate::predicate::file_exists_and_not_empty());
}

pub fn encode_expect_fail<P: AsRef<Path>>(self, input: P, expected_msg: &str) {
self.genesis_command
.encode()
.input(input)
.build()
.assert()
.failure()
.stderr(predicates::str::contains(expected_msg));
}

pub fn hash<P: AsRef<Path>>(self, input: P) -> Hash {
let hash = self
.genesis_command
.hash()
.input(input)
.build()
.assert()
.success()
.get_output()
.as_single_line();

Hash::from_str(&hash).unwrap()
}

pub fn hash_expect_fail<P: AsRef<Path>>(self, input: P, expected_msg: &str) {
self.genesis_command
.hash()
.input(input)
.build()
.assert()
.failure()
.stderr(predicates::str::contains(expected_msg));
}

pub fn init(self) -> String {
self.genesis_command
.init()
.assert()
.success()
.get_output()
.as_lossy_string()
}
}
161 changes: 161 additions & 0 deletions testing/jormungandr-integration-tests/src/common/jcli/api/key.rs
@@ -0,0 +1,161 @@
use crate::common::jcli::command::KeyCommand;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::fixture::ChildPath;
use assert_fs::fixture::FileWriteStr;
use assert_fs::{assert::PathAssert, NamedTempFile};
use jormungandr_lib::crypto::hash::Hash;
use jortestkit::prelude::ProcessOutput;
use std::path::Path;
use std::str::FromStr;
const DEFAULT_KEY_TYPE: &str = "Ed25519Extended";

pub struct JCliKey {
key_command: KeyCommand,
}

impl JCliKey {
pub fn new(key_command: KeyCommand) -> Self {
Self { key_command }
}

pub fn generate_default(self) -> String {
self.generate(DEFAULT_KEY_TYPE)
}

pub fn generate<S: Into<String>>(self, key_type: S) -> String {
self.key_command
.generate()
.key_type(key_type.into())
.build()
.assert()
.success()
.get_output()
.as_single_line()
}

pub fn generate_expect_fail<S: Into<String>>(self, key_type: S, expected_msg_path: &str) {
self.key_command
.generate()
.key_type(key_type.into())
.build()
.assert()
.failure()
.stderr(predicates::str::contains(expected_msg_path));
}

pub fn generate_with_seed<S: Into<String>>(self, key_type: S, seed: S) -> String {
self.key_command
.generate()
.key_type(key_type.into())
.seed(seed.into())
.build()
.assert()
.success()
.get_output()
.as_single_line()
}

pub fn generate_with_seed_expect_fail<S: Into<String>>(
self,
key_type: S,
seed: S,
expected_msg_path: &str,
) {
self.key_command
.generate()
.key_type(key_type.into())
.seed(seed.into())
.build()
.assert()
.failure()
.stderr(predicates::str::contains(expected_msg_path));
}

pub fn to_public<S: Into<String>>(self, private_key: S) -> String {
let input_file = NamedTempFile::new("key_to_public.input").unwrap();
input_file.write_str(&private_key.into()).unwrap();

self.key_command
.to_public()
.input(input_file.path())
.build()
.assert()
.success()
.get_output()
.as_single_line()
}

pub fn to_public_expect_fail<S: Into<String>>(self, private_key: S, expected_msg_path: &str) {
let input_file = NamedTempFile::new("key_to_public.input").unwrap();
input_file.write_str(&private_key.into()).unwrap();

self.key_command
.to_public()
.input(input_file.path())
.build()
.assert()
.failure()
.stderr(predicates::str::contains(expected_msg_path));
}

pub fn to_bytes<S: Into<String>, P: AsRef<Path>>(self, private_key: S, output: P) {
let input = NamedTempFile::new("key_to_bytes.input").unwrap();
input.write_str(&private_key.into()).unwrap();

self.to_bytes_from_file(input.path(), output.as_ref())
}

pub fn to_bytes_from_file<P: AsRef<Path>, Q: AsRef<Path>>(self, input: P, output: Q) {
self.key_command
.to_bytes()
.output(output)
.input(input)
.build()
.assert()
.success();
}

pub fn to_bytes_expect_fail<P: AsRef<Path>, Q: AsRef<Path>>(
self,
input: P,
output: Q,
expected_msg_path: &str,
) {
self.key_command
.to_bytes()
.output(output)
.input(input)
.build()
.assert()
.failure()
.stderr(predicates::str::contains(expected_msg_path));
}

pub fn from_bytes<P: AsRef<Path>, S: Into<String>>(self, key_type: S, input: P) -> String {
self.key_command
.from_bytes()
.key_type(key_type)
.input(input)
.build()
.assert()
.success()
.get_output()
.as_single_line()
}

pub fn from_bytes_expect_fail<P: AsRef<Path>, S: Into<String>>(
self,
key_type: S,
input: P,
expected_msg_path: &str,
) {
self.key_command
.from_bytes()
.key_type(key_type)
.input(input)
.build()
.assert()
.failure()
.stderr(predicates::str::contains(expected_msg_path));
}
}
@@ -0,0 +1,7 @@
mod address;
mod genesis;
mod key;

pub use address::JCliAddress;
pub use genesis::JCliGenesis;
pub use key::JCliKey;

0 comments on commit 410e18c

Please sign in to comment.