mod common;

#[test]
fn valid_license() {
    let ditto = common::get_builder_ditto(None).unwrap();
    valid_license_impl(ditto);

    let ditto = common::get_revamp_ditto(None).unwrap();
    valid_license_impl(ditto);

    let ditto = common::get_revamp_ditto_async_sync(None).unwrap();
    valid_license_impl(ditto);
}

fn valid_license_impl(ditto: dittolive_ditto::prelude::Ditto) {
    let license_token = std::env::var("DITTO_LICENSE").expect("No License Env Var provided");
    let res = ditto.set_offline_only_license_token(&license_token);
    assert!(res.is_ok());
}

#[test]
fn license_nonsense() {
    let ditto = common::get_builder_ditto(None).unwrap();
    license_nonsense_impl(ditto);

    let ditto = common::get_revamp_ditto(None).unwrap();
    license_nonsense_impl(ditto);

    let ditto = common::get_revamp_ditto_async_sync(None).unwrap();
    license_nonsense_impl(ditto);
}

fn license_nonsense_impl(ditto: dittolive_ditto::prelude::Ditto) {
    let invalid_license = "this is total nonsense and not base64";
    let res = ditto.set_offline_only_license_token(invalid_license);
    assert!(res.is_err());
    let err = res.err().unwrap();
    assert_eq!(err.to_string(), "The license failed verification. Obtain a valid license token at https://portal.ditto.live.");
}

#[test]
fn license_expired() {
    let ditto = common::get_builder_ditto(None).unwrap();
    license_expired_impl(ditto);

    let ditto = common::get_revamp_ditto(None).unwrap();
    license_expired_impl(ditto);

    let ditto = common::get_revamp_ditto_async_sync(None).unwrap();
    license_expired_impl(ditto);
}

fn license_expired_impl(ditto: dittolive_ditto::prelude::Ditto) {
    let expired_license = "o2d1c2VyX2lkb3Rlc3RAZGl0dG8ubGl2ZWZleHBpcnl0MjAxOC0xMC0zMVQwMDowMDowMFppc2lnbmF0dXJleFh1U2xqVTZyT2xSMVNKQ2RDNFJoakNjYWR1NkdyV1Z3MUV4anlDRlNuQ3l2cjA5OU9iaGV6RGMwUnRFTEE2RmJxdk9KcHhOWU1Zbk5VeFNubnlpUExUZz09";
    let res = ditto.set_offline_only_license_token(expired_license);
    assert!(res.is_err());
    let err = res.err().unwrap();
    assert_eq!(err.to_string(), "The license expired on 2018-10-31 00:00:00 UTC. Obtain a valid license token at https://portal.ditto.live.");
}

#[test]
fn license_tampered() {
    let ditto = common::get_builder_ditto(None).unwrap();
    license_tampered_impl(ditto);

    let ditto = common::get_revamp_ditto(None).unwrap();
    license_tampered_impl(ditto);

    let ditto = common::get_revamp_ditto_async_sync(None).unwrap();
    license_tampered_impl(ditto);
}

fn license_tampered_impl(ditto: dittolive_ditto::prelude::Ditto) {
    let tampered_license = "o2d1c2VyX2lkcnJ1c3NlbGxAZGl0dG8ubGl2ZWZleHBpcnl4HjIwMTktMDktMThUMTA6MzM6MTkuNTU4NzYzODk4WmlzaWduYXR1cmV4WDdieUtId080b1dlLy9PbUllNlcrZ2EwZS9mdlQ0R3hxTCs2OFEyZ2JFWEJvYU9LOGJsaUd5dktoNEVzc2FYaFQybVFMOEtHalZGYWtWUjlibG5jNzB3PT0=";
    let res = ditto.set_offline_only_license_token(tampered_license);
    assert!(res.is_err());
    let err = res.err().unwrap();
    assert_eq!(err.to_string(), "The license failed verification. Obtain a valid license token at https://portal.ditto.live.");
}

#[test]
fn license_on_online_instance() {
    let ditto = common::get_online_ditto().unwrap();
    let some_license = "some-license-that-will-never-be-set";
    let result = ditto.set_offline_only_license_token(some_license);

    assert!(result.is_err());
    assert_eq!(
        result.err().unwrap().to_string(),
        "Offline license tokens should only be used for Manual, SharedKey or OfflinePlayground \
         identities"
    );
}
