Skip to content

Commit 3d2dafd

Browse files
authored
Add a .createForCurrentUser API for creating application passwords (#1021)
* Add a `.createForCurrentUser` API for creating application passwords * Add an integration test
1 parent d02efa6 commit 3d2dafd

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

wp_api/src/request/endpoint/application_passwords_endpoint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use wp_derive_request_builder::WpDerivedRequest;
1818
enum ApplicationPasswordsRequest {
1919
#[post(url = "/users/<user_id>/application-passwords", params = &ApplicationPasswordCreateParams, output = ApplicationPasswordWithEditContext)]
2020
Create,
21+
#[post(url = "/users/me/application-passwords", params = &ApplicationPasswordCreateParams, output = ApplicationPasswordWithEditContext)]
22+
CreateForCurrentUser,
2123
#[delete(url = "/users/<user_id>/application-passwords/<application_password_uuid>", output = ApplicationPasswordDeleteResponse)]
2224
Delete,
2325
#[delete(url = "/users/<user_id>/application-passwords", output = ApplicationPasswordDeleteAllResponse)]

wp_api_integration_tests/tests/test_application_passwords_mut.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,49 @@ async fn create_application_password() {
4242
RestoreServer::db().await;
4343
}
4444

45+
#[tokio::test]
46+
#[serial]
47+
async fn create_application_password_for_current_user() {
48+
let client = api_client();
49+
let user_id = client
50+
.users()
51+
.retrieve_me_with_edit_context()
52+
.await
53+
.assert_response()
54+
.data
55+
.id;
56+
let password_name = "IntegrationTest";
57+
// Assert that the application password name doesn't exist
58+
assert!(
59+
!application_password_meta_for_user(&user_id)
60+
.await
61+
.unwrap()
62+
.meta_value
63+
.contains(password_name)
64+
);
65+
66+
// Create an application password using the API
67+
let params = ApplicationPasswordCreateParams {
68+
app_id: None,
69+
name: password_name.to_string(),
70+
};
71+
let created_application_password = client
72+
.application_passwords()
73+
.create_for_current_user(&params)
74+
.await
75+
.assert_response()
76+
.data;
77+
78+
// Assert that the application password is created
79+
let user_meta_after_update = application_password_meta_for_user(&user_id).await;
80+
assert!(user_meta_after_update.is_some());
81+
let meta_value = user_meta_after_update.unwrap().meta_value;
82+
assert!(meta_value.contains(password_name));
83+
assert!(meta_value.contains(&created_application_password.uuid.uuid));
84+
85+
RestoreServer::db().await;
86+
}
87+
4588
#[tokio::test]
4689
#[serial]
4790
async fn update_application_password() {

0 commit comments

Comments
 (0)