File: test_interface.py

package info (click to toggle)
pyipp 0.17.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 640 kB
  • sloc: python: 2,282; makefile: 3
file content (96 lines) | stat: -rw-r--r-- 2,758 bytes parent folder | download
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
87
88
89
90
91
92
93
94
95
96
"""Tests for IPP public interface."""
import pytest
from aiohttp import ClientSession
from aresponses import ResponsesMockServer

from pyipp import IPP, Printer
from pyipp.const import DEFAULT_PRINTER_ATTRIBUTES
from pyipp.enums import IppOperation

from . import (
    DEFAULT_PRINTER_HOST,
    DEFAULT_PRINTER_PATH,
    DEFAULT_PRINTER_PORT,
    DEFAULT_PRINTER_URI,
    load_fixture_binary,
)

MATCH_DEFAULT_HOST = f"{DEFAULT_PRINTER_HOST}:{DEFAULT_PRINTER_PORT}"


@pytest.mark.asyncio
async def test_printer(aresponses: ResponsesMockServer) -> None:
    """Test getting IPP printer information."""
    aresponses.add(
        MATCH_DEFAULT_HOST,
        DEFAULT_PRINTER_PATH,
        "POST",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/ipp"},
            body=load_fixture_binary("get-printer-attributes-epsonxp6000.bin"),
        ),
    )

    async with ClientSession() as session:
        ipp = IPP(DEFAULT_PRINTER_URI, session=session)
        printer = await ipp.printer()

        assert printer
        assert isinstance(printer, Printer)


@pytest.mark.asyncio
async def test_printer_update_logic(aresponses: ResponsesMockServer) -> None:
    """Test getting updated IPP printer information."""
    aresponses.add(
        MATCH_DEFAULT_HOST,
        DEFAULT_PRINTER_PATH,
        "POST",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/ipp"},
            body=load_fixture_binary("get-printer-attributes-epsonxp6000.bin"),
        ),
        repeat=2,
    )

    async with ClientSession() as session:
        ipp = IPP(DEFAULT_PRINTER_URI, session=session)
        printer = await ipp.printer()

        assert printer
        assert isinstance(printer, Printer)

        printer = await ipp.printer()
        assert printer
        assert isinstance(printer, Printer)


@pytest.mark.asyncio
async def test_raw(aresponses: ResponsesMockServer) -> None:
    """Test raw method is handled correctly."""
    aresponses.add(
        MATCH_DEFAULT_HOST,
        DEFAULT_PRINTER_PATH,
        "POST",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/ipp"},
            body=load_fixture_binary("get-printer-attributes-epsonxp6000.bin"),
        ),
    )

    async with ClientSession() as session:
        ipp = IPP(DEFAULT_PRINTER_URI, session=session)
        response = await ipp.raw(
            IppOperation.GET_PRINTER_ATTRIBUTES,
            {
                "operation-attributes-tag": {
                    "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES,
                },
            },
        )

        assert response
        assert isinstance(response, bytes)