Skip to content

Commit

Permalink
fixup! ✨(dashboard) add validation and data updates for consent manag…
Browse files Browse the repository at this point in the history
…ement
  • Loading branch information
ssorin committed Feb 5, 2025
1 parent 1ab145c commit e438b6a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 243 deletions.
46 changes: 18 additions & 28 deletions src/dashboard/apps/consent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,14 @@
"maxLength": 5,
"pattern": r"^\d{4}[A-Z]$",
},
"address": {
"type": "object",
"properties": {
"line_1": {"type": ["string", "null"], "maxLength": 255},
"line_2": {"type": ["string", "null"], "maxLength": 255},
"zip_code": {
"type": ["string", "null"],
"maxLength": 5,
"pattern": "^[0-9]{1,5}$",
},
"city": {"type": ["string", "null"], "maxLength": 255},
},
"required": ["line_1", "zip_code", "city"],
"address_1": {"type": ["string", "null"], "maxLength": 255},
"address_2": {"type": ["string", "null"], "maxLength": 255},
"zip_code": {
"type": ["string", "null"],
"maxLength": 5,
"pattern": "^[0-9]{1,5}$",
},
"city": {"type": ["string", "null"], "maxLength": 255},
},
"required": [
"company_type",
Expand All @@ -55,7 +49,9 @@
"trade_name",
"siret",
"naf",
"address",
"address_1",
"zip_code",
"city",
],
"additionalProperties": False,
}
Expand All @@ -78,22 +74,16 @@
"name": {"type": ["string", "null"], "maxLength": 255},
"represented_by": {"type": ["string", "null"], "maxLength": 255},
"email": {"type": ["string", "null"], "format": "email"},
"address": {
"type": "object",
"properties": {
"line_1": {"type": ["string", "null"], "maxLength": 255},
"line_2": {"type": ["string", "null"], "maxLength": 255},
"zip_code": {
"type": ["string", "null"],
"maxLength": 5,
"pattern": "^[0-9]{1,5}$",
"address_1": {"type": ["string", "null"], "maxLength": 255},
"address_2": {"type": ["string", "null"], "maxLength": 255},
"zip_code": {
"type": ["string", "null"],
"maxLength": 5,
"pattern": "^[0-9]{1,5}$",
},
"city": {"type": ["string", "null"], "maxLength": 255},
},
"required": ["line_1", "zip_code", "city"],
},
"city": {"type": ["string", "null"], "maxLength": 255},
},
"required": ["name", "represented_by", "email", "address"],
"required": ["name", "represented_by", "email", "address_1", "zip_code", "city"],
"additionalProperties": False,
}

Expand Down
50 changes: 21 additions & 29 deletions src/dashboard/apps/consent/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
"trade_name": "The test company",
"siret": "12345678901234",
"naf": "1234A",
"address": {
"line_1": "1 rue Exemple",
"line_2": None,
"zip_code": "75000",
"city": "Paris",
},
"address_1": "1 rue Exemple",
"address_2": None,
"zip_code": "75000",
"city": "Paris",
}

VALID_REPRESENTATIVE_DATA = {
Expand All @@ -35,12 +33,10 @@
"name": "QualiCharge",
"represented_by": "John Doe",
"email": "[email protected]",
"address": {
"line_1": "1 Rue Exemple",
"line_2": None,
"zip_code": "75000",
"city": "Paris",
},
"address_1": "1 Rue Exemple",
"address_2": None,
"zip_code": "75000",
"city": "Paris",
}


Expand Down Expand Up @@ -111,38 +107,38 @@ def test_validate_compnay_naf_code_invalid(value):
def test_validate_zip_code_valid(value):
"""Tests validation of valid zip codes does not raise an exception."""
valid_company_data = VALID_COMPANY_DATA
valid_company_data["address"]["zip_code"] = value
valid_company_data["zip_code"] = value
assert validate_company_schema(valid_company_data) is None

valid_authority_data = VALID_CONTROL_AUTHORITY_DATA
valid_authority_data["address"]["zip_code"] = value
valid_authority_data["zip_code"] = value
assert validate_control_authority_schema(valid_authority_data) is None


@pytest.mark.parametrize("value", ["123456", "12a45", "abcde", "", " ", 12345])
def test_validate_zip_code_invalid(value):
"""Tests validation of invalid zip codes raise a ValidationError."""
valid_company_data = VALID_COMPANY_DATA
valid_company_data["address"]["zip_code"] = value
valid_company_data["zip_code"] = value
with pytest.raises(ValidationError):
validate_company_schema(valid_company_data)
# reset with valid zip
VALID_COMPANY_DATA["address"]["zip_code"] = "12345"
VALID_COMPANY_DATA["zip_code"] = "12345"

valid_authority_data = VALID_CONTROL_AUTHORITY_DATA
valid_authority_data["address"]["zip_code"] = value
valid_authority_data["zip_code"] = value
with pytest.raises(ValidationError):
validate_control_authority_schema(valid_authority_data)
# reset with valid zip
VALID_CONTROL_AUTHORITY_DATA["address"]["zip_code"] = "12345"
VALID_CONTROL_AUTHORITY_DATA["zip_code"] = "12345"


