use serde::{Deserialize, Serialize};
use crate::clients::RestClient;
use crate::rest::{ReadOnlyResource, ResourceError, ResourceResponse, RestResource, ResourceOperation, ResourcePath};
use crate::HttpMethod;
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct AccessScope {
#[serde(skip_serializing)]
pub handle: Option<String>,
}
impl AccessScope {
pub async fn all(client: &RestClient) -> Result<ResourceResponse<Vec<Self>>, ResourceError> {
let url = "oauth/access_scopes";
let response = client.get(url, None).await?;
if !response.is_ok() {
return Err(ResourceError::from_http_response(
response.code,
&response.body,
Self::NAME,
None,
response.request_id(),
));
}
let key = Self::PLURAL;
ResourceResponse::from_http_response(response, key)
}
}
impl RestResource for AccessScope {
type Id = String;
type FindParams = ();
type AllParams = ();
type CountParams = ();
const NAME: &'static str = "AccessScope";
const PLURAL: &'static str = "access_scopes";
const PATHS: &'static [ResourcePath] = &[
ResourcePath::new(
HttpMethod::Get,
ResourceOperation::All,
&[],
"oauth/access_scopes",
),
];
fn get_id(&self) -> Option<Self::Id> {
self.handle.clone()
}
}
impl ReadOnlyResource for AccessScope {}
#[cfg(test)]
mod tests {
use super::*;
use crate::rest::{get_path, ReadOnlyResource, ResourceOperation, RestResource};
#[test]
fn test_access_scope_implements_read_only_resource() {
fn assert_read_only<T: ReadOnlyResource>() {}
assert_read_only::<AccessScope>();
}
#[test]
fn test_access_scope_deserialization() {
let json = r#"{
"handle": "read_products"
}"#;
let scope: AccessScope = serde_json::from_str(json).unwrap();
assert_eq!(scope.handle, Some("read_products".to_string()));
}
#[test]
fn test_access_scope_list_deserialization() {
let json = r#"[
{"handle": "read_products"},
{"handle": "write_products"},
{"handle": "read_orders"}
]"#;
let scopes: Vec<AccessScope> = serde_json::from_str(json).unwrap();
assert_eq!(scopes.len(), 3);
assert_eq!(scopes[0].handle, Some("read_products".to_string()));
assert_eq!(scopes[1].handle, Some("write_products".to_string()));
assert_eq!(scopes[2].handle, Some("read_orders".to_string()));
}
#[test]
fn test_access_scope_special_oauth_path() {
let all_path = get_path(AccessScope::PATHS, ResourceOperation::All, &[]);
assert!(all_path.is_some());
assert_eq!(all_path.unwrap().template, "oauth/access_scopes");
}
#[test]
fn test_access_scope_list_only_no_other_operations() {
let find_path = get_path(AccessScope::PATHS, ResourceOperation::Find, &["id"]);
assert!(find_path.is_none());
let count_path = get_path(AccessScope::PATHS, ResourceOperation::Count, &[]);
assert!(count_path.is_none());
let create_path = get_path(AccessScope::PATHS, ResourceOperation::Create, &[]);
assert!(create_path.is_none());
let update_path = get_path(AccessScope::PATHS, ResourceOperation::Update, &["id"]);
assert!(update_path.is_none());
let delete_path = get_path(AccessScope::PATHS, ResourceOperation::Delete, &["id"]);
assert!(delete_path.is_none());
}
#[test]
fn test_access_scope_constants() {
assert_eq!(AccessScope::NAME, "AccessScope");
assert_eq!(AccessScope::PLURAL, "access_scopes");
}
#[test]
fn test_access_scope_get_id_returns_handle() {
let scope_with_handle = AccessScope {
handle: Some("read_products".to_string()),
};
assert_eq!(scope_with_handle.get_id(), Some("read_products".to_string()));
let scope_without_handle = AccessScope::default();
assert_eq!(scope_without_handle.get_id(), None);
}
#[test]
fn test_access_scope_all_fields_are_read_only() {
let scope = AccessScope {
handle: Some("write_orders".to_string()),
};
let json = serde_json::to_value(&scope).unwrap();
assert_eq!(json, serde_json::json!({}));
}
}