def test_function_return_HttpResponse(self): ajax_data = DummyAjaxRequest(browTabName='folders_contents', parent_id='1', selRowIndex='0').ajax_data() request = self.client.request() request.POST = ajax_data request.session = { 'Selections': { 'folders_contents': { '1': { 'model': "user", 'id': "1", 'selRowIndex': '0' } } } } response = ajaxStartRowIndexFromSession(request) self.assertTrue(isinstance(response, HttpResponse)) # Чи ф-ція повертає правильний response? expected = [ b'{"server_response": {"id": "1", "selRowIndex": "0", "model": "user"}}' ] self.assertEqual(json.loads(response._container[0].decode()), json.loads(expected[0].decode())) expected = {'content-type': ('Content-Type', 'application/json')} self.assertEqual(response._headers, expected)
def test_function_return_response_bad_request_if_non_matching_data(self): ajax_data = DummyAjaxRequest(browTabName='folders_contents', model='USER', parent_id='1', selRowIndex='0').ajax_data() request = self.client.request() request.POST = ajax_data request.session = { 'Selections': { 'folders_contents': { '1': { 'model': "user", 'id': "1", 'selRowIndex': '0' } } } } response = ajaxStartRowIndexFromSession(request) self.assertTrue(isinstance(response, HttpResponseBadRequest)) expected = [ b'ajaxStartRowIndexFromSession: \n Bad data in request.POST: model name does not correspond to table name \n model=USER \n browTabName=folders_contents' ] self.assertEqual(response._container, expected) expected = { 'content-type': ('Content-Type', 'text/html; charset=utf-8') } self.assertEqual(response._headers, expected)
def test_function_return_HttpResponse_2(self): json_s = '{"browTabName":"folders_contents","parent_id":"1","selRowIndex":"0"}' request = self.client.request() request.POST = {'client_request': json_s} expected = {'sendMail': None, 'id': None, 'name': None, 'browTabName': 'folders_contents', 'parent_id': '1', 'selRowIndex': '0', 'model': None} expected = {'browTabName': 'folders_contents', 'parent_id': '1', 'selRowIndex': '0'} self.assertEqual(parseClientRequest(request.POST), expected) request.session = { 'Selections': {"folders_contents": {"1": {'model' : "folder", 'id' : "1", 'selRowIndex' : "2", }, }, }, } response = ajaxStartRowIndexFromSession(request) self.assertTrue(isinstance(response, HttpResponse)) # Чи ф-ція повертає правильний response? expected = [b'{"server_response": {"id": "1", "selRowIndex": "2", "model": "folder"}}'] self.assertEqual(json.loads(response._container[0].decode()) , json.loads(expected[0].decode())) expected = {'content-type': ('Content-Type', 'application/json')} self.assertEqual(response._headers, expected)
def test_function_return_response_bad_request_if_no_client_request(self): request = self.client.request() request.POST = {} response = ajaxStartRowIndexFromSession(request) self.assertTrue(isinstance(response, HttpResponseBadRequest)) expected = [b"ajaxStartRowIndexFromSession: No 'client_request' in request.POST"] self.assertEqual(response._container, expected) expected = {'content-type': ('Content-Type', 'text/html; charset=utf-8')} self.assertEqual(response._headers, expected)
def test_function_return_response_bad_request_if_no_client_request(self): request = self.client.request() request.POST = {} response = ajaxStartRowIndexFromSession(request) self.assertTrue(isinstance(response, HttpResponseBadRequest)) expected = [ b"ajaxStartRowIndexFromSession: No 'client_request' in request.POST" ] self.assertEqual(response._container, expected) expected = { 'content-type': ('Content-Type', 'text/html; charset=utf-8') } self.assertEqual(response._headers, expected)
def test_function_return_response_bad_request_if_non_matching_data(self): ajax_data = DummyAjaxRequest(browTabName='folders_contents', model='USER', parent_id='1', selRowIndex='0').ajax_data() request = self.client.request() request.POST = ajax_data request.session = {'Selections': {'folders_contents': {'1': {'model': "user", 'id': "1", 'selRowIndex': '0'}}}} response = ajaxStartRowIndexFromSession(request) self.assertTrue(isinstance(response, HttpResponseBadRequest)) expected = [b'ajaxStartRowIndexFromSession: \n Bad data in request.POST: model name does not correspond to table name \n model=USER \n browTabName=folders_contents'] self.assertEqual(response._container, expected) expected = {'content-type': ('Content-Type', 'text/html; charset=utf-8')} self.assertEqual(response._headers, expected)
def test_function_return_HttpResponse(self): ajax_data = DummyAjaxRequest(browTabName='folders_contents', parent_id='1', selRowIndex='0').ajax_data() request = self.client.request() request.POST = ajax_data request.session = {'Selections': {'folders_contents': {'1': {'model': "user", 'id': "1", 'selRowIndex': '0'}}}} response = ajaxStartRowIndexFromSession(request) self.assertTrue(isinstance(response, HttpResponse)) # Чи ф-ція повертає правильний response? expected = [b'{"server_response": {"id": "1", "selRowIndex": "0", "model": "user"}}'] self.assertEqual(json.loads(response._container[0].decode()) , json.loads(expected[0].decode())) expected = {'content-type': ('Content-Type', 'application/json')} self.assertEqual(response._headers, expected)
def test_function_return_HttpResponse_2(self): json_s = '{"browTabName":"folders_contents","parent_id":"1","selRowIndex":"0"}' request = self.client.request() request.POST = {'client_request': json_s} expected = { 'sendMail': None, 'id': None, 'name': None, 'browTabName': 'folders_contents', 'parent_id': '1', 'selRowIndex': '0', 'model': None } expected = { 'browTabName': 'folders_contents', 'parent_id': '1', 'selRowIndex': '0' } self.assertEqual(parseClientRequest(request.POST), expected) request.session = { 'Selections': { "folders_contents": { "1": { 'model': "folder", 'id': "1", 'selRowIndex': "2", }, }, }, } response = ajaxStartRowIndexFromSession(request) self.assertTrue(isinstance(response, HttpResponse)) # Чи ф-ція повертає правильний response? expected = [ b'{"server_response": {"id": "1", "selRowIndex": "2", "model": "folder"}}' ] self.assertEqual(json.loads(response._container[0].decode()), json.loads(expected[0].decode())) expected = {'content-type': ('Content-Type', 'application/json')} self.assertEqual(response._headers, expected)