use migamake_api_cloudflare::parameters::DnsRecordType; use migamake_api_cloudflare::{dns, zone, Cloudflare}; use std::env; /// The tests assume a root domain. The root domain should not be deleted. /// The tests will be executed for rust-test. /// These 2 keys need to be exported: /// export CLOUDFLARE_DOMAIN= /// export CLOUDFLARE_ZONE= pub const SUB_DOMAIN: &str = "rust-test"; #[test] fn get_zoneid_for_domain() { let test_domain = format!("{}.{}", SUB_DOMAIN, env::var("CLOUDFLARE_DOMAIN").unwrap()); let expected_zoneid = env::var("CLOUDFLARE_ZONE").unwrap(); let cloudflare = Cloudflare::default(None); let zone = zone::Zone::default(test_domain); let result = cloudflare.list(zone); println!("{:?}", result); assert_eq!(result.success, true); assert_eq!(result.result_info.unwrap().count, 1); assert_eq!(result.result.unwrap()[0].id, expected_zoneid); } #[test] fn add_record_for_domain() { let test_domain = format!("{}.{}", SUB_DOMAIN, env::var("CLOUDFLARE_DOMAIN").unwrap()); let expected_zoneid = env::var("CLOUDFLARE_ZONE").unwrap(); let cloudflare = Cloudflare::default(None); // check TXT dns record exists let dns_result = cloudflare.list_dns_record(DnsRecordType::TXT, &test_domain, &expected_zoneid); let res = dns_result.result.unwrap(); // if dns record exists, delete if dns_result.success && res.len() == 1 { let dns_record_id = &res[0].id; cloudflare.delete_dns_record(&expected_zoneid, dns_record_id); } let mut txt_dns_record = dns::TXTRecord::new(); txt_dns_record.name = test_domain; txt_dns_record.content = "bring your own lib".into(); let result = cloudflare.create_dns_record(txt_dns_record, &expected_zoneid); assert_eq!(result.unwrap().success, true); } #[test] fn remove_dns_record_for_domain() { let test_domain = format!("{}.{}", SUB_DOMAIN, env::var("CLOUDFLARE_DOMAIN").unwrap()); let expected_zoneid = env::var("CLOUDFLARE_ZONE").unwrap(); let cloudflare = Cloudflare::default(None); //get dns record id let dns_result = cloudflare.list_dns_record(DnsRecordType::A, &test_domain, &expected_zoneid); let res = dns_result.result.unwrap(); // if dns record exists, get the id let dns_record_id: String = if dns_result.success && res.len() == 1 { res[0].id.clone() } else { // if dns record does not exist, create let mut dns_record = dns::ARecord::new(); dns_record.name = test_domain; dns_record.content = "127.0.0.1".into(); let result = cloudflare.create_dns_record(dns_record, &expected_zoneid); result.unwrap().result.unwrap().id }; let result = cloudflare.delete_dns_record(&expected_zoneid, &dns_record_id); assert_eq!(result.result.id, dns_record_id); } #[test] fn add_mxrecord_fails_with_incorrectdns() { let test_domain = format!("{}.{}", SUB_DOMAIN, env::var("CLOUDFLARE_DOMAIN").unwrap()); let expected_zoneid = env::var("CLOUDFLARE_ZONE").unwrap(); let cloudflare = Cloudflare::default(None); let mut txt_dns_record = dns::MXRecord::new(); txt_dns_record.name = test_domain; txt_dns_record.content = "mx ".into(); let result = cloudflare.create_dns_record(txt_dns_record, &expected_zoneid); println!("{:?}", result); let data = result.unwrap(); assert_eq!(data.success, false); assert_eq!(data.errors[0].code, 1004); }