summaryrefslogtreecommitdiff
path: root/src/mailman/rest/tests/test_helpers.py
blob: cab1f06fa00bc2a07c60c415057b3c6bc6b13266 (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
# Copyright (C) 2016 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Additional tests for helpers."""

__all__ = [
    'TestHelpers',
    ]


import unittest

from datetime import timedelta
from mailman.rest import helpers
from mailman.testing.layers import ConfigLayer


class FakeResponse:
    def __init__(self):
        self.body = 'not set'


class Unserializable:
    pass



class TestHelpers(unittest.TestCase):
    layer = ConfigLayer

    def test_not_found_body_is_none(self):
        response = FakeResponse()
        helpers.not_found(response, body=None)
        self.assertEqual(response.body, 'not set')

    def test_accepted_body_is_not_none(self):
        response = FakeResponse()
        helpers.accepted(response, body='set')
        self.assertEqual(response.body, 'set')

    def test_bad_request_body_is_none(self):
        response = FakeResponse()
        helpers.bad_request(response, body=None)
        self.assertEqual(response.body, 'not set')

    def test_conflict_body_is_none(self):
        response = FakeResponse()
        helpers.conflict(response, body=None)
        self.assertEqual(response.body, 'not set')

    def test_forbidden_body_is_none(self):
        response = FakeResponse()
        helpers.forbidden(response, body=None)
        self.assertEqual(response.body, 'not set')

    def test_json_encoding_datetime_seconds(self):
        resource = dict(interval=timedelta(seconds=2))
        unjson = eval(helpers.etag(resource))
        self.assertEqual(unjson['interval'], '0d2.0s')

    def test_json_encoding_datetime_microseconds(self):
        resource = dict(interval=timedelta(microseconds=2))
        unjson = eval(helpers.etag(resource))
        self.assertEqual(unjson['interval'], '0d2e-06s')

    def test_json_encoding_default(self):
        resource = dict(interval=Unserializable())
        self.assertRaises(TypeError, helpers.etag, resource)