using constants for routes

This commit is contained in:
realaravinth
2021-05-02 12:39:37 +05:30
parent c7bac9e623
commit 4f27e1ab8d
10 changed files with 587 additions and 363 deletions

View File

@@ -24,6 +24,22 @@ use crate::errors::*;
use crate::CheckLogin;
use crate::Data;
pub mod routes {
pub struct Duration {
pub update: &'static str,
pub get: &'static str,
}
impl Default for Duration {
fn default() -> Self {
Self {
update: "/api/v1/mcaptcha/domain/token/duration/update",
get: "/api/v1/mcaptcha/domain/token/duration/get",
}
}
}
}
#[derive(Deserialize, Serialize)]
pub struct UpdateDuration {
pub key: String,
@@ -94,6 +110,7 @@ mod tests {
use actix_web::test;
use super::*;
use crate::api::v1::ROUTES;
use crate::tests::*;
use crate::*;
@@ -102,8 +119,6 @@ mod tests {
const NAME: &str = "testuserduration";
const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "testuserduration@a.com";
const GET_URL: &str = "/api/v1/mcaptcha/domain/token/duration/get";
const UPDATE_URL: &str = "/api/v1/mcaptcha/domain/token/duration/update";
{
let data = Data::new().await;
@@ -124,7 +139,7 @@ mod tests {
let get_level_resp = test::call_service(
&mut app,
post_request!(&token_key, GET_URL)
post_request!(&token_key, ROUTES.duration.get)
.cookie(cookies.clone())
.to_request(),
)
@@ -137,7 +152,7 @@ mod tests {
let update_duration = test::call_service(
&mut app,
post_request!(&update, UPDATE_URL)
post_request!(&update, ROUTES.duration.update)
.cookie(cookies.clone())
.to_request(),
)
@@ -145,7 +160,7 @@ mod tests {
assert_eq!(update_duration.status(), StatusCode::OK);
let get_level_resp = test::call_service(
&mut app,
post_request!(&token_key, GET_URL)
post_request!(&token_key, ROUTES.duration.get)
.cookie(cookies.clone())
.to_request(),
)

View File

@@ -25,6 +25,31 @@ use crate::errors::*;
use crate::CheckLogin;
use crate::Data;
pub mod routes {
pub struct Levels {
pub add: &'static str,
pub update: &'static str,
pub delete: &'static str,
pub get: &'static str,
}
impl Default for Levels {
fn default() -> Self {
let add = "/api/v1/mcaptcha/levels/add";
let update = "/api/v1/mcaptcha/levels/update";
let delete = "/api/v1/mcaptcha/levels/delete";
let get = "/api/v1/mcaptcha/levels/get";
Self {
add,
get,
update,
delete,
}
}
}
}
#[derive(Serialize, Deserialize)]
pub struct AddLevels {
pub levels: Vec<Level>,
@@ -206,6 +231,7 @@ mod tests {
use actix_web::test;
use super::*;
use crate::api::v1::ROUTES;
use crate::tests::*;
use crate::*;
@@ -214,9 +240,6 @@ mod tests {
const NAME: &str = "testuserlevelroutes";
const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "testuserlevelrouts@a.com";
const UPDATE_URL: &str = "/api/v1/mcaptcha/levels/update";
const DEL_URL: &str = "/api/v1/mcaptcha/levels/delete";
const GET_URL: &str = "/api/v1/mcaptcha/levels/get";
{
let data = Data::new().await;
@@ -251,7 +274,7 @@ mod tests {
let get_level_resp = test::call_service(
&mut app,
post_request!(&key, GET_URL)
post_request!(&key, ROUTES.levels.get)
.cookie(cookies.clone())
.to_request(),
)
@@ -277,7 +300,7 @@ mod tests {
};
let add_token_resp = test::call_service(
&mut app,
post_request!(&add_level, UPDATE_URL)
post_request!(&add_level, ROUTES.levels.update)
.cookie(cookies.clone())
.to_request(),
)
@@ -285,7 +308,7 @@ mod tests {
assert_eq!(add_token_resp.status(), StatusCode::OK);
let get_level_resp = test::call_service(
&mut app,
post_request!(&key, GET_URL)
post_request!(&key, ROUTES.levels.get)
.cookie(cookies.clone())
.to_request(),
)
@@ -310,7 +333,7 @@ mod tests {
};
let add_token_resp = test::call_service(
&mut app,
post_request!(&add_level, DEL_URL)
post_request!(&add_level, ROUTES.levels.delete)
.cookie(cookies.clone())
.to_request(),
)
@@ -318,7 +341,7 @@ mod tests {
assert_eq!(add_token_resp.status(), StatusCode::OK);
let get_level_resp = test::call_service(
&mut app,
post_request!(&key, GET_URL)
post_request!(&key, ROUTES.levels.get)
.cookie(cookies.clone())
.to_request(),
)

View File

@@ -25,6 +25,26 @@ use crate::errors::*;
use crate::CheckLogin;
use crate::Data;
pub mod routes {
pub struct MCaptcha {
pub add: &'static str,
pub delete: &'static str,
pub get_token: &'static str,
pub update_key: &'static str,
}
impl Default for MCaptcha {
fn default() -> Self {
Self {
add: "/api/v1/mcaptcha/add",
update_key: "/api/v1/mcaptcha/update/key",
get_token: "/api/v1/mcaptcha/get",
delete: "/api/v1/mcaptcha/delete",
}
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MCaptchaID {
pub name: Option<String>,
@@ -36,6 +56,7 @@ pub struct MCaptchaDetails {
pub key: String,
}
// this should be called from within add levels
#[post("/api/v1/mcaptcha/add", wrap = "CheckLogin")]
pub async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl Responder> {
let username = id.identity().unwrap();
@@ -193,6 +214,7 @@ mod tests {
use actix_web::test;
use super::*;
use crate::api::v1::ROUTES;
use crate::tests::*;
use crate::*;
@@ -201,7 +223,6 @@ mod tests {
const NAME: &str = "testusermcaptcha";
const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "testusermcaptcha@a.com";
const DEL_URL: &str = "/api/v1/mcaptcha/delete";
{
let data = Data::new().await;
@@ -221,7 +242,7 @@ mod tests {
// 4. delete token
let del_token = test::call_service(
&mut app,
post_request!(&token_key, DEL_URL)
post_request!(&token_key, ROUTES.mcaptcha.delete)
.cookie(cookies.clone())
.to_request(),
)
@@ -234,8 +255,6 @@ mod tests {
const NAME: &str = "updateusermcaptcha";
const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "testupdateusermcaptcha@a.com";
const UPDATE_URL: &str = "/api/v1/mcaptcha/update/key";
const GET_URL: &str = "/api/v1/mcaptcha/get";
{
let data = Data::new().await;
@@ -251,7 +270,7 @@ mod tests {
// 2. update token key
let update_token_resp = test::call_service(
&mut app,
post_request!(&token_key, UPDATE_URL)
post_request!(&token_key, ROUTES.mcaptcha.update_key)
.cookie(cookies.clone())
.to_request(),
)
@@ -262,7 +281,7 @@ mod tests {
// get token key with updated key
let get_token_resp = test::call_service(
&mut app,
post_request!(&updated_token, GET_URL)
post_request!(&updated_token, ROUTES.mcaptcha.get_token)
.cookie(cookies.clone())
.to_request(),
)
@@ -277,7 +296,7 @@ mod tests {
let get_nonexistent_token_resp = test::call_service(
&mut app,
post_request!(&get_token_key, GET_URL)
post_request!(&get_token_key, ROUTES.mcaptcha.get_token)
.cookie(cookies.clone())
.to_request(),
)