2021-08-06 17:58:40 +00:00
use std ::fs ::File ;
use std ::io ::Read ;
use rocket ::http ::{ ContentType , Status } ;
2021-11-04 21:30:29 +00:00
use rocket ::local ::blocking ::Client ;
2021-11-15 09:59:38 +00:00
use rocket ::serde ::json ::{ json , Value } ;
2021-11-25 09:33:09 +00:00
use rocket ::{ Build , Config , Rocket } ;
2021-08-06 17:58:40 +00:00
2021-11-04 21:59:46 +00:00
use crate ::utils ::build_json_response ;
2021-10-28 12:35:00 +00:00
2021-11-04 21:30:29 +00:00
use super ::init_rocket ;
2021-08-06 17:58:40 +00:00
2021-11-25 09:33:09 +00:00
// define authentication mode
const DISABLE_AUTH : bool = true ;
/// Wrapper around `init_rocket()` to simplify the process of invoking the application with the desired authentication status. This is particularly useful for testing purposes.
fn init_test_rocket ( disable_auth : bool ) -> Rocket < Build > {
// set authentication based on provided `disable_auth` value
Config ::figment ( ) . merge ( ( " disable_auth " , disable_auth ) ) ;
init_rocket ( )
2021-11-23 10:30:09 +00:00
}
2021-08-06 17:58:40 +00:00
// helper function to test correct retrieval and content of a file
fn test_query_file < T > ( path : & str , file : T , status : Status )
where
T : Into < Option < & 'static str > > ,
{
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . unwrap ( ) ;
2021-11-04 21:39:40 +00:00
let response = client . get ( path ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , status ) ;
2021-11-04 21:30:29 +00:00
let body_data = response . into_bytes ( ) ;
2021-08-06 17:58:40 +00:00
if let Some ( filename ) = file . into ( ) {
let expected_data = read_file_content ( filename ) ;
assert! ( body_data . map_or ( false , | s | s = = expected_data ) ) ;
}
}
// helper function to return the content of a file, given a path
fn read_file_content ( path : & str ) -> Vec < u8 > {
let mut fp = File ::open ( & path ) . expect ( & format! ( " Can't open {} " , path ) ) ;
let mut file_content = vec! [ ] ;
fp . read_to_end ( & mut file_content )
. expect ( & format! ( " Reading {} failed. " , path ) ) ;
file_content
}
// WEB PAGE ROUTES
#[ test ]
fn index_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client . get ( " / " ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " /peers " ) ) ;
assert! ( body . contains ( " /profile " ) ) ;
2021-11-15 09:59:38 +00:00
assert! ( body . contains ( " /private " ) ) ;
2021-11-15 15:32:30 +00:00
assert! ( body . contains ( " /status " ) ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " /help " ) ) ;
2021-11-15 15:32:30 +00:00
assert! ( body . contains ( " /settings " ) ) ;
2021-08-06 17:58:40 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn help_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /help " ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Help " ) ) ;
2021-08-06 17:58:40 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn login_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /login " ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Login " ) ) ;
2021-08-06 17:58:40 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn logout_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 ) ;
2021-08-06 17:58:40 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn power_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /power " ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Shutdown Device " ) ) ;
2021-08-06 17:58:40 +00:00
}
2021-11-17 09:40:02 +00:00
/*
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 .
2021-08-06 17:58:40 +00:00
#[ test ]
2021-11-17 09:40:02 +00:00
fn reboot ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /power/reboot " ) . dispatch ( ) ;
// check for redirect
assert_eq! ( response . status ( ) , Status ::SeeOther ) ;
2021-08-06 17:58:40 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn shutdown ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /power/shutdown " ) . dispatch ( ) ;
// check for redirect
assert_eq! ( response . status ( ) , Status ::SeeOther ) ;
2021-08-06 17:58:40 +00:00
}
2021-11-17 09:40:02 +00:00
* /
// SCUTTLEBUTT ROUTES
2021-08-06 17:58:40 +00:00
#[ test ]
2021-11-17 09:40:02 +00:00
fn block ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client
. post ( " /scuttlebutt/block " )
. header ( ContentType ::Form )
. body ( " key=HEqy940T6uB+T+d9Jaa58aNfRzLx9eRWqkZljBmnkmk=.ed25519 " )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::SeeOther ) ;
2021-08-06 17:58:40 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn blocks_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /scuttlebutt/blocks " ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Blocks " ) ) ;
2021-08-06 17:58:40 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn follow ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /scuttlebutt/follows " ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Follows " ) ) ;
2021-08-06 17:58:40 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn followers_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /scuttlebutt/followers " ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Followers " ) ) ;
2021-08-06 17:58:40 +00:00
}
2021-11-15 09:59:38 +00:00
#[ test ]
fn friends_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-15 09:59:38 +00:00
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 ]
2021-11-17 09:40:02 +00:00
fn peers_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /scuttlebutt/peers " ) . dispatch ( ) ;
2021-11-15 09:59:38 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Scuttlebutt Peers " ) ) ;
2021-11-15 09:59:38 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn private_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /scuttlebutt/private " ) . dispatch ( ) ;
2021-11-15 09:59:38 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Private Messages " ) ) ;
2021-11-15 09:59:38 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn profile_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /scuttlebutt/profile " ) . dispatch ( ) ;
2021-11-15 09:59:38 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Profile " ) ) ;
2021-11-15 09:59:38 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn publish_post ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-15 09:59:38 +00:00
let response = client
2021-11-17 09:40:02 +00:00
. post ( " /scuttlebutt/publish " )
2021-11-15 09:59:38 +00:00
. header ( ContentType ::Form )
2021-11-17 09:40:02 +00:00
. body ( " text='golden ripples in the meshwork' " )
2021-11-15 09:59:38 +00:00
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::SeeOther ) ;
}
#[ test ]
fn unfollow ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-15 09:59:38 +00:00
let response = client
. post ( " /scuttlebutt/unfollow " )
. header ( ContentType ::Form )
. body ( " key=@HEqy940T6uB+T+d9Jaa58aNfRzLx9eRWqkZljBmnkmk=.ed25519 " )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::SeeOther ) ;
}
2021-11-17 09:40:02 +00:00
// ADMIN SETTINGS ROUTES
2021-11-15 09:59:38 +00:00
#[ test ]
2021-11-17 09:40:02 +00:00
fn admin_settings_menu_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-15 09:59:38 +00:00
let response = client
2021-11-17 09:40:02 +00:00
. post ( " /settings/admin/add " )
2021-11-15 09:59:38 +00:00
. header ( ContentType ::Form )
2021-11-17 09:40:02 +00:00
. body ( " ssb_id=@HEqy940T6uB+T+d9Jaa58aNfRzLx9eRWqkZljBmnkmk=.ed25519 " )
2021-11-15 09:59:38 +00:00
. dispatch ( ) ;
2021-11-17 09:40:02 +00:00
// check for redirect
2021-11-15 09:59:38 +00:00
assert_eq! ( response . status ( ) , Status ::SeeOther ) ;
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn change_password_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 " ) ) ;
2021-11-23 10:30:09 +00:00
assert! ( body . contains ( " Current password " ) ) ;
assert! ( body . contains ( " New password " ) ) ;
assert! ( body . contains ( " New password duplicate " ) ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Save " ) ) ;
}
#[ test ]
fn configure_admin_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 ]
fn network_settings_menu_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /settings/network " ) . 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 Configuration " ) ) ;
}
#[ test ]
fn deploy_ap ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /settings/network/ap/activate " ) . dispatch ( ) ;
// check for 303 status (redirect)
2021-11-15 09:59:38 +00:00
assert_eq! ( response . status ( ) , Status ::SeeOther ) ;
2021-11-17 09:40:02 +00:00
assert_eq! ( response . content_type ( ) , None ) ;
2021-11-15 09:59:38 +00:00
}
2021-08-06 17:58:40 +00:00
#[ test ]
2021-11-17 09:40:02 +00:00
fn dns_settings_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /settings/network/dns " ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
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 " ) ) ;
2021-08-06 17:58:40 +00:00
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn list_aps_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /settings/network/wifi " ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " WiFi Networks " ) ) ;
assert! ( body . contains ( " No saved or available networks found. " ) ) ;
2021-08-06 17:58:40 +00:00
}
2021-11-17 09:40:02 +00:00
// TODO: needs further testing once template has been refactored
2021-08-06 17:58:40 +00:00
#[ test ]
2021-11-17 09:40:02 +00:00
fn ap_details_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /settings/network/wifi?ssid=Home " ) . 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 not found"));
}
#[ test ]
fn deploy_client ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 ]
fn add_ap_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /settings/network/wifi/add " ) . dispatch ( ) ;
2021-08-06 17:58:40 +00:00
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-11-17 09:40:02 +00:00
assert! ( body . contains ( " Add WiFi Network " ) ) ;
assert! ( body . contains ( " SSID " ) ) ;
assert! ( body . contains ( " Password " ) ) ;
assert! ( body . contains ( " Add " ) ) ;
assert! ( body . contains ( " Cancel " ) ) ;
}
#[ test ]
fn add_ap_ssid_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client
. get ( " /settings/network/wifi/add?ssid=Home " )
. 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 WiFi Network " ) ) ;
assert! ( body . contains ( " Home " ) ) ;
assert! ( body . contains ( " Password " ) ) ;
assert! ( body . contains ( " Add " ) ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " Cancel " ) ) ;
}
#[ test ]
fn add_credentials ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-08-06 17:58:40 +00:00
let response = client
2021-11-15 15:32:30 +00:00
. post ( " /settings/network/wifi/add " )
2021-08-06 17:58:40 +00:00
. header ( ContentType ::Form )
. body ( " ssid=Home&pass=Password " )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::HTML ) ) ;
}
#[ test ]
fn forget_wifi ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-08-06 17:58:40 +00:00
let response = client
2021-11-15 15:32:30 +00:00
. post ( " /settings/network/wifi/forget " )
2021-08-06 17:58:40 +00:00
. header ( ContentType ::Form )
. body ( " ssid=Home " )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::SeeOther ) ;
assert_eq! ( response . content_type ( ) , None ) ;
}
#[ test ]
fn modify_password ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-08-06 17:58:40 +00:00
let response = client
2021-11-15 15:32:30 +00:00
. post ( " /settings/network/wifi/modify " )
2021-08-06 17:58:40 +00:00
. header ( ContentType ::Form )
. body ( " ssid=Home&pass=Password " )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::SeeOther ) ;
assert_eq! ( response . content_type ( ) , None ) ;
}
#[ test ]
2021-11-17 09:40:02 +00:00
fn data_usage_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 " ) ) ;
2021-08-06 17:58:40 +00:00
}
2021-11-17 09:40:02 +00:00
// SCUTTLEBUTT SETTINGS HTML ROUTES
2021-08-06 17:58:40 +00:00
#[ test ]
2021-11-17 09:40:02 +00:00
fn scuttlebutt_settings_menu_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
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 network_status_html ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-17 09:40:02 +00:00
let response = client . get ( " /status/network " ) . 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 Status " ) ) ;
assert! ( body . contains ( " Mode " ) ) ;
assert! ( body . contains ( " SSID " ) ) ;
assert! ( body . contains ( " IP " ) ) ;
assert! ( body . contains ( " DOWNLOAD " ) ) ;
assert! ( body . contains ( " UPLOAD " ) ) ;
2021-08-06 17:58:40 +00:00
}
// JSON API ROUTES
#[ test ]
fn activate_ap ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-08-06 17:58:40 +00:00
let response = client
. post ( " /api/v1/network/activate_ap " )
. header ( ContentType ::JSON )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
}
#[ test ]
fn activate_client ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-08-06 17:58:40 +00:00
let response = client
. post ( " /api/v1/network/activate_client " )
. header ( ContentType ::JSON )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
}
#[ test ]
fn return_ip ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client
2021-08-06 17:58:40 +00:00
. get ( " /api/v1/network/ip " )
. header ( ContentType ::JSON )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " wlan0 " ) ) ;
assert! ( body . contains ( " ap0 " ) ) ;
}
#[ test ]
fn return_rssi ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client
2021-08-06 17:58:40 +00:00
. get ( " /api/v1/network/rssi " )
. header ( ContentType ::JSON )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " Not currently connected to an access point. " ) ) ;
}
#[ test ]
fn return_ssid ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client
2021-08-06 17:58:40 +00:00
. get ( " /api/v1/network/ssid " )
. header ( ContentType ::JSON )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " Not currently connected to an access point. " ) ) ;
}
#[ test ]
fn return_state ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client
2021-08-06 17:58:40 +00:00
. get ( " /api/v1/network/state " )
. header ( ContentType ::JSON )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " ap0 " ) ) ;
assert! ( body . contains ( " wlan0 " ) ) ;
assert! ( body . contains ( " unavailable " ) ) ;
}
#[ test ]
fn return_status ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client
2021-08-06 17:58:40 +00:00
. get ( " /api/v1/network/status " )
. header ( ContentType ::JSON )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " Not currently connected to an access point. " ) ) ;
}
#[ test ]
fn scan_networks ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client
2021-08-06 17:58:40 +00:00
. get ( " /api/v1/network/wifi " )
. header ( ContentType ::JSON )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " Unable to scan for networks. Interface may be deactivated. " ) ) ;
}
#[ test ]
fn add_wifi ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client
2021-08-06 17:58:40 +00:00
. post ( " /api/v1/network/wifi " )
. header ( ContentType ::JSON )
. body ( r # "{ "ssid": "Home", "pass": "Password" }"# )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " Failed to add WiFi credentials. " ) ) ;
}
#[ test ]
fn remove_wifi ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client
2021-08-06 17:58:40 +00:00
. post ( " /api/v1/network/wifi/forget " )
. header ( ContentType ::JSON )
. body ( r # "{ "ssid": "Home" }"# )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " Failed to remove WiFi network credentials. " ) ) ;
}
#[ test ]
fn new_password ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client
2021-08-06 17:58:40 +00:00
. post ( " /api/v1/network/wifi/modify " )
. header ( ContentType ::JSON )
. body ( r # "{ "ssid": "Home", "pass": "Password" }"# )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " Failed to update WiFi password. " ) ) ;
}
#[ test ]
fn ping_pong ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . expect ( " valid rocket instance " ) ;
2021-11-04 21:39:40 +00:00
let response = client
2021-08-06 17:58:40 +00:00
. get ( " /api/v1/ping " )
. header ( ContentType ::JSON )
. dispatch ( ) ;
assert_eq! ( response . status ( ) , Status ::Ok ) ;
assert_eq! ( response . content_type ( ) , Some ( ContentType ::JSON ) ) ;
2021-11-04 21:30:29 +00:00
let body = response . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " pong! " ) ) ;
}
// HELPER FUNCTION TESTS
2021-11-05 10:44:41 +00:00
#[ test ]
fn test_build_json_response ( ) {
let status = " success " . to_string ( ) ;
let data = json! ( " WiFi credentials added. " . to_string ( ) ) ;
let j : Value = build_json_response ( status , Some ( data ) , None ) ;
assert_eq! ( j [ " status " ] , " success " ) ;
assert_eq! ( j [ " data " ] , " WiFi credentials added. " ) ;
assert_eq! ( j [ " msg " ] , json! ( null ) ) ;
}
2021-08-06 17:58:40 +00:00
// FILE TESTS
#[ test ]
fn nested_file ( ) {
test_query_file (
" /images/placeholder.txt " ,
" static/images/placeholder.txt " ,
Status ::Ok ,
) ;
test_query_file (
" /images/placeholder.txt?v=1 " ,
" static/images/placeholder.txt " ,
Status ::Ok ,
) ;
test_query_file (
" /images/placeholder.txt?v=1&a=b " ,
" static/images/placeholder.txt " ,
Status ::Ok ,
) ;
}
#[ test ]
fn icon_file ( ) {
test_query_file (
" /icons/peach-icon.png " ,
" static/icons/peach-icon.png " ,
Status ::Ok ,
) ;
}
#[ test ]
fn invalid_path ( ) {
test_query_file ( " /thou_shalt_not_exist " , None , Status ::NotFound ) ;
test_query_file ( " /thou_shalt_not_exist " , None , Status ::NotFound ) ;
test_query_file ( " /thou/shalt/not/exist?a=b&c=d " , None , Status ::NotFound ) ;
}
#[ test ]
fn invalid_get_request ( ) {
2021-11-25 09:33:09 +00:00
let client = Client ::tracked ( init_test_rocket ( DISABLE_AUTH ) ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
// try to get a path that doesn't exist
2021-11-04 21:39:40 +00:00
let res = client
2021-08-06 17:58:40 +00:00
. get ( " /message/99 " )
. header ( ContentType ::JSON )
. dispatch ( ) ;
assert_eq! ( res . status ( ) , Status ::NotFound ) ;
2021-11-04 21:30:29 +00:00
let body = res . into_string ( ) . unwrap ( ) ;
2021-08-06 17:58:40 +00:00
assert! ( body . contains ( " 404: Page Not Found " ) ) ;
assert! ( body . contains ( " No PeachCloud resource exists for this URL. " ) ) ;
}