---
- name: Verify
  hosts: all
  gather_facts: false

  vars_files:
    - ../../defaults/main.yml

  tasks:
    - name: check if webserver is up
      uri:
        url: "http://{{ paperlessng_listen_address }}:{{ paperlessng_listen_port }}"
        status_code: [200, 302]
        return_content: yes
      register: landingpage
      failed_when: "'Sign in</button>' not in landingpage.content"

    - name: generate random name and content
      set_fact:
        content: "{{ lookup('password', '/dev/null length=65536 chars=ascii_letters') }}"
        filename: "{{ lookup('password', '/dev/null length=8 chars=ascii_letters') }}"

    - name: check if document posting works
      uri:
        url: "http://{{ paperlessng_listen_address }}:{{ paperlessng_listen_port }}/api/documents/post_document/"
        method: POST
        body_format: form-multipart
        body:
          document:
            content: "{{ content }}"
            filename: "{{ filename }}.txt"
        headers:
          Authorization: 'Basic {{ (paperlessng_superuser_name + ":" + paperlessng_superuser_password) | b64encode }}'
          Content-Type: text/plain
        return_content: yes
      register: post_document
      failed_when: "'OK' not in post_document.content"

    - name: verify uploaded document has been accepted
      uri:
        url: "http://{{ paperlessng_listen_address }}:{{ paperlessng_listen_port }}/api/logs/paperless/"
        headers:
          Authorization: 'Basic {{ (paperlessng_superuser_name + ":" + paperlessng_superuser_password) | b64encode }}'
        return_content: yes
      register: logs
      failed_when: "('Consuming ' + filename + '.txt') not in logs.content"

    - name: sleep till consumption finished
      pause:
        seconds: 10

    - name: verify uploaded document has been consumed
      uri:
        url: "http://{{ paperlessng_listen_address }}:{{ paperlessng_listen_port }}/api/logs/paperless/"
        headers:
          Authorization: 'Basic {{ (paperlessng_superuser_name + ":" + paperlessng_superuser_password) | b64encode }}'
        return_content: yes
      register: logs
      failed_when: "filename + ' consumption finished' not in logs.content"

    - name: get documents
      uri:
        url: "http://{{ paperlessng_listen_address }}:{{ paperlessng_listen_port }}/api/documents/"
        headers:
          Authorization: 'Basic {{ (paperlessng_superuser_name + ":" + paperlessng_superuser_password) | b64encode }}'
        return_content: yes
      register: documents

    - name: set document index
      set_fact:
        index: "{{ documents.json['results'][0]['id'] }}"

    - name: verify uploaded document is avaiable
      uri:
        url: "http://{{ paperlessng_listen_address }}:{{ paperlessng_listen_port }}/api/documents/{{ index }}/"
        headers:
          Authorization: 'Basic {{ (paperlessng_superuser_name + ":" + paperlessng_superuser_password) | b64encode }}'
        return_content: yes
      register: document
      failed_when: "'Not found.' in document.content or content not in document.json['content']"

    - name: check if deleting uploaded document works
      uri:
        url: "http://{{ paperlessng_listen_address }}:{{ paperlessng_listen_port }}/api/documents/bulk_edit/"
        method: POST
        body_format: json
        body:
          documents: ["{{ index }}"]
          method: delete
          parameters: {}
        headers:
          Authorization: 'Basic {{ (paperlessng_superuser_name + ":" + paperlessng_superuser_password) | b64encode }}'
      register: delete_document
      failed_when: "'OK' not in delete_document.json['result']"
