minccino/src/yui/test.rs
2022-09-23 22:39:17 -07:00

146 lines
4.4 KiB
Rust

use super::routes::*;
use super::InstanceResponse;
use crate::rocket;
use crate::test::*;
use rocket::http::{ContentType, Header, Status};
use rocket::local::blocking::Client;
use std::net::{Ipv6Addr, SocketAddr, SocketAddrV6};
#[test]
fn auth_works_properly() {
let client = Client::tracked(rocket()).expect("valid rocket instance");
let api_header: Header = Header::new("x-api-key", SNAKEOIL_KEY);
// wrong client IP - should return Forbidden
let response = client
.get(uri!("/_yui/", get_instances()))
.remote(SocketAddr::V6(SocketAddrV6::new(LLV6_IP, 8080, 0, 0)))
.dispatch();
assert_eq!(response.status(), Status::Forbidden);
// no API key - should return Unauthorized
let response = client
.get(uri!("/_yui/", get_instances()))
.remote(saddr6!(localhost))
.dispatch();
assert_eq!(response.status(), Status::Unauthorized);
// API key, and localhost -- should be OK
let response = client
.get(uri!("/_yui/", get_instances()))
.remote(saddr6!(localhost))
.header(api_header)
.dispatch();
assert_eq!(response.status(), Status::Ok);
}
#[test]
fn endpoints_require_auth() {
let client = Client::tracked(rocket()).expect("valid rocket instance");
let test_instance = super::InstanceReq {
name: "test-pc".to_owned(),
mac_address: LLV6_MACSTR.to_owned(),
ssh_keys: None,
user_data: None,
};
let reqs = vec![
client
.post(uri!("/_yui/", new_instance()))
.json(&test_instance),
client.get(uri!("/_yui/", get_instances())),
client.get(uri!("/_yui/", get_instance(0))),
client.delete(uri!("/_yui/", delete_instance(0))),
client.get(uri!("/_yui/", get_defaults("ssh_keys".to_owned()))),
client
.post(uri!("/_yui/", set_defaults("ssh_keys".to_owned())))
.header(ContentType::Plain)
.body(SPEEX),
];
for r in reqs {
let resp = r.remote(saddr6!(localhost)).dispatch();
assert_eq!(resp.status(), Status::Unauthorized);
}
}
#[test]
fn crud() {
let client = Client::tracked(rocket()).expect("valid rocket instance");
let test_instance = super::InstanceReq {
name: "test-pc".to_owned(),
mac_address: LLV6_MACSTR.to_owned(),
ssh_keys: None,
user_data: None,
};
let api_header: Header = Header::new("x-api-key", SNAKEOIL_KEY);
// Create the instance
let response = client
.post(uri!("/_yui/", new_instance()))
.json(&test_instance)
.remote(saddr6!(localhost))
.header(api_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);
let json_resp = response
.into_json::<InstanceResponse>()
.expect("json in form InstanceResponse");
// make sure there's no error...
assert_eq!(json_resp.error, None);
// ... and that the Vec<> is available
let instances = json_resp.instances.expect("Some(Vec<InstanceInfo>)");
// ... and that it's not empty
assert_eq!(instances.len(), 1);
let saved = instances[0].clone();
// ... and that the id is correct
let got_id = saved.id & 0xffffff00;
assert_eq!(got_id, 0x32a0fe00);
// Try to get the instance
let response = client
.get(uri!("/_yui/", get_instance(saved.id)))
.remote(saddr6!(localhost))
.header(api_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);
let json_resp = response
.into_json::<InstanceResponse>()
.expect("json in form InstanceResponse");
let read_inst = json_resp.instances.unwrap()[0].clone();
assert_eq!(read_inst, saved);
// Delete instance
let response = client
.delete(uri!("/_yui/", delete_instance(saved.id)))
.remote(saddr6!(localhost))
.header(api_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);
// Make sure it's deleted
let response = client
.get(uri!("/_yui/", get_instances()))
.remote(saddr6!(localhost))
.header(api_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);
let json_resp = response
.into_json::<InstanceResponse>()
.expect("json in form InstanceResponse");
assert_eq!(
json_resp.instances.expect("empty Vec of instances").len(),
0
);
}