Skip to content

Commit

Permalink
new rest tests for all endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
dkijania committed Jul 8, 2020
1 parent 31d946c commit f7d46e9
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
46 changes: 46 additions & 0 deletions vit-servicing-station-tests/src/tests/rest/funds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::common::{data, startup::{db::DbBuilder, quick_start, server::ServerBootstrapper}};
use assert_fs::TempDir;
use reqwest::StatusCode;

#[test]
pub fn get_funds_list_is_not_empty() {
let temp_dir = TempDir::new().unwrap();
let (server, hash) = quick_start(&temp_dir);
let funds = server
.rest_client_with_token(hash)
.funds()
.expect("cannot get funds");
assert!(funds.len() > 0);
}


#[test]
pub fn get_funds_by_id() -> Result<(), Box<dyn std::error::Error>>{
let temp_dir = TempDir::new().unwrap().into_persistent();
let expected_fund = data::funds().first().unwrap().clone();
let (token, hash) = data::token();

let db_path = DbBuilder::new()
.with_token(token)
.with_funds(vec![expected_fund.clone()])
.build(&temp_dir)?;

let server = ServerBootstrapper::new()
.with_db_path(db_path.to_str().unwrap())
.start()?;

let rest_client = server.rest_client_with_token(hash);

let actual_fund = rest_client.fund(&expected_fund.id.to_string())?;
assert_eq!(actual_fund,expected_fund);

// non existing
assert_eq!(rest_client.fund_raw("2")?.status(), StatusCode::NOT_FOUND);
// malformed index
assert_eq!(rest_client.fund_raw("a")?.status(), StatusCode::NOT_FOUND);
// overflow index
assert_eq!(rest_client.fund_raw("3147483647")?.status(), StatusCode::NOT_FOUND);

Ok(())
}

32 changes: 32 additions & 0 deletions vit-servicing-station-tests/src/tests/rest/genesis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

use crate::common::{data, startup::{db::DbBuilder, server::ServerBootstrapper}};
use assert_fs::TempDir;

use crate::common::paths::BLOCK0_BIN;

#[test]
pub fn genesis_deserialize_bijection() {
let temp_dir = TempDir::new().unwrap();
let (token, hash) = data::token();

let db_path = DbBuilder::new()
.with_token(token)
.build(&temp_dir)
.unwrap();

let server = ServerBootstrapper::new()
.with_db_path(db_path.to_str().unwrap())
.with_block0_path(BLOCK0_BIN)
.start()
.unwrap();

let expected = std::fs::read(BLOCK0_BIN).unwrap();


let genesis_as_bytes = server
.rest_client_with_token(hash)
.genesis()
.expect("cannot get genesis block bytes");

assert_eq!(expected,genesis_as_bytes);
}
2 changes: 2 additions & 0 deletions vit-servicing-station-tests/src/tests/rest/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod proposals;
pub mod genesis;
pub mod funds;
35 changes: 34 additions & 1 deletion vit-servicing-station-tests/src/tests/rest/proposals.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::common::startup::quick_start;
use crate::common::{data, startup::{db::DbBuilder, quick_start, server::ServerBootstrapper}};
use assert_fs::TempDir;
use reqwest::StatusCode;

#[test]
pub fn get_proposals_list_is_not_empty() {
Expand All @@ -11,3 +12,35 @@ pub fn get_proposals_list_is_not_empty() {
.expect("cannot get proposals");
assert!(proposals.len() > 0);
}


#[test]
pub fn get_proposal_by_id() -> Result<(), Box<dyn std::error::Error>>{
let temp_dir = TempDir::new().unwrap();
let expected_proposal = data::proposals().first().unwrap().clone();
let (token, hash) = data::token();

let db_path = DbBuilder::new()
.with_token(token)
.with_proposals(vec![expected_proposal.clone()])
.build(&temp_dir)?;

let server = ServerBootstrapper::new()
.with_db_path(db_path.to_str().unwrap())
.start()?;

let rest_client = server.rest_client_with_token(hash);

let actual_proposal = rest_client.proposal(&expected_proposal.proposal_id)?;
assert_eq!(actual_proposal,expected_proposal);

// non existing
assert_eq!(rest_client.proposal_raw("2")?.status(), StatusCode::NOT_FOUND);
// malformed index
assert_eq!(rest_client.proposal_raw("a")?.status(), StatusCode::NOT_FOUND);
// overflow index
assert_eq!(rest_client.proposal_raw("3147483647")?.status(), StatusCode::NOT_FOUND);

Ok(())
}

0 comments on commit f7d46e9

Please sign in to comment.