-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.rs
More file actions
180 lines (165 loc) · 6.47 KB
/
Copy pathlist.rs
File metadata and controls
180 lines (165 loc) · 6.47 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use crate::cli::{
pact_broker::main::{
HALClient, PactBrokerError,
utils::{
follow_broker_relation, get_auth, get_broker_relation, get_broker_url,
get_custom_headers, get_ssl_options,
},
},
utils,
};
use comfy_table::Table;
use comfy_table::presets::UTF8_FULL;
pub fn list_environments(args: &clap::ArgMatches) -> Result<String, PactBrokerError> {
let broker_url = get_broker_url(args).trim_end_matches('/').to_string();
let auth = get_auth(args);
let custom_headers = get_custom_headers(args);
let ssl_options = get_ssl_options(args);
tokio::runtime::Runtime::new().unwrap().block_on(async {
let hal_client: HALClient = HALClient::with_url(
&broker_url,
Some(auth.clone()),
ssl_options.clone(),
custom_headers.clone(),
);
let pb_environments_href_path = get_broker_relation(
hal_client.clone(),
"pb:environments".to_string(),
broker_url.to_string(),
)
.await?;
let res = follow_broker_relation(
hal_client,
"pb:environments".to_string(),
pb_environments_href_path,
)
.await;
let default_output = "text".to_string();
let output = args.get_one::<String>("output").unwrap_or(&default_output);
match res {
Ok(res) => {
if output == "pretty" {
let json = serde_json::to_string_pretty(&res).unwrap();
println!("{}", json);
} else if output == "json" {
println!("{}", serde_json::to_string(&res).unwrap());
} else {
let mut table = Table::new();
#[derive(Debug, serde::Deserialize)]
struct Environment {
uuid: String,
name: String,
#[serde(rename = "displayName")]
display_name: String,
production: bool,
}
table.load_preset(UTF8_FULL).set_header(vec![
"UUID",
"NAME",
"DISPLAY NAME",
"PRODUCTION",
]);
if let Some(embedded) = res["_embedded"].as_object() {
if let Some(environments) = embedded["environments"].as_array() {
for environment in environments {
let environment: Environment =
serde_json::from_value(environment.clone()).unwrap();
table.add_row(vec![
environment.uuid,
environment.name,
environment.display_name,
environment.production.to_string(),
]);
}
}
}
println!("{table}");
}
Ok("".to_string())
}
Err(err) => Err(err),
}
})
}
#[cfg(test)]
mod list_environments_tests {
use crate::cli::pact_broker::main::environments::list::list_environments;
use crate::cli::pact_broker::main::subcommands::add_list_environments_subcommand;
use pact_consumer::prelude::*;
use pact_models::PactSpecification;
#[test]
fn list_environments_test() {
// arrange - set up the pact mock server (as v2 for compatibility with pact-ruby)
let config = MockServerConfig {
pact_specification: PactSpecification::V2,
..MockServerConfig::default()
};
let response_body = json_pattern!({
"_embedded": {
"environments": each_like!({
"uuid": "78e85fb2-9df1-48da-817e-c9bea6294e01",
"name": "test",
"displayName": "Test",
"production": false,
"contacts": [{
"name": "Foo team",
"details": {
"emailAddress": "foo@bar.com"
}
}]
}, min = 1)
}
});
let pact_broker_service = PactBuilder::new("pact-broker-cli", "Pact Broker")
.interaction(
"a request for the index resource for list_environments_test",
"",
|mut i| {
i.given("the pb:environments relation exists in the index resource");
i.request
.get()
.path("/")
.header("Accept", "application/hal+json")
.header("Accept", "application/json");
i.response
.status(200)
.header("Content-Type", "application/hal+json;charset=utf-8")
.json_body(json_pattern!({
"_links": {
"pb:environments": {
"href": term!("http:\\/\\/.*","http://localhost/environments"),
}
}
}));
i
},
)
.interaction("a request to list the environments", "", |mut i| {
i.given("an environment exists");
i.request
.get()
.path("/environments")
.header("Accept", "application/hal+json")
.header("Accept", "application/json");
i.response
.status(200)
.header("Content-Type", "application/hal+json;charset=utf-8")
.json_body(response_body);
i
})
.start_mock_server(None, Some(config));
let mock_server_url = pact_broker_service.url();
// arrange - set up the command line arguments
let matches = add_list_environments_subcommand().get_matches_from(vec![
"list-environments",
"-b",
mock_server_url.as_str(),
"--output",
"text",
]);
// act
let sut = list_environments(&matches);
// assert
assert!(sut.is_ok());
}
}