Skip to content

Commit

Permalink
tests: [torrust#107] E2E tests for API entrypoint and about routes
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Apr 21, 2023
1 parent abb4e91 commit 5678e4d
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 32 deletions.
4 changes: 2 additions & 2 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Build and run locally:

```s
docker context use default
export TORRUST_IDX_BACK_USER_UID=1000
export TORRUST_IDX_BACK_USER_UID=$(id -u)
./docker/bin/build.sh $TORRUST_IDX_BACK_USER_UID
./bin/install.sh
./docker/bin/run.sh $TORRUST_IDX_BACK_USER_UID
Expand All @@ -30,7 +30,7 @@ export TORRUST_IDX_BACK_USER_UID=1000
Run using the pre-built public docker image:

```s
export TORRUST_IDX_BACK_USER_UID=1000
export TORRUST_IDX_BACK_USER_UID=$(id -u)
docker run -it \
--user="$TORRUST_IDX_BACK_USER_UID" \
--publish 3000:3000/tcp \
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/asserts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::e2e::response::Response;

pub fn assert_response_title(response: &Response, title: &str) {
let title_element = format!("<title>{title}</title>");

assert!(
response.body.contains(&title),
":\n response does not contain the title element: `\"{title_element}\"`."
);
}

pub fn assert_ok(response: &Response) {
assert_eq!(response.status, 200);
assert_eq!(response.content_type, "text/html; charset=utf-8");
}
12 changes: 5 additions & 7 deletions tests/e2e/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use reqwest::Response;
use reqwest::Response as ReqwestResponse;

use crate::e2e::connection_info::ConnectionInfo;
use crate::e2e::http::{Query, ReqwestQuery};
use crate::e2e::response::Response;

/// API Client
pub struct Client {
Expand All @@ -17,10 +18,6 @@ impl Client {
}
}

pub async fn entrypoint(&self) -> Response {
self.get("", Query::default()).await
}

pub async fn get(&self, path: &str, params: Query) -> Response {
self.get_request_with_query(path, params).await
}
Expand Down Expand Up @@ -53,7 +50,7 @@ impl Client {
}

async fn get(path: &str, query: Option<Query>) -> Response {
match query {
let response: ReqwestResponse = match query {
Some(params) => reqwest::Client::builder()
.build()
.unwrap()
Expand All @@ -63,5 +60,6 @@ async fn get(path: &str, query: Option<Query>) -> Response {
.await
.unwrap(),
None => reqwest::Client::builder().build().unwrap().get(path).send().await.unwrap(),
}
};
Response::from(response).await
}
21 changes: 0 additions & 21 deletions tests/e2e/contexts/about.rs

This file was deleted.

19 changes: 17 additions & 2 deletions tests/e2e/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
//! End-to-end tests.
//!
//! Execute E2E tests with:
//!
//! ```
//! cargo test --features e2e-tests
//! ```
//! cargo test -- --ignored
//!
//! or the Cargo alias
//!
//! ```
//! cargo e2e
//! ```
//!
//! > **NOTICE**: E2E tests are not executed by default, because they require
//! a running instance of the API.
//!
//! See the docker documentation for more information on how to run the API.
mod asserts;
mod client;
mod connection_info;
mod contexts;
mod http;
mod response;
mod routes;
17 changes: 17 additions & 0 deletions tests/e2e/response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use reqwest::Response as ReqwestResponse;

pub struct Response {
pub status: u16,
pub content_type: String,
pub body: String,
}

impl Response {
pub async fn from(response: ReqwestResponse) -> Self {
Self {
status: response.status().as_u16(),
content_type: response.headers().get("content-type").unwrap().to_str().unwrap().to_owned(),
body: response.text().await.unwrap(),
}
}
}
26 changes: 26 additions & 0 deletions tests/e2e/routes/about.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::e2e::asserts::{assert_ok, assert_response_title};
use crate::e2e::client::Client;
use crate::e2e::connection_info::connection_with_no_token;
use crate::e2e::http::Query;

#[tokio::test]
#[cfg_attr(not(feature = "e2e-tests"), ignore)]
async fn it_should_load_the_about_page_with_information_about_the_api() {
let client = Client::new(connection_with_no_token("localhost:3000"));

let response = client.get("about", Query::empty()).await;

assert_ok(&response);
assert_response_title(&response, "About");
}

#[tokio::test]
#[cfg_attr(not(feature = "e2e-tests"), ignore)]
async fn it_should_load_the_license_page_at_the_api_entrypoint() {
let client = Client::new(connection_with_no_token("localhost:3000"));

let response = client.get("about/license", Query::empty()).await;

assert_ok(&response);
assert_response_title(&response, "Licensing");
}
1 change: 1 addition & 0 deletions tests/e2e/contexts/mod.rs → tests/e2e/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod about;
pub mod root;
15 changes: 15 additions & 0 deletions tests/e2e/routes/root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::e2e::asserts::{assert_ok, assert_response_title};
use crate::e2e::client::Client;
use crate::e2e::connection_info::connection_with_no_token;
use crate::e2e::http::Query;

#[tokio::test]
#[cfg_attr(not(feature = "e2e-tests"), ignore)]
async fn it_should_load_the_about_page_at_the_api_entrypoint() {
let client = Client::new(connection_with_no_token("localhost:3000"));

let response = client.get("", Query::empty()).await;

assert_ok(&response);
assert_response_title(&response, "About");
}

0 comments on commit 5678e4d

Please sign in to comment.