|
| 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 | + |
| 7 | +use derive_builder::Builder; |
| 8 | + |
| 9 | +use crate::api::common::NameOrId; |
| 10 | +use crate::api::endpoint_prelude::*; |
| 11 | + |
| 12 | +/// Get a single project access token. |
| 13 | +#[derive(Debug, Builder, Clone)] |
| 14 | +pub struct ProjectAccessToken<'a> { |
| 15 | + /// The ID of the project. |
| 16 | + #[builder(setter(into))] |
| 17 | + project: NameOrId<'a>, |
| 18 | + /// The ID of the project access token. |
| 19 | + id: u64, |
| 20 | +} |
| 21 | + |
| 22 | +impl<'a> ProjectAccessToken<'a> { |
| 23 | + /// Create a builder for the endpoint. |
| 24 | + pub fn builder() -> ProjectAccessTokenBuilder<'a> { |
| 25 | + ProjectAccessTokenBuilder::default() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl<'a> Endpoint for ProjectAccessToken<'a> { |
| 30 | + fn method(&self) -> Method { |
| 31 | + Method::GET |
| 32 | + } |
| 33 | + |
| 34 | + fn endpoint(&self) -> Cow<'static, str> { |
| 35 | + format!("projects/{}/access_tokens/{}", self.project, self.id).into() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#[cfg(test)] |
| 40 | +mod tests { |
| 41 | + use http::Method; |
| 42 | + |
| 43 | + use crate::api::projects::access_tokens::{ProjectAccessToken, ProjectAccessTokenBuilderError}; |
| 44 | + use crate::api::{self, Query}; |
| 45 | + use crate::test::client::{ExpectedUrl, SingleTestClient}; |
| 46 | + |
| 47 | + #[test] |
| 48 | + fn project_id_is_necessary() { |
| 49 | + let err = ProjectAccessToken::builder().build().unwrap_err(); |
| 50 | + crate::test::assert_missing_field!(err, ProjectAccessTokenBuilderError, "project"); |
| 51 | + } |
| 52 | + |
| 53 | + #[test] |
| 54 | + fn project_is_necessary() { |
| 55 | + let err = ProjectAccessToken::builder().id(1).build().unwrap_err(); |
| 56 | + crate::test::assert_missing_field!(err, ProjectAccessTokenBuilderError, "project"); |
| 57 | + } |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn id_is_necessary() { |
| 61 | + let err = ProjectAccessToken::builder() |
| 62 | + .project(1) |
| 63 | + .build() |
| 64 | + .unwrap_err(); |
| 65 | + crate::test::assert_missing_field!(err, ProjectAccessTokenBuilderError, "id"); |
| 66 | + } |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn project_and_id_are_sufficient() { |
| 70 | + ProjectAccessToken::builder() |
| 71 | + .project(1) |
| 72 | + .id(1) |
| 73 | + .build() |
| 74 | + .unwrap(); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn endpoint() { |
| 79 | + let endpoint = ExpectedUrl::builder() |
| 80 | + .method(Method::GET) |
| 81 | + .endpoint("projects/foo/access_tokens/1") |
| 82 | + .build() |
| 83 | + .unwrap(); |
| 84 | + let client = SingleTestClient::new_raw(endpoint, ""); |
| 85 | + |
| 86 | + let endpoint = ProjectAccessToken::builder() |
| 87 | + .project("foo") |
| 88 | + .id(1) |
| 89 | + .build() |
| 90 | + .unwrap(); |
| 91 | + api::ignore(endpoint).query(&client).unwrap(); |
| 92 | + } |
| 93 | +} |
0 commit comments