def test_validate_company_schema_valid():
"""Test the json schema validator with a valid company data."""
assert validate_company_schema(VALID_COMPANY_DATA) is None

# valid with specific zip code
VALID_COMPANY_DATA["address"]["zip_code"] = "978"
VALID_COMPANY_DATA["zip_code"] = "978"
assert validate_company_schema(VALID_COMPANY_DATA) is None

# test with null values
Expand All @@ -153,11 +149,9 @@ def test_validate_company_schema_valid():
"trade_name": None,
"siret": None,
"naf": None,
"address": {
"line_1": None,
"zip_code": None,
"city": None,
},
"address_1": None,
"zip_code": None,
"city": None,
}
assert validate_company_schema(valid_company_data) is None

Expand Down Expand Up @@ -218,11 +212,9 @@ def test_validate_control_authority_schema_valid():
"name": None,
"represented_by": None,
"email": None,
"address": {
"line_1": None,
"zip_code": None,
"city": None,
},
"address_1": None,
"zip_code": None,
"city": None,
}
assert validate_control_authority_schema(validate_control_authority_data) is None

Expand Down
77 changes: 21 additions & 56 deletions src/dashboard/apps/consent/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@

import pytest
from django.conf import settings
from django.core.exceptions import ValidationError
from django.urls import reverse

from apps.auth.factories import UserFactory
from apps.consent import AWAITING, REVOKED, VALIDATED
from apps.consent.factories import ConsentFactory
from apps.consent.models import Consent
from apps.consent.views import (
ConsentFormView,
_get_validate_company_representative,
_get_validate_control_authority,
)
from apps.consent.views import ConsentFormView
from apps.core.factories import DeliveryPointFactory, EntityFactory

FORM_CLEANED_DATA = {
Expand Down Expand Up @@ -90,22 +85,36 @@ def test_bulk_update_consent_status(rf):
)
assert c.signature_location == settings.CONSENT_SIGNATURE_LOCATION

# check company data are present
# check company data are present in company json field
assert c.company is not None
assert c.company["name"] == entity.name
assert c.company["company_type"] == entity.company_type
assert c.company["name"] == entity.name
assert c.company["legal_form"] == entity.legal_form
assert c.company["trade_name"] == entity.trade_name
assert c.company["siret"] == entity.siret
assert c.company["naf"] == entity.naf
assert c.company["address_1"] == entity.address_1
assert c.company["address_2"] == entity.address_2
assert c.company["zip_code"] == entity.address_zip_code
assert c.company["city"] == entity.address_city

# check company representative data are present
assert c.company is not None
assert c.company_representative["firstname"] == user.first_name
assert c.company_representative["lastname"] == user.last_name
assert c.company_representative["email"] == user.email

# check control authority data are presents
assert c.control_authority is not None
assert c.control_authority["name"] == settings.CONSENT_CONTROL_AUTHORITY["name"]
assert (
c.control_authority["email"] == settings.CONSENT_CONTROL_AUTHORITY["email"]
)
control_authority = settings.CONSENT_CONTROL_AUTHORITY
assert c.control_authority["name"] == control_authority["name"]
assert (c.control_authority["represented_by"] ==
control_authority["represented_by"])
assert c.control_authority["email"] == control_authority["email"]
assert c.control_authority["address_1"] == control_authority["address_1"]
assert c.control_authority["address_2"] == control_authority["address_2"]
assert c.control_authority["zip_code"] == control_authority["zip_code"]
assert c.control_authority["city"] == control_authority["city"]

# check authorizations (boolean fields)
assert c.is_authoritative_signatory is True
Expand Down Expand Up @@ -393,47 +402,3 @@ def test_form_post_empty(rf):

expected = 'id="checkboxes-error-message-error"'
assert (expected in html) is True


@pytest.mark.django_db
def test_get_validate_control_authority_with_valid_data():
"""Test _get_validate_control_authority with valid data."""
result = _get_validate_control_authority()

control_authority = settings.CONSENT_CONTROL_AUTHORITY
assert result["name"] == control_authority.get("name")
assert result["represented_by"] == control_authority.get("represented_by")
assert result["email"] == control_authority.get("email")
assert result["address"]["line_1"] == control_authority.get("address_1")
assert result["address"]["zip_code"] == control_authority.get("zip_code")
assert result["address"]["city"] == control_authority.get("city")


def test_get_validate_control_authority_raise_validation_error(settings):
"""Test _get_validate_control_authority raises exception with invalid data."""
settings.CONSENT_CONTROL_AUTHORITY = {}

with pytest.raises(ValidationError):
_get_validate_control_authority()


@pytest.mark.django_db
def test_get_validate_company_representative_valid_user():
"""Test _get_validate_company_representative with a valid user."""
user = UserFactory()

result = _get_validate_company_representative(user)

assert result["firstname"] == user.first_name
assert result["lastname"] == user.last_name
assert result["email"] == user.email
assert result["phone"] is None


@pytest.mark.django_db
def test_get_validate_company_representative_raise_validation_error():
"""Test _get_validate_company_representative raises exception with invalid data."""
user = None

with pytest.raises(ValidationError):
_get_validate_company_representative(user)
Loading

0 comments on commit e438b6a

Please sign in to comment.