---
######## MX record tests #################

- name: "Test: MX record creation"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: MX
    value: "mx1-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}."
    ttl: 150
    priority: 20
  register: cloudflare_dns

- name: "Validate: MX record creation"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == 'mx1-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.priority == 20
      - cloudflare_dns.result.record.type == 'MX'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: MX record idempotency"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: MX
    value: "mx1-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}."
    ttl: 150
    priority: 20
  register: cloudflare_dns

- name: "Validate: MX record idempotency"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed

- name: "Test: MX record update"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: MX
    value: "mx1-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 300
    priority: 10
  register: cloudflare_dns

- name: "Validate: MX record update"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.ttl == 300
      - cloudflare_dns.result.record.priority == 10

- name: "Test: MX record duplicate (create new record)"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: MX
    value: "mx2-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 150
    priority: 30
  register: cloudflare_dns

- name: "Validate: MX record duplicate (create new record)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == 'mx2-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.priority == 30
      - cloudflare_dns.result.record.type == 'MX'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: MX record duplicate (old record present)"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: MX
    value: "mx1-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 300
    priority: 10
  register: cloudflare_dns

- name: "Validate: MX record duplicate (old record present)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed
      - cloudflare_dns.result.record.content == 'mx1-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.ttl == 300
      - cloudflare_dns.result.record.priority == 10
      - cloudflare_dns.result.record.type == 'MX'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: MX record duplicate (make new record solo)"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: MX
    value: "mx2-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 150
    priority: 30
    solo: true
  register: cloudflare_dns

- name: "Validate: MX record duplicate (make new record solo)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == 'mx2-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.priority == 30
      - cloudflare_dns.result.record.type == 'MX'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: MX record duplicate (old record absent)"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: MX
    value: "mx1-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 300
    priority: 10
    state: absent
  register: cloudflare_dns

- name: "Validate: MX record duplicate (old record absent)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed

- name: "Test: MX record deletion"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: MX
    value: "mx2-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 150
    priority: 30
    state: absent
  register: cloudflare_dns

- name: "Validate: MX record deletion"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed

- name: "Test: MX record deletion succeeded"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: MX
    value: "mx2-{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 150
    priority: 30
    state: absent
  register: cloudflare_dns

- name: "Validate: MX record deletion succeeded"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed
