##// END OF EJS Templates
Added a generator for the 'get' request for sync
neko259 -
r810:6bdf5d6a decentral
parent child Browse files
Show More
@@ -0,0 +1,56 b''
1 import xml.etree.ElementTree as et
2
3 TAG_MODEL = 'model'
4 TAG_REQUEST = 'request'
5 TAG_ID = 'id'
6
7 TYPE_GET = 'get'
8
9 ATTR_VERSION = 'version'
10 ATTR_TYPE = 'type'
11 ATTR_NAME = 'name'
12 ATTR_KEY = 'key'
13 ATTR_KEY_TYPE = 'type'
14 ATTR_LOCAL_ID = 'local-id'
15
16
17 class ModelId:
18 """
19 Model ID describes a global ID that consists of sender's key (with the key
20 type) and local ID on the sender node.
21 """
22
23 def __init__(self, key, key_type, local_id):
24 self.key = key
25 self.key_type = key_type
26 self.local_id = local_id
27
28
29 def respond_pull(request):
30 pass
31
32
33 def respond_get(request):
34 pass
35
36
37 def generate_request_get(id_list: list):
38 """
39 Form a get request from a list of ModelId objects.
40 """
41
42 request = et.Element(TAG_REQUEST)
43 request.set(ATTR_TYPE, TYPE_GET)
44 request.set(ATTR_VERSION, '1.0')
45
46 model = et.SubElement(request, TAG_MODEL)
47 model.set(ATTR_VERSION, '1.0')
48 model.set(ATTR_NAME, 'post')
49
50 for model_id in id_list:
51 tag_id = et.SubElement(model, TAG_ID)
52 tag_id.set(ATTR_KEY, model_id.key)
53 tag_id.set(ATTR_KEY_TYPE, model_id.key_type)
54 tag_id.set(ATTR_LOCAL_ID, model_id.local_id)
55
56 return et.tostring(request, 'unicode') No newline at end of file
@@ -14,6 +14,7 b' from boards import urls'
14 14 from boards import settings
15 15 from boards.views.api import api_get_threaddiff
16 16 from boards.utils import datetime_to_epoch
17 from boards.views.sync import ModelId, generate_request_get
17 18 import neboard
18 19
19 20 TEST_TAG = 'test_tag'
@@ -146,7 +147,7 b' class PagesTest(TestCase):'
146 147 def test_404(self):
147 148 """Test receiving error 404 when opening a non-existent page"""
148 149
149 tag_name = u'test_tag'
150 tag_name = 'test_tag'
150 151 tag = Tag.objects.create(name=tag_name)
151 152 client = Client()
152 153
@@ -156,22 +157,22 b' class PagesTest(TestCase):'
156 157 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
157 158 '/')
158 159 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
159 u'Cannot open existing thread')
160 'Cannot open existing thread')
160 161
161 162 response_not_existing = client.get(THREAD_PAGE + str(
162 163 existing_post_id + 1) + '/')
163 164 self.assertEqual(PAGE_404, response_not_existing.templates[0].name,
164 u'Not existing thread is opened')
165 'Not existing thread is opened')
165 166
166 167 response_existing = client.get(TAG_PAGE + tag_name + '/')
167 168 self.assertEqual(HTTP_CODE_OK,
168 169 response_existing.status_code,
169 u'Cannot open existing tag')
170 'Cannot open existing tag')
170 171
171 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
172 response_not_existing = client.get(TAG_PAGE + 'not_tag' + '/')
172 173 self.assertEqual(PAGE_404,
173 174 response_not_existing.templates[0].name,
174 u'Not existing tag is opened')
175 'Not existing tag is opened')
175 176
176 177 reply_id = Post.objects.create_post('', TEST_TEXT,
177 178 thread=Post.objects.all()[0]
@@ -180,15 +181,15 b' class PagesTest(TestCase):'
180 181 reply_id) + '/')
181 182 self.assertEqual(PAGE_404,
182 183 response_not_existing.templates[0].name,
183 u'Reply is opened as a thread')
184 'Reply is opened as a thread')
184 185
185 186
186 187 class FormTest(TestCase):
187 188 def test_post_validation(self):
188 189 client = Client()
189 190
190 valid_tags = u'tag1 tag_2 тег_3'
191 invalid_tags = u'$%_356 ---'
191 valid_tags = 'tag1 tag_2 тег_3'
192 invalid_tags = '$%_356 ---'
192 193
193 194 response = client.post(NEW_THREAD_PAGE, {'title': 'test title',
194 195 'text': TEST_TEXT,
@@ -213,13 +214,13 b' class FormTest(TestCase):'
213 214 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
214 215 'tags': valid_tags})
215 216 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
216 msg=u'Posting new message failed: got code ' +
217 msg='Posting new message failed: got code ' +
217 218 str(response.status_code))
218 219 # Restore posting delay
219 220 neboard.settings.POSTING_DELAY = old_posting_delay
220 221
221 222 self.assertEqual(2, Post.objects.count(),
222 msg=u'No posts were created')
223 msg='No posts were created')
223 224
224 225
225 226 class ViewTest(TestCase):
@@ -258,7 +259,7 b' class AbstractTest(TestCase):'
258 259
259 260 settings_manager.set_setting('test_setting', 'test_value')
260 261 self.assertEqual('test_value', settings_manager.get_setting(
261 'test_setting'), u'Setting update failed.')
262 'test_setting'), 'Setting update failed.')
262 263
263 264
264 265 class MockRequest:
@@ -292,7 +293,15 b' class KeyTest(TestCase):'
292 293 ' one primary key.')
293 294 except Exception:
294 295 pass
295
296
297 def test_request_get(self):
298 model_id = ModelId('111', '222', '333')
299 self.assertTrue('<request type="get" version="1.0"><model '
300 'name="post" version="1.0"><id key="111" '
301 'local-id="333" type="222" /></model></request>' in
302 generate_request_get([model_id]),
303 'Wrong XML generated for the GET request.')
304
296 305
297 306 class ApiTest(TestCase):
298 307 def test_thread_diff(self):
@@ -1,7 +1,7 b''
1 1 <?xml version="1.1" encoding="UTF-8" ?>
2 2 <request version="1.0" type="get">
3 3 <model version="1.0" name="post">
4 <id key="id1" local-id="1" />
5 <id key="id1" local-id="2" />
4 <id key="id1" type="ecdsa" local-id="1" />
5 <id key="id1" type="ecdsa" local-id="2" />
6 6 </model>
7 7 </request>
@@ -2,9 +2,9 b''
2 2 <response>
3 3 <status>success</status>
4 4 <models>
5 <id key="id1" local-id="1" />
6 <id key="id1" local-id="2" />
7 <id key="id2" local-id="1" />
8 <id key="id2" local-id="5" />
5 <id key="id1" type="ecdsa" local-id="1" />
6 <id key="id1" type="ecdsa" local-id="2" />
7 <id key="id2" type="ecdsa" local-id="1" />
8 <id key="id2" type="ecdsa" local-id="5" />
9 9 </models>
10 10 </response>
@@ -1,3 +1,4 b''
1 simplejson
1 2 south>=0.8.4
2 3 haystack
3 4 pillow
General Comments 0
You need to be logged in to leave comments. Login now