Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Add days_left field (#1281)
Browse files Browse the repository at this point in the history
* Add `days_left` field

* Fix lints and update tests

* Move property to model

* Run black

* Update CHANGELOG.md

* Add new test and fix issue

* Fix pylint issue

* Fix brittle tests issue

* Address PR feedback
  • Loading branch information
TheAndrewJackson authored Sep 9, 2022
1 parent 11d3b5e commit 33552ef
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The types of changes are:
* Added `execution_timeframe` to Policy model and schema [#1244](https://github.com/ethyca/fidesops/pull/1244)
* Added `due_date` to Privacy request model [#1259](https://github.com/ethyca/fidesops/pull/1259)
* Wrap up the email connector - it sends an email with erasure instructions as part of request execution [#1246](https://github.com/ethyca/fidesops/pull/1246)
* Added `days_left` field to Privacy request response [#1281](https://github.com/ethyca/fidesops/pull/1281)
* Mapping Vault environment variables in docker-compose.yml [#1275](https://github.com/ethyca/fidesops/pull/1275)
* Foundations for a new "manual_webhook" connector type [#1267](https://github.com/ethyca/fidesops/pull/1267)
* Data seeding for Datadog access tests [#1269](https://github.com/ethyca/fidesops/pull/1269)
Expand Down
10 changes: 9 additions & 1 deletion src/fidesops/ops/models/privacy_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
from datetime import datetime, timedelta
from enum import Enum as EnumType
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Union

from celery.result import AsyncResult
from fideslib.cryptography.cryptographic_util import hash_with_salt
Expand Down Expand Up @@ -213,6 +213,14 @@ class PrivacyRequest(Base): # pylint: disable=R0904
identity_verified_at = Column(DateTime(timezone=True), nullable=True)
due_date = Column(DateTime(timezone=True), nullable=True)

@property
def days_left(self: PrivacyRequest) -> Union[int, None]:
if self.due_date is None:
return None

delta = self.due_date.date() - datetime.utcnow().date()
return delta.days

@classmethod
def create(cls, db: Session, *, data: Dict[str, Any]) -> FidesBase:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/fidesops/ops/schemas/privacy_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ class RowCountRequest(BaseSchema):


class CheckpointActionRequiredDetails(CheckpointActionRequired):

collection: Optional[str] = None # type: ignore


Expand Down Expand Up @@ -166,6 +165,7 @@ class PrivacyRequestResponse(BaseSchema):
policy: PolicySchema
action_required_details: Optional[CheckpointActionRequiredDetails] = None
resume_endpoint: Optional[str]
days_left: Optional[int]

class Config:
"""Set orm_mode and use_enum_values"""
Expand Down
51 changes: 50 additions & 1 deletion tests/ops/api/v1/endpoints/test_privacy_request_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import csv
import io
import json
from datetime import datetime
from datetime import datetime, timedelta
from typing import List
from unittest import mock

Expand Down Expand Up @@ -559,7 +559,10 @@ def test_get_privacy_requests_by_id(
privacy_request,
postgres_execution_log,
mongo_execution_log,
db,
):
privacy_request.due_date = None
privacy_request.save(db=db)
auth_header = generate_auth_header(scopes=[PRIVACY_REQUEST_READ])
response = api_client.get(
url + f"?request_id={privacy_request.id}", headers=auth_header
Expand All @@ -571,6 +574,7 @@ def test_get_privacy_requests_by_id(
{
"id": privacy_request.id,
"created_at": stringify_date(privacy_request.created_at),
"days_left": None,
"started_processing_at": stringify_date(
privacy_request.started_processing_at
),
Expand Down Expand Up @@ -615,7 +619,10 @@ def test_get_privacy_requests_by_partial_id(
privacy_request,
postgres_execution_log,
mongo_execution_log,
db,
):
privacy_request.due_date = None
privacy_request.save(db=db)
auth_header = generate_auth_header(scopes=[PRIVACY_REQUEST_READ])
response = api_client.get(
url + f"?request_id={privacy_request.id[:5]}", headers=auth_header
Expand All @@ -627,6 +634,7 @@ def test_get_privacy_requests_by_partial_id(
{
"id": privacy_request.id,
"created_at": stringify_date(privacy_request.created_at),
"days_left": None,
"started_processing_at": stringify_date(
privacy_request.started_processing_at
),
Expand Down Expand Up @@ -970,6 +978,9 @@ def test_verbose_privacy_requests(
db,
):
"""Test privacy requests endpoint with verbose query param to show execution logs"""
privacy_request.due_date = None
privacy_request.save(db)

auth_header = generate_auth_header(scopes=[PRIVACY_REQUEST_READ])
response = api_client.get(url + f"?verbose=True", headers=auth_header)
assert 200 == response.status_code
Expand All @@ -983,6 +994,7 @@ def test_verbose_privacy_requests(
{
"id": privacy_request.id,
"created_at": stringify_date(privacy_request.created_at),
"days_left": None,
"started_processing_at": stringify_date(
privacy_request.started_processing_at
),
Expand Down Expand Up @@ -1322,6 +1334,41 @@ def test_get_failed_request_resume_info_from_email_send(
}
assert data["resume_endpoint"] == f"/privacy-request/{privacy_request.id}/retry"

@pytest.mark.parametrize(
"due_date, days_left",
[
(
datetime.utcnow() + timedelta(days=7),
7,
),
(
datetime.utcnow(),
0,
),
(
datetime.utcnow() + timedelta(days=-7),
-7,
),
],
)
def test_get_privacy_requests_sets_days_left(
self,
api_client: TestClient,
db,
url,
generate_auth_header,
privacy_request,
due_date,
days_left,
):
privacy_request.due_date = due_date
privacy_request.save(db)

auth_header = generate_auth_header(scopes=[PRIVACY_REQUEST_READ])
response = api_client.get(url, headers=auth_header)
data = response.json()["items"][0]
assert data["days_left"] == days_left


class TestGetExecutionLogs:
@pytest.fixture(scope="function")
Expand Down Expand Up @@ -2018,6 +2065,7 @@ def test_resume_privacy_request(
db,
):
privacy_request.status = PrivacyRequestStatus.paused
privacy_request.due_date = None
privacy_request.save(db=db)
auth_header = generate_webhook_auth_header(
webhook=policy_pre_execution_webhooks[0]
Expand All @@ -2031,6 +2079,7 @@ def test_resume_privacy_request(
assert response_body == {
"id": privacy_request.id,
"created_at": stringify_date(privacy_request.created_at),
"days_left": None,
"started_processing_at": stringify_date(
privacy_request.started_processing_at
),
Expand Down

0 comments on commit 33552ef

Please sign in to comment.