# https://github.com/ansible/ansible/issues/42484
# Skipping macOS for now since there is a bug when changing home directory
- name: Test home directory creation
  when: ansible_facts.system != 'Darwin'
  block:
    - name: create user specifying home
      user:
        name: ansibulluser
        state: present
        home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser"
      register: user_test3_0

    - name: create user again specifying home
      user:
        name: ansibulluser
        state: present
        home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser"
      register: user_test3_1

    - name: change user home
      user:
        name: ansibulluser
        state: present
        home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser-mod"
      register: user_test3_2

    - name: change user home back
      user:
        name: ansibulluser
        state: present
        home: "{{ user_home_prefix[ansible_facts.system] }}/ansibulluser"
      register: user_test3_3

    - name: validate results for testcase 3
      assert:
        that:
          - user_test3_0 is not changed
          - user_test3_1 is not changed
          - user_test3_2 is changed
          - user_test3_3 is changed

# https://github.com/ansible/ansible/issues/41393
# Create a new user account with a path that has parent directories that do not exist
- name: Create user with home path that has parents that do not exist
  user:
    name: ansibulluser2
    state: present
    home: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
  register: create_home_with_no_parent_1

- name: Create user with home path that has parents that do not exist again
  user:
    name: ansibulluser2
    state: present
    home: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
  register: create_home_with_no_parent_2

- name: Check the created home directory
  stat:
    path: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
  register: home_with_no_parent_3

- name: Ensure user with non-existing parent paths was created successfully
  assert:
    that:
      - create_home_with_no_parent_1 is changed
      - create_home_with_no_parent_1.home == user_home_prefix[ansible_facts.system] ~ '/in2deep/ansibulluser2'
      - create_home_with_no_parent_2 is not changed
      - home_with_no_parent_3.stat.uid == create_home_with_no_parent_1.uid
      - home_with_no_parent_3.stat.gr_name == default_user_group[ansible_facts.distribution] | default('ansibulluser2')

- name: Cleanup test account
  user:
    name: ansibulluser2
    home: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/ansibulluser2"
    state: absent
    remove: yes

- name: Remove testing dir
  file:
    path: "{{ user_home_prefix[ansible_facts.system] }}/in2deep/"
    state: absent


# https://github.com/ansible/ansible/issues/60307
# Make sure we can create a user when the home directory is missing
- name: Create user with home path that does not exist
  user:
    name: ansibulluser3
    state: present
    home: "{{ user_home_prefix[ansible_facts.system] }}/nosuchdir"
    createhome: no

- name: Cleanup test account
  user:
    name: ansibulluser3
    state: absent
    remove: yes

# https://github.com/ansible/ansible/issues/70589
# Create user with create_home: no and parent directory does not exist.
- name: "Check if parent dir for home dir for user exists (before)"
  stat:
    path: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir"
  register: create_user_no_create_home_with_no_parent_parent_dir_before

- name: "Create user with create_home == no and home path parent dir does not exist"
  user:
    name: randomuser
    state: present
    create_home: false
    home: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir/randomuser"
  register: create_user_no_create_home_with_no_parent

- name: "Check if parent dir for home dir for user exists (after)"
  stat:
    path: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir"
  register: create_user_no_create_home_with_no_parent_parent_dir_after

- name: "Check if home for user is created"
  stat:
    path: "{{ user_home_prefix[ansible_facts.system] }}/thereisnodir/randomuser"
  register: create_user_no_create_home_with_no_parent_home_dir

- name: "Ensure user with non-existing parent paths with create_home: no was created successfully"
  assert:
    that:
      - not create_user_no_create_home_with_no_parent_parent_dir_before.stat.exists
      - not create_user_no_create_home_with_no_parent_parent_dir_after.stat.isdir is defined
      - not create_user_no_create_home_with_no_parent_home_dir.stat.exists

- name: Cleanup test account
  user:
    name: randomuser
    state: absent
    remove: yes
