gitlab/api/projects/job_token_scopes/
allow_project.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/// Add a project to the 'CI/CD job token inbound allowlist' of a project.
13#[derive(Debug, Builder, Clone)]
14pub struct AllowJobTokenProject<'a> {
15    /// The ID or URL-encoded path of the project.
16    #[builder(setter(into))]
17    project: NameOrId<'a>,
18
19    /// The ID of the project added to the 'CI/CD job token inbound allowlist'.
20    target_project_id: u64,
21}
22
23impl<'a> AllowJobTokenProject<'a> {
24    /// Create a builder for the endpoint.
25    pub fn builder() -> AllowJobTokenProjectBuilder<'a> {
26        AllowJobTokenProjectBuilder::default()
27    }
28}
29
30impl Endpoint for AllowJobTokenProject<'_> {
31    fn method(&self) -> Method {
32        Method::POST
33    }
34
35    fn endpoint(&self) -> Cow<'static, str> {
36        format!("projects/{}/job_token_scope/allowlist", self.project).into()
37    }
38
39    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
40        let mut params = FormParams::default();
41
42        params.push("target_project_id", self.target_project_id);
43
44        params.into_body()
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use http::Method;
51
52    use crate::api::projects::job_token_scopes::{
53        AllowJobTokenProject, AllowJobTokenProjectBuilderError,
54    };
55    use crate::api::{self, Query};
56    use crate::test::client::{ExpectedUrl, SingleTestClient};
57
58    #[test]
59    fn project_is_required() {
60        let err = AllowJobTokenProject::builder().build().unwrap_err();
61        crate::test::assert_missing_field!(err, AllowJobTokenProjectBuilderError, "project");
62    }
63
64    #[test]
65    fn target_is_required() {
66        let err = AllowJobTokenProject::builder()
67            .project("blah")
68            .build()
69            .unwrap_err();
70        crate::test::assert_missing_field!(
71            err,
72            AllowJobTokenProjectBuilderError,
73            "target_project_id",
74        );
75    }
76
77    #[test]
78    fn project_and_target_is_sufficient() {
79        AllowJobTokenProject::builder()
80            .project("blah")
81            .target_project_id(42)
82            .build()
83            .unwrap();
84    }
85
86    #[test]
87    fn endpoint() {
88        let endpoint = ExpectedUrl::builder()
89            .method(Method::POST)
90            .endpoint("projects/blah/job_token_scope/allowlist")
91            .content_type("application/x-www-form-urlencoded")
92            .body_str("target_project_id=42")
93            .build()
94            .unwrap();
95        let client = SingleTestClient::new_raw(endpoint, "");
96
97        let endpoint = AllowJobTokenProject::builder()
98            .project("blah")
99            .target_project_id(42)
100            .build()
101            .unwrap();
102        api::ignore(endpoint).query(&client).unwrap();
103    }
104}