Skip to content

Commit

Permalink
tests: [#109] E2E test for category routes
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Apr 21, 2023
1 parent 652f50b commit 73a26ae
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
11 changes: 10 additions & 1 deletion tests/e2e/asserts.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::e2e::response::Response;

// Text responses

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

assert!(
response.body.contains(&title),
response.body.contains(title),
":\n response does not contain the title element: `\"{title_element}\"`."
);
}
Expand All @@ -14,6 +16,13 @@ pub fn assert_text_ok(response: &Response) {
assert_eq!(response.content_type, "text/html; charset=utf-8");
}

pub fn _assert_text_bad_request(response: &Response) {
assert_eq!(response.status, 400);
assert_eq!(response.content_type, "text/plain; charset=utf-8");
}

// JSON responses

pub fn assert_json_ok(response: &Response) {
assert_eq!(response.status, 200);
assert_eq!(response.content_type, "application/json");
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ impl Client {

/*
pub async fn post(&self, path: &str) -> Response {
reqwest::Client::new().post(self.base_url(path).clone()).send().await.unwrap()
let response = reqwest::Client::new().post(self.base_url(path).clone()).send().await.unwrap();
Response::from(response).await
}
async fn delete(&self, path: &str) -> Response {
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct TestEnv {
}

impl TestEnv {
pub fn guess_client(&self) -> Client {
pub fn unauthenticated_client(&self) -> Client {
Client::new(anonymous_connection(&self.authority))
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/routes/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::e2e::env::TestEnv;
#[tokio::test]
#[cfg_attr(not(feature = "e2e-tests"), ignore)]
async fn it_should_load_the_about_page_with_information_about_the_api() {
let client = TestEnv::default().guess_client();
let client = TestEnv::default().unauthenticated_client();

let response = client.about().await;

Expand All @@ -15,7 +15,7 @@ async fn it_should_load_the_about_page_with_information_about_the_api() {
#[tokio::test]
#[cfg_attr(not(feature = "e2e-tests"), ignore)]
async fn it_should_load_the_license_page_at_the_api_entrypoint() {
let client = TestEnv::default().guess_client();
let client = TestEnv::default().unauthenticated_client();

let response = client.license().await;

Expand Down
21 changes: 21 additions & 0 deletions tests/e2e/routes/category.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::e2e::asserts::assert_json_ok;
use crate::e2e::env::TestEnv;
use crate::e2e::http::Query;

#[tokio::test]
#[cfg_attr(not(feature = "e2e-tests"), ignore)]
async fn it_should_return_an_empty_category_list_when_there_are_no_categories() {
let client = TestEnv::default().unauthenticated_client();

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

assert_json_ok(&response);
}

/* todo:
- it_should_not_allow_adding_a_new_category_to_unauthenticated_clients
- it should allow adding a new category to authenticated clients
- it should not allow adding a new category with an empty name
- it should not allow adding a new category with an empty icon
- ...
*/
2 changes: 1 addition & 1 deletion tests/e2e/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod about;

pub mod category;
pub mod root;
2 changes: 1 addition & 1 deletion tests/e2e/routes/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::e2e::env::TestEnv;
#[tokio::test]
#[cfg_attr(not(feature = "e2e-tests"), ignore)]
async fn it_should_load_the_about_page_at_the_api_entrypoint() {
let client = TestEnv::default().guess_client();
let client = TestEnv::default().unauthenticated_client();

let response = client.root().await;

Expand Down

0 comments on commit 73a26ae

Please sign in to comment.