2017-02-28 04:57:40 +00:00
|
|
|
import pytest
|
|
|
|
|
2018-07-27 21:46:46 +00:00
|
|
|
from data.database import LogEntry, User
|
2017-03-13 14:52:36 +00:00
|
|
|
from data.model import config as _config
|
2017-02-28 04:57:40 +00:00
|
|
|
from data.model.log import log_action
|
2017-03-13 14:52:36 +00:00
|
|
|
|
|
|
|
from mock import patch, Mock, DEFAULT, sentinel
|
2017-02-28 04:57:40 +00:00
|
|
|
from peewee import PeeweeException
|
|
|
|
|
2017-03-13 14:52:36 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def app_config():
|
|
|
|
with patch.dict(_config.app_config, {}, clear=True):
|
|
|
|
yield _config.app_config
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def logentry_kind():
|
|
|
|
kinds = {'pull_repo': 'pull_repo_kind', 'push_repo': 'push_repo_kind'}
|
|
|
|
with patch('data.model.log.get_log_entry_kinds', return_value=kinds, spec=True):
|
|
|
|
yield kinds
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def logentry(logentry_kind):
|
2018-07-27 21:46:46 +00:00
|
|
|
with patch('data.database.LogEntry.create', spec=True):
|
|
|
|
yield LogEntry
|
2017-03-13 14:52:36 +00:00
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def user():
|
|
|
|
with patch.multiple('data.database.User', username=DEFAULT, get=DEFAULT, select=DEFAULT) as user:
|
|
|
|
user['get'].return_value = Mock(id='mock_user_id')
|
|
|
|
user['select'].return_value.tuples.return_value.get.return_value = ['default_user_id']
|
|
|
|
yield User
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('action_kind', [('pull'), ('oops')])
|
|
|
|
def test_log_action_unknown_action(action_kind):
|
|
|
|
''' test unknown action types throw an exception when logged '''
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
log_action(action_kind, None)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('user_or_org_name,account_id,account', [
|
|
|
|
('my_test_org', 'N/A', 'mock_user_id' ),
|
|
|
|
(None, 'test_account_id', 'test_account_id'),
|
|
|
|
(None, None, 'default_user_id')
|
|
|
|
])
|
|
|
|
@pytest.mark.parametrize('unlogged_pulls_ok,action_kind,db_exception,throws', [
|
2017-02-28 04:57:40 +00:00
|
|
|
(False, 'pull_repo', None, False),
|
|
|
|
(False, 'push_repo', None, False),
|
2017-03-13 14:52:36 +00:00
|
|
|
(False, 'pull_repo', PeeweeException, True ),
|
|
|
|
(False, 'push_repo', PeeweeException, True ),
|
|
|
|
|
2017-02-28 04:57:40 +00:00
|
|
|
(True, 'pull_repo', PeeweeException, False),
|
2017-03-13 14:52:36 +00:00
|
|
|
(True, 'push_repo', PeeweeException, True ),
|
|
|
|
(True, 'pull_repo', Exception, True ),
|
|
|
|
(True, 'push_repo', Exception, True )
|
2017-02-28 04:57:40 +00:00
|
|
|
])
|
2017-03-13 14:52:36 +00:00
|
|
|
def test_log_action(user_or_org_name, account_id, account, unlogged_pulls_ok, action_kind,
|
|
|
|
db_exception, throws, app_config, logentry, user):
|
2017-02-28 04:57:40 +00:00
|
|
|
log_args = {
|
2017-03-13 14:52:36 +00:00
|
|
|
'performer' : Mock(id='TEST_PERFORMER_ID'),
|
|
|
|
'repository' : Mock(id='TEST_REPO'),
|
|
|
|
'ip' : 'TEST_IP',
|
|
|
|
'metadata' : { 'test_key' : 'test_value' },
|
|
|
|
'timestamp' : 'TEST_TIMESTAMP'
|
2017-02-28 04:57:40 +00:00
|
|
|
}
|
2017-03-13 14:52:36 +00:00
|
|
|
app_config['SERVICE_LOG_ACCOUNT_ID'] = account_id
|
2017-02-28 04:57:40 +00:00
|
|
|
app_config['ALLOW_PULLS_WITHOUT_STRICT_LOGGING'] = unlogged_pulls_ok
|
|
|
|
|
|
|
|
logentry.create.side_effect = db_exception
|
2017-03-13 14:52:36 +00:00
|
|
|
|
2017-02-28 04:57:40 +00:00
|
|
|
if throws:
|
|
|
|
with pytest.raises(db_exception):
|
2017-03-13 14:52:36 +00:00
|
|
|
log_action(action_kind, user_or_org_name, **log_args)
|
2017-02-28 04:57:40 +00:00
|
|
|
else:
|
2017-03-13 14:52:36 +00:00
|
|
|
log_action(action_kind, user_or_org_name, **log_args)
|
2017-02-28 04:57:40 +00:00
|
|
|
|
2017-03-13 14:52:36 +00:00
|
|
|
logentry.create.assert_called_once_with(kind=action_kind+'_kind', account=account,
|
2017-02-28 04:57:40 +00:00
|
|
|
performer='TEST_PERFORMER_ID', repository='TEST_REPO',
|
|
|
|
ip='TEST_IP', metadata_json='{"test_key": "test_value"}',
|
|
|
|
datetime='TEST_TIMESTAMP')
|