gitlab 0.1810.0

Gitlab API client.
Documentation
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use derive_builder::Builder;

use crate::api::endpoint_prelude::*;

/// Delete a user from an instance.
#[derive(Debug, Builder, Clone)]
#[builder(setter(strip_option))]
pub struct DeleteUser {
    /// The user to delete.
    id: u64,
    /// Whether to delete resources owned solely by this user instead of moving them to a ghost user.
    #[builder(default)]
    hard_delete: Option<bool>,
}

impl DeleteUser {
    /// Create a builder for the endpoint.
    pub fn builder() -> DeleteUserBuilder {
        DeleteUserBuilder::default()
    }
}

impl Endpoint for DeleteUser {
    fn method(&self) -> Method {
        Method::DELETE
    }

    fn endpoint(&self) -> Cow<'static, str> {
        format!("users/{}", self.id).into()
    }

    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
        let mut params = FormParams::default();

        params.push_opt("hard_delete", self.hard_delete);

        params.into_body()
    }
}

#[cfg(test)]
mod tests {
    use http::Method;

    use crate::api::users::delete::{DeleteUser, DeleteUserBuilderError};
    use crate::api::{self, Query};
    use crate::test::client::{ExpectedUrl, SingleTestClient};

    #[test]
    fn id_is_necessary() {
        let err = DeleteUser::builder().build().unwrap_err();
        crate::test::assert_missing_field!(err, DeleteUserBuilderError, "id");
    }

    #[test]
    fn sufficient_parameters() {
        DeleteUser::builder().id(1).build().unwrap();
    }

    #[test]
    fn endpoint() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::DELETE)
            .endpoint("users/1")
            .content_type("application/x-www-form-urlencoded")
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = DeleteUser::builder().id(1).build().unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }

    #[test]
    fn endpoint_hard_delete() {
        let endpoint = ExpectedUrl::builder()
            .method(Method::DELETE)
            .endpoint("users/1")
            .content_type("application/x-www-form-urlencoded")
            .body_str("hard_delete=true")
            .build()
            .unwrap();
        let client = SingleTestClient::new_raw(endpoint, "");

        let endpoint = DeleteUser::builder()
            .id(1)
            .hard_delete(true)
            .build()
            .unwrap();
        api::ignore(endpoint).query(&client).unwrap();
    }
}