56 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
use rocket::{Route, State, get, http::ContentType, routes};
 | 
						|
use sqlx::SqlitePool;
 | 
						|
 | 
						|
use crate::model::{personal::cal::get_personal_cal, planned::event::Event, user::User};
 | 
						|
 | 
						|
#[get("/cal")]
 | 
						|
async fn cal(db: &State<SqlitePool>) -> (ContentType, String) {
 | 
						|
    //TODO: add unit test once proper functionality is there
 | 
						|
    (ContentType::Calendar, Event::get_ics_feed(db).await)
 | 
						|
}
 | 
						|
 | 
						|
#[get("/cal/personal/<user_id>/<uuid>")]
 | 
						|
async fn cal_registered(
 | 
						|
    db: &State<SqlitePool>,
 | 
						|
    user_id: i32,
 | 
						|
    uuid: &str,
 | 
						|
) -> Result<(ContentType, String), String> {
 | 
						|
    let Some(user) = User::find_by_id(db, user_id).await else {
 | 
						|
        return Err("Invalid".into());
 | 
						|
    };
 | 
						|
 | 
						|
    if user.user_token != uuid {
 | 
						|
        return Err("Invalid".into());
 | 
						|
    }
 | 
						|
 | 
						|
    Ok((ContentType::Calendar, get_personal_cal(db, &user).await))
 | 
						|
}
 | 
						|
 | 
						|
pub fn routes() -> Vec<Route> {
 | 
						|
    routes![cal, cal_registered]
 | 
						|
}
 | 
						|
 | 
						|
#[cfg(test)]
 | 
						|
mod test {
 | 
						|
    use rocket::{http::Status, local::asynchronous::Client};
 | 
						|
    use sqlx::SqlitePool;
 | 
						|
 | 
						|
    use crate::testdb;
 | 
						|
 | 
						|
    #[sqlx::test]
 | 
						|
    fn test_without_login() {
 | 
						|
        let db = testdb!();
 | 
						|
 | 
						|
        let rocket = rocket::build().manage(db.clone());
 | 
						|
        let rocket = crate::tera::config(rocket);
 | 
						|
 | 
						|
        let client = Client::tracked(rocket).await.unwrap();
 | 
						|
 | 
						|
        let req = client.get("/");
 | 
						|
        let response = req.dispatch().await;
 | 
						|
 | 
						|
        assert_eq!(response.status(), Status::SeeOther);
 | 
						|
        assert_eq!(response.headers().get("Location").next(), Some("/auth"));
 | 
						|
    }
 | 
						|
}
 |