widget: verification works

This commit is contained in:
realaravinth
2021-05-29 21:19:45 +05:30
parent d9cb38ac13
commit 98cf4a476d
13 changed files with 188 additions and 39 deletions

View File

@@ -43,15 +43,17 @@ struct Level {
struct IndexPage {
duration: u32,
name: String,
key: String,
levels: Vec<Level>,
}
impl IndexPage {
fn new(config: McaptchaConfig, levels: Vec<Level>) -> Self {
fn new(config: McaptchaConfig, levels: Vec<Level>, key: String) -> Self {
IndexPage {
duration: config.duration as u32,
name: config.name,
levels,
key,
}
}
}
@@ -91,7 +93,7 @@ pub async fn view_sitekey(
let (stats, levels) = try_join!(Stats::new(&key, &data.db), levels_fut)?;
let body = IndexPage::new(config, levels).render_once().unwrap();
let body = IndexPage::new(config, levels, key).render_once().unwrap();
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body))

View File

@@ -46,6 +46,20 @@ macro_rules! post_request {
};
}
#[macro_export]
macro_rules! get_works {
($app:expr,$route:expr ) => {
let list_sitekey_resp = test::call_service(
&mut $app,
test::TestRequest::get()
.uri($route)
.to_request(),
)
.await;
assert_eq!(list_sitekey_resp.status(), StatusCode::OK);
};
}
#[macro_export]
macro_rules! get_app {
() => {

View File

@@ -14,27 +14,41 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::borrow::Cow;
use actix_web::body::Body;
use actix_web::{get, http::header, web, HttpResponse, Responder};
use mime_guess::from_path;
use rust_embed::RustEmbed;
use lazy_static::lazy_static;
use sailfish::TemplateOnce;
use crate::errors::*;
pub const WIDGET_ROUTES: routes::Widget = routes::Widget::new();
pub mod routes {
pub struct Widget {
pub verification_widget: &'static str,
pub js: &'static str,
pub wasm: &'static str,
}
impl Widget {
pub const fn new() -> Self {
Widget { verification_widget: "/widget" }
Widget {
verification_widget: "/widget",
js: "/widget/bundle.js",
wasm: "/widget/1476099975f2b060264c.module.wasm",
}
}
}
}
use actix_web::{web, HttpResponse, Responder};
use lazy_static::lazy_static;
use sailfish::TemplateOnce;
use crate::errors::*;
#[derive(TemplateOnce, Clone)]
#[template(path = "widget/index.html")]
@@ -60,14 +74,42 @@ async fn show_widget() -> PageResult<impl Responder> {
.body(&*INDEX_PAGE))
}
#[derive(RustEmbed)]
#[folder = "static/widget/"]
struct WidgetAssets;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(show_widget);
fn handle_widget_assets(path: &str) -> HttpResponse {
match WidgetAssets::get(path) {
Some(content) => {
let body: Body = match content {
Cow::Borrowed(bytes) => bytes.into(),
Cow::Owned(bytes) => bytes.into(),
};
HttpResponse::Ok()
.set(header::CacheControl(vec![header::CacheDirective::MaxAge(
crate::CACHE_AGE,
)]))
.content_type(from_path(path).first_or_octet_stream().as_ref())
.body(body)
}
None => HttpResponse::NotFound().body("404 Not Found"),
}
}
#[get("/widget/{_:.*}")]
pub async fn widget_assets(path: web::Path<String>) -> impl Responder {
handle_widget_assets(&path.0)
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(show_widget);
cfg.service(widget_assets);
}
#[cfg(test)]
mod test {
use actix_web::http::StatusCode;
@@ -77,18 +119,20 @@ mod test {
#[actix_rt::test]
async fn captcha_widget_route_works() {
let mut app = get_app!().await;
// let list_sitekey_resp = test::call_service(
// &mut app,
// test::TestRequest::get()
// .uri(crate::WIDGET_ROUTES.verification_widget)
// .to_request(),
// )
// .await;
// assert_eq!(list_sitekey_resp.status(), StatusCode::OK);
let list_sitekey_resp = test::call_service(
&mut app,
test::TestRequest::get()
.uri(crate::WIDGET_ROUTES.verification_widget)
.to_request(),
)
.await;
assert_eq!(list_sitekey_resp.status(), StatusCode::OK);
get_works!(app, crate::WIDGET_ROUTES.verification_widget);
get_works!(app, crate::WIDGET_ROUTES.js);
get_works!(app, crate::WIDGET_ROUTES.wasm);
}
}