bitwarden_sm/projects/
create.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use bitwarden_api_api::models::ProjectCreateRequestModel;
use bitwarden_core::{validate_only_whitespaces, Client, Error};
use bitwarden_crypto::KeyEncryptable;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use validator::Validate;

use super::ProjectResponse;

#[derive(Serialize, Deserialize, Debug, JsonSchema, Validate)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ProjectCreateRequest {
    /// Organization where the project will be created
    pub organization_id: Uuid,
    #[validate(length(min = 1, max = 500), custom(function = validate_only_whitespaces))]
    pub name: String,
}

pub(crate) async fn create_project(
    client: &Client,
    input: &ProjectCreateRequest,
) -> Result<ProjectResponse, Error> {
    input.validate()?;

    let enc = client.internal.get_encryption_settings()?;
    let key = enc.get_key(&Some(input.organization_id))?;

    let project = Some(ProjectCreateRequestModel {
        name: input.name.clone().trim().encrypt_with_key(key)?.to_string(),
    });

    let config = client.internal.get_api_configurations().await;
    let res = bitwarden_api_api::apis::projects_api::organizations_organization_id_projects_post(
        &config.api,
        input.organization_id,
        project,
    )
    .await?;

    ProjectResponse::process_response(res, &enc)
}

#[cfg(test)]
mod tests {
    use super::*;

    async fn create_project(name: String) -> Result<ProjectResponse, Error> {
        let input = ProjectCreateRequest {
            organization_id: Uuid::new_v4(),
            name,
        };

        super::create_project(&Client::new(None), &input).await
    }

    #[tokio::test]
    async fn test_create_project_request_name_empty_string() {
        let response = create_project("".into()).await;
        assert!(response.is_err());
        assert_eq!(
            response.err().unwrap().to_string(),
            "name must not be empty"
        );
    }

    #[tokio::test]
    async fn test_create_project_request_name_all_whitespaces_space() {
        let response = create_project(" ".into()).await;
        assert!(response.is_err());
        assert_eq!(
            response.err().unwrap().to_string(),
            "name must not contain only whitespaces"
        );
    }

    #[tokio::test]
    async fn test_create_project_request_name_all_whitespaces_tab() {
        let response = create_project("\t".into()).await;
        assert!(response.is_err());
        assert_eq!(
            response.err().unwrap().to_string(),
            "name must not contain only whitespaces"
        );
    }

    #[tokio::test]
    async fn test_create_project_request_name_all_whitespaces_newline() {
        let response = create_project("\n".into()).await;
        assert!(response.is_err());
        assert_eq!(
            response.err().unwrap().to_string(),
            "name must not contain only whitespaces"
        );
    }

    #[tokio::test]
    async fn test_create_project_request_name_all_whitespaces_combined() {
        let response = create_project(" \t\n".into()).await;
        assert!(response.is_err());
        assert_eq!(
            response.err().unwrap().to_string(),
            "name must not contain only whitespaces"
        );
    }

    #[tokio::test]
    async fn test_create_project_request_name_501_character_length() {
        let response = create_project("a".repeat(501)).await;
        assert!(response.is_err());
        assert_eq!(
            response.err().unwrap().to_string(),
            "name must not exceed 500 characters in length"
        );
    }
}