def get_list_params_test(self, resource_name, get_list_params_method): with patch('custom_admin.services.lists.get_paginator') as paginator: paginator.return_value = static_paginator mock_fail_get_list = extract_mock_file('fail_list', resource_name[:-1]) with patch( 'custom_admin.services.lists.requests.get') as mocked_get: mocked_get.return_value.json = lambda: mock_fail_get_list mocked_get.return_value.status_code = 500 list_params = get_list_params_method("", {}) self.assertDictEqual( list_params, { 'error_message': 'failed to get {}'.format(resource_name), 'response': mock_fail_get_list, }) with patch( 'custom_admin.services.lists.requests.get') as mocked_get: mocked_get.return_value.json = lambda: extract_mock_file( 'list', resource_name[:-1]) mocked_get.return_value.status_code = 200 list_params = get_list_params_method("", {}) self.assertDictEqual(list_params, get_result('list', resource_name[:-1]))
def detail_view_test(test_case, resource_name, detail_id): with patch('custom_admin.services.auth.is_authenticated') as is_authenticated: is_authenticated.return_value = True mock_fail_get_detail = extract_mock_file('fail_detail', resource_name) with patch('custom_admin.services.details.requests.get') as mocked_get: mocked_get.return_value.json = lambda: mock_fail_get_detail mocked_get.return_value.status_code = 500 test_case.client.cookies = SimpleCookie({'token': token}) resp = test_case.client.get(reverse('custom_admin:{}'.format(resource_name), args=[detail_id])) test_case.assertIs(resp.status_code, 200) test_case.assertTemplateUsed(resp, 'custom_admin/error_page.html') test_case.assertDictEqual(resp.context['msg'], { 'error_message': 'failed to get {}'.format(resource_name), 'response': mock_fail_get_detail, }) mock_fail_tts_filename = extract_mock_file('fail_tts_filename', 'other') with patch('custom_admin.services.details.requests.get') as mocked_get: mocked_get.return_value.json.side_effect = [extract_mock_file('detail', resource_name)] + \ 6 * [mock_fail_tts_filename] mocked_get.return_value.status_code = 200 test_case.client.cookies = SimpleCookie({'token': token}) resp = test_case.client.get(reverse('custom_admin:{}'.format(resource_name), args=[detail_id])) test_case.assertIs(resp.status_code, 200) test_case.assertTemplateUsed(resp, 'custom_admin/detail.html') for key, val in get_result('detail', resource_name).items(): test_case.assertEqual(resp.context[key], val)
def test_index(self): with patch('custom_admin.services.auth.is_authenticated') as is_authenticated: is_authenticated.return_value = True with patch('custom_admin.services.auth.requests.post') as mocked_post: mocked_post.return_value.json = lambda: extract_mock_file('fail_authorize', 'other') mocked_post.return_value.status_code = 500 form_data = {'username': '', 'password': ''} resp = self.client.post(reverse('custom_admin:index'), data=form_data) self.assertIs(resp.status_code, 200) self.assertTemplateUsed(resp, 'custom_admin/error_page.html') self.assertDictEqual(resp.context['msg'], { 'error_message': 'failed to authorize', 'response': extract_mock_file('fail_authorize', 'other'), }) with patch('custom_admin.services.auth.requests.post') as mocked_post: mocked_post.return_value.json = lambda: extract_mock_file('authorize', 'other') mocked_post.return_value.status_code = 200 form_data = {'username': '', 'password': ''} resp = self.client.post(reverse('custom_admin:index'), data=form_data) self.assertIs(resp.status_code, 200) self.assertTemplateUsed(resp, 'custom_admin/index.html') self.assertEqual(resp.cookies['token'].value, token)
def edit_view_test(test_case, resource_name, item_id): with patch('custom_admin.services.auth.is_authenticated') as is_authenticated: is_authenticated.return_value = True mock_fail_change_item = extract_mock_file('fail_change', resource_name) with patch('custom_admin.services.modify.requests.put') as mocked_put: mocked_put.return_value.json = lambda: mock_fail_change_item mocked_put.return_value.status_code = 500 test_case.client.cookies = SimpleCookie({'token': token}) resp = test_case.client.post(reverse('custom_admin:edit_{}'.format(resource_name), args=[item_id]), data={}) test_case.assertIs(resp.status_code, 200) test_case.assertTemplateUsed(resp, 'custom_admin/error_page.html') test_case.assertDictEqual(resp.context['msg'], { 'error_message': 'failed to change {}'.format(resource_name), 'response': mock_fail_change_item, }) mock_change_item = extract_mock_file('change', resource_name) with patch('custom_admin.services.modify.requests.put') as mocked_put, \ patch('custom_admin.services.details.requests.get') as mocked_get: mocked_put.return_value.json = lambda: mock_change_item mocked_put.return_value.status_code = 200 mocked_get.return_value.json = lambda: mock_change_item mocked_get.return_value.status_code = 200 test_case.client.cookies = SimpleCookie({'token': token}) resp = test_case.client.post(reverse('custom_admin:edit_{}'.format(resource_name), args=[item_id]), data={}) test_case.assertIs(resp.status_code, 200) test_case.assertTemplateUsed(resp, 'custom_admin/edit_detail.html') detail = get_result('change', resource_name) for col_name in ('ctime', 'utime'): if col_name in detail: detail[col_name] = to_normal_time(detail[col_name], using_ms=False) result = get_result('detail', resource_name) result['detail'] = detail for key, val in result.items(): test_case.assertEqual(resp.context[key], val)
def test_authorize(self): with patch('custom_admin.services.auth.requests.post') as mocked_post: mocked_post.return_value.json = lambda: extract_mock_file( 'fail_authorize', 'other') mocked_post.return_value.status_code = 500 result = authorize("", "") self.assertDictEqual( result, { 'error_message': 'failed to authorize', 'response': extract_mock_file('fail_authorize', 'other'), }) with patch('custom_admin.services.auth.requests.post') as mocked_post: mocked_post.return_value.json = lambda: extract_mock_file( 'authorize', 'other') mocked_post.return_value.status_code = 200 result_token = authorize("", "") self.assertEqual(result_token, token)
def list_view_test(test_case, resource_name): with patch('custom_admin.services.auth.is_authenticated') as is_authenticated: is_authenticated.return_value = True with patch('custom_admin.services.lists.get_paginator') as paginator: paginator.return_value = static_paginator mock_fail_get_list = extract_mock_file('fail_list', resource_name[:-1]) with patch('custom_admin.services.lists.requests.get') as mocked_get: mocked_get.return_value.json = lambda: mock_fail_get_list mocked_get.return_value.status_code = 500 test_case.client.cookies = SimpleCookie({'token': token}) resp = test_case.client.get(reverse('custom_admin:{}'.format(resource_name))) test_case.assertIs(resp.status_code, 200) test_case.assertTemplateUsed(resp, 'custom_admin/error_page.html') test_case.assertDictEqual(resp.context['msg'], { 'error_message': 'failed to get {}'.format(resource_name), 'response': mock_fail_get_list, }) with patch('custom_admin.services.lists.requests.get') as mocked_get: mocked_get.return_value.json = lambda: extract_mock_file('list', resource_name[:-1]) mocked_get.return_value.status_code = 200 test_case.client.cookies = SimpleCookie({'token': token}) resp = test_case.client.get(reverse('custom_admin:{}'.format(resource_name))) test_case.assertIs(resp.status_code, 200) test_case.assertTemplateUsed(resp, 'custom_admin/list_items.html') result = get_result('list', resource_name[:-1]) for key, val in result.items(): if type(val) == list: test_case.assertListEqual(list(resp.context[key]), list(val)) else: test_case.assertEqual(resp.context[key], val)
def change_item_test(self, resource_name, change_item_method, item_id, item_params): mock_fail_change_item = extract_mock_file('fail_change', resource_name) with patch('custom_admin.services.modify.requests.put') as mocked_put: mocked_put.return_value.json = lambda: mock_fail_change_item mocked_put.return_value.status_code = 500 result = change_item_method("", item_id, item_params) self.assertDictEqual( result, { 'error_message': 'failed to change {}'.format(resource_name), 'response': mock_fail_change_item, }) with patch('custom_admin.services.modify.requests.put') as mocked_put: mocked_put.return_value.json = lambda: extract_mock_file( 'change', resource_name) mocked_put.return_value.status_code = 200 result = change_item_method("", item_id, item_params) self.assertDictEqual(result, get_result('change', resource_name))
def test_get_tts_filename(self): mock_fail_tts_filename = extract_mock_file('fail_tts_filename', 'other') with patch('custom_admin.services.tts_generation.requests.get' ) as mocked_get: mocked_get.return_value.json = lambda: mock_fail_tts_filename mocked_get.return_value.status_code = 404 result = get_tts_filename('') self.assertDictEqual( result, { 'error_message': 'failed to get tts filename', 'response': mock_fail_tts_filename, }) with patch('custom_admin.services.tts_generation.requests.get' ) as mocked_get: mocked_get.return_value.json = lambda: extract_mock_file( 'tts_filename', 'other') mocked_get.return_value.status_code = 200 result = get_tts_filename('') self.assertEqual(result, tts_filename)
def get_detail_params_test(self, resource_name, get_detail_params_method, detail_id): mock_fail_get_detail = extract_mock_file('fail_detail', resource_name) with patch('custom_admin.services.details.requests.get') as mocked_get: mocked_get.return_value.json = lambda: mock_fail_get_detail mocked_get.return_value.status_code = 500 detail_params = get_detail_params_method("", detail_id) self.assertDictEqual( detail_params, { 'error_message': 'failed to get {}'.format(resource_name), 'response': mock_fail_get_detail, }) mock_fail_tts_filename = extract_mock_file('fail_tts_filename', 'other') with patch('custom_admin.services.details.requests.get') as mocked_get: mocked_get.return_value.json.side_effect = [extract_mock_file('detail', resource_name)] + \ 6 * [mock_fail_tts_filename] mocked_get.return_value.status_code = 200 detail_params = get_detail_params_method("", detail_id) self.assertDictEqual(detail_params, get_result('detail', resource_name))
def test_get_calls_params(self): resource_name = 'calls' self.get_list_params_test(resource_name, get_calls_params) # testing case with breadcrumbs with patch('custom_admin.services.lists.get_paginator') as paginator: paginator.return_value = static_paginator with patch( 'custom_admin.services.lists.requests.get') as mocked_get: mocked_get.return_value.json = lambda: extract_mock_file( 'list', resource_name[:-1]) mocked_get.return_value.status_code = 200 list_params = get_calls_params("", {'transit': 1}) self.assertDictEqual(list_params, get_result('list2', resource_name[:-1]))
def change_order(token, order_id, params): with patch('custom_admin.services.modify.requests.put') as mocked_put: mocked_put.return_value.json = lambda: extract_mock_file('change', 'order') mocked_put.return_value.status_code = 200 fields = ['url', 'type', 'integration_id', 'method'] url = CORE_API_CP1.format('orders/{}'.format(order_id)) request_method = wrap_request_method(requests.put, url, json={ 'access_token': token, **pre_process_params(fields, params) }) content = handle_request_errors(request_method, 'change order') if 'error_message' in content: return content return content['result']
def get_orders_params(token, get_params): list_name = 'orders' with patch('custom_admin.services.lists.get_paginator') as paginator: paginator.return_value = static_paginator with patch('custom_admin.services.lists.requests.get') as mocked_get: mocked_get.return_value.json = lambda: extract_mock_file( 'list', list_name[:-1]) mocked_get.return_value.status_code = 200 orders_params = get_list_params(token, get_params, list_name) if 'error_message' in orders_params: return orders_params orders_params['can_edit'] = True return orders_params