Skip to content

Commit

Permalink
Support for listing permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
mkuratczyk committed Jun 7, 2023
1 parent 044c6b0 commit f03b996
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,46 @@ impl<'a> Client<'a> {
Ok(())
}

pub fn list_permissions(&self) -> Result<Vec<responses::Permissions>> {
let response = self.http_get("permissions")?;
let response2 = self.ok_or_status_code_error(response)?;
response2
.json::<Vec<responses::Permissions>>()
.map_err(Error::from)
}

pub fn list_permissions_in(&self, vhost: &str) -> Result<Vec<responses::Permissions>> {
let response = self.http_get(&format!(
"vhosts/{}/permissions",
self.percent_encode(vhost)
))?;
let response2 = self.ok_or_status_code_error(response)?;
response2
.json::<Vec<responses::Permissions>>()
.map_err(Error::from)
}

pub fn list_permissions_of(&self, user: &str) -> Result<Vec<responses::Permissions>> {
let response =
self.http_get(&format!("users/{}/permissions", self.percent_encode(user)))?;
let response2 = self.ok_or_status_code_error(response)?;
response2
.json::<Vec<responses::Permissions>>()
.map_err(Error::from)
}

pub fn get_permissions(&self, vhost: &str, user: &str) -> Result<responses::Permissions> {
let response = self.http_get(&format!(
"permissions/{}/{}",
self.percent_encode(vhost),
self.percent_encode(user)
))?;
let response2 = self.ok_or_status_code_error(response)?;
response2
.json::<responses::Permissions>()
.map_err(Error::from)
}

//
// Implementation
//
Expand Down
9 changes: 9 additions & 0 deletions src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,12 @@ pub struct PolicyParams<'a> {
pub priority: i32,
pub definition: PolicyDefinition,
}

#[derive(Serialize)]
pub struct Permissions<'a> {
pub user: &'a str,
pub vhost: &'a str,
pub configure: &'a str,
pub read: &'a str,
pub write: &'a str,
}
10 changes: 10 additions & 0 deletions src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,13 @@ pub struct Policy {
pub priority: i16,
pub definition: PolicyDefinition,
}

#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
#[allow(dead_code)]
pub struct Permissions {
pub user: String,
pub vhost: String,
pub configure: String,
pub read: String,
pub write: String,
}
140 changes: 140 additions & 0 deletions tests/permissions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
use rabbitmq_http_client::blocking::Client;
use rabbitmq_http_client::requests::VirtualHostParams;
use rabbitmq_http_client::responses;

mod common;
use crate::common::{endpoint, PASSWORD, USERNAME};

#[test]
fn test_list_permissions() {
let endpoint = endpoint();
let rc = Client::new_with_basic_auth_credentials(&endpoint, USERNAME, PASSWORD);

let vh_params = VirtualHostParams {
name: "test_list_permissions",
tracing: false,
description: None,
tags: None,
default_queue_type: None,
};
let _ = rc.delete_vhost(vh_params.name);
let result1 = rc.create_vhost(&vh_params);
assert!(result1.is_ok());

let result = rc.list_permissions();
assert!(result.is_ok());

let vec = result.unwrap();
assert!(vec.iter().any(|p| p
== &responses::Permissions {
user: "guest".to_owned(),
vhost: "test_list_permissions".to_owned(),
configure: ".*".to_owned(),
read: ".*".to_owned(),
write: ".*".to_owned(),
}));

let _ = rc.delete_vhost(vh_params.name);
}

#[test]
fn test_list_permissions_in() {
let endpoint = endpoint();
let rc = Client::new_with_basic_auth_credentials(&endpoint, USERNAME, PASSWORD);

let vh_params = VirtualHostParams {
name: "test_list_permissions_in",
tracing: false,
description: None,
tags: None,
default_queue_type: None,
};
let _ = rc.delete_vhost(vh_params.name);
let result1 = rc.create_vhost(&vh_params);
assert!(result1.is_ok());

let result = rc.list_permissions_in("test_list_permissions_in");
assert!(result.is_ok(), "list_permissions_in returned {:?}", result);

let vec = result.unwrap();
assert!(vec.iter().any(|p| p
== &responses::Permissions {
user: "guest".to_owned(),
vhost: "test_list_permissions_in".to_owned(),
configure: ".*".to_owned(),
read: ".*".to_owned(),
write: ".*".to_owned(),
}));

let _ = rc.delete_vhost(vh_params.name);
}

#[test]
fn test_list_permissions_of() {
let endpoint = endpoint();
let rc = Client::new_with_basic_auth_credentials(&endpoint, USERNAME, PASSWORD);

let vh_params = VirtualHostParams {
name: "test_list_permissions_of",
tracing: false,
description: None,
tags: None,
default_queue_type: None,
};
let _ = rc.delete_vhost(vh_params.name);
let result1 = rc.create_vhost(&vh_params);
assert!(result1.is_ok());

let result = rc.list_permissions_of("guest");
assert!(result.is_ok(), "list_permissions_of returned {:?}", result);

let vec = result.unwrap();
assert!(vec.iter().any(|p| p
== &responses::Permissions {
user: "guest".to_owned(),
vhost: "test_list_permissions_of".to_owned(),
configure: ".*".to_owned(),
read: ".*".to_owned(),
write: ".*".to_owned(),
}));

let _ = rc.delete_vhost(vh_params.name);
}

#[test]
fn test_get_permissions() {
let endpoint = endpoint();
let rc = Client::new_with_basic_auth_credentials(&endpoint, USERNAME, PASSWORD);

let vh_params = VirtualHostParams {
name: "test_get_permissions",
tracing: false,
description: None,
tags: None,
default_queue_type: None,
};
let _ = rc.delete_vhost(vh_params.name);
let result1 = rc.create_vhost(&vh_params);
assert!(result1.is_ok());

let result2 = rc.get_permissions("test_get_permissions", "guest");
assert!(
result2.is_ok(),
"list_permissions_of returned {:?}",
result2
);

let result3 = result2.unwrap();
assert_eq!(
result3,
responses::Permissions {
user: "guest".to_owned(),
vhost: "test_get_permissions".to_owned(),
configure: ".*".to_owned(),
read: ".*".to_owned(),
write: ".*".to_owned(),
}
);

let _ = rc.delete_vhost(vh_params.name);
}

0 comments on commit f03b996

Please sign in to comment.