#[cfg(not(target_arch = "wasm32"))] use { jsonwebtoken::{encode, EncodingKey, Header}, mio_license::Claims, mockito::mock, }; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; #[cfg(not(target_arch = "wasm32"))] fn generate_token(start_date: u64, end_date: u64) -> String { let claims = Claims { base_url: mockito::server_url(), company: "Testing Company".to_string(), exp: end_date, nbf: start_date, aud: "Media-IO Guignol Web-Player".to_string(), iss: "Media-IO Support Platform".to_string(), features: vec!["master_web_player".to_string()], domain_name: None, }; encode( &Header::default(), &claims, &EncodingKey::from_secret("test_secret".as_ref()), ) .unwrap() } #[cfg(not(target_arch = "wasm32"))] #[tokio::test] async fn test_valid_token() { let _m = mock("GET", "/api/sign") .with_header("content-type", "text/plain") .with_body(r#"fake_signed_token_data"#) .create(); let _m = mock("GET", "/api/validate/license") .with_header("content-type", "application/json") .with_body(r#"{"response": "validated"}"#) .create(); let token = generate_token(1577833200, 7889180400); let product = "master_web_player"; let signer_url = &mockito::server_url(); let _may_claims = mio_license::validate(&token, product, signer_url).await; } #[cfg(not(target_arch = "wasm32"))] #[tokio::test] async fn test_expired_token() { let _m = mock("GET", "/api/sign") .with_header("content-type", "text/plain") .with_body(r#"fake_signed_token_data"#) .create(); let _m = mock("GET", "/api/validate/license") .with_header("content-type", "application/json") .with_body(r#"{"response": "validated"}"#) .create(); let token = generate_token(1577833200, 1577833200); let product = "player"; let signer_url = &mockito::server_url(); let may_claims = mio_license::validate(&token, signer_url, product).await; assert!(may_claims.is_err()); assert!("license has expired".to_string() == may_claims.err().unwrap()); } #[cfg(not(target_arch = "wasm32"))] #[tokio::test] async fn test_wrong_product() { let _m = mock("GET", "/api/sign") .with_header("content-type", "text/plain") .with_body(r#"fake_signed_token_data"#) .create(); let _m = mock("GET", "/api/validate/license") .with_header("content-type", "application/json") .with_body(r#"{"response": "validated"}"#) .create(); let token = generate_token(1577833200, 7889180400); let product = "unknown_product"; let signer_url = &mockito::server_url(); let may_claims = mio_license::validate(&token, signer_url, product).await; assert!(may_claims.is_err()); assert!("license not valid for this product".to_string() == may_claims.err().unwrap()); } #[cfg(target_arch = "wasm32")] #[wasm_bindgen_test] fn test_wasm_valid_token() { assert!(true); }