gitlab/api/projects/deploy_keys/
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 derive_builder::Builder;
8
9use crate::api::common::NameOrId;
10use crate::api::endpoint_prelude::*;
11
12/// Edit a new deploy key on project.
13#[derive(Debug, Builder, Clone)]
14#[builder(setter(strip_option))]
15pub struct EditDeployKey<'a> {
16    /// The project to open the deploy key on.
17    #[builder(setter(into))]
18    project: NameOrId<'a>,
19    /// The deploy key to edit.
20    deploy_key: u64,
21    /// The title of the deploy key
22    #[builder(setter(into), default)]
23    title: Option<Cow<'a, str>>,
24    /// Can this deploy key push to the project repository
25    #[builder(default)]
26    can_push: Option<bool>,
27}
28
29impl<'a> EditDeployKey<'a> {
30    /// Create a builder for the endpoint.
31    pub fn builder() -> EditDeployKeyBuilder<'a> {
32        EditDeployKeyBuilder::default()
33    }
34}
35
36impl Endpoint for EditDeployKey<'_> {
37    fn method(&self) -> Method {
38        Method::PUT
39    }
40
41    fn endpoint(&self) -> Cow<'static, str> {
42        format!("projects/{}/deploy_keys/{}", self.project, self.deploy_key).into()
43    }
44
45    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
46        let mut params = FormParams::default();
47
48        params
49            .push_opt("title", self.title.as_ref())
50            .push_opt("can_push", self.can_push);
51
52        params.into_body()
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use http::Method;
59
60    use crate::api::projects::deploy_keys::{EditDeployKey, EditDeployKeyBuilderError};
61    use crate::api::{self, Query};
62    use crate::test::client::{ExpectedUrl, SingleTestClient};
63
64    #[test]
65    fn project_and_deploy_key_are_necessary() {
66        let err = EditDeployKey::builder().build().unwrap_err();
67        crate::test::assert_missing_field!(err, EditDeployKeyBuilderError, "project");
68    }
69
70    #[test]
71    fn project_is_necessary() {
72        let err = EditDeployKey::builder().deploy_key(1).build().unwrap_err();
73        crate::test::assert_missing_field!(err, EditDeployKeyBuilderError, "project");
74    }
75
76    #[test]
77    fn deploy_key_is_necessary() {
78        let err = EditDeployKey::builder().project(1).build().unwrap_err();
79        crate::test::assert_missing_field!(err, EditDeployKeyBuilderError, "deploy_key");
80    }
81
82    #[test]
83    fn project_and_deploy_key_are_sufficient() {
84        EditDeployKey::builder()
85            .project(1)
86            .deploy_key(1)
87            .build()
88            .unwrap();
89    }
90
91    #[test]
92    fn endpoint() {
93        let endpoint = ExpectedUrl::builder()
94            .method(Method::PUT)
95            .endpoint("projects/simple%2Fproject/deploy_keys/1")
96            .content_type("application/x-www-form-urlencoded")
97            .body_str("")
98            .build()
99            .unwrap();
100        let client = SingleTestClient::new_raw(endpoint, "");
101
102        let endpoint = EditDeployKey::builder()
103            .project("simple/project")
104            .deploy_key(1)
105            .build()
106            .unwrap();
107        api::ignore(endpoint).query(&client).unwrap();
108    }
109
110    #[test]
111    fn endpoint_title() {
112        let endpoint = ExpectedUrl::builder()
113            .method(Method::PUT)
114            .endpoint("projects/simple%2Fproject/deploy_keys/1")
115            .content_type("application/x-www-form-urlencoded")
116            .body_str("title=title")
117            .build()
118            .unwrap();
119        let client = SingleTestClient::new_raw(endpoint, "");
120
121        let endpoint = EditDeployKey::builder()
122            .project("simple/project")
123            .deploy_key(1)
124            .title("title")
125            .build()
126            .unwrap();
127        api::ignore(endpoint).query(&client).unwrap();
128    }
129
130    #[test]
131    fn endpoint_can_push() {
132        let endpoint = ExpectedUrl::builder()
133            .method(Method::PUT)
134            .endpoint("projects/simple%2Fproject/deploy_keys/1")
135            .content_type("application/x-www-form-urlencoded")
136            .body_str("can_push=true")
137            .build()
138            .unwrap();
139        let client = SingleTestClient::new_raw(endpoint, "");
140
141        let endpoint = EditDeployKey::builder()
142            .project("simple/project")
143            .deploy_key(1)
144            .can_push(true)
145            .build()
146            .unwrap();
147        api::ignore(endpoint).query(&client).unwrap();
148    }
149}