-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #528 from uktrade/release/vodka
Release vodka
- Loading branch information
Showing
47 changed files
with
1,547 additions
and
571 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,155 @@ | ||
from operator import eq | ||
from types import SimpleNamespace | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from rest_framework.exceptions import ValidationError | ||
|
||
from datahub.core.validate_utils import DataCombiner | ||
from datahub.core.validators import ( | ||
AnyOfValidator, Condition, RequiredUnlessAlreadyBlankValidator, RulesBasedValidator, | ||
ValidationRule | ||
) | ||
|
||
|
||
def test_any_of_none(): | ||
"""Tests that validation fails if no any-of fields provided.""" | ||
instance = SimpleNamespace(field_a=None, field_b=None) | ||
validator = AnyOfValidator('field_a', 'field_b') | ||
validator.set_context(Mock(instance=instance)) | ||
with pytest.raises(ValidationError): | ||
validator({}) | ||
|
||
|
||
def test_any_of_some(): | ||
"""Tests that validation passes if some any-of fields provided.""" | ||
instance = SimpleNamespace(field_a=None, field_b=None) | ||
validator = AnyOfValidator('field_a', 'field_b') | ||
validator.set_context(Mock(instance=instance)) | ||
validator({'field_a': Mock()}) | ||
|
||
|
||
def test_any_of_all(): | ||
"""Tests that validation passes if all any-of fields provided.""" | ||
instance = SimpleNamespace(field_a=None, field_b=None) | ||
validator = AnyOfValidator('field_a', 'field_b') | ||
validator.set_context(Mock(instance=instance)) | ||
validator({'field_a': Mock(), 'field_b': Mock()}) | ||
|
||
|
||
@pytest.mark.parametrize('data,field,op,args,res', ( | ||
({'colour': 'red'}, 'colour', eq, ('red',), True), | ||
({'colour': 'red'}, 'colour', eq, ('blue',), False), | ||
)) | ||
def test_validation_condition(data, field, op, args, res): | ||
"""Tests ValidationCondition for various cases.""" | ||
combiner = Mock(spec_set=DataCombiner, get_value=lambda field_: data[field_]) | ||
condition = Condition(field, op, args) | ||
assert condition(combiner) == res | ||
|
||
|
||
@pytest.mark.parametrize('data,field,op,condition,res', ( | ||
({'colour': 'red', 'valid': True}, 'valid', bool, lambda x: True, True), | ||
({'colour': 'red', 'valid': False}, 'valid', bool, lambda x: True, False), | ||
({'colour': 'red', 'valid': True}, 'valid', bool, lambda x: False, True), | ||
({'colour': 'red', 'valid': False}, 'valid', bool, lambda x: False, True), | ||
)) | ||
def test_validation_rule(data, field, op, condition, res): | ||
"""Tests ValidationRule for various cases.""" | ||
combiner = Mock(spec_set=DataCombiner, get_value=lambda field_: data[field_]) | ||
rule = ValidationRule( | ||
'error_key', field, op, condition=condition | ||
) | ||
assert rule(combiner) == res | ||
|
||
|
||
def _make_stub_rule(field, return_value): | ||
return Mock(return_value=return_value, error_key='error', rule=Mock(field=field)) | ||
|
||
|
||
class TestRulesBasedValidator: | ||
"""RulesBasedValidator tests.""" | ||
|
||
@pytest.mark.parametrize('rules', ( | ||
(_make_stub_rule('field1', True),), | ||
(_make_stub_rule('field1', True), _make_stub_rule(True, 'field2')), | ||
)) | ||
def test_validation_passes(self, rules): | ||
"""Test that validation passes when the rules pass.""" | ||
instance = Mock() | ||
serializer = Mock(instance=instance, error_messages={'error': 'test error'}) | ||
validator = RulesBasedValidator(*rules) | ||
validator.set_context(serializer) | ||
assert validator({}) is None | ||
|
||
@pytest.mark.parametrize('rules,errors', ( | ||
( | ||
(_make_stub_rule('field1', False),), | ||
{'field1': 'test error'} | ||
), | ||
( | ||
(_make_stub_rule('field1', False), _make_stub_rule('field2', False),), | ||
{'field1': 'test error', 'field2': 'test error'} | ||
), | ||
( | ||
(_make_stub_rule('field1', False), _make_stub_rule('field2', True),), | ||
{'field1': 'test error'} | ||
), | ||
)) | ||
def test_validation_fails(self, rules, errors): | ||
"""Test that validation fails when any rule fails.""" | ||
instance = Mock() | ||
serializer = Mock(instance=instance, error_messages={'error': 'test error'}) | ||
validator = RulesBasedValidator(*rules) | ||
validator.set_context(serializer) | ||
with pytest.raises(ValidationError) as excinfo: | ||
validator({}) | ||
assert excinfo.value.detail == errors | ||
|
||
|
||
class TestRequiredUnlessAlreadyBlankValidator: | ||
"""RequiredUnlessAlreadyBlank tests.""" | ||
|
||
@pytest.mark.parametrize('create_data,update_data,partial,should_raise', ( | ||
({'field1': None}, {'field1': None}, False, False), | ||
({'field1': None}, {'field1': None}, True, False), | ||
({'field1': None}, {'field1': 'blah'}, False, False), | ||
({'field1': None}, {'field1': 'blah'}, True, False), | ||
({'field1': None}, {}, False, False), | ||
({'field1': None}, {}, True, False), | ||
({'field1': 'blah'}, {'field1': None}, False, True), | ||
({'field1': 'blah'}, {'field1': None}, True, True), | ||
({'field1': 'blah'}, {'field1': 'blah'}, False, False), | ||
({'field1': 'blah'}, {'field1': 'blah'}, True, False), | ||
({'field1': 'blah'}, {}, False, True), | ||
({'field1': 'blah'}, {}, True, False), | ||
)) | ||
def test_update(self, create_data, update_data, partial, should_raise): | ||
"""Tests validation during updates.""" | ||
instance = Mock(**create_data) | ||
serializer = Mock(instance=instance, partial=partial) | ||
validator = RequiredUnlessAlreadyBlankValidator('field1') | ||
validator.set_context(serializer) | ||
if should_raise: | ||
with pytest.raises(ValidationError) as excinfo: | ||
validator(update_data) | ||
assert excinfo.value.detail['field1'] == validator.required_message | ||
else: | ||
validator(update_data) | ||
|
||
@pytest.mark.parametrize('create_data,should_raise', ( | ||
({}, True), | ||
({'field1': None}, True), | ||
({'field1': 'blah'}, False), | ||
)) | ||
def test_create(self, create_data, should_raise): | ||
"""Tests validation during instance creation.""" | ||
serializer = Mock(instance=None, partial=False) | ||
validator = RequiredUnlessAlreadyBlankValidator('field1') | ||
validator.set_context(serializer) | ||
if should_raise: | ||
with pytest.raises(ValidationError) as excinfo: | ||
validator(create_data) | ||
assert excinfo.value.detail['field1'] == validator.required_message | ||
else: | ||
validator(create_data) |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
import pytest | ||
from django.contrib.auth import get_user_model | ||
from django.test.client import Client | ||
from django.utils.timezone import now | ||
from oauth2_provider.models import AccessToken, Application | ||
from rest_framework.test import APIClient | ||
|
@@ -27,6 +28,45 @@ def get_test_user(): | |
return test_user | ||
|
||
|
||
def get_admin_user(password=None): | ||
"""Return the test admin user.""" | ||
email = '[email protected]' | ||
user_model = get_user_model() | ||
try: | ||
admin_user = user_model.objects.get(email=email) | ||
except user_model.DoesNotExist: | ||
admin_user = user_model.objects.create_superuser(email=email, password=password) | ||
return admin_user | ||
|
||
|
||
class AdminTestMixin: | ||
"""All the tests using the DB and accessing admin endpoints should use this class.""" | ||
|
||
pytestmark = pytest.mark.django_db # use db | ||
|
||
PASSWORD = 'password' | ||
|
||
@property | ||
def user(self): | ||
"""Returns admin user.""" | ||
if not hasattr(self, '_user'): | ||
self._user = get_admin_user(self.PASSWORD) | ||
return self._user | ||
|
||
@property | ||
def client(self): | ||
"""Returns an authenticated admin client.""" | ||
return self.create_client() | ||
|
||
def create_client(self, user=None): | ||
"""Creates a client with admin access.""" | ||
if not user: | ||
user = self.user | ||
client = Client() | ||
client.login(username=user.email, password=self.PASSWORD) | ||
return client | ||
|
||
|
||
class APITestMixin: | ||
"""All the tests using the DB and accessing end points behind auth should use this class.""" | ||
|
||
|
Oops, something went wrong.