Skip to content

Commit 2752dbd

Browse files
authored
Plugin errors (#92)
* Implement CannotViewPlugins error and its test * Implement CannotInstallPlugin error and its test * Implement PluginNotFound error and its test * Implement CannotManagePlugins error and its test * Implement CannotDeleteActivePlugin error and its test * Adds CannotManageNetworkPlugins error * Adds CannotActivatePlugin error * Adds CannotDeactivatePlugin error * Implement CannotViewPlugin error and its test * Adds UnableToConnectToFilesystem & UnableToDetermineInstalledPlugin errors * Adds FsUnavailable & NetworkOnlyPlugin errors * Adds WPInternalErrorCode * Fix missing & in PluginUpdateParams in test_plugins_mut
1 parent e160743 commit 2752dbd

File tree

5 files changed

+174
-2
lines changed

5 files changed

+174
-2
lines changed

wp_api/src/api_error.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,20 @@ pub struct UnrecognizedWPRestError {
4141
pub enum WPRestErrorCode {
4242
#[serde(rename = "rest_cannot_create_user")]
4343
CannotCreateUser,
44+
#[serde(rename = "rest_cannot_delete_active_plugin")]
45+
CannotDeleteActivePlugin,
4446
#[serde(rename = "rest_cannot_edit")]
4547
CannotEdit,
4648
#[serde(rename = "rest_cannot_edit_roles")]
4749
CannotEditRoles,
50+
#[serde(rename = "rest_cannot_install_plugin")]
51+
CannotInstallPlugin,
52+
#[serde(rename = "rest_cannot_manage_plugins")]
53+
CannotManagePlugins,
54+
#[serde(rename = "rest_cannot_view_plugin")]
55+
CannotViewPlugin,
56+
#[serde(rename = "rest_cannot_view_plugins")]
57+
CannotViewPlugins,
4858
#[serde(rename = "rest_forbidden_context")]
4959
ForbiddenContext,
5060
#[serde(rename = "rest_forbidden_orderby")]
@@ -53,6 +63,8 @@ pub enum WPRestErrorCode {
5363
ForbiddenWho,
5464
#[serde(rename = "rest_invalid_param")]
5565
InvalidParam,
66+
#[serde(rename = "rest_plugin_not_found")]
67+
PluginNotFound,
5668
#[serde(rename = "rest_not_logged_in")]
5769
Unauthorized,
5870
#[serde(rename = "rest_user_cannot_delete")]
@@ -69,19 +81,82 @@ pub enum WPRestErrorCode {
6981
UserInvalidRole,
7082
#[serde(rename = "rest_user_invalid_slug")]
7183
UserInvalidSlug,
84+
// ---
7285
// Tested, but we believe these errors are imppossible to get unless the requests are manually modified
86+
// ---
7387
#[serde(rename = "rest_user_exists")]
7488
UserExists,
7589
#[serde(rename = "rest_user_invalid_argument")]
7690
UserInvalidArgument,
7791
#[serde(rename = "rest_trash_not_supported")]
7892
TrashNotSupported,
93+
// ---
7994
// Untested, because we believe these errors require multisite
95+
// ---
96+
#[serde(rename = "rest_cannot_manage_network_plugins")]
97+
CannotManageNetworkPlugins,
98+
#[serde(rename = "rest_network_only_plugin")]
99+
NetworkOnlyPlugin,
80100
#[serde(rename = "rest_user_create")]
81101
UserCreate,
102+
// ---
82103
// Untested, because we believe these errors are impossible to get
104+
// ---
105+
/// Happens while activating a plugin without the `activate_plugin` permission.
106+
/// However, in a default setup a prior check of `activate_plugins` will fail
107+
/// resulting in `CannotManagePlugins` error instead.
108+
#[serde(rename = "rest_cannot_activate_plugin")]
109+
CannotActivatePlugin,
110+
/// Happens while deactivating a plugin without the `deactivate_plugin` permission.
111+
/// However, in a default setup a prior check of `activate_plugins` will fail
112+
/// resulting in `CannotManagePlugins` error instead.
113+
#[serde(rename = "rest_cannot_deactivate_plugin")]
114+
CannotDeactivatePlugin,
83115
#[serde(rename = "rest_user_invalid_username")]
84116
UserInvalidUsername,
85117
#[serde(rename = "rest_user_invalid_password")]
86118
UserInvalidPassword,
87119
}
120+
121+
// All internal errors _should_ be wrapped as a `WPRestErrorCode` by the server. However, there
122+
// is a good chance that some internal errors do make it into the response, so these error types
123+
// are provided.
124+
//
125+
// Currently, we don't parse the response for these error types, but we could consider adding it
126+
// as a fallback. For the moment, clients can manually try parsing an `Unrecognized` error
127+
// into this type.
128+
#[derive(Debug, Deserialize, PartialEq, Eq, uniffi::Error)]
129+
pub enum WPInternalErrorCode {
130+
#[serde(rename = "fs_error")]
131+
FsError,
132+
#[serde(rename = "fs_no_plugins_dir")]
133+
FsNoPluginsDir,
134+
#[serde(rename = "fs_unavailable")]
135+
FsUnavailable,
136+
#[serde(rename = "could_not_remove_plugin")]
137+
CouldNotRemovePlugin,
138+
#[serde(rename = "could_not_resume_plugin")]
139+
CouldNotResumePlugin,
140+
#[serde(rename = "no_plugin_header")]
141+
NoPluginHeader,
142+
#[serde(rename = "plugin_missing_dependencies")]
143+
PluginMissingDependencies,
144+
#[serde(rename = "plugin_not_found")]
145+
PluginNotFound,
146+
#[serde(rename = "plugin_invalid")]
147+
PluginInvalid,
148+
#[serde(rename = "plugin_php_incompatible")]
149+
PluginPhpIncompatible,
150+
#[serde(rename = "plugin_wp_incompatible")]
151+
PluginWpIncompatible,
152+
#[serde(rename = "plugin_wp_php_incompatible")]
153+
PluginWpPhpIncompatible,
154+
#[serde(rename = "plugins_invalid")]
155+
PluginsInvalid,
156+
#[serde(rename = "unable_to_connect_to_filesystem")]
157+
UnableToConnectToFilesystem,
158+
#[serde(rename = "unable_to_determine_installed_plugin")]
159+
UnableToDetermineInstalledPlugin,
160+
#[serde(rename = "unexpected_output")]
161+
UnexpectedOutput,
162+
}

wp_api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl WPApiHelper {
267267
pub fn update_plugin_request(
268268
&self,
269269
plugin: &PluginSlug,
270-
params: PluginUpdateParams,
270+
params: &PluginUpdateParams,
271271
) -> WPNetworkRequest {
272272
WPNetworkRequest {
273273
method: RequestMethod::POST,

wp_networking/tests/test_helpers.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,22 @@ pub fn read_test_credentials_from_file() -> TestCredentials {
128128

129129
fn expected_status_code_for_wp_rest_error_code(error_code: &WPRestErrorCode) -> u16 {
130130
match error_code {
131+
WPRestErrorCode::CannotActivatePlugin => 403,
131132
WPRestErrorCode::CannotCreateUser => 403,
133+
WPRestErrorCode::CannotDeactivatePlugin => 403,
134+
WPRestErrorCode::CannotDeleteActivePlugin => 400,
132135
WPRestErrorCode::CannotEdit => 403,
133136
WPRestErrorCode::CannotEditRoles => 403,
137+
WPRestErrorCode::CannotInstallPlugin => 403,
138+
WPRestErrorCode::CannotManageNetworkPlugins => 403,
139+
WPRestErrorCode::CannotManagePlugins => 403,
140+
WPRestErrorCode::CannotViewPlugin => 403,
141+
WPRestErrorCode::CannotViewPlugins => 403,
134142
WPRestErrorCode::ForbiddenContext => 403,
135143
WPRestErrorCode::ForbiddenOrderBy => 403,
136144
WPRestErrorCode::ForbiddenWho => 403,
145+
WPRestErrorCode::NetworkOnlyPlugin => 400,
146+
WPRestErrorCode::PluginNotFound => 404,
137147
WPRestErrorCode::InvalidParam => 400,
138148
WPRestErrorCode::TrashNotSupported => 501,
139149
WPRestErrorCode::Unauthorized => 401,
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use wp_api::{PluginCreateParams, PluginStatus, PluginUpdateParams, WPContext, WPRestErrorCode};
2+
3+
use crate::test_helpers::{
4+
api, api_as_subscriber, AssertWpError, WPNetworkRequestExecutor, WPNetworkResponseParser,
5+
HELLO_DOLLY_PLUGIN_SLUG, WP_ORG_PLUGIN_SLUG_CLASSIC_WIDGETS,
6+
};
7+
8+
pub mod test_helpers;
9+
10+
#[tokio::test]
11+
async fn create_plugin_err_cannot_install_plugin() {
12+
api_as_subscriber()
13+
.create_plugin_request(&PluginCreateParams {
14+
slug: WP_ORG_PLUGIN_SLUG_CLASSIC_WIDGETS.into(),
15+
status: PluginStatus::Active,
16+
})
17+
.execute()
18+
.await
19+
.unwrap()
20+
.parse(wp_api::parse_create_plugin_response)
21+
.assert_wp_error(WPRestErrorCode::CannotInstallPlugin);
22+
}
23+
24+
#[tokio::test]
25+
async fn delete_plugin_err_cannot_delete_active_plugin() {
26+
api()
27+
.delete_plugin_request(&HELLO_DOLLY_PLUGIN_SLUG.into())
28+
.execute()
29+
.await
30+
.unwrap()
31+
.parse(wp_api::parse_delete_plugin_response)
32+
.assert_wp_error(WPRestErrorCode::CannotDeleteActivePlugin);
33+
}
34+
35+
#[tokio::test]
36+
async fn list_plugins_err_cannot_view_plugins() {
37+
api_as_subscriber()
38+
.list_plugins_request(WPContext::Edit, &None)
39+
.execute()
40+
.await
41+
.unwrap()
42+
.parse(wp_api::parse_retrieve_plugin_response_with_edit_context)
43+
.assert_wp_error(WPRestErrorCode::CannotViewPlugins);
44+
}
45+
46+
#[tokio::test]
47+
async fn retrieve_plugin_err_cannot_view_plugin() {
48+
api_as_subscriber()
49+
.retrieve_plugin_request(WPContext::Edit, &HELLO_DOLLY_PLUGIN_SLUG.into())
50+
.execute()
51+
.await
52+
.unwrap()
53+
.parse(wp_api::parse_retrieve_plugin_response_with_edit_context)
54+
.assert_wp_error(WPRestErrorCode::CannotViewPlugin);
55+
}
56+
57+
#[tokio::test]
58+
async fn update_plugin_err_plugin_not_found() {
59+
api()
60+
.update_plugin_request(
61+
&"foo".into(),
62+
&PluginUpdateParams {
63+
status: PluginStatus::Active,
64+
},
65+
)
66+
.execute()
67+
.await
68+
.unwrap()
69+
.parse(wp_api::parse_update_plugin_response)
70+
.assert_wp_error(WPRestErrorCode::PluginNotFound);
71+
}
72+
73+
#[tokio::test]
74+
async fn update_plugin_err_cannot_manage_plugins() {
75+
api_as_subscriber()
76+
.update_plugin_request(
77+
&HELLO_DOLLY_PLUGIN_SLUG.into(),
78+
&PluginUpdateParams {
79+
status: PluginStatus::Active,
80+
},
81+
)
82+
.execute()
83+
.await
84+
.unwrap()
85+
.parse(wp_api::parse_update_plugin_response)
86+
.assert_wp_error(WPRestErrorCode::CannotManagePlugins);
87+
}

wp_networking/tests/test_plugins_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async fn update_plugin() {
3838
let updated_plugin = api()
3939
.update_plugin_request(
4040
&HELLO_DOLLY_PLUGIN_SLUG.into(),
41-
PluginUpdateParams { status },
41+
&PluginUpdateParams { status },
4242
)
4343
.execute()
4444
.await

0 commit comments

Comments
 (0)