Skip to content

Commit

Permalink
change api methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Jan 13, 2024
1 parent c95115a commit 40961dd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
22 changes: 11 additions & 11 deletions src/tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use crate::{gd, handle::account::LoginRes, routes::*, sd, tests::router, va, Aut
#[tokio::test]
async fn creation() {
let (state, route) = router();
let res = req!(route => SEND_CAPTCHA,
let res = req!(route, POST => SEND_CAPTCHA,
json!({ "email": "[email protected]" }) => json
);
assert!(!res.status().is_success());
let res = req!(route => SEND_CAPTCHA,
let res = req!(route, POST => SEND_CAPTCHA,
json!({ "email": "[email protected]" }) => json
);
assert!(res.status().is_success());
Expand All @@ -28,7 +28,7 @@ async fn creation() {

// Simulates a wrong captcha.
let wrong_captcha = sms4_backend::account::verify::Captcha::from(captcha.into_inner() + 1);
let res = req!(route => REGISTER,
let res = req!(route, PUT => REGISTER,
json!({
"email": "[email protected]",
"name": "Genshine Player",
Expand All @@ -40,7 +40,7 @@ async fn creation() {
);
assert!(!res.status().is_success());

let res = req!(route => REGISTER,
let res = req!(route, PUT => REGISTER,
json!({
"email": "[email protected]",
"name": "Genshine Player",
Expand Down Expand Up @@ -103,7 +103,7 @@ async fn login() {
state.worlds.account.insert(acc_exp!(DCK)).await.unwrap();

// Login with wrong password
let res = req!(route => LOGIN,
let res = req!(route, POST => LOGIN,
json!({
"email": "[email protected]",
"password": "123456",
Expand All @@ -112,7 +112,7 @@ async fn login() {
assert!(!res.status().is_success());

// Login successfully
let res = req!(route => LOGIN,
let res = req!(route, POST => LOGIN,
json!({
"email": "[email protected]",
"password": "shanlilinghuo",
Expand Down Expand Up @@ -143,12 +143,12 @@ async fn logout() {
account: id,
token: token0,
};
let wrong_res = req!(route => LOGOUT, auth_wrong, axum::body::Body::empty() => bytes);
let wrong_res = req!(route, POST => LOGOUT, auth_wrong);
assert!(!wrong_res.status().is_success());
let select = sd!(state.worlds.account, id);
assert!(async { Ok(va!(auth, select)) }.await.is_ok());

let res = req!(route => LOGOUT, auth, axum::body::Body::empty() => bytes);
let res = req!(route, POST => LOGOUT, auth);
assert!(res.status().is_success());
assert!(async { Ok(va!(auth, select)) }.await.is_err());
let auth1 = Auth {
Expand All @@ -165,7 +165,7 @@ async fn reset_password() {
let id = account.id();
state.worlds.account.insert(account).await.unwrap();

let res = req!(route => SEND_RESET_PASSWORD_CAPTCHA,
let res = req!(route, POST => SEND_RESET_PASSWORD_CAPTCHA,
json!({
"email": "[email protected]",
}) => json
Expand All @@ -178,7 +178,7 @@ async fn reset_password() {
.await
.expect("captcha not sent");
let wrong_captcha = sms4_backend::account::verify::Captcha::from(captcha.into_inner() + 1);
let res = req!(route => RESET_PASSWORD,
let res = req!(route, PATCH => RESET_PASSWORD,
json!({
"email": "[email protected]",
"captcha": wrong_captcha,
Expand All @@ -194,7 +194,7 @@ async fn reset_password() {
assert!(account.password_matches("shanlilinghuo"));
}

let res = req!(route => RESET_PASSWORD,
let res = req!(route, PATCH => RESET_PASSWORD,
json!({
"email": "[email protected]",
"captcha": captcha,
Expand Down
51 changes: 29 additions & 22 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{path::PathBuf, sync::Arc};

use axum::Router;
use axum::{
routing::{delete, patch, put},
Router,
};
use dmds::{mem_io_handle::MemStorage, world};
use sms4_backend::config::Config;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -59,41 +62,45 @@ fn router() -> (Global<MemStorage>, Router) {
let router: Router<()> = Router::new()
// account services
.route(SEND_CAPTCHA, post(handle::account::send_captcha))
.route(REGISTER, post(handle::account::register))
.route(REGISTER, put(handle::account::register))
.route(LOGIN, post(handle::account::login))
.route(GET_ACCOUNT_INFO, get(handle::account::get_info))
.route(
SEND_RESET_PASSWORD_CAPTCHA,
post(handle::account::send_reset_password_captcha),
)
.route(RESET_PASSWORD, post(handle::account::reset_password))
.route(MODIFY_ACCOUNT, post(handle::account::modify))
.route(RESET_PASSWORD, patch(handle::account::reset_password))
.route(MODIFY_ACCOUNT, patch(handle::account::modify))
.route(LOGOUT, post(handle::account::logout))
.route(SET_PERMISSIONS, post(handle::account::set_permissions))
.route(SET_PERMISSIONS, patch(handle::account::set_permissions))
.route(BULK_GET_ACCOUNT_INFO, post(handle::account::bulk_get_info))
// post services
.route(NEW_POST, post(handle::post::new_post))
.route(NEW_POST, put(handle::post::new_post))
.route(FILTER_POSTS, get(handle::post::filter))
.route(GET_POST, get(handle::post::get_info))
.route(GET_POSTS, post(handle::post::bulk_get_info))
.route(MODIFY_POST, post(handle::post::modify))
.route(REVIEW_POST, post(handle::post::review))
.route(DELETE_POST, post(handle::post::remove))
.route(BULK_DELETE_POST, post(handle::post::bulk_remove))
.route(MODIFY_POST, patch(handle::post::modify))
.route(REVIEW_POST, patch(handle::post::review))
.route(DELETE_POST, delete(handle::post::remove))
.route(BULK_DELETE_POST, delete(handle::post::bulk_remove))
// append state
.with_state(state.clone());
(state, router)
}

macro_rules! req {
($r:expr => $u:expr, $a:expr) => {{
let mut b = Some(Request::builder().uri($u).method(axum::http::Method::GET));
$a.append_to_req_builder(&mut $b);
let req = b.unwrap().body(Body::empty()).unwrap();
($r:expr, $m:ident => $u:expr, $a:expr) => {{
let mut b = Some(
axum::http::Request::builder()
.uri($u)
.method(axum::http::Method::$m),
);
$a.append_to_req_builder(&mut b);
let req = b.unwrap().body(axum::body::Body::empty()).unwrap();
tower::ServiceExt::oneshot($r.clone(), req).await.unwrap()
}};
($r:expr => $u:expr, $a:expr, $b:expr => json) => {
let mut b = Some(Request::builder().uri($u).method(axum::http::Method::POST));
($r:expr, $m:ident => $u:expr, $a:expr, $b:expr => json) => {
let mut b = Some(Request::builder().uri($u).method(axum::http::Method::$m));
$a.append_to_req_builder(&mut $b);
let req = b
.unwrap()
Expand All @@ -105,20 +112,20 @@ macro_rules! req {
.unwrap();
tower::ServiceExt::oneshot($r.clone(), req).await.unwrap()
};
($r:expr => $u:expr, $a:expr, $b:expr => bytes) => {{
($r:expr, $m:ident => $u:expr, $a:expr, $b:expr => bytes) => {{
let mut b = Some(
axum::http::Request::builder()
.uri($u)
.method(axum::http::Method::POST),
.method(axum::http::Method::$m),
);
$a.append_to_req_builder(&mut b);
let req = b.unwrap().body(axum::body::Body::from($b)).unwrap();
tower::ServiceExt::oneshot($r.clone(), req).await.unwrap()
}};
($r:expr => $u:expr, $b:expr => json) => {{
($r:expr, $m:ident => $u:expr, $b:expr => json) => {{
let req = axum::http::Request::builder()
.uri($u)
.method(axum::http::Method::POST)
.method(axum::http::Method::$m)
.header(
axum::http::header::CONTENT_TYPE,
mime::APPLICATION_JSON.as_ref(),
Expand All @@ -127,10 +134,10 @@ macro_rules! req {
.unwrap();
tower::ServiceExt::oneshot($r.clone(), req).await.unwrap()
}};
($r:expr => $u:expr, $b:expr => bytes) => {{
($r:expr, $m:ident => $u:expr, $b:expr => bytes) => {{
let req = axum::http::Request::builder()
.uri($u)
.method(axum::http::Method::POST)
.method(axum::http::Method::$m)
.body(axum::body::Body::from($b))
.unwrap();
tower::ServiceExt::oneshot($r.clone(), req).await.unwrap()
Expand Down

0 comments on commit 40961dd

Please sign in to comment.