ref: 795764886b3c887a6e4988efe77eea8f7665d71a fanboi2/fanboi2/tests/test_services_banword.py -rw-r--r-- 6.8 KiB View raw
                                                                                
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import unittest
import unittest.mock

from . import ModelSessionMixin


class TestBanwordCreateService(ModelSessionMixin, unittest.TestCase):
    def _get_target_class(self):
        from ..services import BanwordCreateService

        return BanwordCreateService

    def test_create(self):
        banword_create_svc = self._get_target_class()(self.dbsession)
        banword = banword_create_svc.create(
            r"https?:\/\/bit\.ly",
            description="no shortlinks",
            scope="board:foo",
            active=True,
        )
        self.assertEqual(banword.expr, r"https?:\/\/bit\.ly")
        self.assertEqual(banword.description, "no shortlinks")
        self.assertEqual(banword.scope, "board:foo")
        self.assertTrue(banword.active)

    def test_create_without_optional_fields(self):
        banword_create_svc = self._get_target_class()(self.dbsession)
        banword = banword_create_svc.create(r"https?:\/\/bit\.ly")
        self.assertIsNone(banword.description)
        self.assertIsNone(banword.scope)
        self.assertTrue(banword.active)

    def test_create_with_empty_fields(self):
        banword_create_svc = self._get_target_class()(self.dbsession)
        banword = banword_create_svc.create("", description="", scope="", active="")
        self.assertIsNone(banword.expr)
        self.assertIsNone(banword.description)
        self.assertIsNone(banword.scope)
        self.assertFalse(banword.active)

    def test_create_deactivated(self):
        banword_create_svc = self._get_target_class()(self.dbsession)
        banword = banword_create_svc.create(r"https?:\/\/bit\.ly", active=False)
        self.assertFalse(banword.active)


class TestBanwordQueryService(ModelSessionMixin, unittest.TestCase):
    def _get_target_class(self):
        from ..services import BanwordQueryService

        return BanwordQueryService

    def _make_one(self, retval=True):
        class _DummyScopeService(object):
            def evaluate(self, _scope, _obj):
                return retval

        return self._get_target_class()(self.dbsession, _DummyScopeService())

    def test_list_active(self):
        from ..models import Banword

        banword1 = self._make(Banword(expr=r"https?:\/\/bit\.ly"))
        banword2 = self._make(Banword(expr=r"https?:\/\/goo\.gl"))
        self._make(Banword(expr=r"https?:\/\/youtu\.be", active=False))
        self._make(Banword(expr=r"https?:\/\/example\.com", active=False))
        self.dbsession.commit()
        banword_query_svc = self._make_one()
        self.assertEqual(banword_query_svc.list_active(), [banword2, banword1])

    def test_list_inactive(self):
        from ..models import Banword

        self._make(Banword(expr=r"https?:\/\/bit\.ly"))
        self._make(Banword(expr=r"https?:\/\/goo\.gl"))
        banword3 = self._make(Banword(expr=r"https?:\/\/youtu\.be", active=False))
        banword4 = self._make(Banword(expr=r"https?:\/\/example\.com", active=False))
        self.dbsession.commit()
        banword_query_svc = self._make_one()
        self.assertEqual(banword_query_svc.list_inactive(), [banword4, banword3])

    def test_is_banned(self):
        from ..models import Banword

        self._make(Banword(expr=r"https?:\/\/bit\.ly"))
        self._make(Banword(expr=r"https?:\/\/goo\.gl"))
        self._make(Banword(expr=r"https?:\/\/youtu\.be", active=False))
        self._make(Banword(expr=r"https?:\/\/example\.com", active=False))
        self.dbsession.commit()
        banword_query_svc = self._make_one()
        self.assertTrue(banword_query_svc.is_banned("a\nb\nhttps://bit.ly/Spam\nd"))
        self.assertTrue(banword_query_svc.is_banned("a\nb\nhttps://goo.gl/Spam\nd"))
        self.assertFalse(banword_query_svc.is_banned("a\nb\nhttps://youtu.be/Spam\nd"))
        self.assertFalse(
            banword_query_svc.is_banned("a\nb\nhttps://example.com/Spam\nd")
        )

    def test_banword_from_id(self):
        from ..models import Banword

        banword = self._make(Banword(expr=r"https?:\/\/bit\.ly"))
        self.dbsession.commit()
        banword_query_svc = self._make_one()
        self.assertEqual(banword_query_svc.banword_from_id(banword.id), banword)


class TestBanwordUpdateService(ModelSessionMixin, unittest.TestCase):
    def _get_target_class(self):
        from ..services import BanwordUpdateService

        return BanwordUpdateService

    def test_update(self):
        from ..models import Banword

        banword = self._make(
            Banword(
                expr=r"https?:\/\/bit\.ly",
                description="no shortlinks",
                scope="board:foo",
                active=True,
            )
        )
        self.dbsession.commit()
        banword_update_svc = self._get_target_class()(self.dbsession)
        banword = banword_update_svc.update(
            banword.id,
            expr=r"https?:\/\/(bit\.ly|goo\.gl)",
            description="no any shortlinks",
            scope="board:bar",
            active=False,
        )
        self.assertEqual(banword.expr, r"https?:\/\/(bit\.ly|goo\.gl)")
        self.assertEqual(banword.description, "no any shortlinks")
        self.assertEqual(banword.scope, "board:bar")
        self.assertFalse(banword.active)

    def test_update_not_found(self):
        from sqlalchemy.orm.exc import NoResultFound

        banword_update_svc = self._get_target_class()(self.dbsession)
        with self.assertRaises(NoResultFound):
            banword_update_svc.update(-1, active=False)

    def test_update_none(self):
        from ..models import Banword

        banword = self._make(
            Banword(
                expr=r"https?:\/\/bit\.ly",
                description="no shortlinks",
                scope="board:foo",
                active=True,
            )
        )
        self.dbsession.commit()
        banword_update_svc = self._get_target_class()(self.dbsession)
        banword = banword_update_svc.update(
            banword.id, expr=None, description=None, scope=None, active=None
        )
        self.assertIsNone(banword.expr)
        self.assertIsNone(banword.description)
        self.assertIsNone(banword.scope)
        self.assertFalse(banword.active)

    def test_update_empty(self):
        from ..models import Banword

        banword = self._make(
            Banword(
                expr=r"https?:\/\/bit\.ly",
                description="no shortlinks",
                scope="board:foo",
                active=True,
            )
        )
        self.dbsession.commit()
        banword_update_svc = self._get_target_class()(self.dbsession)
        banword = banword_update_svc.update(
            banword.id, expr="", description="", scope="", active=""
        )
        self.assertIsNone(banword.expr)
        self.assertIsNone(banword.description)
        self.assertIsNone(banword.scope)
        self.assertFalse(banword.active)