it appears actix-web's scope behaviour has changed in the latest beta

release:

Routes 404'd when scope contained trailing slash like so:
let scope = "/api/v1/pow/";
web::scope(scope)//

So had to rm trailing slash in scope
This commit is contained in:
realaravinth
2021-11-29 17:33:08 +05:30
parent f2f8632679
commit b5af9ee259
9 changed files with 53 additions and 127 deletions

View File

@@ -29,11 +29,11 @@ pub use routes::ROUTES;
pub fn services(cfg: &mut ServiceConfig) {
meta::services(cfg);
pow::services(cfg);
auth::services(cfg);
account::services(cfg);
mcaptcha::services(cfg);
notifications::services(cfg);
pow::services(cfg);
}
#[cfg(test)]

View File

@@ -30,12 +30,6 @@ use crate::stats::record::record_fetch;
use crate::AppData;
use crate::V1_API_ROUTES;
//#[derive(Clone, Debug, Deserialize, Serialize)]
//pub struct PoWConfig {
// pub name: String,
// pub domain: String,
//}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GetConfigPayload {
pub key: String,
@@ -44,9 +38,7 @@ pub struct GetConfigPayload {
// API keys are mcaptcha actor names
/// get PoW configuration for an mcaptcha key
#[my_codegen::post(
path = "V1_API_ROUTES.pow.get_config.strip_prefix(V1_API_ROUTES.pow.scope).unwrap()"
)]
#[my_codegen::post(path = "V1_API_ROUTES.pow.get_config()")]
pub async fn get_config(
payload: web::Json<GetConfigPayload>,
data: AppData,
@@ -153,20 +145,15 @@ async fn init_mcaptcha(data: &AppData, key: &str) -> ServiceResult<()> {
#[cfg(test)]
mod tests {
use actix_web::http::StatusCode;
use actix_web::test;
use libmcaptcha::pow::PoWConfig;
use super::*;
use crate::tests::*;
use crate::*;
#[test]
fn feature() {
actix_rt::System::new().block_on(async move { get_pow_config_works().await });
}
#[actix_rt::test]
async fn get_pow_config_works() {
use super::*;
use crate::tests::*;
use crate::*;
use actix_web::test;
const NAME: &str = "powusrworks";
const PASSWORD: &str = "testingpas";
const EMAIL: &str = "randomuser@a.com";
@@ -177,8 +164,7 @@ mod tests {
}
register_and_signin(NAME, EMAIL, PASSWORD).await;
let (data, _, signin_resp, token_key) = add_levels_util(NAME, PASSWORD).await;
let cookies = get_cookie!(signin_resp);
let (data, _, _signin_resp, token_key) = add_levels_util(NAME, PASSWORD).await;
let app = get_app!(data).await;
let get_config_payload = GetConfigPayload {
@@ -187,10 +173,11 @@ mod tests {
// update and check changes
let url = V1_API_ROUTES.pow.get_config;
println!("{}", &url);
let get_config_resp = test::call_service(
&app,
post_request!(&get_config_payload, V1_API_ROUTES.pow.get_config)
.cookie(cookies.clone())
.to_request(),
)
.await;

View File

@@ -16,7 +16,6 @@
*/
use actix_web::web;
use actix_web::*;
pub mod get_config;
pub mod verify_pow;
@@ -28,13 +27,14 @@ pub use super::mcaptcha::levels::I32Levels;
pub fn services(cfg: &mut web::ServiceConfig) {
let cors = actix_cors::Cors::default()
.allow_any_origin()
.allowed_methods(vec!["POST"])
.allowed_methods(vec!["POST", "GET"])
.allow_any_header()
.max_age(3600)
.send_wildcard();
let routes = crate::V1_API_ROUTES.pow;
cfg.service(
Scope::new(crate::V1_API_ROUTES.pow.scope)
web::scope(routes.scope)
.wrap(cors)
.service(verify_pow::verify_pow)
.service(get_config::get_config)
@@ -50,9 +50,25 @@ pub mod routes {
pub scope: &'static str,
}
macro_rules! rm_scope {
($name:ident) => {
/// remove scope for $name route
pub fn $name(&self) -> &str {
self.$name
//.strip_prefix(&self.scope[..self.scope.len() - 1])
.strip_prefix(self.scope)
.unwrap()
}
};
}
impl PoW {
pub const fn new() -> Self {
let scope = "/api/v1/pow/";
// date: 2021-11-29 16:31
// commit: 6eb75d7
// route 404s when scope contained trailing slash
//let scope = "/api/v1/pow/";
let scope = "/api/v1/pow";
PoW {
get_config: "/api/v1/pow/config",
verify_pow: "/api/v1/pow/verify",
@@ -60,35 +76,22 @@ pub mod routes {
scope,
}
}
rm_scope!(get_config);
rm_scope!(verify_pow);
rm_scope!(validate_captcha_token);
}
}
//#[allow(non_camel_case_types, missing_docs)]
//pub struct post;
//impl actix_web::dev::HttpServiceFactory for post {
// fn register(self, __config: &mut actix_web::dev::AppService) {
// async fn post() -> impl Responder {
// HttpResponse::Ok()
// }
// let __resource = actix_web::Resource::new("/test/post")
// .guard(actix_web::guard::Post())
// .to(post);
// actix_web::dev::HttpServiceFactory::register(__resource, __config)
// }
//}
#[cfg(test)]
mod tests {
use super::*;
use super::routes::PoW;
#[test]
fn scope_pow_works() {
let pow = routes::PoW::new();
assert_eq!(pow.get_config.strip_prefix(pow.scope).unwrap(), "config");
assert_eq!(pow.verify_pow.strip_prefix(pow.scope).unwrap(), "verify");
assert_eq!(
pow.validate_captcha_token.strip_prefix(pow.scope).unwrap(),
"siteverify"
);
let pow = PoW::new();
assert_eq!(pow.get_config(), "/config");
assert_eq!(pow.verify_pow(), "/verify");
assert_eq!(pow.validate_captcha_token(), "/siteverify");
}
}

View File

@@ -36,9 +36,7 @@ pub struct ValidationToken {
/// route handler that verifies PoW and issues a solution token
/// if verification is successful
#[my_codegen::post(
path = "V1_API_ROUTES.pow.verify_pow.strip_prefix(V1_API_ROUTES.pow.scope).unwrap()"
)]
#[my_codegen::post(path = "V1_API_ROUTES.pow.verify_pow()")]
pub async fn verify_pow(
payload: web::Json<Work>,
data: AppData,

View File

@@ -33,9 +33,7 @@ pub struct CaptchaValidateResp {
// API keys are mcaptcha actor names
/// route hander that validates a PoW solution token
#[my_codegen::post(
path = "V1_API_ROUTES.pow.validate_captcha_token.strip_prefix(V1_API_ROUTES.pow.scope).unwrap()"
)]
#[my_codegen::post(path = "V1_API_ROUTES.pow.validate_captcha_token()")]
pub async fn validate_captcha_token(
payload: web::Json<VerifyCaptchaResult>,
data: AppData,