GLPI

GLPI open source ITSM solution.

Case Management · GLPI

Details

IDGLPI
ProviderOpen Source
CategoryCase Management
From Version6.2.0
Docker Imagedemisto/glpi:1.0.0.10133006
Supported ModulesAgentix XSIAM

README

GLPI open source ITSM solution
This integration was integrated and tested with version 9.5.5 of GLPI

Configure GLPI in Cortex

Parameter Description Required
Server URL (e.g. https://example.net/apirest.php)   True
Application Token   True
User Token   True
Fetch incidents   False
Incident type   False
Trust any certificate (not secure)   False
Use system proxy settings   False
Incidents Fetch Interval   False
Long running instance   False
Maximum number of incidents to mirror each time   False
Incident Mirror Direction Cortex XSOAR only parameter. False
Tag used for work note mirroring   False
Tag used for comment mirroring   False
Tag used for file mirroring   False
First fetch timestamp (<number> <time unit>, e.g., 12 hours, 7 days)   False
Max incidents fetch at the same time   False

Commands

You can execute these commands from the CLI, as part of an automation, or in a playbook.
After you successfully execute a command, a DBot message appears in the War Room with the command details.

glpi-create-user


Create a new GLPI user

Base Command

glpi-create-user

Input

Argument Name Description Required
name Login. Required
firstname Firstname. Optional
lastname Lastname. Optional
email Email address. Optional
password Password. Required
profile Profile name. Optional
additional_fields Additional fields in the format: fieldname1=value;fieldname2=value;. Optional

Context Output

Path Type Description
GLPI.User.id unknown Created user ID
GLPI.User.message unknown User creation message

glpi-delete-user


Delete GLPI user

Base Command

glpi-delete-user

Input

Argument Name Description Required
name Username. Required
purge Default : False, will use the trash, use True to definitively remove the user from the system. Possible values are: True, False. Default is False. Optional

Context Output

There is no context output for this command.

glpi-disable-user


Disable GLPI user

Base Command

glpi-disable-user

Input

Argument Name Description Required
name Username. Required

Context Output

There is no context output for this command.

glpi-enable-user


Enable GLPI user

Base Command

glpi-enable-user

Input

Argument Name Description Required
name Username. Required

Context Output

There is no context output for this command.

get-mapping-fields


Return the list of fields for an incident type

Base Command

get-mapping-fields

Input

| Argument Name | Description | Required |
| — | — | — |

Context Output

There is no context output for this command.

get-modified-remote-data


Mirroring feature, use only for debug

Base Command

get-modified-remote-data

Input

Argument Name Description Required
lastUpdate lastUpdate parameter. Possible values are: . Optional

Context Output

There is no context output for this command.

glpi-get-username


Get username by user ID

Base Command

glpi-get-username

Input

Argument Name Description Required
id User ID. Required

Context Output

Path Type Description
GLPI.User Unknown GLPI User details
GLPI.User.id unknown User id
GLPI.User.username unknown Username

glpi-delete-ticket


Delete GLPI ticket

Base Command

glpi-delete-ticket

Input

Argument Name Description Required
ticket_id Ticket ID. Required
purge Default : False, will use the trash, use True to definitively remove the ticket from the system. Possible values are: False, True. Default is False. Optional

Context Output

There is no context output for this command.

glpi-get-ticket


Get ticket details by ticket ID

Base Command

glpi-get-ticket

Input

Argument Name Description Required
ticket_id The Ticket ID. Required
get_attachments If “True” will retrieve ticket attachments. Default is “False”. Possible values are: False, True. Default is False. Optional

Context Output

Path Type Description
GLPI.Ticket.actiontime Unknown Ticket action time
GLPI.Ticket.begin_waiting_date Unknown Ticket begin waiting date
GLPI.Ticket.close_delay_stat Unknown Ticket close delay stat
GLPI.Ticket.closedate Unknown Tocket closed date
GLPI.Ticket.content Unknown Ticket content
GLPI.Ticket.date Unknown Ticket date
GLPI.Ticket.date_creation Unknown Ticket date creation
GLPI.Ticket.date_mod Unknown Ticket date modification
GLPI.Ticket.entities_id Unknown Ticket entities ids
GLPI.Ticket.global_validation Unknown Ticket global validation
GLPI.Ticket.id Unknown Ticket ID
GLPI.Ticket.impact Unknown Ticket Impact
GLPI.Ticket.internal_time_to_own Unknown Ticket internal time to own
GLPI.Ticket.internal_time_to_resolve Unknown Ticket internal time to resolve
GLPI.Ticket.is_deleted Unknown Ticket is_deleted?
GLPI.Ticket.itilcategories_id Unknown Ticket ITIL categories id
GLPI.Ticket.links Unknown Ticket links
GLPI.Ticket.locations_id Unknown Tickets locations id
GLPI.Ticket.name Unknown Ticket name
GLPI.Ticket.ola_ttr_begin_date Unknown Ticket ola ttr begin date
GLPI.Ticket.ola_waiting_duration Unknown Ticket ola waiting duration
GLPI.Ticket.olalevels_id_ttr Unknown Ticket ola levels id ttr
GLPI.Ticket.olas_id_tto Unknown Ticket olas id tto
GLPI.Ticket.olas_id_ttr Unknown Ticket olas id ttr
GLPI.Ticket.priority Unknown Ticket priority
GLPI.Ticket.requesttypes_id Unknown Ticket request types id
GLPI.Ticket.sla_waiting_duration Unknown Ticket sla waiting duration
GLPI.Ticket.slalevels_id_ttr Unknown Ticket slale levels id ttr
GLPI.Ticket.slas_id_tto Unknown Ticket slas id tto
GLPI.Ticket.slas_id_ttr Unknown Ticket stats id ttr
GLPI.Ticket.solve_delay_stat Unknown Ticket solve delay stat
GLPI.Ticket.solvedate Unknown Ticket solve date
GLPI.Ticket.status Unknown Ticket status
GLPI.Ticket.takeintoaccount_delay_stat Unknown Ticket take into account delay stat
GLPI.Ticket.time_to_own Unknown Ticket time to own
GLPI.Ticket.time_to_resolve Unknown Ticket time to resolve
GLPI.Ticket.type Unknown Ticket type
GLPI.Ticket.urgency Unknown Ticket urgency
GLPI.Ticket.users_id_lastupdater Unknown Ticket users id last updater
GLPI.Ticket.users_id_recipient Unknown Ticket users id recipient
GLPI.Ticket.validation_percent Unknown Ticket validation percent
GLPI.Ticket.waiting_duration Unknown Ticket waiting duration
File.Info string Attachment file info.
File.Name unknown Attachment file name.
File.Size number Attachment file size.
File.SHA1 string Attachment file SHA1 hash.
File.SHA256 string Attachment file SHA256 hash.
File.EntryID string Attachment file entry ID.
File.Type string Attachment file type.
File.MD5 string Attachment file MD5 hash.

glpi-create-ticket


Create a GLPI ticket

Base Command

glpi-create-ticket

Input

Argument Name Description Required
name Ticket name. Required
content Ticket description. Required
type Ticket type. Possible values are: Incident, Request. Required
status Ticket status. Possible values are: New, Processing(assigned), Processing(planned), Pending, Solved, Closed. Optional
urgency Ticket urgency. Possible values are: Veryhigh, High, Medium, Low, Verylow. Optional
impact Ticket impact. Possible values are: Veryhigh, High, Medium, Low, Verylow. Optional
priority Ticket priority. Possible values are: Major, Veryhigh, High, Medium, Low, Verylow. Optional
entryid File EntryID to upload , multiple files supported. Optional
entities_id Ticket entities ID. Optional
locations_id Ticket locations ID. Optional
itilcategories_id Ticket ITIL categories ID. Optional
time_to_resolve Ticket time to resolve, in the format: YYYY-MM-DD HH:MM:SS. Optional
time_to_own Ticket time to own, in the format: YYYY-MM-DD HH:MM:SS. Optional
internal_time_to_resolve Ticket internal time to resolve, in the format: YYYY-MM-DD HH:MM:SS. Optional
internal_time_to_own Ticket internal time to own, in the format: YYYY-MM-DD HH:MM:SS. Optional
requesttypes_id Ticket Request source. Possible values are: Direct, E-Mail, Helpdesk, Other, Phone, Written. Optional
additional_fields Additional fields in the format: fieldname1=value;fieldname2=value;. Optional

Context Output

Path Type Description
GLPI.Ticket.id unknown The created ticket ID
GLPI.Ticket.message unknown Result message from ticket creation

glpi-search


Search GLPI items

Base Command

glpi-search

Input

Argument Name Description Required
item_type Item type to search (ex : Ticket). Required
query The search query, please visit https://github.com/glpi-project/glpi/blob/master/apirest.md#search-items. Optional
forcedisplay Coma separated additional fields to display. Optional
srange Search range limit (ex : 0-50). Optional

Context Output

Path Type Description
GLPI.Search unknown Search results
GLPI.Search.item_type unknown This varies depending on the input item_type (case sensitive)

glpi-add-comment


Add comment to ticket ID

Base Command

glpi-add-comment

Input

Argument Name Description Required
ticket_id The ticket ID. Required
comment ticket comment. Possible values are: . Required

Context Output

Path Type Description
GLPI.Comment.id unknown Created comment ID
GLPI.Comment.message unknown GLPI message

glpi-upload-file


Upload document

Base Command

glpi-upload-file

Input

Argument Name Description Required
entryid File EntryID. Possible values are: . Required
filename Filename. Possible values are: . Optional
doc_name Doc name. Possible values are: . Optional

Context Output

Path Type Description
GLPI.Document Unknown Document
GLPI.Document.id unknown Created document ID
GLPI.Document.message unknown GLPI upload message
GLPI.Document.upload_result.filename.size unknown Uploaded file size
GLPI.Document.upload_result.filename.display unknown File display name
GLPI.Document.upload_result.filename.deleteUrl Unknown Document deleteURL
GLPI.Document.upload_result.filename.deleteType Unknown Document deleteType
GLPI.Document.upload_result.filename.name unknown system file name
GLPI.Document.upload_result.filename.url unknown Document URL
GLPI.Document.upload_result.filename.id Unknown Document filename id

glpi-get-item


Get item details by item type and item ID

Base Command

glpi-get-item

Input

Argument Name Description Required
item_type Item type. Possible values are: . Required
item_id Item ID. Possible values are: . Required

Context Output

Path Type Description
GLPI.item_type unknown This varies depending on the input item_type (case sensitive)

glpi-update-ticket


Update a GLPI ticket

Base Command

glpi-update-ticket

Input

Argument Name Description Required
id Ticket id. Required
description Ticket description. Optional
type Ticket type. Possible values are: Incident, Request. Optional
status Ticket status. Possible values are: New, Processing(assigned), Processing(planned), Pending, Solved, Closed. Optional
urgency Ticket urgency. Possible values are: Veryhigh, High, Medium, Low, Verylow. Optional
impact Ticket impact. Possible values are: Veryhigh, High, Medium, Low, Verylow. Optional
priority Ticket priority. Possible values are: Major, Veryhigh, High, Medium, Low, Verylow. Optional
entryid File EntryID to upload , multiple files supported. Optional
entities_id Ticket entities ID. Optional
locations_id Ticket locations ID. Optional
itilcategories_id Ticket ITIL categories ID. Optional
global_validation Global validation. Possible values are: . Optional
time_to_resolve Ticket time to resolve, in the format: YYYY-MM-DD HH:MM:SS. Optional
time_to_own Ticket time to own, in the format: YYYY-MM-DD HH:MM:SS. Optional
internal_time_to_resolve Ticket internal time to resolve, in the format: YYYY-MM-DD HH:MM:SS. Optional
internal_time_to_own Ticket internal time to own, in the format: YYYY-MM-DD HH:MM:SS. Optional
additional_fields Additional fields in the format: fieldname1=value;fieldname2=value;. Optional

Context Output

Path Type Description
GLPI.Ticket.id unknown The updated ticket ID
GLPI.Ticket.message unknown Result message from ticket update

get-remote-data


get remote data command

Base Command

get-remote-data

Input

Argument Name Description Required
id ticket id. Possible values are: . Optional
lastUpdate last update parameter. Possible values are: . Optional

Context Output

There is no context output for this command.

glpi-add-link


Link tickets

Base Command

glpi-add-link

Input

Argument Name Description Required
ticket_ID_1 First ticket ID. Required
ticket_ID_2 Second ticket ID. Required
link Relation between tickets. Possible values are: Link, Duplicate, Child, Parent. Required

Context Output

There is no context output for this command.

glpi-update-user


Update a user

Base Command

glpi-update-user

Input

| Argument Name | Description | Required |
| — | — | — |

Context Output

There is no context output for this command.

glpi-get-userid


Get GLPI User ID by Username

Base Command

glpi-get-userid

Input

Argument Name Description Required
name Username. Required

Context Output

Path Type Description
GLPI.User Unknown GLPI User details
GLPI.User.id unknown User id
GLPI.User.username unknown Username

Configuration parameters

  • url — Server URL (e.g. https://example.net/apirest.php) (required)
  • app_token — Application Token (required)
  • user_token — User Token (required)
  • isFetch — Fetch incidents
  • incidentType — Incident type
  • insecure — Trust any certificate (not secure)
  • proxy — Use system proxy settings
  • incidentFetchInterval — Incidents Fetch Interval
  • longRunning — Long running instance
  • mirror_limit — Maximum number of incidents to mirror each time
  • mirror_direction — Incident Mirror Direction
  • work_notes_tag — Tag used for work note mirroring
  • comment_tag — Tag used for comment mirroring
  • file_tag — Tag used for file mirroring
  • first_fetch — First fetch timestamp (<number> <time unit>, e.g., 12 hours, 7 days)
  • max_fetch — Max incidents fetch at the same time

Commands (19)

  • get-mapping-fields

    Return the list of fields for an incident type.

  • get-modified-remote-data

    Mirroring feature, use only for debug.

  • get-remote-data

    get remote data command.

  • glpi-add-comment

    Add comment to ticket ID.

  • glpi-add-link

    Link tickets.

  • glpi-create-ticket

    Create a GLPI ticket.

  • glpi-create-user

    Create a new GLPI user.

  • glpi-delete-ticket

    Delete GLPI ticket.

  • glpi-delete-user

    Delete GLPI user.

  • glpi-disable-user

    Disable GLPI user.

  • glpi-enable-user

    Enable GLPI user.

  • glpi-get-item

    Get item details by item type and item ID.

  • glpi-get-ticket

    Get ticket details by ticket ID.

  • glpi-get-userid

    Get GLPI User ID by Username.

  • glpi-get-username

    Get username by user ID.

  • glpi-search

    Search GLPI items.

  • glpi-update-ticket

    Update a GLPI ticket.

  • glpi-update-user

    Update a user.

  • glpi-upload-file

    Upload document.

"""GLPI Integration for Cortex XSOAR - Unit Tests file"""

import json
import ast

# from unittest.mock import Mock
import demistomock as demisto
from GLPI import (
    Client,
    upload_file_command,
    get_user_name_command,
    create_user_command,
    update_user_command,
    delete_user_command,
    enable_user_command,
    disable_user_command,
    add_link_command,
    add_comment_command,
    create_ticket_command,
    update_ticket_command,
    delete_ticket_command,
    get_ticket_command,
    get_item_command,
    search_command,
    fetch_incidents,
    get_mapping_fields_command,
    get_remote_data_command,
    update_remote_system_command,
    get_modified_remote_data_command,
    get_user_id_command,
)
from test_data.glpi_fetch_incidents import (
    FETCHINCIDENTS_SEARCHTICKET,
    FETCHINCIDENTS_TICKET,
    FETCHINCIDENTS_TICKETUSER,
    FETCHINCIDENTS_TICKETDOC,
    FETCHINCIDENTS_TICKETDOCFILE,
)


def util_load_json(path):
    with open(path, encoding="utf-8") as f:
        return json.loads(f.read())


def util_load_mock(path):
    with open(path) as f:
        data = f.read()
    return ast.literal_eval(data)


def util_load_mock_without_ast(path):
    with open(path) as f:
        return f.read()


MOCK_URL = "mock://myglpi.mydomain.tld/apirest.php"
PARAMETERS = {
    "base_url": "mock://myglpi.mydomain.tld/apirest.php",
    "app_token": "TESTAPPTOKEN",
    "auth_token": "TESTUSERTOKEN",
    "verify": "True",
    "first_fetch_time": "3 days",
    "mirror_limit": "100",
    "proxy": "False",
}
MOCK_INIT_SESSION = "/initSession"
TESTDATA_INITSESSION = {"session_token": "SAMPLETOKEN"}


def test_glpi_upload_file(requests_mock, mocker):
    command_mock_args = {"entryid": "3@101177"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_document = util_load_mock("test_data/glpi_upload_file_document.mock")
    requests_mock.post("mock://myglpi.mydomain.tld/apirest.php/Document", json=mock_response_document)
    client = Client(PARAMETERS)
    mocker.patch.object(demisto, "getFilePath", return_value={"id": id, "path": "test_data/test.txt", "name": "test.txt"})
    response = upload_file_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.Document"
    assert response.outputs == util_load_mock("test_data/glpi_upload_file_document.resmock")


def test_glpi_get_username(requests_mock):
    command_mock_args = {"id": 2}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_user = util_load_mock("test_data/glpi_getusername_user.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/user/2", json=mock_response_user)
    client = Client(PARAMETERS)
    response = get_user_name_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.User"
    assert response.outputs == {"id": 2, "username": "glpi"}


def test_glpi_get_userid(requests_mock):
    command_mock_args = {"name": "glpi"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_user = {"data": [{"1": "glpi", "2": 2}]}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/search/user", json=mock_response_user)
    client = Client(PARAMETERS)
    response = get_user_id_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.User"
    assert response.outputs == {"id": 2, "username": "glpi"}


def test_glpi_create_user(requests_mock):
    command_mock_args = {
        "name": "MyUser",
        "firstname": "MyFirstName",
        "lastname": "MyLastName",
        "email": "myuser@company.com",
        "password": "azerty1",
    }
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_user = [{"id": 31, "message": "Item successfully added: MyLastName MyFirstName"}]
    requests_mock.post("mock://myglpi.mydomain.tld/apirest.php/user", json=mock_response_user, status_code=201)
    client = Client(PARAMETERS)
    response = create_user_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.User"
    assert response.outputs == [{"id": 31, "message": "Item successfully added: MyLastName MyFirstName"}]


def test_glpi_update_user(requests_mock):
    command_mock_args = {"id": 31, "update_fields": "firstname=MyNewUser"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_user = [{"31": True, "message": ""}]
    requests_mock.put("mock://myglpi.mydomain.tld/apirest.php/user", json=mock_response_user)
    client = Client(PARAMETERS)
    response = update_user_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.User"


def test_glpi_delete_user(requests_mock):
    command_mock_args = {"name": "MyUser"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_search = {"data": [{"1": "MyUser", "2": 31}]}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/search/user", json=mock_response_search)
    mock_response_user = [{"31": True, "message": ""}]
    requests_mock.delete("mock://myglpi.mydomain.tld/apirest.php/user", json=mock_response_user)
    client = Client(PARAMETERS)
    response = delete_user_command(client, command_mock_args)
    assert response == "User MyUser successfully deleted"
    assert type(response) is not object


def test_glpi_enable_user(requests_mock):
    command_mock_args = {"name": "MyUser"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_search = {"data": [{"1": "MyUser", "2": 31}]}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/search/user", json=mock_response_search)
    mock_response_user = [{"31": True, "message": ""}]
    requests_mock.put("mock://myglpi.mydomain.tld/apirest.php/user", json=mock_response_user)
    client = Client(PARAMETERS)
    response = enable_user_command(client, command_mock_args)
    assert response == "User MyUser successfully enabled"
    assert type(response) is not object


def test_glpi_disable_user(requests_mock):
    command_mock_args = {"name": "MyUser"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_search = {"data": [{"1": "MyUser", "2": 31}]}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/search/user", json=mock_response_search)
    mock_response_user = [{"31": True, "message": ""}]
    requests_mock.put("mock://myglpi.mydomain.tld/apirest.php/user", json=mock_response_user)
    client = Client(PARAMETERS)
    response = disable_user_command(client, command_mock_args)
    assert response == "User MyUser successfully disabled"
    assert type(response) is not object


def test_glpi_add_comment(requests_mock):
    command_mock_args = {"ticket_id": 289, "comment": "My new comment"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_ticketfollowup = [{"id": 63, "message": ""}]
    requests_mock.post(
        "mock://myglpi.mydomain.tld/apirest.php/ticketfollowup", json=mock_response_ticketfollowup, status_code=201
    )
    client = Client(PARAMETERS)
    response = add_comment_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.Comment"
    assert response.outputs == [{"id": 63, "message": ""}]


def test_glpi_add_link(requests_mock):
    command_mock_args = {"ticket_ID_1": 289, "ticket_ID_2": 287, "link": "Duplicate"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_ticket = [{"id": 4, "message": "Item successfully added: General - ID 4"}]
    requests_mock.post("mock://myglpi.mydomain.tld/apirest.php/ticket_ticket", json=mock_response_ticket, status_code=201)
    client = Client(PARAMETERS)
    response = add_link_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.Link"
    assert response.outputs == [{"id": 4, "message": "Item successfully added: General - ID 4"}]


def test_glpi_create_ticket(requests_mock):
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    command_mock_args = {"name": "MyNewTicket", "type": "Incident", "content": "My Ticket description"}
    mock_response_ticket = [{"id": 290, "message": "Item successfully added: MyNewTicket"}]
    requests_mock.post("mock://myglpi.mydomain.tld/apirest.php/ticket", json=mock_response_ticket, status_code=201)
    client = Client(PARAMETERS)
    response = create_ticket_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.Ticket"
    assert response.outputs == [{"id": 290, "message": "Item successfully added: MyNewTicket"}]


def test_glpi_update_ticket(requests_mock):
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    command_mock_args = {"id": 290, "severity": 3, "description": "My new description"}
    mock_response_ticket = [{"290": True, "message": ""}]
    requests_mock.put("mock://myglpi.mydomain.tld/apirest.php/ticket", json=mock_response_ticket)
    client = Client(PARAMETERS)
    response = update_ticket_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.Ticket"
    assert response.outputs == [{"290": True, "message": ""}]


def test_glpi_delete_ticket(requests_mock):
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    command_mock_args = {"ticket_id": 290, "purge": True}
    mock_response_ticket = [{"290": True, "message": ""}]
    requests_mock.delete("mock://myglpi.mydomain.tld/apirest.php/ticket", json=mock_response_ticket)
    client = Client(PARAMETERS)
    response = delete_ticket_command(client, command_mock_args)
    assert response == "Ticket ID 290 successfully deleted"
    assert type(response) is not object


def test_glpi_get_ticket(requests_mock):
    command_mock_args = {"ticket_id": 289}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_ticket = util_load_mock("test_data/glpi_ticket_ticket.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/289", json=mock_response_ticket)
    mock_response_ticket_user = util_load_mock("test_data/glpi_ticket_user.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/289/Ticket_User", json=mock_response_ticket_user)
    mock_response_ticket_group = util_load_mock("test_data/glpi_ticket_group.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/289/Group_Ticket", json=mock_response_ticket_group)
    mock_response_ticketfollowup = util_load_mock("test_data/glpi_ticket_followup.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/289/ticketfollowup", json=mock_response_ticketfollowup)
    client = Client(PARAMETERS)
    response = get_ticket_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.Ticket"
    assert response.outputs == util_load_mock("test_data/glpi_ticket.resmock")
    assert response.outputs["id"] == 289


def test_glpi_get_item(requests_mock):
    command_mock_args = {"item_id": 2, "item_type": "user"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_user = util_load_mock("test_data/glpi_item.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/user/2", json=mock_response_user)
    client = Client(PARAMETERS)
    response = get_item_command(client, command_mock_args)
    assert response.outputs_prefix == "GLPI.user"
    assert response.outputs == util_load_mock("test_data/glpi_item.resmock")


def test_glpi_search(requests_mock):
    command_mock_args = {"query": [{"field": 1, "searchtype": "contains", "value": "^MyUser$"}], "item_type": "user"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_search = {"totalcount": 1, "count": 1, "data": {"33": {"1": "MyUser"}}}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/listSearchOptions/user", json=mock_response_search)
    mock_response_user = util_load_mock("test_data/glpi_search.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/search/user", json=mock_response_user)
    client = Client(PARAMETERS)
    response = search_command(client, command_mock_args)
    assert response[0].outputs_prefix == "GLPI.Search.User"
    assert response[0].outputs == {
        "name": "MyUser",
        "realname": "MyLastName",
        "UserEmail.email": "myuser@company.com",
        "phone": None,
        "Location.completename": None,
        "is_active": 1,
    }


def test_glpi_fetch_incidents(requests_mock):
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/search/ticket", json=FETCHINCIDENTS_SEARCHTICKET)
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/291", json=FETCHINCIDENTS_TICKET)
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/291/Ticket_User", json=FETCHINCIDENTS_TICKETUSER)
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/291/Group_Ticket", json=[])
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/291/Document_Item", json=FETCHINCIDENTS_TICKETDOC)
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/Document/33847", json=FETCHINCIDENTS_TICKETDOCFILE)
    last_run_mock = {"last_fetch": "2022-04-14T13:51:36"}
    client = Client(PARAMETERS)
    new_run, incidents = fetch_incidents(client=client, last_run=last_run_mock, max_results=50, first_fetch_time="3 days")
    assert new_run["last_fetch"] == "2022-04-14T13:51:36Z"
    assert len(incidents) == 1
    assert incidents[0]["name"] == "testing fetch incident"
    assert incidents[0]["occurred"] == "2022-04-14T11:47:34Z"
    assert incidents[0]["attachment"][0]["name"] == "testingupload.txt"


def test_glpi_get_mapping_fields():
    response = get_mapping_fields_command()
    assert response.extract_mapping() == util_load_mock("test_data/glpi_mapping.resmock")


def test_glpi_get_remote_data(requests_mock):
    command_mock_args = {"id": 289, "lastUpdate": "Thu Apr 07 2022 17:50:13 GMT+0200 (CEST)"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_ticket = util_load_mock("test_data/glpi_remotedata_ticket.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/289", json=mock_response_ticket)
    mock_response_ticketdocs = util_load_mock("test_data/glpi_remotedata_ticketdocs.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/289/Document_Item", json=mock_response_ticketdocs)
    mock_response_document = util_load_mock("test_data/glpi_remotedata_document.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/Document/33836", json=mock_response_document)
    mock_response_ticketfollowup = util_load_mock("test_data/glpi_ticket_followup.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/ticket/289/ticketfollowup", json=mock_response_ticketfollowup)
    client = Client(PARAMETERS)
    response = get_remote_data_command(client, command_mock_args, params={})
    assert response.entries[0]["File"] == "test_file.txt"
    assert response.entries[0]["ContentsFormat"] == "text"
    assert response.mirrored_object == util_load_mock("test_data/glpi_remotedata_mirror.resmock")


def test_glpi_update_remote_system(requests_mock):
    command_mock_args = {"remoteId": 289, "delta": {}, "data": {}}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    client = Client(PARAMETERS)
    response = update_remote_system_command(client, command_mock_args)
    assert response == 289


def test_glpi_get_modified_remote_data(requests_mock):
    command_mock_args = {"lastUpdate": "Thu Apr 07 2022 17:50:13 GMT+0200 (CEST)"}
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/initSession", json=TESTDATA_INITSESSION)
    mock_response_search = util_load_mock("test_data/glpi_modified_remotedata_search.mock")
    requests_mock.get("mock://myglpi.mydomain.tld/apirest.php/search/ticket", json=mock_response_search)
    client = Client(PARAMETERS)
    response = get_modified_remote_data_command(client, command_mock_args, 50)
    assert response.modified_incident_ids == [
        "146",
        "193",
        "260",
        "279",
        "230",
        "289",
        "197",
        "278",
        "133",
        "250",
        "212",
        "144",
        "192",
        "186",
        "187",
        "156",
        "210",
        "259",
        "282",
        "135",
        "207",
        "184",
        "163",
        "169",
        "245",
        "258",
        "149",
        "136",
        "152",
        "221",
        "285",
        "191",
        "147",
        "132",
        "275",
        "142",
        "219",
        "244",
        "231",
        "200",
        "280",
        "178",
        "199",
        "246",
        "263",
        "261",
        "288",
        "220",
        "264",
        "176",
        "235",
    ]