aboutsummaryrefslogtreecommitdiff
path: root/test_utils/managers/network/conftest.py
blob: 70fda4e3811c18714face7e34665d61beda3ccda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
from datetime import datetime, timezone
from typing import Callable, TYPE_CHECKING
from uuid import uuid4

import pytest

from generalresearch.managers.network.label import IPLabelManager
from generalresearch.managers.network.nmap import NmapManager
from generalresearch.managers.network.tool_run import ToolRunManager
from generalresearch.models.network.rdns import (
    RDNSResult,
    get_dig_version,
    get_dig_rdns_command,
)
from generalresearch.models.network.tool_run import (
    RDnsRun,
    ToolName,
    ToolClass,
    Status,
)
from generalresearch.models.network.tool_utils import ToolRunCommand
from generalresearch.models.network.xml_parser import NmapXmlParser


@pytest.fixture(scope="session")
def iplabel_manager(thl_web_rw) -> IPLabelManager:
    assert "/unittest-" in thl_web_rw.dsn.path

    return IPLabelManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def nmap_manager(thl_web_rw) -> NmapManager:
    assert "/unittest-" in thl_web_rw.dsn.path

    return NmapManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def toolrun_manager(thl_web_rw) -> ToolRunManager:
    assert "/unittest-" in thl_web_rw.dsn.path

    return ToolRunManager(pg_config=thl_web_rw)


@pytest.fixture(scope="session")
def nmap_xml_str(request) -> str:
    fp = os.path.join(request.config.rootpath, "data/nmaprun1.xml")
    with open(fp, "r") as f:
        data = f.read()
    return data


@pytest.fixture(scope="session")
def nmap_run(nmap_xml_str):
    return NmapXmlParser.parse_xml(nmap_xml_str)


@pytest.fixture(scope="session")
def raw_dig_output():
    return "156.32.33.45.in-addr.arpa. 300	IN	PTR	scanme.nmap.org."


@pytest.fixture(scope="session")
def reverse_dns_run(raw_dig_output):
    ip = "45.33.32.156"
    rdns_result = RDNSResult.from_dig(ip=ip, raw_output=raw_dig_output)
    scan_group_id = uuid4().hex
    started_at = datetime.now(tz=timezone.utc)
    tool_version = get_dig_version()
    finished_at = datetime.now(tz=timezone.utc)
    raw_command = get_dig_rdns_command(ip)
    return RDnsRun(
        tool_name=ToolName.DIG,
        tool_class=ToolClass.RDNS,
        tool_version=tool_version,
        status=Status.SUCCESS,
        ip=ip,
        started_at=started_at,
        finished_at=finished_at,
        raw_command=raw_command,
        scan_group_id=scan_group_id or uuid4().hex,
        config=ToolRunCommand.from_raw_command(raw_command),
        parsed=rdns_result,
    )