use std::borrow::Cow;
use derive_builder::Builder;
use crate::api::common::NameOrId;
use crate::api::endpoint_prelude::*;
#[derive(Debug, Clone, Builder)]
pub struct EditCustomAttribute<'a> {
#[builder(setter(into))]
user: NameOrId<'a>,
#[builder(setter(into))]
key: Cow<'a, str>,
#[builder(setter(into))]
value: Cow<'a, str>,
}
impl<'a> EditCustomAttribute<'a> {
pub fn builder() -> EditCustomAttributeBuilder<'a> {
EditCustomAttributeBuilder::default()
}
}
impl<'a> Endpoint for EditCustomAttribute<'a> {
fn method(&self) -> Method {
Method::PUT
}
fn endpoint(&self) -> Cow<'static, str> {
format!("users/{}/custom_attributes/{}", self.user, self.key).into()
}
fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
let mut params = FormParams::default();
params.push("value", &self.value);
params.into_body()
}
}
#[cfg(test)]
mod tests {
use crate::api::users::custom_attributes::{
EditCustomAttribute, EditCustomAttributeBuilderError,
};
use crate::api::{self, Query};
use crate::test::client::{ExpectedUrl, SingleTestClient};
use http::Method;
#[test]
fn user_is_needed() {
let err = EditCustomAttribute::builder()
.key("test")
.value("value")
.build()
.unwrap_err();
crate::test::assert_missing_field!(err, EditCustomAttributeBuilderError, "user");
}
#[test]
fn key_is_needed() {
let err = EditCustomAttribute::builder()
.user(1)
.value("value")
.build()
.unwrap_err();
crate::test::assert_missing_field!(err, EditCustomAttributeBuilderError, "key");
}
#[test]
fn value_is_needed() {
let err = EditCustomAttribute::builder()
.user(1)
.key("test")
.build()
.unwrap_err();
crate::test::assert_missing_field!(err, EditCustomAttributeBuilderError, "value");
}
#[test]
fn user_key_value_are_sufficient() {
EditCustomAttribute::builder()
.user(1)
.key("test")
.value("value")
.build()
.unwrap();
}
#[test]
fn endpoint() {
let endpoint = ExpectedUrl::builder()
.method(Method::PUT)
.endpoint("users/user/custom_attributes/test")
.content_type("application/x-www-form-urlencoded")
.body_str("value=value")
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = EditCustomAttribute::builder()
.user("user")
.key("test")
.value("value")
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
}