-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlib.rs
More file actions
240 lines (218 loc) · 8.93 KB
/
lib.rs
File metadata and controls
240 lines (218 loc) · 8.93 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::prelude::*;
use wp_api::{
auth::CookiesNonceAuthenticationProvider,
login::login_client::WpLoginClient,
nav_menus::NavMenuId,
wp_com::{WpComBaseUrl, client::WpComApiClient, endpoint::WpComDotOrgApiUrlResolver},
};
pub mod backend;
pub mod mock;
pub mod prelude;
// The first user is also the current user
pub const FIRST_USER_ID: UserId = UserId(1);
pub const FIRST_USER_EMAIL: &str = "[email protected]";
pub const SECOND_USER_ID: UserId = UserId(2);
pub const SECOND_USER_EMAIL: &str = "[email protected]";
pub const SECOND_USER_SLUG: &str = "themedemos";
pub const USER_ID_INVALID: UserId = UserId(99999999);
pub const HELLO_DOLLY_PLUGIN_SLUG: &str = "hello-dolly/hello";
pub const CLASSIC_EDITOR_PLUGIN_SLUG: &str = "classic-editor/classic-editor";
pub const WP_ORG_PLUGIN_SLUG_CLASSIC_WIDGETS: &str = "classic-widgets";
pub const FIRST_COMMENT_ID: CommentId = CommentId(1);
pub const SECOND_COMMENT_ID: CommentId = CommentId(2);
pub const COMMENT_ID_INVALID: CommentId = CommentId(99999999);
pub const FIRST_POST_ID: PostId = PostId(1);
pub const POST_ID_555: PostId = PostId(555);
pub const POST_ID_DRAFT: PostId = PostId(1164);
pub const POST_ID_INVALID: PostId = PostId(99999999);
pub const POST_ID_NAV_MENUS_PARAM: PostId = PostId(1768);
pub const MEDIA_ID_611: MediaId = MediaId(611);
pub const MEDIA_ID_VIDEO: MediaId = MediaId(1690);
pub const MEDIA_ID_AUDIO: MediaId = MediaId(821);
pub const MEDIA_ID_IMAGE: MediaId = MediaId(1692);
pub const MEDIA_TEST_FILE_PATH: &str = "../test-data/test_media.jpg";
pub const MEDIA_TEST_FILE_CONTENT_TYPE: &str = "image/jpeg";
pub const NAV_MENU_ID_179: NavMenuId = NavMenuId(179);
pub const CATEGORY_ID_48: TermId = TermId(48);
pub const CATEGORY_ID_59: TermId = TermId(59);
pub const TAG_ID_100: TermId = TermId(100);
pub const TERM_ID_INVALID: TermId = TermId(99999999);
pub const TEMPLATE_TWENTY_TWENTY_FOUR_SINGLE: &str = "twentytwentyfour//single";
pub const POST_TEMPLATE_SINGLE_WITH_SIDEBAR: &str = "single-with-sidebar";
pub const PAGE_TEMPLATE_WITH_SIDEBAR: &str = "page-with-sidebar";
pub const THEME_TWENTY_TWENTY_FIVE: &str = "twentytwentyfive";
pub const THEME_TWENTY_TWENTY_FOUR: &str = "twentytwentyfour";
pub const THEME_TWENTY_TWENTY_THREE: &str = "twentytwentythree";
pub const WIDGET_ID_BLOCK_2: &str = "block-2";
pub const WIDGET_INACTIVE_WIDGETS_SIDEBAR: &str = "wp_inactive_widgets";
pub const WIDGET_TYPE_TEXT: &str = "text";
pub fn api_client() -> WpApiClient {
WpApiClient::new(
test_site_api_url_resolver(),
WpApiClientDelegate {
auth_provider: Arc::new(WpAuthenticationProvider::static_with_username_and_password(
TestCredentials::instance().admin_username.to_string(),
TestCredentials::instance().admin_password.to_string(),
)),
request_executor: Arc::new(ReqwestRequestExecutor::default()),
middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()),
app_notifier: Arc::new(EmptyAppNotifier),
},
)
}
pub fn api_client_as_author() -> WpApiClient {
WpApiClient::new(
test_site_api_url_resolver(),
WpApiClientDelegate {
auth_provider: Arc::new(WpAuthenticationProvider::static_with_username_and_password(
TestCredentials::instance().author_username.to_string(),
TestCredentials::instance().author_password.to_string(),
)),
request_executor: Arc::new(ReqwestRequestExecutor::default()),
middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()),
app_notifier: Arc::new(EmptyAppNotifier),
},
)
}
pub fn api_client_as_subscriber() -> WpApiClient {
WpApiClient::new(
test_site_api_url_resolver(),
WpApiClientDelegate {
auth_provider: Arc::new(WpAuthenticationProvider::static_with_username_and_password(
TestCredentials::instance().subscriber_username.to_string(),
TestCredentials::instance().subscriber_password.to_string(),
)),
request_executor: Arc::new(ReqwestRequestExecutor::default()),
middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()),
app_notifier: Arc::new(EmptyAppNotifier),
},
)
}
pub fn api_client_with_auth_provider(auth_provider: Arc<WpAuthenticationProvider>) -> WpApiClient {
WpApiClient::new(
test_site_api_url_resolver(),
WpApiClientDelegate {
auth_provider,
request_executor: Arc::new(ReqwestRequestExecutor::default()),
middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()),
app_notifier: Arc::new(EmptyAppNotifier),
},
)
}
pub async fn api_client_with_account_credentials(
username: String,
password: String,
) -> WpApiClient {
let request_executor = Arc::new(ReqwestRequestExecutor::new_with_cookie_store());
let login_client = WpLoginClient::new(
request_executor.clone(),
Arc::new(WpApiMiddlewarePipeline::default()),
);
let discovery_result = login_client
.api_discovery(TestCredentials::instance().site_url.to_string(), None)
.await;
let details = discovery_result
.combined_result()
.expect("API discovery should succeed");
let nonce_auth_provider = Arc::new(CookiesNonceAuthenticationProvider::new(
username,
password,
details.clone(),
request_executor.clone(),
));
WpApiClient::new(
test_site_api_url_resolver(),
WpApiClientDelegate {
auth_provider: Arc::new(WpAuthenticationProvider::dynamic(nonce_auth_provider)),
request_executor,
middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()),
app_notifier: Arc::new(EmptyAppNotifier),
},
)
}
pub fn wp_com_client() -> WpComApiClient {
WpComApiClient::new(WpApiClientDelegate {
auth_provider: WpAuthenticationProvider::static_with_auth(WpAuthentication::Bearer {
token: WpComTestCredentials::instance().bearer_token.to_string(),
})
.into(),
request_executor: Arc::new(ReqwestRequestExecutor::default()),
middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()),
app_notifier: Arc::new(EmptyAppNotifier),
})
}
pub fn wp_com_client_with_invalid_token() -> WpComApiClient {
WpComApiClient::new(WpApiClientDelegate {
auth_provider: WpAuthenticationProvider::static_with_auth(WpAuthentication::Bearer {
token: "invalid_token".to_string(),
})
.into(),
request_executor: Arc::new(ReqwestRequestExecutor::default()),
middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()),
app_notifier: Arc::new(EmptyAppNotifier),
})
}
pub fn api_client_backed_by_wp_com(site_id: String) -> WpApiClient {
WpApiClient::new(
Arc::new(WpComDotOrgApiUrlResolver::new(
site_id,
WpComBaseUrl::Production,
)),
WpApiClientDelegate {
auth_provider: Arc::new(WpAuthenticationProvider::static_with_auth(
WpAuthentication::Bearer {
token: WpComTestCredentials::instance().bearer_token.to_string(),
},
)),
request_executor: Arc::new(ReqwestRequestExecutor::default()),
middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()),
app_notifier: Arc::new(EmptyAppNotifier),
},
)
}
pub fn test_site_url() -> ParsedUrl {
let mut url: Url = TestCredentials::instance()
.site_url
.parse()
.expect("Site url is generated by our tooling");
url.path_segments_mut().unwrap().push("wp-json");
url.into()
}
pub fn test_site_api_url_resolver() -> Arc<dyn ApiUrlResolver> {
Arc::new(WpOrgSiteApiUrlResolver::new(test_site_url().into()))
}
pub trait AssertWpError<T: std::fmt::Debug> {
fn assert_wp_error(self, expected_error_code: WpErrorCode);
}
impl<T: std::fmt::Debug> AssertWpError<T> for Result<T, WpApiError> {
fn assert_wp_error(self, expected_error_code: WpErrorCode) {
let err = self.unwrap_err();
if let WpApiError::WpError {
error_code,
response,
..
} = err
{
assert_eq!(
expected_error_code, error_code,
"Incorrect error code. Expected '{expected_error_code:?}', found '{error_code:?}'. Response was: '{response:?}'"
);
} else {
panic!("Unexpected wp_error '{err}'");
}
}
}
pub trait AssertResponse {
type Item;
fn assert_response(self) -> Self::Item;
}
impl<T: std::fmt::Debug, E: std::error::Error> AssertResponse for Result<T, E> {
type Item = T;
fn assert_response(self) -> T {
assert!(self.is_ok(), "Request failed with: {}", self.unwrap_err());
self.unwrap()
}
}
pub fn unwrapped_wp_gmt_date_time(s: &str) -> WpGmtDateTime {
s.parse::<WpGmtDateTime>().expect("Expected a valid date")
}