Merge pull request 'Improve HTML route & template test coverage' (#31) from route_test_coverage into main

Reviewed-on: PeachCloud/peach-workspace#31
This commit is contained in:
glyph 2021-11-22 10:20:10 +00:00
commit 6fb5b58d6c
3 changed files with 342 additions and 185 deletions

View File

@ -206,7 +206,6 @@ pub fn save_reset_password_form(password_form: ResetPasswordForm) -> Result<(),
/// Password reset request handler. This route is used by a user who is not logged in /// Password reset request handler. This route is used by a user who is not logged in
/// and is specifically for users who have forgotten their password. /// and is specifically for users who have forgotten their password.
/// All routes under /public/* are excluded from nginx basic auth via the nginx config.
#[get("/reset_password")] #[get("/reset_password")]
pub fn reset_password(flash: Option<FlashMessage>) -> Template { pub fn reset_password(flash: Option<FlashMessage>) -> Template {
let mut context = ResetPasswordContext::build(); let mut context = ResetPasswordContext::build();
@ -223,7 +222,6 @@ pub fn reset_password(flash: Option<FlashMessage>) -> Template {
/// Password reset form request handler. This route is used by a user who is not logged in /// Password reset form request handler. This route is used by a user who is not logged in
/// and is specifically for users who have forgotten their password. /// and is specifically for users who have forgotten their password.
/// This route is excluded from nginx basic auth via the nginx config.
#[post("/reset_password", data = "<reset_password_form>")] #[post("/reset_password", data = "<reset_password_form>")]
pub fn reset_password_post(reset_password_form: Form<ResetPasswordForm>) -> Template { pub fn reset_password_post(reset_password_form: Form<ResetPasswordForm>) -> Template {
let result = save_reset_password_form(reset_password_form.into_inner()); let result = save_reset_password_form(reset_password_form.into_inner());
@ -396,7 +394,7 @@ pub fn change_password_post(password_form: Form<PasswordForm>, _auth: Authentica
let mut context = ChangePasswordContext::build(); let mut context = ChangePasswordContext::build();
// set back icon link to network route // set back icon link to network route
context.back = Some("/settings/admin".to_string()); context.back = Some("/settings/admin".to_string());
context.title = Some("Configure DNS".to_string()); context.title = Some("Change Password".to_string());
context.flash_name = Some("error".to_string()); context.flash_name = Some("error".to_string());
context.flash_msg = Some(format!("Failed to save new password: {}", err)); context.flash_msg = Some(format!("Failed to save new password: {}", err));
Template::render("settings/admin/change_password", &context) Template::render("settings/admin/change_password", &context)

View File

@ -52,6 +52,260 @@ fn index_html() {
assert!(body.contains("/settings")); assert!(body.contains("/settings"));
} }
#[test]
fn help_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/help").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Help"));
}
#[test]
fn login_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/login").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Login"));
}
#[test]
fn logout_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/logout").dispatch();
// check for 303 status (redirect to "/login")
assert_eq!(response.status(), Status::SeeOther);
assert_eq!(response.content_type(), None);
}
#[test]
fn power_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/power").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Shutdown Device"));
}
/*
NOTE: these tests are comment-out for the moment, due to the fact that they invoke system commands (resulting in a `sudo` password request during test execution). see if we can find a way to test the results without triggering the shutdown or restart.
#[test]
fn reboot() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/power/reboot").dispatch();
// check for redirect
assert_eq!(response.status(), Status::SeeOther);
}
#[test]
fn shutdown() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/power/shutdown").dispatch();
// check for redirect
assert_eq!(response.status(), Status::SeeOther);
}
*/
// SCUTTLEBUTT ROUTES
#[test]
fn block() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client
.post("/scuttlebutt/block")
.header(ContentType::Form)
.body("key=HEqy940T6uB+T+d9Jaa58aNfRzLx9eRWqkZljBmnkmk=.ed25519")
.dispatch();
assert_eq!(response.status(), Status::SeeOther);
}
#[test]
fn blocks_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/blocks").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Blocks"));
}
#[test]
fn follow() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client
.post("/scuttlebutt/follow")
.header(ContentType::Form)
.body("key=@HEqy940T6uB+T+d9Jaa58aNfRzLx9eRWqkZljBmnkmk=.ed25519")
.dispatch();
// ensure we redirect (303)
assert_eq!(response.status(), Status::SeeOther);
}
#[test]
fn follows_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/follows").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Follows"));
}
#[test]
fn followers_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/followers").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Followers"));
}
#[test]
fn friends_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/friends").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Friends"));
}
#[test]
fn peers_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/peers").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Scuttlebutt Peers"));
}
#[test]
fn private_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/private").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Private Messages"));
}
#[test]
fn profile_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/profile").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Profile"));
}
#[test]
fn publish_post() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client
.post("/scuttlebutt/publish")
.header(ContentType::Form)
.body("text='golden ripples in the meshwork'")
.dispatch();
assert_eq!(response.status(), Status::SeeOther);
}
#[test]
fn unfollow() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client
.post("/scuttlebutt/unfollow")
.header(ContentType::Form)
.body("key=@HEqy940T6uB+T+d9Jaa58aNfRzLx9eRWqkZljBmnkmk=.ed25519")
.dispatch();
assert_eq!(response.status(), Status::SeeOther);
}
// ADMIN SETTINGS ROUTES
#[test]
fn admin_settings_menu_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/admin").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Administrator Settings"));
assert!(body.contains("Change Password"));
assert!(body.contains("Configure Admin"));
}
#[test]
fn add_admin_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/admin/add").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Add Admin"));
assert!(body.contains("SSB ID"));
assert!(body.contains("Add"));
assert!(body.contains("Cancel"));
}
#[test]
fn add_admin() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client
.post("/settings/admin/add")
.header(ContentType::Form)
.body("ssb_id=@HEqy940T6uB+T+d9Jaa58aNfRzLx9eRWqkZljBmnkmk=.ed25519")
.dispatch();
// check for redirect
assert_eq!(response.status(), Status::SeeOther);
}
#[test]
fn change_password_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/admin/change_password").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Change Password"));
assert!(body.contains("Old Password"));
assert!(body.contains("Enter New Password"));
assert!(body.contains("Re-Enter New Password"));
assert!(body.contains("Save"));
}
#[test]
fn configure_admin_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/admin/configure").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Configure Admin"));
assert!(body.contains("Current Admins"));
assert!(body.contains("Add Admin"));
}
#[test]
fn forgot_password_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/admin/forgot_password").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Send Password Reset"));
}
// NETWORK SETTINGS ROUTES
#[test] #[test]
fn network_settings_menu_html() { fn network_settings_menu_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let client = Client::tracked(init_rocket()).expect("valid rocket instance");
@ -62,6 +316,29 @@ fn network_settings_menu_html() {
assert!(body.contains("Network Configuration")); assert!(body.contains("Network Configuration"));
} }
#[test]
fn deploy_ap() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/network/ap/activate").dispatch();
// check for 303 status (redirect)
assert_eq!(response.status(), Status::SeeOther);
assert_eq!(response.content_type(), None);
}
#[test]
fn dns_settings_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/network/dns").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Configure DNS"));
assert!(body.contains("External Domain (optional)"));
assert!(body.contains("Enable Dynamic DNS"));
assert!(body.contains("Dynamic DNS Domain"));
assert!(body.contains("Save"));
}
#[test] #[test]
fn list_aps_html() { fn list_aps_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let client = Client::tracked(init_rocket()).expect("valid rocket instance");
@ -84,6 +361,15 @@ fn ap_details_html() {
//assert!(body.contains("Network not found")); //assert!(body.contains("Network not found"));
} }
#[test]
fn deploy_client() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/network/wifi/activate").dispatch();
// check for 303 status (redirect)
assert_eq!(response.status(), Status::SeeOther);
assert_eq!(response.content_type(), None);
}
#[test] #[test]
fn add_ap_html() { fn add_ap_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let client = Client::tracked(init_rocket()).expect("valid rocket instance");
@ -114,177 +400,6 @@ fn add_ap_ssid_html() {
assert!(body.contains("Cancel")); assert!(body.contains("Cancel"));
} }
#[test]
fn status_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/status").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Device Status"));
assert!(body.contains("Networking"));
assert!(body.contains("Display"));
assert!(body.contains("Statistics"));
}
#[test]
fn help_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/help").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Help"));
}
#[test]
fn login_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/login").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Login"));
}
#[test]
fn private_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/private").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Private Messages"));
}
#[test]
fn peers_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/peers").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Scuttlebutt Peers"));
}
#[test]
fn friends_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/friends").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Friends"));
}
#[test]
fn follows_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/follows").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Follows"));
}
#[test]
fn followers_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/followers").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Followers"));
}
#[test]
fn block_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/blocks").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Blocks"));
}
#[test]
fn follow() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client
.post("/scuttlebutt/follow")
.header(ContentType::Form)
.body("key=@HEqy940T6uB+T+d9Jaa58aNfRzLx9eRWqkZljBmnkmk=.ed25519")
.dispatch();
// ensure we redirect (303)
assert_eq!(response.status(), Status::SeeOther);
}
#[test]
fn unfollow() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client
.post("/scuttlebutt/unfollow")
.header(ContentType::Form)
.body("key=@HEqy940T6uB+T+d9Jaa58aNfRzLx9eRWqkZljBmnkmk=.ed25519")
.dispatch();
assert_eq!(response.status(), Status::SeeOther);
}
#[test]
fn block() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client
.post("/scuttlebutt/block")
.header(ContentType::Form)
.body("key=HEqy940T6uB+T+d9Jaa58aNfRzLx9eRWqkZljBmnkmk=.ed25519")
.dispatch();
assert_eq!(response.status(), Status::SeeOther);
}
#[test]
fn publish_post() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client
.post("/scuttlebutt/publish")
.header(ContentType::Form)
.body("text='golden ripples in the meshwork'")
.dispatch();
assert_eq!(response.status(), Status::SeeOther);
}
#[test]
fn profile_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/scuttlebutt/profile").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Profile"));
}
#[test]
fn power_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/power").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Shutdown Device"));
}
#[test]
fn data_usage_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/network/wifi/usage").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Network Data Usage"));
assert!(body.contains("WARNING THRESHOLD"));
assert!(body.contains("Update"));
assert!(body.contains("Cancel"));
}
#[test] #[test]
fn add_credentials() { fn add_credentials() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let client = Client::tracked(init_rocket()).expect("valid rocket instance");
@ -322,21 +437,65 @@ fn modify_password() {
} }
#[test] #[test]
fn deploy_ap() { fn data_usage_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/network/ap/activate").dispatch(); let response = client.get("/settings/network/wifi/usage").dispatch();
// check for 303 status (redirect) assert_eq!(response.status(), Status::Ok);
assert_eq!(response.status(), Status::SeeOther); assert_eq!(response.content_type(), Some(ContentType::HTML));
assert_eq!(response.content_type(), None); let body = response.into_string().unwrap();
assert!(body.contains("Network Data Usage"));
assert!(body.contains("WARNING THRESHOLD"));
assert!(body.contains("Update"));
assert!(body.contains("Cancel"));
}
// SCUTTLEBUTT SETTINGS HTML ROUTES
#[test]
fn scuttlebutt_settings_menu_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/scuttlebutt").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Scuttlebutt Settings"));
assert!(body.contains("Set Network Key"));
assert!(body.contains("Set Replication Hops"));
assert!(body.contains("Remove Blocked Feeds"));
assert!(body.contains("Set Database Directory"));
assert!(body.contains("Check Filesystem"));
assert!(body.contains("Repair Filesystem"));
assert!(body.contains("Restart Sbot"));
}
// STATUS HTML ROUTES
#[test]
fn status_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/status").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::HTML));
let body = response.into_string().unwrap();
assert!(body.contains("Device Status"));
assert!(body.contains("Networking"));
assert!(body.contains("Display"));
assert!(body.contains("Statistics"));
} }
#[test] #[test]
fn deploy_client() { fn network_status_html() {
let client = Client::tracked(init_rocket()).expect("valid rocket instance"); let client = Client::tracked(init_rocket()).expect("valid rocket instance");
let response = client.get("/settings/network/wifi/activate").dispatch(); let response = client.get("/status/network").dispatch();
// check for 303 status (redirect) assert_eq!(response.status(), Status::Ok);
assert_eq!(response.status(), Status::SeeOther); assert_eq!(response.content_type(), Some(ContentType::HTML));
assert_eq!(response.content_type(), None); let body = response.into_string().unwrap();
assert!(body.contains("Network Status"));
assert!(body.contains("Mode"));
assert!(body.contains("SSID"));
assert!(body.contains("IP"));
assert!(body.contains("DOWNLOAD"));
assert!(body.contains("UPLOAD"));
} }
// JSON API ROUTES // JSON API ROUTES

View File

@ -34,7 +34,7 @@
<div id="buttonDiv"> <div id="buttonDiv">
<input id="changePasswordButton" class="button button-primary center" title="Add" type="submit" value="Save"> <input id="changePasswordButton" class="button button-primary center" title="Add" type="submit" value="Save">
</div> </div>
<a class="button button-secondary center" href="/network" title="Cancel">Cancel</a> <a class="button button-secondary center" href="/settings/admin" title="Cancel">Cancel</a>
</form> </form>
<!-- FLASH MESSAGE --> <!-- FLASH MESSAGE -->