---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

- block:
  - name: Log out server
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      username: testuser
      password: hunter2
      state: absent

  - name: Log in with wrong password (check mode)
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      username: testuser
      password: "1234"
      state: present
    register: login_failed_check
    ignore_errors: true
    check_mode: true

  - name: Log in with wrong password
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      username: testuser
      password: "1234"
      state: present
    register: login_failed
    ignore_errors: true

  - name: Make sure that login failed
    assert:
      that:
        - login_failed_check is failed
        - "('login attempt to http://' ~ registry_frontend_address ~ '/v2/ failed') in login_failed_check.msg"
        - login_failed is failed
        - "('login attempt to http://' ~ registry_frontend_address ~ '/v2/ failed') in login_failed.msg"

  - name: Log in (check mode)
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      username: testuser
      password: hunter2
      state: present
    register: login_1
    check_mode: true

  - name: Log in
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      username: testuser
      password: hunter2
      state: present
    register: login_2

  - name: Get permissions of ~/.docker/config.json
    stat:
      path: ~/.docker/config.json
    register: login_2_stat

  - name: Log in (idempotent)
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      username: testuser
      password: hunter2
      state: present
    register: login_3

  - name: Log in (idempotent, check mode)
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      username: testuser
      password: hunter2
      state: present
    register: login_4
    check_mode: true

  - name: Make sure that login worked
    assert:
      that:
        - login_1 is changed
        - login_2 is changed
        - login_3 is not changed
        - login_4 is not changed
        - login_2_stat.stat.mode == '0600'

  - name: Log in again with wrong password (check mode)
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      username: testuser
      password: "1234"
      state: present
    register: login_failed_check
    ignore_errors: true
    check_mode: true

  - name: Log in again with wrong password
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      username: testuser
      password: "1234"
      state: present
    register: login_failed
    ignore_errors: true

  - name: Make sure that login failed again
    assert:
      that:
        - login_failed_check is failed
        - "('login attempt to http://' ~ registry_frontend_address ~ '/v2/ failed') in login_failed_check.msg"
        - login_failed is failed
        - "('login attempt to http://' ~ registry_frontend_address ~ '/v2/ failed') in login_failed.msg"

  - name: Log out (check mode)
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      state: absent
    register: logout_1
    check_mode: true

  - name: Log out
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      state: absent
    register: logout_2

  - name: Log out (idempotent)
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      state: absent
    register: logout_3

  - name: Log out (idempotent, check mode)
    docker_login:
      registry_url: "{{ registry_frontend_address }}"
      state: absent
    register: logout_4
    check_mode: true

  - name: Make sure that login worked
    assert:
      that:
        - logout_1 is changed
        - logout_2 is changed
        - logout_3 is not changed
        - logout_4 is not changed

  when: registry_frontend_address != 'n/a'
