# Due to the special environment setup required for proxies, you can manually test out the proxy options with
# https://github.com/jborean93/ansible-powershell-url-proxy.
---
- name: set fact out special testing dir
  set_fact:
    testing_dir: '{{ remote_tmp_dir }}\win_get_url .ÅÑŚÌβŁÈ [$!@^&test(;)]'

- name: create test directory with space and special chars
  win_file:
    path: '{{ testing_dir }}'
    state: directory

- name: copy across testing files
  win_copy:
    src: files/
    dest: '{{ testing_dir }}'

# False positive in Windows Defender is flagging the file as a virus and removing it. We need to add an exclusion so
# the tests continue to work
- name: add exclusion for the SlimFTPd binary
  win_defender_exclusion:
    path: '{{ remote_tmp_dir | win_dirname }}'
    state: present

- name: download SlimFTPd binary
  win_get_url:
    url: https://ansible-ci-files.s3.amazonaws.com/test/integration/roles/test_win_get_url/SlimFTPd.exe
    dest: '{{ testing_dir }}\SlimFTPd.exe'

# SlimFTPd does not work with special chars because it is so old, use a symlink as a workaround
- name: set fact of temp symlink for SlimFTPd
  set_fact:
    slimftpd_link: C:\SlimFTPd

- name: template SlimFTPd configuration file
  win_template:
    src: slimftpd.conf.tmpl
    dest: '{{ testing_dir }}\slimftpd.conf'

- block:
  - name: create SLimFTPd symlink
    win_command: cmd.exe /c mklink /d "{{ slimftpd_link }}" "{{ testing_dir }}"

  - name: create SlimFTPd service
    win_service:
      name: SlimFTPd
      path: '"{{ slimftpd_link }}\SlimFTPd.exe" -service'
      state: started
      dependencies:
      - tcpip

  - name: run URL tests
    import_tasks: tests_url.yml

  - name: run FTP tests
    import_tasks: tests_ftp.yml

  - name: run checksum tests
    import_tasks: tests_checksum.yml

  always:
  - name: remove SlimFTPd service
    win_service:
      name: SlimFTPd
      state: absent

  - name: remove temp symlink
    win_file:
      path: '{{ slimftpd_link }}'
      state: absent

  - name: remove exclusion for the SlimFTPd binary
    win_defender_exclusion:
      path: '{{ remote_tmp_dir | win_dirname }}'
      state: absent
