---
######## TXT record tests #################

- set_fact:
    # values breaking the api: ,<>:
    txt_teststring: 'abc123 !@#$%^&*()_+=-;./{}?\|'

- name: "Test: TXT record creation"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: TXT
    value: "{{ txt_teststring }}"
    ttl: 150
  register: cloudflare_dns

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

- name: "Test: TXT record idempotency"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: TXT
    value: "{{ txt_teststring }}"
    ttl: 150
  register: cloudflare_dns

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

- name: "Test: TXT record update"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: TXT
    value: "{{ txt_teststring }}"
    ttl: 300
  register: cloudflare_dns

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

- name: "Test: TXT 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: TXT
    value: 'teststring'
    ttl: 150
  register: cloudflare_dns

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

- name: "Test: TXT 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: TXT
    value: "{{ txt_teststring }}"
    ttl: 300
  register: cloudflare_dns

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

- name: "Test: TXT 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: TXT
    value: 'teststring'
    ttl: 150
    solo: true
  register: cloudflare_dns

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

- name: "Test: TXT 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: TXT
    value: "{{ txt_teststring }}"
    ttl: 300
    state: absent
  register: cloudflare_dns

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

- name: "Test: TXT record deletion"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: TXT
    value: 'teststring'
    ttl: 150
    state: absent
  register: cloudflare_dns

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

- name: "Test: TXT record deletion succeeded"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: TXT
    value: 'teststring'
    ttl: 150
    state: absent
  register: cloudflare_dns

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