use crate::endpoint::FleetGet;
use crate::pricing::Pricing;
use crate::query::QuerySpec;
use crate::scope::{Scope, ScopeExpr};
pub struct GetRegion;
impl crate::sealed::Sealed for GetRegion {}
impl FleetGet for GetRegion {
passthrough_endpoint!(GetRegion, RegionInfo);
fn required_scopes(&self) -> ScopeExpr {
ScopeExpr::Any(vec![
ScopeExpr::One(Scope::Openid),
ScopeExpr::One(Scope::UserData),
ScopeExpr::One(Scope::VehicleDeviceData),
])
}
fn pricing(&self) -> Pricing {
Pricing::NotCharged
}
fn path(&self) -> String {
"/api/1/users/region".to_owned()
}
fn query(&self) -> QuerySpec {
QuerySpec::new()
}
}
#[derive(Debug, serde::Deserialize)]
#[non_exhaustive]
pub struct RegionInfo {
#[serde(default)]
pub region: Option<String>,
#[serde(default)]
pub fleet_api_base_url: Option<String>,
}
pub struct GetMe;
impl crate::sealed::Sealed for GetMe {}
impl FleetGet for GetMe {
passthrough_endpoint!(GetMe, serde_json::Value);
fn required_scopes(&self) -> ScopeExpr {
ScopeExpr::One(Scope::UserData)
}
fn pricing(&self) -> Pricing {
Pricing::NotCharged
}
fn path(&self) -> String {
"/api/1/users/me".to_owned()
}
fn query(&self) -> QuerySpec {
QuerySpec::new()
}
}
pub struct ListOrders;
impl crate::sealed::Sealed for ListOrders {}
impl FleetGet for ListOrders {
passthrough_endpoint!(ListOrders, serde_json::Value);
fn required_scopes(&self) -> ScopeExpr {
ScopeExpr::One(Scope::VehicleDeviceData)
}
fn pricing(&self) -> Pricing {
Pricing::NotCharged
}
fn path(&self) -> String {
"/api/1/users/orders".to_owned()
}
fn query(&self) -> QuerySpec {
QuerySpec::new()
}
}
pub struct GetFeatureConfig;
impl crate::sealed::Sealed for GetFeatureConfig {}
impl FleetGet for GetFeatureConfig {
passthrough_endpoint!(GetFeatureConfig, serde_json::Value);
fn required_scopes(&self) -> ScopeExpr {
ScopeExpr::One(Scope::UserData)
}
fn pricing(&self) -> Pricing {
Pricing::NotCharged
}
fn path(&self) -> String {
"/api/1/users/feature_config".to_owned()
}
fn query(&self) -> QuerySpec {
QuerySpec::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::scope::Scope;
#[test]
fn get_region_contract() {
let r = GetRegion;
assert_eq!(r.path(), "/api/1/users/region");
assert!(matches!(r.pricing(), Pricing::NotCharged));
assert_eq!(r.query().to_encoded(), "");
}
#[test]
fn get_region_assemble_passes_wire_through_and_ignores_pagination() {
let wire = RegionInfo {
region: Some("na".to_owned()),
fleet_api_base_url: None,
};
let p = crate::model::Pagination {
previous: None,
next: Some(2),
current: Some(1),
per_page: Some(1),
count: Some(9),
pages: Some(9),
};
let out = <GetRegion as FleetGet>::assemble(wire, Some(p));
assert_eq!(out.region.as_deref(), Some("na"));
}
#[test]
fn get_region_scopes_are_any_not_all() {
let required = GetRegion.required_scopes();
assert!(required.is_satisfied_by(&[Scope::Openid]));
assert!(required.is_satisfied_by(&[Scope::UserData]));
assert!(required.is_satisfied_by(&[Scope::VehicleDeviceData]));
assert!(!required.is_satisfied_by(&[Scope::VehicleLocation]));
}
#[test]
fn region_info_tolerates_missing_fields() {
let v: RegionInfo = serde_json::from_str("{}").unwrap();
assert!(v.region.is_none());
assert!(v.fleet_api_base_url.is_none());
}
#[test]
fn region_info_parses_both_fields() {
let v: RegionInfo = serde_json::from_str(
r#"{"region":"na","fleet_api_base_url":"https://fleet-api.prd.na.vn.cloud.tesla.com"}"#,
)
.unwrap();
assert_eq!(v.region.as_deref(), Some("na"));
assert_eq!(
v.fleet_api_base_url.as_deref(),
Some("https://fleet-api.prd.na.vn.cloud.tesla.com")
);
}
#[test]
fn orders_requires_vehicle_device_data_while_me_and_feature_config_require_user_data() {
let cases = [
{
let r = GetMe;
(
r.path(),
r.required_scopes(),
r.pricing(),
Scope::UserData,
Scope::VehicleDeviceData,
)
},
{
let r = ListOrders;
(
r.path(),
r.required_scopes(),
r.pricing(),
Scope::VehicleDeviceData,
Scope::UserData,
)
},
{
let r = GetFeatureConfig;
(
r.path(),
r.required_scopes(),
r.pricing(),
Scope::UserData,
Scope::VehicleDeviceData,
)
},
];
for (path, scopes, pricing, wanted, rejected) in cases {
assert!(matches!(pricing, Pricing::NotCharged), "{path}");
assert!(
scopes.is_satisfied_by(&[wanted]),
"{path}: should be satisfied by {wanted:?} alone"
);
assert!(
!scopes.is_satisfied_by(&[rejected]),
"{path}: should not be satisfied by {rejected:?} — \
this is the regression that grouped the three endpoints together"
);
assert!(
!scopes.is_satisfied_by(&[Scope::Openid]),
"{path}: should not be satisfied by openid alone (O1 correction no. 4)"
);
}
}
#[test]
fn user_endpoint_paths_are_exact() {
assert_eq!(GetMe.path(), "/api/1/users/me");
assert_eq!(ListOrders.path(), "/api/1/users/orders");
assert_eq!(GetFeatureConfig.path(), "/api/1/users/feature_config");
}
}