~sirn/fanboi2

ref: 98796226802a26b82c888365ad5b9cd331006792 fanboi2/fanboi2/tests/__init__.py -rw-r--r-- 7.6 KiB
98796226Kridsada Thanabulpong Bump copyright year. 3 years ago
                                                                                
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import logging
import os
import transaction
import unittest
from pyramid import testing
from sqlalchemy import create_engine
from sqlalchemy.orm import Query
from webob.multidict import MultiDict
from fanboi2.models import DBSession, Base, redis_conn


logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)


DATABASE_URI = os.environ.get(
    'POSTGRESQL_TEST_DATABASE',
    'postgresql://fanboi2:@localhost:5432/fanboi2_test')


class DummyRedis(object):

    @classmethod
    def from_url(cls, *args, **kwargs):
        return cls()

    def __init__(self):
        self._store = {}
        self._expire = {}

    def get(self, key):
        return self._store.get(key)

    def set(self, key, value):
        try:
            value = bytes(value.encode('utf-8'))
        except AttributeError:
            pass
        self._store[key] = value

    def setnx(self, key, value):
        if not self.get(key):
            self.set(key, value)

    def exists(self, key):
        return key in self._store

    def expire(self, key, time):
        self._expire[key] = time

    def ttl(self, key):
        return self._expire.get(key, 0)

    def ping(self):
        return True


class _ModelInstanceSetup(object):

    def _newBoard(self, **kwargs):
        from fanboi2.models import Board
        return Board(**kwargs)

    def _newTopic(self, **kwargs):
        from fanboi2.models import Topic
        return Topic(**kwargs)

    def _newTopicMeta(self, **kwargs):
        from fanboi2.models import TopicMeta
        if not kwargs.get('post_count'):
            kwargs['post_count'] = 0
        return TopicMeta(**kwargs)

    def _newPost(self, **kwargs):
        from fanboi2.models import Post
        if not kwargs.get('ip_address'):
            kwargs['ip_address'] = '0.0.0.0'
        return Post(**kwargs)

    def _newPage(self, **kwargs):
        from fanboi2.models import Page
        return Page(**kwargs)

    def _newRule(self, **kwargs):
        from fanboi2.models import Rule
        return Rule(**kwargs)

    def _newRuleBan(self, **kwargs):
        from fanboi2.models import RuleBan
        return RuleBan(**kwargs)

    def _newRuleOverride(self, **kwargs):
        from fanboi2.models import RuleOverride
        return RuleOverride(**kwargs)

    def _makeBoard(self, **kwargs):
        board = self._newBoard(**kwargs)
        DBSession.add(board)
        DBSession.flush()
        return board

    def _makeTopic(self, **kwargs):
        topic = self._newTopic(**kwargs)
        DBSession.add(topic)
        DBSession.flush()
        return topic

    def _makeTopicMeta(self, **kwargs):
        topic_meta = self._newTopicMeta(**kwargs)
        DBSession.add(topic_meta)
        DBSession.flush()
        return topic_meta

    def _makePost(self, **kwargs):
        post = self._newPost(**kwargs)
        DBSession.add(post)
        DBSession.flush()
        return post

    def _makePage(self, **kwargs):
        page = self._newPage(**kwargs)
        DBSession.add(page)
        DBSession.flush()
        return page

    def _makeRule(self, **kwargs):
        rule = self._newRule(**kwargs)
        DBSession.add(rule)
        DBSession.flush()
        return rule

    def _makeRuleBan(self, **kwargs):
        rule_ban = self._newRuleBan(**kwargs)
        DBSession.add(rule_ban)
        DBSession.flush()
        return rule_ban

    def _makeRuleOverride(self, **kwargs):
        rule_override = self._newRuleOverride(**kwargs)
        DBSession.add(rule_override)
        DBSession.flush()
        return rule_override

class ModelMixin(_ModelInstanceSetup, unittest.TestCase):

    @classmethod
    def tearDownClass(cls):
        super(ModelMixin, cls).tearDownClass()
        Base.metadata.bind = None
        DBSession.remove()

    @classmethod
    def setUpClass(cls):
        super(ModelMixin, cls).setUpClass()
        engine = create_engine(DATABASE_URI)
        DBSession.configure(bind=engine)
        Base.metadata.bind = engine

    def setUp(self):
        super(ModelMixin, self).setUp()
        redis_conn._redis = DummyRedis()
        Base.metadata.drop_all()
        Base.metadata.create_all()
        transaction.begin()

    def tearDown(self):
        super(ModelMixin, self).tearDown()
        transaction.abort()
        Base.metadata.drop_all()
        redis_conn._redis = None

    def assertSAEqual(self, first, second, msg=None):
        if isinstance(first, Query):
            return self.assertListEqual(list(first), second, msg)
        else:
            return self.assertEqual(first, second, msg)


class RegistryMixin(unittest.TestCase):

    def tearDown(self):
        super(RegistryMixin, self).tearDown()
        testing.tearDown()

    def _makeConfig(self, request=None, registry=None):
        return testing.setUp(
            request=request,
            registry=registry)

    def _makeRequest(self, **kw):
        """:rtype: pyramid.request.Request"""
        request = testing.DummyRequest(**kw)
        request.user_agent = kw.get('user_agent', 'Mock/1.0')
        request.remote_addr = kw.get('remote_addr', '127.0.0.1')
        request.referrer = kw.get('referrer')
        request.content_type = 'application/x-www-form-urlencoded'
        request.params = MultiDict(kw.get('params') or {})
        return request

    def _makeRegistry(self, **kw):
        """:rtype: pyramid.registry.Registry"""
        from pyramid.registry import Registry
        registry = Registry()
        registry.settings = {
            'app.timezone': 'Asia/Bangkok',
            'app.secret': 'demo',
        }
        registry.settings.update(kw)
        return registry


class DummyAsyncResult(object):

    def __init__(self, id_, status, result=None):
        self._id = id_
        self._status = status
        self._result = result

    @property
    def id(self):
        return self._id

    @property
    def status(self):
        return self._status.upper()

    @property
    def state(self):
        from celery import states
        return getattr(states, self.status)

    def get(self):
        return self._result


class TaskMixin(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        from fanboi2.tasks import celery
        super(TaskMixin, cls).setUpClass()
        celery.config_from_object({'CELERY_ALWAYS_EAGER': True})

    @classmethod
    def tearDownClass(cls):
        from fanboi2.tasks import celery
        super(TaskMixin, cls).tearDownClass()
        celery.config_from_object({'CELERY_ALWAYS_EAGER': False})


class ViewMixin(ModelMixin, RegistryMixin, unittest.TestCase):

    def _make_csrf(self, request):
        import hmac
        import os
        from hashlib import sha1
        request.session['csrf'] = sha1(os.urandom(64)).hexdigest()
        request.params['csrf_token'] = hmac.new(
            bytes(request.registry.settings['app.secret'].encode('utf8')),
            bytes(request.session['csrf'].encode('utf8')),
            digestmod=sha1,
        ).hexdigest()
        return request

    def _POST(self, data=None):
        request = self._makeRequest(params=data)
        request.method = 'POST'
        return request

    def _GET(self, data=None):
        request = self._makeRequest(params=data)
        return request

    def _json_POST(self, data=None):
        request = self._makeRequest()
        request.content_type = 'application/json'
        request.json_body = data
        return request


class CacheMixin(unittest.TestCase):

    def _getRegion(self, store=None):
        from dogpile.cache import make_region
        return make_region().configure('dogpile.cache.memory', arguments={
            'cache_dict': store if store is not None else {}})