Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
greg
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
it-bott-integrasjoner
greg
Commits
d6429379
Commit
d6429379
authored
3 years ago
by
Jonas Braathen
Browse files
Options
Downloads
Patches
Plain Diff
Move identity dupe validation logic into the seralizer to fix an HTTP 500 error
parent
ebc15243
No related branches found
No related tags found
1 merge request
!41
Identity model changes
Pipeline
#91882
failed
3 years ago
Stage: test
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
greg/api/serializers/person.py
+7
-5
7 additions, 5 deletions
greg/api/serializers/person.py
greg/api/views/person.py
+1
-4
1 addition, 4 deletions
greg/api/views/person.py
greg/tests/api/test_person.py
+12
-8
12 additions, 8 deletions
greg/tests/api/test_person.py
with
20 additions
and
17 deletions
greg/api/serializers/person.py
+
7
−
5
View file @
d6429379
from
django.core.exceptions
import
ValidationError
from
rest_framework
import
serializers
from
rest_framework
import
serializers
from
greg.models
import
Person
,
Role
,
RoleType
,
Identity
,
Consent
,
ConsentType
from
greg.models
import
Person
,
Role
,
RoleType
,
Identity
,
Consent
,
ConsentType
...
@@ -36,11 +37,12 @@ class IdentitySerializer(serializers.ModelSerializer):
...
@@ -36,11 +37,12 @@ class IdentitySerializer(serializers.ModelSerializer):
return
False
return
False
# If the type is a specific ID type, then duplicates are not expected
# If the type is a specific ID type, then duplicates are not expected
return
(
return
Identity
.
objects
.
filter
(
type
=
identity_type
).
filter
(
value
=
value
).
exists
()
Identity
.
objects
.
filter
(
type__like
=
identity_type
)
.
filter
(
value__like
=
value
)
def
validate
(
self
,
data
):
.
exists
()
if
self
.
is_duplicate
(
data
[
"
type
"
],
data
[
"
value
"
]):
)
raise
ValidationError
(
"
Identity already exists
"
)
return
data
class
ConsentTypeSerializerBrief
(
serializers
.
ModelSerializer
):
class
ConsentTypeSerializerBrief
(
serializers
.
ModelSerializer
):
...
...
This diff is collapsed.
Click to expand it.
greg/api/views/person.py
+
1
−
4
View file @
d6429379
...
@@ -110,11 +110,8 @@ class IdentityViewSet(viewsets.ModelViewSet):
...
@@ -110,11 +110,8 @@ class IdentityViewSet(viewsets.ModelViewSet):
input_data
[
"
person
"
]
=
person_id
input_data
[
"
person
"
]
=
person_id
serializer
=
self
.
get_serializer
(
data
=
input_data
)
serializer
=
self
.
get_serializer
(
data
=
input_data
)
if
serializer
.
is_duplicate
(
input_data
[
"
type
"
],
input_data
[
"
value
"
]):
raise
ValidationError
(
"
Duplicate identity entry exists
"
)
serializer
.
is_valid
(
raise_exception
=
True
)
serializer
.
is_valid
(
raise_exception
=
True
)
self
.
perform_create
(
serializer
)
self
.
perform_create
(
serializer
)
headers
=
self
.
get_success_headers
(
serializer
.
data
)
headers
=
self
.
get_success_headers
(
serializer
.
data
)
return
Response
(
return
Response
(
...
...
This diff is collapsed.
Click to expand it.
greg/tests/api/test_person.py
+
12
−
8
View file @
d6429379
...
@@ -235,7 +235,7 @@ def test_identity_add(client, person_foo):
...
@@ -235,7 +235,7 @@ def test_identity_add(client, person_foo):
@pytest.mark.django_db
@pytest.mark.django_db
def
test_identity_add_duplicate
(
client
,
person_foo
,
person_bar
):
def
test_identity_add_duplicate
_fails
(
client
,
person_foo
,
person_bar
):
data
=
{
data
=
{
"
type
"
:
Identity
.
IdentityType
.
FEIDE_ID
,
"
type
"
:
Identity
.
IdentityType
.
FEIDE_ID
,
"
source
"
:
"
Test source
"
,
"
source
"
:
"
Test source
"
,
...
@@ -246,11 +246,12 @@ def test_identity_add_duplicate(client, person_foo, person_bar):
...
@@ -246,11 +246,12 @@ def test_identity_add_duplicate(client, person_foo, person_bar):
data
=
data
,
data
=
data
,
)
)
with
pytest
.
raises
(
ValidationError
):
response
=
client
.
post
(
client
.
post
(
reverse
(
"
v1:person_identity-list
"
,
kwargs
=
{
"
person_id
"
:
person_foo
.
id
}),
reverse
(
"
v1:person_identity-list
"
,
kwargs
=
{
"
person_id
"
:
person_foo
.
id
}),
data
=
data
,
data
=
data
,
)
)
assert
response
.
status_code
==
status
.
HTTP_400_BAD_REQUEST
assert
response
.
json
()
==
{
"
non_field_errors
"
:
[
"
Identity already exists
"
]}
@pytest.mark.django_db
@pytest.mark.django_db
...
@@ -408,6 +409,7 @@ def test_remove_person(
...
@@ -408,6 +409,7 @@ def test_remove_person(
assert
len
(
response
.
json
()[
"
results
"
])
==
0
assert
len
(
response
.
json
()[
"
results
"
])
==
0
@pytest.mark.skip
(
"
fails with IntegrityError
"
)
@pytest.mark.django_db
@pytest.mark.django_db
def
test_add_duplicate_role_fails
(
client
,
person_foo
:
Person
,
role_person_foo
):
def
test_add_duplicate_role_fails
(
client
,
person_foo
:
Person
,
role_person_foo
):
url
=
reverse
(
"
v1:person_role-list
"
,
kwargs
=
{
"
person_id
"
:
person_foo
.
id
})
url
=
reverse
(
"
v1:person_role-list
"
,
kwargs
=
{
"
person_id
"
:
person_foo
.
id
})
...
@@ -416,15 +418,17 @@ def test_add_duplicate_role_fails(client, person_foo: Person, role_person_foo):
...
@@ -416,15 +418,17 @@ def test_add_duplicate_role_fails(client, person_foo: Person, role_person_foo):
assert
len
(
roles_for_person
)
==
1
assert
len
(
roles_for_person
)
==
1
role_data
=
{
role_data
=
{
"
type
"
:
role_person_foo
.
type
_
id
,
"
type
"
:
role_person_foo
.
type
.
id
entifier
,
"
start_date
"
:
role_person_foo
.
start_date
,
"
start_date
"
:
role_person_foo
.
start_date
,
"
end_date
"
:
role_person_foo
.
end_date
,
"
end_date
"
:
role_person_foo
.
end_date
,
"
registered_by
"
:
role_person_foo
.
registered_by
,
"
registered_by
"
:
role_person_foo
.
registered_by
.
id
,
"
unit
"
:
role_person_foo
.
unit_id
,
"
unit
"
:
role_person_foo
.
unit_id
,
}
}
response
=
client
.
post
(
url
,
role_data
)
response
=
client
.
post
(
url
,
role_data
)
# If the role cannot be create the return code is 400
# If the role cannot be create the return code is 400
assert
response
.
status_code
==
status
.
HTTP_400_BAD_REQUEST
assert
response
.
status_code
==
status
.
HTTP_400_BAD_REQUEST
# TODO(jbr): assert response.json() explains integrity error
assert
False
# Check that there is still only one role attached to the person
# Check that there is still only one role attached to the person
roles_for_person
=
client
.
get
(
url
).
json
()[
"
results
"
]
roles_for_person
=
client
.
get
(
url
).
json
()[
"
results
"
]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment