Skip to main content

gitlab/api/users/custom_attributes/
edit.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use std::borrow::Cow;
8
9use derive_builder::Builder;
10
11use crate::api::common::NameOrId;
12use crate::api::endpoint_prelude::*;
13
14/// Edit a custom attribute for a user.
15///
16/// Creates or updates a custom attribute for the specified user.
17#[derive(Debug, Clone, Builder)]
18pub struct EditCustomAttribute<'a> {
19    /// The ID of the user.
20    #[builder(setter(into))]
21    user: NameOrId<'a>,
22    /// The key of the custom attribute.
23    #[builder(setter(into))]
24    key: Cow<'a, str>,
25    /// The value of the custom attribute.
26    #[builder(setter(into))]
27    value: Cow<'a, str>,
28}
29
30impl<'a> EditCustomAttribute<'a> {
31    /// Create a builder for the endpoint.
32    pub fn builder() -> EditCustomAttributeBuilder<'a> {
33        EditCustomAttributeBuilder::default()
34    }
35}
36
37impl<'a> Endpoint for EditCustomAttribute<'a> {
38    fn method(&self) -> Method {
39        Method::PUT
40    }
41
42    fn endpoint(&self) -> Cow<'static, str> {
43        format!("users/{}/custom_attributes/{}", self.user, self.key).into()
44    }
45
46    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
47        let mut params = FormParams::default();
48        params.push("value", &self.value);
49        params.into_body()
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use crate::api::users::custom_attributes::{
56        EditCustomAttribute, EditCustomAttributeBuilderError,
57    };
58    use crate::api::{self, Query};
59    use crate::test::client::{ExpectedUrl, SingleTestClient};
60    use http::Method;
61
62    #[test]
63    fn user_is_needed() {
64        let err = EditCustomAttribute::builder()
65            .key("test")
66            .value("value")
67            .build()
68            .unwrap_err();
69        crate::test::assert_missing_field!(err, EditCustomAttributeBuilderError, "user");
70    }
71
72    #[test]
73    fn key_is_needed() {
74        let err = EditCustomAttribute::builder()
75            .user(1)
76            .value("value")
77            .build()
78            .unwrap_err();
79        crate::test::assert_missing_field!(err, EditCustomAttributeBuilderError, "key");
80    }
81
82    #[test]
83    fn value_is_needed() {
84        let err = EditCustomAttribute::builder()
85            .user(1)
86            .key("test")
87            .build()
88            .unwrap_err();
89        crate::test::assert_missing_field!(err, EditCustomAttributeBuilderError, "value");
90    }
91
92    #[test]
93    fn user_key_value_are_sufficient() {
94        EditCustomAttribute::builder()
95            .user(1)
96            .key("test")
97            .value("value")
98            .build()
99            .unwrap();
100    }
101
102    #[test]
103    fn endpoint() {
104        let endpoint = ExpectedUrl::builder()
105            .method(Method::PUT)
106            .endpoint("users/user/custom_attributes/test")
107            .content_type("application/x-www-form-urlencoded")
108            .body_str("value=value")
109            .build()
110            .unwrap();
111        let client = SingleTestClient::new_raw(endpoint, "");
112
113        let endpoint = EditCustomAttribute::builder()
114            .user("user")
115            .key("test")
116            .value("value")
117            .build()
118            .unwrap();
119        api::ignore(endpoint).query(&client).unwrap();
120    }
121}