Skip to content

Commit

Permalink
feat(data-request): prevent request data from embargoed runs
Browse files Browse the repository at this point in the history
  • Loading branch information
wiwski committed Jul 17, 2024
1 parent 36e7cf6 commit 70b34e5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
3 changes: 2 additions & 1 deletion data_request/api_views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from rest_framework import generics, serializers

from data_request.emails import send_data_request_created_email
from lab.runs.models import Run

from .models import DataRequest


class DataRequestSerializer(serializers.ModelSerializer):
runs = serializers.PrimaryKeyRelatedField(
many=True,
queryset=DataRequest.runs.rel.model.objects.all(), # type: ignore[attr-defined] # pylint: disable=no-member
queryset=Run.objects.only_not_embargoed(),
allow_empty=False,
)

Expand Down
30 changes: 24 additions & 6 deletions data_request/tests/test_api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,37 @@

from lab.tests import factories

BASE_BODY_DATA = {
"user_email": "dev@witold.fr",
"user_first_name": "Dev",
"user_last_name": "Witold",
"user_institution": "Witold Institute of Technology",
"description": "I need this data for my research.",
}


@pytest.mark.django_db
def test_create_view():
with mock.patch("data_request.api_views.send_data_request_created_email"):
data = {
"user_email": "dev@witold.fr",
"user_first_name": "Dev",
"user_last_name": "Witold",
"user_institution": "Witold Institute of Technology",
"description": "I need this data for my research.",
"runs": [factories.RunFactory().id],
**BASE_BODY_DATA,
"runs": [factories.NotEmbargoedRun().id],
}
client = Client()
response = client.post("/api/data-request/", data=data)

assert response.status_code == 201


@pytest.mark.django_db
def test_create_view_with_embargoed_run():
with mock.patch("data_request.api_views.send_data_request_created_email"):
data = {
**BASE_BODY_DATA,
"runs": [factories.RunFactory().id],
}
client = Client()
response = client.post("/api/data-request/", data=data)

assert response.status_code == 400
assert "runs" in response.json()
20 changes: 19 additions & 1 deletion data_request/tests/test_api_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import mock

import pytest
from rest_framework import serializers

from lab.tests import factories

Expand All @@ -17,7 +18,7 @@ def test_data_request_create_api_view_send_mail():
"user_last_name": "euphrosyne",
"user_institution": "euphrosyne Institute of Technology",
"description": "I need this data for my research.",
"runs": [factories.RunFactory().id],
"runs": [factories.NotEmbargoedRun().id],
},
)
data.is_valid(raise_exception=True)
Expand All @@ -27,3 +28,20 @@ def test_data_request_create_api_view_send_mail():
) as send_mail_mock:
DataRequestCreateAPIView().perform_create(serializer=data)
send_mail_mock.assert_called_with("dev@euphrosyne.fr")


@pytest.mark.django_db
def test_data_request_create_api_view_when_embargoed_run():
data = DataRequestSerializer(
None,
{
"user_email": "dev@euphrosyne.fr",
"user_first_name": "Dev",
"user_last_name": "euphrosyne",
"user_institution": "euphrosyne Institute of Technology",
"description": "I need this data for my research.",
"runs": [factories.RunFactory().id],
},
)
with pytest.raises(serializers.ValidationError):
data.is_valid(raise_exception=True)
3 changes: 3 additions & 0 deletions lab/runs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class RunManager(models.Manager):
def only_finished(self):
return super().get_queryset().filter(end_date__lt=timezone.now())

def only_not_embargoed(self):
return super().get_queryset().filter(embargo_date__lte=timezone.now())


class Run(TimestampedModel, MethodModel):
class Meta:
Expand Down
11 changes: 11 additions & 0 deletions lab/runs/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from ...models import Run
from ...tests import factories


def test_next_action():
Expand All @@ -10,3 +11,13 @@ def test_next_action():
assert Run(status=Run.Status.ONGOING).next_status() == Run.Status.FINISHED
with pytest.raises(AttributeError):
Run(status=Run.Status.FINISHED).next_status()


@pytest.mark.django_db
def test_not_embargoed_qs():
factories.RunFactory() # create embargoed run
not_embargoed_run = factories.NotEmbargoedRun()

qs = Run.objects.only_not_embargoed()
assert qs.count() == 1
assert qs.first() == not_embargoed_run
4 changes: 4 additions & 0 deletions lab/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def _adjust_kwargs(cls, **kwargs):
}


class NotEmbargoedRun(RunFactory):
embargo_date = NOW.date() - timedelta(days=1)


class RunReadyToAskExecFactory(RunFactory):
# pylint: disable=no-member
status = Run.Status.CREATED.value
Expand Down

0 comments on commit 70b34e5

Please sign in to comment.