---
- name: Generate an org name
  set_fact:
    org_name: "AWX-Collection-tests-tower_organization-org-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"

- name: Make sure {{ org_name }} is not there
  tower_organization:
    name: "{{ org_name }}"
    state: absent
  register: result

- name: "Create a new organization"
  tower_organization:
    name: "{{ org_name }}"
    galaxy_credentials:
      - Ansible Galaxy
  register: result

- assert:
    that: "result is changed"

- name: "Make sure making the same org is not a change"
  tower_organization:
    name: "{{ org_name }}"
  register: result

- assert:
    that:
      - "result is not changed"

- name: "Try adding a bad custom_virtualenv"
  tower_organization:
    name: "{{ org_name }}"
    custom_virtualenv: "/does/not/exit"
  register: result
  ignore_errors: true

- assert:
    that:
      - "result is failed"

- name: "Pass in all parameters"
  tower_organization:
    name: "{{ org_name }}"
    description: "A description"
    custom_virtualenv: ""
  register: result

- assert:
    that:
      - "result is changed"

- name: "Change the description"
  tower_organization:
    name: "{{ org_name }}"
    description: "A new description"
    custom_virtualenv: ""
  register: result

- assert:
    that:
      - "result is changed"

- name: "Remove the organization"
  tower_organization:
    name: "{{ org_name }}"
    state: absent
  register: result

- assert:
    that:
      - "result is changed"

- name: "Remove a missing organization"
  tower_organization:
    name: "{{ org_name }}"
    state: absent
  register: result

- assert:
    that:
      - "result is not changed"

# Test behaviour common to all tower modules
- name: Check that SSL is available and verify_ssl is enabled (task must fail)
  tower_organization:
    name: Default
    validate_certs: true
  ignore_errors: true
  register: check_ssl_is_used

- name: Check that connection failed
  assert:
    that:
      - "'CERTIFICATE_VERIFY_FAILED' in check_ssl_is_used['msg']"

- name: Check that verify_ssl is disabled (task must not fail)
  tower_organization:
    name: Default
    validate_certs: false
