File: test_imapwidget.py

package info (click to toggle)
qtile 0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,004 kB
  • sloc: python: 49,959; ansic: 4,371; xml: 324; sh: 260; makefile: 218
file content (75 lines) | stat: -rw-r--r-- 2,165 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
import sys
from importlib import reload
from types import ModuleType

import pytest

from test.widgets.conftest import FakeBar


class FakeIMAP(ModuleType):
    class IMAP4_SSL:  # noqa: N801
        def __init__(self, *args, **kwargs):
            pass

        def login(self, username, password):
            self.username = username
            self.password = password

        def status(self, path, *args, **kwargs):
            if not (self.username and self.password):
                return False, None

            return ("OK", [f'"{path}" (UNSEEN 2)'.encode()])

        def logout(self):
            pass


class FakeKeyring(ModuleType):
    valid = True

    def get_password(self, _app, user):
        if self.valid:
            return "password"
        return None


@pytest.fixture()
def patched_imap(monkeypatch):
    monkeypatch.delitem(sys.modules, "imaplib", raising=False)
    monkeypatch.delitem(sys.modules, "keyring", raising=False)
    monkeypatch.setitem(sys.modules, "imaplib", FakeIMAP("imaplib"))
    monkeypatch.setitem(sys.modules, "keyring", FakeKeyring("keyring"))
    from libqtile.widget import imapwidget

    reload(imapwidget)
    yield imapwidget


def test_imapwidget(fake_qtile, monkeypatch, fake_window, patched_imap):
    imap = patched_imap.ImapWidget(user="qtile")
    fakebar = FakeBar([imap], window=fake_window)
    imap._configure(fake_qtile, fakebar)
    text = imap.poll()
    assert text == "INBOX: 2"


def test_imapwidget_with_password(fake_qtile, monkeypatch, fake_window, patched_imap):
    # keyring should not be called
    patched_imap.keyring.valid = False
    imap = patched_imap.ImapWidget(user="qtile", password="password")
    fakebar = FakeBar([imap], window=fake_window)
    imap._configure(fake_qtile, fakebar)
    text = imap.poll()
    assert text == "INBOX: 2"


def test_imapwidget_password_none(fake_qtile, monkeypatch, fake_window, patched_imap):
    patched_imap.keyring.valid = False

    imap = patched_imap.ImapWidget(user="qtile")
    fakebar = FakeBar([imap], window=fake_window)
    imap._configure(fake_qtile, fakebar)
    text = imap.poll()
    assert text == "No password error"