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): ...@@ -62,20 +62,6 @@ def get_member_class_id(cur, member_class):
return None 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): def subsystem_exists(cur, member_id, subsystem_code):
"""Check if subsystem exists in Central Server""" """Check if subsystem exists in Central Server"""
cur.execute( cur.execute(
...@@ -83,7 +69,7 @@ def subsystem_exists(cur, member_id, subsystem_code): ...@@ -83,7 +69,7 @@ def subsystem_exists(cur, member_id, subsystem_code):
select exists( select exists(
select * from security_server_clients select * from security_server_clients
where type='Subsystem' and xroad_member_id=%(member_id)s where type='Subsystem' and xroad_member_id=%(member_id)s
and subsystem_code=%(subsystem_code)s and subsystem_code=%(subsystem_code)s
) )
""", {'member_id': member_id, 'subsystem_code': subsystem_code}) """, {'member_id': member_id, 'subsystem_code': subsystem_code})
rec = cur.fetchone() rec = cur.fetchone()
...@@ -101,7 +87,7 @@ def get_member_data(cur, class_id, member_code): ...@@ -101,7 +87,7 @@ def get_member_data(cur, class_id, member_code):
""", {'class_id': class_id, 'member_code': member_code}) """, {'class_id': class_id, 'member_code': member_code})
rec = cur.fetchone() rec = cur.fetchone()
if rec: if rec:
return {'id': rec[0], 'name': rec[2]} return {'id': rec[0], 'name': rec[1]}
return None return None
...@@ -237,7 +223,7 @@ def add_member(member_code, member_name, member_class, json_data): ...@@ -237,7 +223,7 @@ def add_member(member_code, member_name, member_class, json_data):
'http_status': 400, 'code': 'INVALID_MEMBER_CLASS', 'http_status': 400, 'code': 'INVALID_MEMBER_CLASS',
'msg': 'Provided Member Class does not exist'} '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( LOGGER.warning(
'MEMBER_EXISTS: Provided Member already exists ' 'MEMBER_EXISTS: Provided Member already exists '
'(Request: %s)', json_data) '(Request: %s)', json_data)
......
...@@ -67,16 +67,41 @@ reconnect=true ...@@ -67,16 +67,41 @@ reconnect=true
cur.fetchone = MagicMock(return_value=None) cur.fetchone = MagicMock(return_value=None)
self.assertEqual(None, csapi.get_member_class_id(cur, 'MEMBER_CLASS')) self.assertEqual(None, csapi.get_member_class_id(cur, 'MEMBER_CLASS'))
def test_member_exists(self): def test_subsystem_exists(self):
cur = MagicMock() cur = MagicMock()
cur.execute = MagicMock() cur.execute = MagicMock()
cur.fetchone = MagicMock(return_value=[True]) 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( cur.execute.assert_called_with(
"\n select exists(\n select * from security_server_clients\n" "\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 member_code=%(member_code)s\n" " and subsystem_code=%(subsystem_code)s\n"
" )\n ", {'class_id': 123, 'member_code': 'MEMBER_CODE'}) " )\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"
" ", {'class_id': 123, 'member_code': 'MEMBER_CODE'})
cur.fetchone.assert_called_once() cur.fetchone.assert_called_once()
def test_get_utc_time(self): def test_get_utc_time(self):
...@@ -203,7 +228,7 @@ reconnect=true ...@@ -203,7 +228,7 @@ reconnect=true
mock_get_member_class_id.assert_called_with( mock_get_member_class_id.assert_called_with(
mock_get_db_connection().__enter__().cursor().__enter__(), 'MEMBER_CLASS') 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_member_class_id', return_value=12345)
@patch('csapi.get_db_connection') @patch('csapi.get_db_connection')
@patch('csapi.get_db_conf', return_value={ @patch('csapi.get_db_conf', return_value={
...@@ -212,7 +237,7 @@ reconnect=true ...@@ -212,7 +237,7 @@ reconnect=true
'username': 'centerui_user'}) 'username': 'centerui_user'})
def test_add_member_member_exists( def test_add_member_member_exists(
self, mock_get_db_conf, mock_get_db_connection, mock_get_member_class_id, 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: with self.assertLogs(csapi.LOGGER, level='INFO') as cm:
self.assertEqual( self.assertEqual(
{ {
...@@ -228,14 +253,14 @@ reconnect=true ...@@ -228,14 +253,14 @@ reconnect=true
'username': 'centerui_user'}) 'username': 'centerui_user'})
mock_get_member_class_id.assert_called_with( mock_get_member_class_id.assert_called_with(
mock_get_db_connection().__enter__().cursor().__enter__(), 'MEMBER_CLASS') 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_db_connection().__enter__().cursor().__enter__(), 12345, 'MEMBER_CODE')
@patch('csapi.add_client_name') @patch('csapi.add_client_name')
@patch('csapi.add_client') @patch('csapi.add_client')
@patch('csapi.add_identifier', return_value=123456) @patch('csapi.add_identifier', return_value=123456)
@patch('csapi.get_utc_time', return_value='TIME') @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_member_class_id', return_value=12345)
@patch('csapi.get_db_connection') @patch('csapi.get_db_connection')
@patch('csapi.get_db_conf', return_value={ @patch('csapi.get_db_conf', return_value={
...@@ -244,7 +269,7 @@ reconnect=true ...@@ -244,7 +269,7 @@ reconnect=true
'username': 'centerui_user'}) 'username': 'centerui_user'})
def test_add_member_ok( def test_add_member_ok(
self, mock_get_db_conf, mock_get_db_connection, mock_get_member_class_id, 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): mock_add_client_name):
with self.assertLogs(csapi.LOGGER, level='INFO') as cm: with self.assertLogs(csapi.LOGGER, level='INFO') as cm:
self.assertEqual( self.assertEqual(
...@@ -261,7 +286,7 @@ reconnect=true ...@@ -261,7 +286,7 @@ reconnect=true
'username': 'centerui_user'}) 'username': 'centerui_user'})
mock_get_member_class_id.assert_called_with( mock_get_member_class_id.assert_called_with(
mock_get_db_connection().__enter__().cursor().__enter__(), 'MEMBER_CLASS') 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_db_connection().__enter__().cursor().__enter__(), 12345, 'MEMBER_CODE')
mock_get_utc_time.assert_called_with( mock_get_utc_time.assert_called_with(
mock_get_db_connection().__enter__().cursor().__enter__()) 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