142 lines
4.9 KiB
Python
142 lines
4.9 KiB
Python
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from flask import Flask
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
from blueprints import admin_api
|
|
|
|
|
|
class AdminInvalidAsinDataTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.app = Flask(__name__)
|
|
|
|
def test_list_forwards_operator_and_formats_group_fields(self):
|
|
java_response = {
|
|
'data': {
|
|
'items': [{
|
|
'id': 9,
|
|
'dataValue': 'B012345678',
|
|
'brand': 'acme',
|
|
'groupId': 3,
|
|
'groupName': 'group-a',
|
|
'recordSource': 'MANUAL',
|
|
'createdAt': '2026-08-08T12:30:00',
|
|
}],
|
|
'total': 1,
|
|
'page': 1,
|
|
'pageSize': 15,
|
|
},
|
|
}
|
|
with self.app.test_request_context('/api/admin/invalid-asin-data?page=1&page_size=15&group_id=3'):
|
|
with patch.object(
|
|
admin_api,
|
|
'_ensure_backend_menu_access',
|
|
return_value=('admin', {'id': 7}, None),
|
|
), patch.object(
|
|
admin_api,
|
|
'_proxy_permission_java',
|
|
return_value=(java_response, None, 200),
|
|
) as proxy:
|
|
response = admin_api.list_invalid_asin_data.__wrapped__()
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertEqual(response.get_json()['items'], [{
|
|
'id': 9,
|
|
'data_value': 'B012345678',
|
|
'brand': 'acme',
|
|
'group_id': 3,
|
|
'group_name': 'group-a',
|
|
'record_source': 'MANUAL',
|
|
'created_at': '2026-08-08 12:30',
|
|
}])
|
|
self.assertEqual(
|
|
proxy.call_args.kwargs['params'],
|
|
{
|
|
'page': 1,
|
|
'pageSize': 15,
|
|
'keyword': '',
|
|
'groupId': 3,
|
|
},
|
|
)
|
|
self.assertEqual(proxy.call_args.kwargs['current_row'], {'id': 7})
|
|
|
|
def test_create_forwards_group_with_trusted_current_user(self):
|
|
java_response = {
|
|
'message': 'created',
|
|
'data': {
|
|
'id': 9,
|
|
'dataValue': 'B012345678',
|
|
'brand': 'acme',
|
|
'groupId': 3,
|
|
'groupName': 'group-a',
|
|
'recordSource': 'MANUAL',
|
|
},
|
|
}
|
|
with self.app.test_request_context(
|
|
'/api/admin/invalid-asin-data',
|
|
method='POST',
|
|
json={'data_value': ' B012345678 ', 'brand': 'Acme', 'group_id': 3},
|
|
):
|
|
with patch.object(
|
|
admin_api,
|
|
'_ensure_backend_menu_access',
|
|
return_value=('super_admin', {'id': 1}, None),
|
|
), patch.object(
|
|
admin_api,
|
|
'_proxy_permission_java',
|
|
return_value=(java_response, None, 200),
|
|
) as proxy:
|
|
response = admin_api.create_invalid_asin_data.__wrapped__()
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertNotIn('params', proxy.call_args.kwargs)
|
|
self.assertEqual(proxy.call_args.kwargs['current_row'], {'id': 1})
|
|
self.assertEqual(proxy.call_args.kwargs['json_data'], {
|
|
'dataValue': 'B012345678',
|
|
'brand': 'Acme',
|
|
'groupId': 3,
|
|
})
|
|
|
|
def test_group_list_marks_first_group_as_locked_for_normal_account(self):
|
|
groups = [
|
|
{'id': 3, 'groupName': 'group-a'},
|
|
{'id': 8, 'groupName': 'group-b'},
|
|
]
|
|
with self.app.test_request_context('/api/admin/shop-manage-groups'):
|
|
with patch.object(
|
|
admin_api,
|
|
'_ensure_backend_menu_access',
|
|
return_value=('admin', {'id': 7}, None),
|
|
), patch.object(
|
|
admin_api,
|
|
'_load_expanded_shop_manage_groups',
|
|
return_value=(groups, None, 200),
|
|
):
|
|
response = admin_api.list_shop_manage_groups.__wrapped__()
|
|
|
|
body = response.get_json()
|
|
self.assertEqual(body['locked_group_id'], 3)
|
|
self.assertEqual([item['id'] for item in body['items']], [3, 8])
|
|
|
|
def test_group_list_does_not_lock_super_admin(self):
|
|
with self.app.test_request_context('/api/admin/shop-manage-groups'):
|
|
with patch.object(
|
|
admin_api,
|
|
'_ensure_backend_menu_access',
|
|
return_value=('super_admin', {'id': 1}, None),
|
|
), patch.object(
|
|
admin_api,
|
|
'_load_expanded_shop_manage_groups',
|
|
return_value=([{'id': 3, 'groupName': 'group-a'}], None, 200),
|
|
):
|
|
response = admin_api.list_shop_manage_groups.__wrapped__()
|
|
|
|
self.assertIsNone(response.get_json()['locked_group_id'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|