aboutsummaryrefslogtreecommitdiff
path: root/test/test_components.py
blob: f19cfb3348cf01fafc36caa6da6d205354c8f3cd (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from email.headerregistry import Address
from email.parser import BytesParser
import email.policy
from mu4web.components import (
    dl,
    flashed_messages,
    format_email,
    include_stylesheet,
    login_page,
    login_prompt,
    mailto,
    search_field,
    user_info,
)
from typing import cast


def test_dl() -> None:
    assert dl([]) == ('dl', [])
    assert dl([('k', '1'), ('v', '2')]) == ('dl', [('dt', 'k'),
                                                   ('dd', '1'),
                                                   ('dt', 'v'),
                                                   ('dd', '2')])


def test_mailto() -> None:
    assert mailto('hugo@example.com') \
        == ('a', {'href': 'mailto:hugo@example.com'}, 'hugo@example.com')


def test_format_email() -> None:
    assert format_email(Address('Hugo Hörnquist', 'hugo', 'example.com')) == [
        'Hugo Hörnquist',
        ' <',
        mailto('hugo@example.com'),
        '>'
    ]


parser = BytesParser(policy=email.policy.default)

msg = parser.parsebytes(b"""
Date: Fri, 04 Aug 2023 20:15:30 +0200
From: Hugo <hugo@example.com>
To: "Hugo 1" <hugo1@example.com>, "Hugo 2" <hugo2@example.com>
Message-Id: <9a274279-9915-44e7-b4c0-3d29b7929744>
In-Reply-To: <1763399c-9483-4c12-a633-5eb0496886b9>

This is a short message
""".strip())


# TODO The above message claims to contain proper data structures,
# however, it only contains string-like objects.
# def test_header_format():
#     print("to:", len(msg.get('to')), repr(msg['to']), type(msg['to']))
#     assert header_format('to', msg['to']) \
#         == ('ul', ('li',
#                    format_email(msg['to'][0]),
#                    format_email(msg['to'][1])))
#     assert 'from'
#     assert 'in-reply-to'
#     assert 'x-fake-header'

def test_attachement_tree() -> None:
    # TODO
    pass


def test_login_page() -> None:
    assert login_page() == \
        ('form', {'action': '/login', 'method': 'POST', 'class': 'loginform'},
         ('label', {'for': 'username'}, 'Användarnamn'),
         ('input', {'id': 'username', 'name': 'username', 'placeholder': 'Användarnamn'}),
         ('label', {'for': 'password'}, 'Lösenord'),
         ('input', {'id': 'password', 'name': 'password',
                    'placeholder': 'Lösenord',
                    'type': 'password'}),
         ('div',
          ('input', {'id': 'remember', 'name': 'remember', 'type': 'checkbox'}),
          ('label', {'for': 'remember'}, 'Kom ihåg mig')),
         [],
         ('input', {'type': 'submit', 'value': 'Logga in'}),
         )


def test_user_info() -> None:
    assert user_info('hugo') == [
        ('span', 'hugo'),
        ('form', {'action': '/logout', 'method': 'POST'},
         ('input', {'type': 'submit', 'value': 'Logga ut'}))
    ]


def test_login_prompt() -> None:
    assert login_prompt() == ('a', {'href': '/login'}, 'Logga in')


def test_flashed_messages() -> None:
    assert flashed_messages(cast(list[str], [])) == cast(list[str], [])
    assert flashed_messages(['msg1']) \
        == ('ul', {'class': 'flashes'}, ('li', 'msg1'))
    assert flashed_messages([('cat1', 'msg1')]) \
        == ('ul', {'class': 'flashes'}, ('li', 'msg1'))


def test_include_stylesheet() -> None:
    assert include_stylesheet('style.css') == \
        ('link', {'type': 'text/css',
                  'rel': 'stylesheet',
                  'href': 'style.css'})


def test_search_field() -> None:
    assert search_field('test') == \
        ('form', {'id': 'searchform',
                  'action': '/search',
                  'method': 'GET'},
         ('label', {'for': 'search'},
          'Sökförfrågan till Mu'),
         ('input', {'id': 'search',
                    'type': 'text',
                    'placeholder': 'Sök...',
                    'name': 'q',
                    'value': 'test'}),
         ('input', {'type': 'Submit', 'value': 'Sök'}))