---
# These files are pre formatted and we want to get the base64 value so we can use it for httpbin
# dynamic content in checksum_url
- name: set base64 values for checksum files
  set_fact:
    sha1sum: '{{ lookup("file", "ftp/anon/sha1sum.txt") | b64encode }}'
    sha256sum: '{{ lookup("file", "ftp/anon/sha256sum.txt") | b64encode }}'
    sha256sum_dot: '{{ lookup("file", "ftp/anon/sha256sum_with_dot.txt") | b64encode }}'

- name: download file with sha1 checksum url
  win_get_url:
    url: https://{{ httpbin_host }}/base64/cHR1eA==
    dest: '{{ testing_dir }}\sha1.txt'
    checksum_url: https://{{ httpbin_host }}/base64/{{ sha1sum }}
    force: True
  register: download_sha1_url

- name: download file with sha1 checksum value
  win_get_url:
    url: https://{{ httpbin_host }}/base64/cHR1eA==
    dest: '{{ testing_dir }}\sha1.txt'
    checksum: a97e6837f60cec6da4491bab387296bbcd72bdba
    force: True
  register: download_sha1_value

- name: assert download file with sha1 checksum
  assert:
    that:
    - download_sha1_url is changed
    - download_sha1_url.status_code == 200
    - download_sha1_url.checksum_dest == 'a97e6837f60cec6da4491bab387296bbcd72bdba'
    - not download_sha1_value is changed
    - download_sha1_value.status_code == 200
    - download_sha1_value.checksum_dest == 'a97e6837f60cec6da4491bab387296bbcd72bdba'

- name: download file with sha256 checksum ftp
  win_get_url:
    url: https://{{ httpbin_host }}/base64/cHR1eA==
    dest: '{{ testing_dir }}\sha256.txt'
    checksum_url: ftp://localhost/anon/sha256sum.txt  # tests that a checksum can be from an FTP URI
    checksum_algorithm: sha256
    force: True
  register: download_sha256_ftp

- name: download file with sha256 checksum dot leading source as file
  win_get_url:
    url: https://{{ httpbin_host }}/base64/cHR1eA==
    dest: '{{ testing_dir }}\sha256.txt'
    checksum_url: '{{ testing_dir }}\ftp\anon\sha256sum_with_dot.txt'
    checksum_algorithm: sha256
    force: True
  register: download_sha256_file

- name: download file with sha256 checksum value
  win_get_url:
    url: https://{{ httpbin_host }}/base64/cHR1eA==
    dest: '{{ testing_dir }}\sha256.txt'
    checksum: b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006
    checksum_algorithm: sha256
  register: download_sha256_value

- name: assert download file with sha256 checksum
  assert:
    that:
    - download_sha256_ftp is changed
    - download_sha256_ftp.status_code == 200
    - download_sha256_ftp.checksum_dest == 'b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006'
    - not download_sha256_file is changed
    - download_sha256_file.status_code == 200
    - download_sha256_file.checksum_dest == 'b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006'
    - not download_sha256_value is changed
    - download_sha256_value.status_code == 200
    - download_sha256_value.checksum_dest == 'b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006'

- name: fail download with invalid checksum and force=no
  win_get_url:
    url: https://{{ httpbin_host }}/base64/cHR1eA==
    dest: '{{ testing_dir }}\fail.txt'
    checksum: invalid
    force: no
  register: fail_checksum_force_no
  failed_when: fail_checksum_force_no.msg != "The checksum for https://" + httpbin_host + "/base64/cHR1eA== did not match 'invalid', it was 'a97e6837f60cec6da4491bab387296bbcd72bdba'"

- name: fail download with invalid checksum and force=yes
  win_get_url:
    url: https://{{ httpbin_host }}/base64/cHR1eA==
    dest: '{{ testing_dir }}\fail.txt'
    checksum: invalid
    force: yes
  register: fail_checksum_force_yes
  failed_when: fail_checksum_force_yes.msg != "The checksum for https://" + httpbin_host + "/base64/cHR1eA== did not match 'invalid', it was 'a97e6837f60cec6da4491bab387296bbcd72bdba'"
