|
| 1 | +//! API contract for `tag` context. |
| 2 | +use torrust_index_backend::web::api; |
| 3 | + |
| 4 | +use crate::common::asserts::assert_json_ok; |
| 5 | +use crate::common::client::Client; |
| 6 | +use crate::common::contexts::tag::fixtures::random_tag_name; |
| 7 | +use crate::common::contexts::tag::forms::{AddTagForm, DeleteTagForm}; |
| 8 | +use crate::common::contexts::tag::responses::{AddedTagResponse, DeletedTagResponse, ListResponse}; |
| 9 | +use crate::e2e::contexts::tag::steps::{add_random_tag, add_tag}; |
| 10 | +use crate::e2e::contexts::user::steps::{new_logged_in_admin, new_logged_in_user}; |
| 11 | +use crate::e2e::environment::TestEnv; |
| 12 | + |
| 13 | +#[tokio::test] |
| 14 | +async fn it_should_return_an_empty_tag_list_when_there_are_no_tags() { |
| 15 | + let mut env = TestEnv::new(); |
| 16 | + env.start(api::Implementation::ActixWeb).await; |
| 17 | + let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); |
| 18 | + |
| 19 | + let response = client.get_tags().await; |
| 20 | + |
| 21 | + assert_json_ok(&response); |
| 22 | +} |
| 23 | + |
| 24 | +#[tokio::test] |
| 25 | +async fn it_should_return_a_tag_list() { |
| 26 | + let mut env = TestEnv::new(); |
| 27 | + env.start(api::Implementation::ActixWeb).await; |
| 28 | + let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); |
| 29 | + |
| 30 | + // Add a tag |
| 31 | + let tag_name = random_tag_name(); |
| 32 | + let response = add_tag(&tag_name, &env).await; |
| 33 | + assert_eq!(response.status, 200); |
| 34 | + |
| 35 | + let response = client.get_tags().await; |
| 36 | + |
| 37 | + let res: ListResponse = serde_json::from_str(&response.body).unwrap(); |
| 38 | + |
| 39 | + // There should be at least the tag we added. |
| 40 | + // Since this is an E2E test that could be executed in a shred env, |
| 41 | + // there might be more tags. |
| 42 | + assert!(!res.data.is_empty()); |
| 43 | + if let Some(content_type) = &response.content_type { |
| 44 | + assert_eq!(content_type, "application/json"); |
| 45 | + } |
| 46 | + assert_eq!(response.status, 200); |
| 47 | +} |
| 48 | + |
| 49 | +#[tokio::test] |
| 50 | +async fn it_should_not_allow_adding_a_new_tag_to_unauthenticated_users() { |
| 51 | + let mut env = TestEnv::new(); |
| 52 | + env.start(api::Implementation::ActixWeb).await; |
| 53 | + let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); |
| 54 | + |
| 55 | + let response = client |
| 56 | + .add_tag(AddTagForm { |
| 57 | + name: "TAG NAME".to_string(), |
| 58 | + }) |
| 59 | + .await; |
| 60 | + |
| 61 | + assert_eq!(response.status, 401); |
| 62 | +} |
| 63 | + |
| 64 | +#[tokio::test] |
| 65 | +async fn it_should_not_allow_adding_a_new_tag_to_non_admins() { |
| 66 | + let mut env = TestEnv::new(); |
| 67 | + env.start(api::Implementation::ActixWeb).await; |
| 68 | + |
| 69 | + let logged_non_admin = new_logged_in_user(&env).await; |
| 70 | + |
| 71 | + let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_non_admin.token); |
| 72 | + |
| 73 | + let response = client |
| 74 | + .add_tag(AddTagForm { |
| 75 | + name: "TAG NAME".to_string(), |
| 76 | + }) |
| 77 | + .await; |
| 78 | + |
| 79 | + assert_eq!(response.status, 403); |
| 80 | +} |
| 81 | + |
| 82 | +#[tokio::test] |
| 83 | +async fn it_should_allow_admins_to_add_new_tags() { |
| 84 | + let mut env = TestEnv::new(); |
| 85 | + env.start(api::Implementation::ActixWeb).await; |
| 86 | + |
| 87 | + let logged_in_admin = new_logged_in_admin(&env).await; |
| 88 | + let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token); |
| 89 | + |
| 90 | + let tag_name = random_tag_name(); |
| 91 | + |
| 92 | + let response = client |
| 93 | + .add_tag(AddTagForm { |
| 94 | + name: tag_name.to_string(), |
| 95 | + }) |
| 96 | + .await; |
| 97 | + |
| 98 | + let res: AddedTagResponse = serde_json::from_str(&response.body).unwrap(); |
| 99 | + |
| 100 | + assert_eq!(res.data, tag_name); |
| 101 | + if let Some(content_type) = &response.content_type { |
| 102 | + assert_eq!(content_type, "application/json"); |
| 103 | + } |
| 104 | + assert_eq!(response.status, 200); |
| 105 | +} |
| 106 | + |
| 107 | +#[tokio::test] |
| 108 | +async fn it_should_allow_adding_duplicated_tags() { |
| 109 | + // code-review: is this an intended behavior? |
| 110 | + |
| 111 | + let mut env = TestEnv::new(); |
| 112 | + env.start(api::Implementation::ActixWeb).await; |
| 113 | + |
| 114 | + // Add a tag |
| 115 | + let random_tag_name = random_tag_name(); |
| 116 | + let response = add_tag(&random_tag_name, &env).await; |
| 117 | + assert_eq!(response.status, 200); |
| 118 | + |
| 119 | + // Try to add the same tag again |
| 120 | + let response = add_tag(&random_tag_name, &env).await; |
| 121 | + assert_eq!(response.status, 200); |
| 122 | +} |
| 123 | + |
| 124 | +#[tokio::test] |
| 125 | +async fn it_should_allow_adding_a_tag_with_an_empty_name() { |
| 126 | + // code-review: is this an intended behavior? |
| 127 | + |
| 128 | + let mut env = TestEnv::new(); |
| 129 | + env.start(api::Implementation::ActixWeb).await; |
| 130 | + |
| 131 | + let empty_tag_name = String::new(); |
| 132 | + let response = add_tag(&empty_tag_name, &env).await; |
| 133 | + assert_eq!(response.status, 200); |
| 134 | +} |
| 135 | + |
| 136 | +#[tokio::test] |
| 137 | +async fn it_should_allow_admins_to_delete_tags() { |
| 138 | + let mut env = TestEnv::new(); |
| 139 | + env.start(api::Implementation::ActixWeb).await; |
| 140 | + |
| 141 | + let logged_in_admin = new_logged_in_admin(&env).await; |
| 142 | + let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token); |
| 143 | + |
| 144 | + let (tag_id, _tag_name) = add_random_tag(&env).await; |
| 145 | + |
| 146 | + let response = client.delete_tag(DeleteTagForm { tag_id }).await; |
| 147 | + |
| 148 | + let res: DeletedTagResponse = serde_json::from_str(&response.body).unwrap(); |
| 149 | + |
| 150 | + assert_eq!(res.data, tag_id); |
| 151 | + if let Some(content_type) = &response.content_type { |
| 152 | + assert_eq!(content_type, "application/json"); |
| 153 | + } |
| 154 | + assert_eq!(response.status, 200); |
| 155 | +} |
| 156 | + |
| 157 | +#[tokio::test] |
| 158 | +async fn it_should_not_allow_non_admins_to_delete_tags() { |
| 159 | + let mut env = TestEnv::new(); |
| 160 | + env.start(api::Implementation::ActixWeb).await; |
| 161 | + |
| 162 | + let logged_in_non_admin = new_logged_in_user(&env).await; |
| 163 | + let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_non_admin.token); |
| 164 | + |
| 165 | + let (tag_id, _tag_name) = add_random_tag(&env).await; |
| 166 | + |
| 167 | + let response = client.delete_tag(DeleteTagForm { tag_id }).await; |
| 168 | + |
| 169 | + assert_eq!(response.status, 403); |
| 170 | +} |
| 171 | + |
| 172 | +#[tokio::test] |
| 173 | +async fn it_should_not_allow_guests_to_delete_tags() { |
| 174 | + let mut env = TestEnv::new(); |
| 175 | + env.start(api::Implementation::ActixWeb).await; |
| 176 | + let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); |
| 177 | + |
| 178 | + let (tag_id, _tag_name) = add_random_tag(&env).await; |
| 179 | + |
| 180 | + let response = client.delete_tag(DeleteTagForm { tag_id }).await; |
| 181 | + |
| 182 | + assert_eq!(response.status, 401); |
| 183 | +} |
0 commit comments