File: testable_imapclient.py

package info (click to toggle)
python-imapclient 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 644 kB
  • sloc: python: 5,341; sh: 14; makefile: 13
file content (41 lines) | stat: -rw-r--r-- 1,191 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
# Copyright (c) 2014, Menno Smits
# Released subject to the New BSD License
# Please see http://en.wikipedia.org/wiki/BSD_licenses

from typing import Any, Dict, List
from unittest.mock import Mock

from .imapclient import IMAPClient


class TestableIMAPClient(IMAPClient):
    """Wrapper of :py:class:`imapclient.IMAPClient` that mocks all
    interaction with real IMAP server.

    This class should only be used in tests, where you can safely
    interact with imapclient without running commands on a real
    IMAP account.
    """

    def __init__(self) -> None:
        super().__init__("somehost")

    def _create_IMAP4(self) -> "MockIMAP4":
        return MockIMAP4()


class MockIMAP4(Mock):
    def __init__(self, *args: Any, **kwargs: Any):
        super().__init__(*args, **kwargs)
        self.use_uid = True
        self.sent = b""  # Accumulates what was given to send()
        self.tagged_commands: Dict[Any, Any] = {}
        self.untagged_responses: Dict[Any, Any] = {}
        self.capabilities: List[str] = []
        self._starttls_done = False

    def send(self, data: bytes) -> None:
        self.sent += data

    def _new_tag(self) -> str:
        return "tag"