Skip to main content

bitbucket_cli/api/
workspaces.rs

1use anyhow::Result;
2
3use super::BitbucketClient;
4use crate::models::Workspace;
5
6impl BitbucketClient {
7    /// List all workspaces the authenticated user has access to,
8    /// following pagination until every page has been fetched
9    pub async fn list_workspaces(&self) -> Result<Vec<Workspace>> {
10        self.get_all_pages("/workspaces").await
11    }
12}
13
14#[cfg(test)]
15mod tests {
16    use crate::models::{Paginated, Workspace};
17
18    #[test]
19    fn workspace_list_payload_deserializes() {
20        let payload = r#"{
21            "pagelen": 10,
22            "page": 1,
23            "size": 2,
24            "values": [
25                {
26                    "uuid": "{a1b2c3d4-0000-1111-2222-333344445555}",
27                    "slug": "my-workspace",
28                    "name": "My Workspace",
29                    "type": "workspace",
30                    "is_private": true,
31                    "created_on": "2020-04-07T21:38:29.542346+00:00",
32                    "links": {
33                        "html": { "href": "https://bitbucket.org/my-workspace/" },
34                        "avatar": { "href": "https://bitbucket.org/account/my-workspace/avatar/" }
35                    }
36                },
37                {
38                    "uuid": "{b2c3d4e5-0000-1111-2222-333344445555}",
39                    "slug": "other",
40                    "name": "Other",
41                    "type": "workspace"
42                }
43            ]
44        }"#;
45
46        let page: Paginated<Workspace> = serde_json::from_str(payload).unwrap();
47        assert_eq!(page.values.len(), 2);
48        assert_eq!(page.values[0].slug, "my-workspace");
49        assert_eq!(page.values[0].is_private, Some(true));
50        assert!(page.values[0].created_on.is_some());
51        assert_eq!(page.values[1].is_private, None);
52        assert!(page.next.is_none());
53    }
54}