diff --git a/CHANGELOG.md b/CHANGELOG.md index 382fa43f6cfb3b2b9c3aeb414f3eca0ced3904dc..ca9145fee72e6259cc40177e7a0ac4727e7c6992 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * Add `api::paged::pagination::Pagination::AllPerPageLimit(usize)` to get all results with smaller page size. Some endpoints return a 500 Internal Server Error when trying to fetch 100 results at once. + * Add `api::groups::variables` to manage group-level CI/CD variables similar + to the already available project-level CI/CD variable endpoints. # v0.1706.0 diff --git a/src/api/README.md b/src/api/README.md index 499cd8460828b8fc9eef4c619d48799c67565c7d..60a4018cdba716c4cfe78bc66f2925f157a060ae 100644 --- a/src/api/README.md +++ b/src/api/README.md @@ -38,6 +38,9 @@ These API endpoints have been implemented. * `POST /groups/:group/share` `groups/share.rs` * `DELETE /groups/:group/share/:group2` `groups/unshare.rs` * `GET /groups/:group/subgroups` `groups/subgroups/subgroups.rs` + * `POST /groups/:group/variables` `groups/variables/create.rs` + * `GET /groups/:group/variables/:key` `groups/variables/variable.rs` + * `PUT /groups/:group/variables/:key` `groups/variables/update.rs` * `GET /job` `job/job.rs` * `GET /merge_requests` `merge_requests/merge_requests.rs` * `GET /personal_access_tokens` `personal_access_tokens/personal_access_tokens.rs` @@ -343,6 +346,8 @@ instead of having to search the page for missing endpoints. * `POST /groups/:group/transfer` https://gitlab.kitware.com/help/api/groups.md#transfer-a-group-to-a-new-parent-group-turn-a-subgroup-to-a-top-level-group * `GET /groups/:group/transfer_locations` https://gitlab.kitware.com/help/api/groups.md#get-groups-to-which-a-user-can-transfer-a-group * `GET /groups/:group/users` https://gitlab.kitware.com/help/api/groups.md#list-group-users (EXPERIMENTAL) + * `GET /groups/:group/variables` https://gitlab.kitware.com/help/api/group_level_variables.md#list-group-variables + * `DELETE /groups/:group/variables/:key` https://gitlab.kitware.com/help/api/group_level_variables.md#remove-variable * `GET /job/allowed_agents` https://gitlab.kitware.com/help/api/jobs.md#get-gitlab-agent-by-ci_job_token * `POST /projects/:project/approvals` https://gitlab.kitware.com/help/api/merge_request_approvals.md#change-configuration * `POST /projects/:project/approval_rules` https://gitlab.kitware.com/help/api/merge_request_approvals.md#create-project-level-rule diff --git a/src/api/groups.rs b/src/api/groups.rs index 684ab4b0c10299360140a74be4795a1db7112fe4..b1639919b4cb83ea348e38287e63aa35073c29a5 100644 --- a/src/api/groups.rs +++ b/src/api/groups.rs @@ -27,6 +27,8 @@ mod share; pub mod subgroups; mod unshare; +pub mod variables; + pub use create::BranchProtection; pub use create::BranchProtectionAccessLevel; pub use create::BranchProtectionDefaults; diff --git a/src/api/groups/variables.rs b/src/api/groups/variables.rs new file mode 100644 index 0000000000000000000000000000000000000000..23e1d75849a9690c17db9a017c52e42325e4869e --- /dev/null +++ b/src/api/groups/variables.rs @@ -0,0 +1,27 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Project variable API endpoints. +//! +//! These endpoints are used for querying a project's variables. + +mod create; +mod update; +mod variable; + +pub use self::create::CreateGroupVariable; +pub use self::create::CreateGroupVariableBuilder; +pub use self::create::CreateGroupVariableBuilderError; +pub use self::create::GroupVariableType; + +pub use self::update::UpdateGroupVariable; +pub use self::update::UpdateGroupVariableBuilder; +pub use self::update::UpdateGroupVariableBuilderError; + +pub use self::variable::GroupVariable; +pub use self::variable::GroupVariableBuilder; +pub use self::variable::GroupVariableBuilderError; +pub use self::variable::GroupVariableFilter; diff --git a/src/api/groups/variables/create.rs b/src/api/groups/variables/create.rs new file mode 100644 index 0000000000000000000000000000000000000000..d32a42ccf8660fb5d0f6f5d001a76625fc5fe840 --- /dev/null +++ b/src/api/groups/variables/create.rs @@ -0,0 +1,339 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use derive_builder::Builder; + +use crate::api::common::NameOrId; +use crate::api::endpoint_prelude::*; +use crate::api::ParamValue; + +/// The type of a group variable. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] +pub enum GroupVariableType { + /// An environment variable. + /// + /// The value of the variable is available as the value of the named environment variable. + EnvVar, + /// A file variable. + /// + /// The value of the variable is available in a file given by the value of the named + /// environment variable. + File, +} + +impl GroupVariableType { + /// The variable type query parameter. + fn as_str(self) -> &'static str { + match self { + GroupVariableType::EnvVar => "env_var", + GroupVariableType::File => "file", + } + } +} + +impl ParamValue<'static> for GroupVariableType { + fn as_value(&self) -> Cow<'static, str> { + self.as_str().into() + } +} + +/// Add a variable to a group. +#[derive(Debug, Builder, Clone)] +#[builder(setter(strip_option))] +pub struct CreateGroupVariable<'a> { + /// The group to add the variable to. + #[builder(setter(into))] + group: NameOrId<'a>, + /// The key of the variable + #[builder(setter(into))] + key: Cow<'a, str>, + /// The value of a variable + #[builder(setter(into))] + value: Cow<'a, str>, + /// The type of the variable. + #[builder(default)] + variable_type: Option, + /// Whether the variable is protected. + #[builder(default)] + protected: Option, + /// Whether the variable is masked. + #[builder(default)] + masked: Option, + /// Whether the variable is treated as a raw string. + #[builder(default)] + raw: Option, + /// The environment scope of the variable. + #[builder(setter(into), default)] + environment_scope: Option>, + /// The description of the variable. + #[builder(setter(into), default)] + description: Option>, +} + +impl<'a> CreateGroupVariable<'a> { + /// Create a builder for the endpoint. + pub fn builder() -> CreateGroupVariableBuilder<'a> { + CreateGroupVariableBuilder::default() + } +} + +impl Endpoint for CreateGroupVariable<'_> { + fn method(&self) -> Method { + Method::POST + } + + fn endpoint(&self) -> Cow<'static, str> { + format!("groups/{}/variables", self.group).into() + } + + fn body(&self) -> Result)>, BodyError> { + let mut params = FormParams::default(); + + params + .push("key", &self.key) + .push("value", &self.value) + .push_opt("variable_type", self.variable_type) + .push_opt("protected", self.protected) + .push_opt("masked", self.masked) + .push_opt("raw", self.raw) + .push_opt("environment_scope", self.environment_scope.as_ref()) + .push_opt("description", self.description.as_ref()); + + params.into_body() + } +} + +#[cfg(test)] +mod tests { + use http::Method; + + use crate::api::groups::variables::create::{ + CreateGroupVariable, CreateGroupVariableBuilderError, GroupVariableType, + }; + use crate::api::{self, Query}; + use crate::test::client::{ExpectedUrl, SingleTestClient}; + + #[test] + fn group_variable_type_as_str() { + let items = &[ + (GroupVariableType::EnvVar, "env_var"), + (GroupVariableType::File, "file"), + ]; + + for (i, s) in items { + assert_eq!(i.as_str(), *s); + } + } + + #[test] + fn all_parameters_are_needed() { + let err = CreateGroupVariable::builder().build().unwrap_err(); + crate::test::assert_missing_field!(err, CreateGroupVariableBuilderError, "group"); + } + + #[test] + fn group_is_necessary() { + let err = CreateGroupVariable::builder() + .key("testkey") + .value("testvalue") + .build() + .unwrap_err(); + crate::test::assert_missing_field!(err, CreateGroupVariableBuilderError, "group"); + } + + #[test] + fn key_is_necessary() { + let err = CreateGroupVariable::builder() + .group(1) + .value("testvalue") + .build() + .unwrap_err(); + crate::test::assert_missing_field!(err, CreateGroupVariableBuilderError, "key"); + } + + #[test] + fn value_is_necessary() { + let err = CreateGroupVariable::builder() + .group(1) + .key("testkey") + .build() + .unwrap_err(); + crate::test::assert_missing_field!(err, CreateGroupVariableBuilderError, "value"); + } + + #[test] + fn sufficient_parameters() { + CreateGroupVariable::builder() + .group(1) + .key("testkey") + .value("testvalue") + .build() + .unwrap(); + } + + #[test] + fn endpoint() { + let endpoint = ExpectedUrl::builder() + .method(Method::POST) + .endpoint("groups/simple%2Fgroup/variables") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!("key=testkey", "&value=testvalue")) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = CreateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_variable_type() { + let endpoint = ExpectedUrl::builder() + .method(Method::POST) + .endpoint("groups/simple%2Fgroup/variables") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!( + "key=testkey", + "&value=testvalue", + "&variable_type=file", + )) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = CreateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .variable_type(GroupVariableType::File) + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_protected() { + let endpoint = ExpectedUrl::builder() + .method(Method::POST) + .endpoint("groups/simple%2Fgroup/variables") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!( + "key=testkey", + "&value=testvalue", + "&protected=true", + )) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = CreateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .protected(true) + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_masked() { + let endpoint = ExpectedUrl::builder() + .method(Method::POST) + .endpoint("groups/simple%2Fgroup/variables") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!("key=testkey", "&value=testvalue", "&masked=true")) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = CreateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .masked(true) + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_raw() { + let endpoint = ExpectedUrl::builder() + .method(Method::POST) + .endpoint("groups/simple%2Fgroup/variables") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!("key=testkey", "&value=testvalue", "&raw=true")) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = CreateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .raw(true) + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_environment_scope() { + let endpoint = ExpectedUrl::builder() + .method(Method::POST) + .endpoint("groups/simple%2Fgroup/variables") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!( + "key=testkey", + "&value=testvalue", + "&environment_scope=*", + )) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = CreateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .environment_scope("*") + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_description() { + let endpoint = ExpectedUrl::builder() + .method(Method::POST) + .endpoint("groups/simple%2Fgroup/variables") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!( + "key=testkey", + "&value=testvalue", + "&description=desc", + )) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = CreateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .description("desc") + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } +} diff --git a/src/api/groups/variables/update.rs b/src/api/groups/variables/update.rs new file mode 100644 index 0000000000000000000000000000000000000000..7195671e88fd35be86c8c4eec4eeee9580dd55a0 --- /dev/null +++ b/src/api/groups/variables/update.rs @@ -0,0 +1,321 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use derive_builder::Builder; + +use crate::api::common::{self, NameOrId}; +use crate::api::endpoint_prelude::*; +use crate::api::groups::variables::{GroupVariableFilter, GroupVariableType}; + +/// Edit a variable of a group. +#[derive(Debug, Builder, Clone)] +#[builder(setter(strip_option))] +pub struct UpdateGroupVariable<'a> { + /// The group to edit the variable on. + #[builder(setter(into))] + group: NameOrId<'a>, + /// The name of the variable. + #[builder(setter(into))] + key: Cow<'a, str>, + /// The value of the variable. + #[builder(setter(into))] + value: Cow<'a, str>, + /// The type of the variable. + #[builder(default)] + variable_type: Option, + /// Whether the variable is protected. + #[builder(default)] + protected: Option, + /// Whether the variable is masked. + #[builder(default)] + masked: Option, + /// Whether the variable is treated as a raw string. + #[builder(default)] + raw: Option, + /// The environment scope of the variable. + #[builder(setter(into), default)] + environment_scope: Option>, + /// Filters to apply to the returned variables. + #[builder(default)] + filter: Option>, + /// The description of the variable. + #[builder(setter(into), default)] + description: Option>, +} + +impl<'a> UpdateGroupVariable<'a> { + /// Create a builder for the endpoint. + pub fn builder() -> UpdateGroupVariableBuilder<'a> { + UpdateGroupVariableBuilder::default() + } +} + +impl Endpoint for UpdateGroupVariable<'_> { + fn method(&self) -> Method { + Method::PUT + } + + fn endpoint(&self) -> Cow<'static, str> { + format!( + "groups/{}/variables/{}", + self.group, + common::path_escaped(&self.key), + ) + .into() + } + + fn body(&self) -> Result)>, BodyError> { + let mut params = FormParams::default(); + + params + .push("value", &self.value) + .push_opt("variable_type", self.variable_type) + .push_opt("protected", self.protected) + .push_opt("masked", self.masked) + .push_opt("raw", self.raw) + .push_opt("environment_scope", self.environment_scope.as_ref()) + .push_opt("description", self.description.as_ref()); + + if let Some(filter) = self.filter.as_ref() { + filter.add_query(&mut params); + } + + params.into_body() + } +} + +#[cfg(test)] +mod tests { + use http::Method; + + use crate::api::groups::variables::update::{ + GroupVariableFilter, GroupVariableType, UpdateGroupVariable, + UpdateGroupVariableBuilderError, + }; + use crate::api::{self, Query}; + use crate::test::client::{ExpectedUrl, SingleTestClient}; + + #[test] + fn all_parameters_are_needed() { + let err = UpdateGroupVariable::builder().build().unwrap_err(); + crate::test::assert_missing_field!(err, UpdateGroupVariableBuilderError, "group"); + } + + #[test] + fn group_is_necessary() { + let err = UpdateGroupVariable::builder() + .key("testkey") + .value("testvalue") + .build() + .unwrap_err(); + crate::test::assert_missing_field!(err, UpdateGroupVariableBuilderError, "group"); + } + + #[test] + fn key_is_necessary() { + let err = UpdateGroupVariable::builder() + .group(1) + .value("testvalue") + .build() + .unwrap_err(); + crate::test::assert_missing_field!(err, UpdateGroupVariableBuilderError, "key"); + } + + #[test] + fn value_is_necessary() { + let err = UpdateGroupVariable::builder() + .group(1) + .key("testkey") + .build() + .unwrap_err(); + crate::test::assert_missing_field!(err, UpdateGroupVariableBuilderError, "value"); + } + + #[test] + fn sufficient_parameters() { + UpdateGroupVariable::builder() + .group(1) + .key("testkey") + .value("testvalue") + .build() + .unwrap(); + } + + #[test] + fn endpoint() { + let endpoint = ExpectedUrl::builder() + .method(Method::PUT) + .endpoint("groups/simple%2Fgroup/variables/testkey") + .content_type("application/x-www-form-urlencoded") + .body_str("value=testvalue") + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = UpdateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_variable_type() { + let endpoint = ExpectedUrl::builder() + .method(Method::PUT) + .endpoint("groups/simple%2Fgroup/variables/testkey") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!("value=testvalue", "&variable_type=file")) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = UpdateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .variable_type(GroupVariableType::File) + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_protected() { + let endpoint = ExpectedUrl::builder() + .method(Method::PUT) + .endpoint("groups/simple%2Fgroup/variables/testkey") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!("value=testvalue", "&protected=true")) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = UpdateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .protected(true) + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_masked() { + let endpoint = ExpectedUrl::builder() + .method(Method::PUT) + .endpoint("groups/simple%2Fgroup/variables/testkey") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!("value=testvalue", "&masked=true")) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = UpdateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .masked(true) + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_raw() { + let endpoint = ExpectedUrl::builder() + .method(Method::PUT) + .endpoint("groups/simple%2Fgroup/variables/testkey") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!("value=testvalue", "&raw=true")) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = UpdateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .raw(true) + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_environment_scope() { + let endpoint = ExpectedUrl::builder() + .method(Method::PUT) + .endpoint("groups/simple%2Fgroup/variables/testkey") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!("value=testvalue", "&environment_scope=*")) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = UpdateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .environment_scope("*") + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_filter() { + let endpoint = ExpectedUrl::builder() + .method(Method::PUT) + .endpoint("groups/simple%2Fgroup/variables/testkey") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!( + "value=testvalue", + "&filter%5Benvironment_scope%5D=production", + )) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = UpdateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .filter( + GroupVariableFilter::builder() + .environment_scope("production") + .build() + .unwrap(), + ) + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_description() { + let endpoint = ExpectedUrl::builder() + .method(Method::PUT) + .endpoint("groups/simple%2Fgroup/variables/testkey") + .content_type("application/x-www-form-urlencoded") + .body_str(concat!("value=testvalue", "&description=desc")) + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = UpdateGroupVariable::builder() + .group("simple/group") + .key("testkey") + .value("testvalue") + .description("desc") + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } +} diff --git a/src/api/groups/variables/variable.rs b/src/api/groups/variables/variable.rs new file mode 100644 index 0000000000000000000000000000000000000000..b548dcec25d12ac17a1e06792b92138eddec640a --- /dev/null +++ b/src/api/groups/variables/variable.rs @@ -0,0 +1,158 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use derive_builder::Builder; + +use crate::api::common::{self, NameOrId}; +use crate::api::endpoint_prelude::*; + +/// Filter parameters. +#[derive(Debug, Clone, Builder)] +#[builder(setter(strip_option))] +pub struct GroupVariableFilter<'a> { + /// Filter based on the environment scope. + #[builder(setter(into), default)] + environment_scope: Option>, +} + +impl<'a> GroupVariableFilter<'a> { + /// Create a builder for the endpoint. + pub fn builder() -> GroupVariableFilterBuilder<'a> { + GroupVariableFilterBuilder::default() + } + + pub(crate) fn add_query<'b>(&'b self, params: &mut FormParams<'b>) { + params.push_opt("filter[environment_scope]", self.environment_scope.as_ref()); + } +} + +/// Get the variable from a group. +#[derive(Debug, Builder, Clone)] +#[builder(setter(strip_option))] +pub struct GroupVariable<'a> { + /// The group to get the variable from. + #[builder(setter(into))] + group: NameOrId<'a>, + /// The name of the variable. + #[builder(setter(into))] + key: Cow<'a, str>, + /// Filter + #[builder(default)] + filter: Option>, +} + +impl<'a> GroupVariable<'a> { + /// Create a builder for the endpoint. + pub fn builder() -> GroupVariableBuilder<'a> { + GroupVariableBuilder::default() + } +} + +impl Endpoint for GroupVariable<'_> { + fn method(&self) -> Method { + Method::GET + } + + fn endpoint(&self) -> Cow<'static, str> { + format!( + "groups/{}/variables/{}", + self.group, + common::path_escaped(self.key.as_ref()), + ) + .into() + } + + fn body(&self) -> Result)>, BodyError> { + let mut params = FormParams::default(); + + if let Some(filter) = self.filter.as_ref() { + filter.add_query(&mut params); + } + + params.into_body() + } +} + +#[cfg(test)] +mod tests { + use http::Method; + + use crate::api::groups::variables::variable::{ + GroupVariable, GroupVariableBuilderError, GroupVariableFilter, + }; + use crate::api::{self, Query}; + use crate::test::client::{ExpectedUrl, SingleTestClient}; + + #[test] + fn all_parameters_are_needed() { + let err = GroupVariable::builder().build().unwrap_err(); + crate::test::assert_missing_field!(err, GroupVariableBuilderError, "group"); + } + + #[test] + fn group_is_necessary() { + let err = GroupVariable::builder().key("testkey").build().unwrap_err(); + crate::test::assert_missing_field!(err, GroupVariableBuilderError, "group"); + } + + #[test] + fn key_is_necessary() { + let err = GroupVariable::builder().group(1).build().unwrap_err(); + crate::test::assert_missing_field!(err, GroupVariableBuilderError, "key"); + } + + #[test] + fn sufficient_parameters() { + GroupVariable::builder() + .group(1) + .key("testkey") + .build() + .unwrap(); + } + + #[test] + fn endpoint() { + let endpoint = ExpectedUrl::builder() + .method(Method::GET) + .endpoint("groups/simple%2Fgroup/variables/testkey%2F") + .content_type("application/x-www-form-urlencoded") + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = GroupVariable::builder() + .group("simple/group") + .key("testkey/") + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } + + #[test] + fn endpoint_filter() { + let endpoint = ExpectedUrl::builder() + .method(Method::GET) + .endpoint("groups/simple%2Fgroup/variables/testkey%2F") + .content_type("application/x-www-form-urlencoded") + .body_str("filter%5Benvironment_scope%5D=production") + .build() + .unwrap(); + let client = SingleTestClient::new_raw(endpoint, ""); + + let endpoint = GroupVariable::builder() + .group("simple/group") + .key("testkey/") + .filter( + GroupVariableFilter::builder() + .environment_scope("production") + .build() + .unwrap(), + ) + .build() + .unwrap(); + api::ignore(endpoint).query(&client).unwrap(); + } +}