-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Further coverage fixes, now with mocks. (#568)
* Replace a few no-cover pragmas with mocks. * Use log.exception to log exceptions. - Also set exc_info=True to ensure structlog deals with the stack trace. * Updates for new user model, also add trivial 401 checks for hostpolicy endpoints. * Only fetch user after `IsAuthenticated.has_permission()` has been called. - This is mostly due a coverage fix with the way IsAuthenticated is implemented the same way as the raising of NotAuthenticated in User.from_request. * Make it so illegal CIDRs in range queries return 400/Bad request instead of empty results. * Test put disallowed, add explicit toml support for coverage. * Support `pragma: no cover` again. * no cover-pragmas for settings.py.
- Loading branch information
Showing
13 changed files
with
153 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from unittest import TestCase, mock | ||
from django.core.exceptions import ValidationError | ||
from mreg.api.v1.history import HistoryLog | ||
|
||
class TestHistoryLog(TestCase): | ||
|
||
@mock.patch('django.db.models.Model.full_clean') | ||
@mock.patch('mreg.api.v1.history.HistoryLog.manipulate_data') | ||
@mock.patch('mreg.api.v1.history.HistoryLog.get_jsondata') | ||
def test_save_log_handles_validation_error(self, mock_get_jsondata, mock_manipulate_data, mock_full_clean): | ||
mock_full_clean.side_effect = ValidationError('Test error') | ||
mock_get_jsondata.return_value = "json data" | ||
mock_manipulate_data.return_value = None | ||
|
||
# Mocking Serializer and its Meta | ||
mock_serializer = mock.Mock() | ||
mock_meta = mock.Mock() | ||
mock_meta.model = mock.Mock(__name__='TestModel') | ||
mock_serializer.Meta = mock_meta | ||
mock_serializer.data = {'id': 1, 'name': 'Test'} | ||
|
||
data = {'key': 'value'} | ||
|
||
# Creating HistoryLog instance | ||
log = HistoryLog() | ||
log.model = mock_meta.model | ||
log.foreign_key_name = 'key' | ||
log.request = mock.Mock(user=mock.Mock()) | ||
log.log_resource = 'test_resource' | ||
log.m2m_field = 'test_field' | ||
log.object = mock.Mock(name='TestObject', id=2) | ||
|
||
# Mocking an instance for save_log_m2m_alteration method | ||
instance = mock.Mock(id=1, name='TestInstance') | ||
|
||
# Mocking a method for save_log_m2m_alteration method | ||
method = mock.Mock(__name__='TestMethod') | ||
|
||
with self.assertLogs('mreg.history', level='ERROR') as cm: | ||
log.save_log('test_action', mock_serializer, data) | ||
log.save_log_m2m_alteration(method, instance) | ||
|
||
self.assertEqual(mock_full_clean.call_count, 2) | ||
|
||
# Assert that the error was logged twice, with tracebacks. | ||
self.assertTrue(cm.output[0].startswith('ERROR:mreg.history:')) | ||
self.assertTrue(cm.output[1].startswith('ERROR:mreg.history:')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters