Skip to content
Snippets Groups Projects
Commit bb073734 authored by Tore.Brede's avatar Tore.Brede
Browse files

GREG-130: Allowing D-number in national ID-field

parent a5fe0678
No related branches found
No related tags found
1 merge request!187GREG-130: Allow dnumber
......@@ -270,7 +270,7 @@ class Identity(BaseModel):
FEIDE_ID = "feide_id"
FEIDE_EMAIL = "feide_email"
PASSPORT_NUMBER = "passport_number"
# Norwegian national ID - "fødselsnummer"
# Norwegian national ID - "fødselsnummer" or D-number
NORWEGIAN_NATIONAL_ID_NUMBER = "norwegian_national_id_number"
PRIVATE_EMAIL = "private_email"
PRIVATE_MOBILE_NUMBER = "private_mobile"
......
......@@ -9,9 +9,17 @@ def camel_to_snake(s: str) -> str:
return re.sub("([A-Z])", "_\\1", s).lower().lstrip("_")
def is_valid_id_number(input_digits: str) -> bool:
"""
Checks whether the input represents a valid national ID number (fødselsnummer) or a valid D-number.
"""
is_dnumber = int(input_digits[0:1]) >= 4
return is_valid_norwegian_national_id_number(input_digits, is_dnumber)
def is_valid_norwegian_national_id_number(input_digits: str, is_dnumber: bool) -> bool:
"""
Checks whether input_digits is a valid national ID number of D-number.
Checks whether input_digits is a valid national ID number or D-number.
It is based on the code found here:
https://github.com/navikt/fnrvalidator/blob/master/src/validator.js
......
......@@ -467,3 +467,29 @@ def test_saves_consents(client, invited_person, consent_type_foo, consent_type_b
type=consent_type_bar,
choice=consent_type_bar.choices.filter(value="yes").first(),
).exists()
@pytest.mark.django_db
def test_post_invited_info_valid_dnumber(client, invited_person):
person, invitation_link = invited_person
d_number = "53097248016"
data = {
"person": {
"mobile_phone": "+4797543992",
"email": "test@example.com",
"fnr": d_number,
}
}
url = reverse("gregui-v1:invited-info")
assert person.fnr is None
session = client.session
session["invite_id"] = str(invitation_link.uuid)
session.save()
response = client.post(url, data, format="json")
assert response.status_code == status.HTTP_200_OK, response.data
person.refresh_from_db()
assert person.fnr.value == d_number
......@@ -3,16 +3,17 @@ from typing import Optional
import phonenumbers
from rest_framework import serializers
from greg.utils import is_valid_norwegian_national_id_number
from greg.utils import is_valid_id_number
_valid_email_regex = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
def validate_norwegian_national_id_number(value):
# Not excepted that D-numbers will be entered through the form, so only
# accept national ID numbers
if not is_valid_norwegian_national_id_number(value, False):
raise serializers.ValidationError("Not a valid Norwegian national ID number")
"""
Both fødselsnummer and D-nummer are accepted
"""
if not is_valid_id_number(value):
raise serializers.ValidationError("Not a valid ID number")
def validate_phone_number(value):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment