gitlab/api/projects/environments/
environment.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/// Query for an environment within a project.
13#[derive(Debug, Builder, Clone)]
14pub struct Environment<'a> {
15    /// The project to query for the environment.
16    #[builder(setter(into))]
17    project: NameOrId<'a>,
18    /// The ID of the environment.
19    environment: u64,
20}
21
22impl<'a> Environment<'a> {
23    /// Create a builder for the endpoint.
24    pub fn builder() -> EnvironmentBuilder<'a> {
25        EnvironmentBuilder::default()
26    }
27}
28
29impl Endpoint for Environment<'_> {
30    fn method(&self) -> Method {
31        Method::GET
32    }
33
34    fn endpoint(&self) -> Cow<'static, str> {
35        format!(
36            "projects/{}/environments/{}",
37            self.project, self.environment,
38        )
39        .into()
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use crate::api::projects::environments::{Environment, EnvironmentBuilderError};
46    use crate::api::{self, Query};
47    use crate::test::client::{ExpectedUrl, SingleTestClient};
48
49    #[test]
50    fn project_and_environment_are_needed() {
51        let err = Environment::builder().build().unwrap_err();
52        crate::test::assert_missing_field!(err, EnvironmentBuilderError, "project");
53    }
54
55    #[test]
56    fn project_is_needed() {
57        let err = Environment::builder().environment(1).build().unwrap_err();
58        crate::test::assert_missing_field!(err, EnvironmentBuilderError, "project");
59    }
60
61    #[test]
62    fn environment_is_needed() {
63        let err = Environment::builder().project(1).build().unwrap_err();
64        crate::test::assert_missing_field!(err, EnvironmentBuilderError, "environment");
65    }
66
67    #[test]
68    fn project_and_environment_are_sufficient() {
69        Environment::builder()
70            .project(1)
71            .environment(1)
72            .build()
73            .unwrap();
74    }
75
76    #[test]
77    fn endpoint() {
78        let endpoint = ExpectedUrl::builder()
79            .endpoint("projects/simple%2Fproject/environments/1")
80            .build()
81            .unwrap();
82        let client = SingleTestClient::new_raw(endpoint, "");
83
84        let endpoint = Environment::builder()
85            .project("simple/project")
86            .environment(1)
87            .build()
88            .unwrap();
89        api::ignore(endpoint).query(&client).unwrap();
90    }
91}