Commit f4cbb7d5 authored by Vitali Stupin's avatar Vitali Stupin
Browse files

Adding tests, bugfix

parent ef3e16db
......@@ -62,20 +62,6 @@ def get_member_class_id(cur, member_class):
return None
def member_exists(cur, class_id, member_code):
"""Check if member exists in Central Server"""
cur.execute(
"""
select exists(
select * from security_server_clients
where type='XRoadMember' member_class_id=%(class_id)s
and member_code=%(member_code)s
)
""", {'class_id': class_id, 'member_code': member_code})
rec = cur.fetchone()
return rec[0]
def subsystem_exists(cur, member_id, subsystem_code):
"""Check if subsystem exists in Central Server"""
cur.execute(
......@@ -101,7 +87,7 @@ def get_member_data(cur, class_id, member_code):
""", {'class_id': class_id, 'member_code': member_code})
rec = cur.fetchone()
if rec:
return {'id': rec[0], 'name': rec[2]}
return {'id': rec[0], 'name': rec[1]}
return None
......@@ -237,7 +223,7 @@ def add_member(member_code, member_name, member_class, json_data):
'http_status': 400, 'code': 'INVALID_MEMBER_CLASS',
'msg': 'Provided Member Class does not exist'}
if member_exists(cur, class_id, member_code):
if get_member_data(cur, class_id, member_code) is not None:
LOGGER.warning(
'MEMBER_EXISTS: Provided Member already exists '
'(Request: %s)', json_data)
......
......@@ -67,16 +67,41 @@ reconnect=true
cur.fetchone = MagicMock(return_value=None)
self.assertEqual(None, csapi.get_member_class_id(cur, 'MEMBER_CLASS'))
def test_member_exists(self):
def test_subsystem_exists(self):
cur = MagicMock()
cur.execute = MagicMock()
cur.fetchone = MagicMock(return_value=[True])
self.assertEqual(True, csapi.member_exists(cur, 123, 'MEMBER_CODE'))
self.assertEqual(True, csapi.subsystem_exists(cur, 123, 'SUBSYSTEM_CODE'))
cur.execute.assert_called_with(
"\n select exists(\n select * from security_server_clients\n"
" where type='XRoadMember' member_class_id=%(class_id)s\n"
" where type='Subsystem' and xroad_member_id=%(member_id)s\n"
" and subsystem_code=%(subsystem_code)s\n"
" )\n ", {'member_id': 123, 'subsystem_code': 'SUBSYSTEM_CODE'})
cur.fetchone.assert_called_once()
def test_get_member_data(self):
cur = MagicMock()
cur.execute = MagicMock()
cur.fetchone = MagicMock(return_value=[1234, 'M_NAME'])
self.assertEqual(
{'id': 1234, 'name': 'M_NAME'}, csapi.get_member_data(cur, 123, 'MEMBER_CODE'))
cur.execute.assert_called_with(
"\n select id, name\n from security_server_clients\n"
" where type='XRoadMember' and member_class_id=%(class_id)s\n"
" and member_code=%(member_code)s\n"
" ", {'class_id': 123, 'member_code': 'MEMBER_CODE'})
cur.fetchone.assert_called_once()
def test_get_member_data_no_member(self):
cur = MagicMock()
cur.execute = MagicMock()
cur.fetchone = MagicMock(return_value=None)
self.assertEqual(None, csapi.get_member_data(cur, 123, 'MEMBER_CODE'))
cur.execute.assert_called_with(
"\n select id, name\n from security_server_clients\n"
" where type='XRoadMember' and member_class_id=%(class_id)s\n"
" and member_code=%(member_code)s\n"
" )\n ", {'class_id': 123, 'member_code': 'MEMBER_CODE'})
" ", {'class_id': 123, 'member_code': 'MEMBER_CODE'})
cur.fetchone.assert_called_once()
def test_get_utc_time(self):
......@@ -203,7 +228,7 @@ reconnect=true
mock_get_member_class_id.assert_called_with(
mock_get_db_connection().__enter__().cursor().__enter__(), 'MEMBER_CLASS')
@patch('csapi.member_exists', return_value=True)
@patch('csapi.get_member_data', return_value={'id': 111, 'name': 'M_NAME'})
@patch('csapi.get_member_class_id', return_value=12345)
@patch('csapi.get_db_connection')
@patch('csapi.get_db_conf', return_value={
......@@ -212,7 +237,7 @@ reconnect=true
'username': 'centerui_user'})
def test_add_member_member_exists(
self, mock_get_db_conf, mock_get_db_connection, mock_get_member_class_id,
mock_member_exists):
mock_get_member_data):
with self.assertLogs(csapi.LOGGER, level='INFO') as cm:
self.assertEqual(
{
......@@ -228,14 +253,14 @@ reconnect=true
'username': 'centerui_user'})
mock_get_member_class_id.assert_called_with(
mock_get_db_connection().__enter__().cursor().__enter__(), 'MEMBER_CLASS')
mock_member_exists.assert_called_with(
mock_get_member_data.assert_called_with(
mock_get_db_connection().__enter__().cursor().__enter__(), 12345, 'MEMBER_CODE')
@patch('csapi.add_client_name')
@patch('csapi.add_client')
@patch('csapi.add_identifier', return_value=123456)
@patch('csapi.get_utc_time', return_value='TIME')
@patch('csapi.member_exists', return_value=False)
@patch('csapi.get_member_data', return_value=None)
@patch('csapi.get_member_class_id', return_value=12345)
@patch('csapi.get_db_connection')
@patch('csapi.get_db_conf', return_value={
......@@ -244,7 +269,7 @@ reconnect=true
'username': 'centerui_user'})
def test_add_member_ok(
self, mock_get_db_conf, mock_get_db_connection, mock_get_member_class_id,
mock_member_exists, mock_get_utc_time, mock_add_identifier, mock_add_client,
mock_get_member_data, mock_get_utc_time, mock_add_identifier, mock_add_client,
mock_add_client_name):
with self.assertLogs(csapi.LOGGER, level='INFO') as cm:
self.assertEqual(
......@@ -261,7 +286,7 @@ reconnect=true
'username': 'centerui_user'})
mock_get_member_class_id.assert_called_with(
mock_get_db_connection().__enter__().cursor().__enter__(), 'MEMBER_CLASS')
mock_member_exists.assert_called_with(
mock_get_member_data.assert_called_with(
mock_get_db_connection().__enter__().cursor().__enter__(), 12345, 'MEMBER_CODE')
mock_get_utc_time.assert_called_with(
mock_get_db_connection().__enter__().cursor().__enter__())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment