def test_ajax_message(self): """ Test that messages encode nicely when we package them into an AjaxMessage() """ request_factory = RequestFactory() auth_result = request_factory.login(username='******', password='******') self.failUnlessEqual(auth_result, True, "Assert that our test client can log in.") request = request_factory.request() messages.add_message(request,messages.INFO,'Testing') test_response = AjaxMessage(messages.get_messages(request)) self.failUnlessEqual(str(test_response), """{"type": "Messages", "djangoPayload": true, "payload": [{"type": "MessageItem", "djangoPayload": true, "payload": " <li class=\\"info message-item ui-corner-all\\">\\n\\t<div class=\\"icon\\"></div>Testing\\n</li>"}]}""")
class DecoratorTests(TestCase): def setUp(self): self.rf = RequestFactory() def test_require_websocket_decorator(self): # view requires websocket -> bad request request = self.rf.get('/echo/') response = echo_once(request) self.assertEquals(response.status_code, 400) def test_accept_websocket_decorator(self): request = self.rf.get('/add/', {'value': '23'}) response = add_one(request) self.assertEquals(response.status_code, 200) self.assertEquals(response.content, '24')
def test_build_response(self): """ Test the buid_response shortcut function, which automatically appends messages and accepts an arguments list of `AjaxContent` as additional parameters. """ request_factory = RequestFactory() auth_result = request_factory.login(username='******', password='******') self.failUnlessEqual(auth_result, True, "Assert that our test client can log in.") request = request_factory.request() messages.add_message(request,messages.INFO,'Testing') test_response = build_response(request) self.failUnlessEqual(str(test_response), """Content-Type: application/json\n\n{"type": "Response", "djangoPayload": true, "payload": [{"type": "Messages", "djangoPayload": true, "payload": [{"type": "MessageItem", "djangoPayload": true, "payload": " <li class=\\"info message-item ui-corner-all\\">\\n\\t<div class=\\"icon\\"></div>Testing\\n</li>"}]}]}""")
def test_build_response_namespace(self): """ Tests that our optional namespace gets captured if it's part of the post variables. """ request_factory = RequestFactory() auth_result = request_factory.login(username='******', password='******') self.failUnlessEqual(auth_result, True, "Assert that our test client can log in.") request = request_factory.post('',{"namespace" : "test-namespace"}) messages.add_message(request,messages.INFO,'Testing') test_response = build_response(request) self.failUnlessEqual(str(test_response), """Content-Type: application/json\n\n{"type": "Response", "namespace": "test-namespace", "djangoPayload": true, "payload": [{"type": "Messages", "djangoPayload": true, "payload": [{"type": "MessageItem", "djangoPayload": true, "payload": " <li class=\\"info message-item ui-corner-all\\">\\n\\t<div class=\\"icon\\"></div>Testing\\n</li>"}]}]}""")
def test_urlpattern_reverse(self): for name, expected, args, kwargs in test_data: # Prepare our data by jsonifying it - this is how javascript clients should be transmitting information. args_json = json.dumps(args) kwargs_json = json.dumps(kwargs) # Simulate a request with our json-encoded parameters - note: we don't actually post to the url specified. # Instead, we pass the request directly to the view. rf = RequestFactory() post_request = rf.post('', {'djangoajax_args': args_json, 'djangoajax_kwargs': kwargs_json }) response = url_reverse(post_request, name) # If there is no reverse match, we return an HttpResponseNotFound if expected is NoReverseMatch: self.assertEquals(HttpResponseNotFound, type(response)) else: self.assertEqual(expected, response.content)
def test_complex_payload(self): events = [] events.append(AjaxEvent('CartRemove', # See also core/context_processors.py : favorites() { 'cart_item': { 'pk' : 5 }, 'cart': { 'total_price' : 9.99, 'total_qty': 3 } } )) request_factory = RequestFactory() request = request_factory.request() build_response(request,*events)
def setUp(self): self.rf = RequestFactory()