Skip to content

Commit

Permalink
backoffice & ui: standardize author routes
Browse files Browse the repository at this point in the history
  • Loading branch information
DonHaul committed Oct 28, 2024
1 parent 7e3a8e4 commit 3c9d5d3
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 83 deletions.
13 changes: 13 additions & 0 deletions backoffice/backoffice/authors/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ def get_queryset(self):
return self.queryset.filter(status__status=status)
return self.queryset

def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
validation_errors = list(get_validation_errors(instance.data))
validation_errors_msg = utils.render_validation_error_response(
validation_errors
)
response_data = {
"data": serializer.data,
"validation_errors": validation_errors_msg,
}
return Response(response_data)

def perform_destroy(self, instance):
airflow_utils.delete_workflow_dag_runs(instance.id, instance.workflow_type)
super().perform_destroy(instance)
Expand Down
77 changes: 34 additions & 43 deletions backoffice/backoffice/authors/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def setUp(self):


class TestWorkflowViewSet(BaseTransactionTestCase):
endpoint = "/api/workflows/"
endpoint = reverse("api:authors-list")
reset_sequences = True
fixtures = ["backoffice/fixtures/groups.json"]

Expand Down Expand Up @@ -118,7 +118,7 @@ def test_delete(self):
self.workflow.id, self.workflow.workflow_type
)

url = reverse("api:workflows-detail", kwargs={"pk": self.workflow.id})
url = reverse("api:authors-detail", kwargs={"pk": self.workflow.id})
response = self.api_client.delete(url)

self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
Expand All @@ -130,8 +130,8 @@ def test_delete(self):
)


class TestWorkflowSearchViewSet(BaseTransactionTestCase):
endpoint = reverse("search:workflow-list")
class TestAuthorWorkflowSearchViewSet(BaseTransactionTestCase):
endpoint = reverse("search:authors-list")
reset_sequences = True
fixtures = ["backoffice/fixtures/groups.json"]

Expand Down Expand Up @@ -175,7 +175,6 @@ def test_contains_decisions(self):


class TestAuthorWorkflowPartialUpdateViewSet(BaseTransactionTestCase):
endpoint_base_url = "/api/workflow-update"
reset_sequences = True
fixtures = ["backoffice/fixtures/groups.json"]

Expand All @@ -188,7 +187,7 @@ def setUp(self):
@property
def endpoint(self):
return reverse(
"api:workflows-authors-detail",
"api:authors-detail",
kwargs={"pk": self.workflow.id},
)

Expand Down Expand Up @@ -236,7 +235,7 @@ def test_patch_anonymous(self):


class TestAuthorWorkflowTicketViewSet(BaseTransactionTestCase):
endpoint = "/api/workflow-ticket"
endpoint = reverse("api:authors-tickets-list")
reset_sequences = True
fixtures = ["backoffice/fixtures/groups.json"]

Expand All @@ -250,7 +249,7 @@ def setUp(self):
def test_get_missing_params(self):
self.api_client.force_authenticate(user=self.curator)
response = self.api_client.get(
f"{TestAuthorWorkflowTicketViewSet.endpoint}/{self.workflow.id}/",
f"{self.endpoint}{self.workflow.id}/",
format="json",
data={},
)
Expand All @@ -262,7 +261,7 @@ def test_get_ticket_not_found(self):
query_params = {"ticket_type": "test"}
self.api_client.force_authenticate(user=self.curator)
response = self.api_client.get(
f"{TestAuthorWorkflowTicketViewSet.endpoint}/{self.workflow.id}/",
f"{self.endpoint}{self.workflow.id}/",
format="json",
data=query_params,
)
Expand All @@ -275,7 +274,7 @@ def test_get_ticket_happy_flow(self):

query_params = {"ticket_type": self.workflow_ticket.ticket_type}
response = self.api_client.get(
f"{TestAuthorWorkflowTicketViewSet.endpoint}/{self.workflow.id}/",
f"{self.endpoint}{self.workflow.id}/",
format="json",
data=query_params,
)
Expand All @@ -290,9 +289,7 @@ def test_ticket_url(self):

def test_create_missing_params(self):
self.api_client.force_authenticate(user=self.curator)
response = self.api_client.post(
f"{TestAuthorWorkflowTicketViewSet.endpoint}/", format="json", data={}
)
response = self.api_client.post(self.endpoint, format="json", data={})

assert response.status_code == 400
assert response.json() == {
Expand All @@ -308,9 +305,8 @@ def test_create_happy_flow(self):
"ticket_id": "dc94caad1b4f71502d06117a3b4bcb25",
"ticket_type": "author_create_user",
}
response = self.api_client.post(
f"{TestAuthorWorkflowTicketViewSet.endpoint}/", format="json", data=data
)
# import ipdb; ipdb.set_trace()
response = self.api_client.post(self.endpoint, format="json", data=data)

assert response.status_code == 201

Expand All @@ -325,7 +321,7 @@ def test_create_happy_flow(self):


class TestAuthorWorkflowViewSet(BaseTransactionTestCase):
endpoint = "/api/authors/"
endpoint = reverse("api:authors-list")
reset_sequences = True
fixtures = ["backoffice/fixtures/groups.json"]

Expand Down Expand Up @@ -366,7 +362,7 @@ def test_create_author(self):
},
}

url = reverse("api:workflows-authors-list")
url = reverse("api:authors-list")
response = self.api_client.post(url, format="json", data=data)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json(), data)
Expand All @@ -378,7 +374,7 @@ def test_accept_author(self):
data = {"create_ticket": True, "value": action}

response = self.api_client.post(
reverse("api:workflows-authors-resolve", kwargs={"pk": self.workflow.id}),
reverse("api:authors-resolve", kwargs={"pk": self.workflow.id}),
format="json",
data=data,
)
Expand All @@ -400,7 +396,7 @@ def test_reject_author(self):
data = {"create_ticket": True, "value": action}

response = self.api_client.post(
reverse("api:workflows-authors-resolve", kwargs={"pk": self.workflow.id}),
reverse("api:authors-resolve", kwargs={"pk": self.workflow.id}),
format="json",
data=data,
)
Expand All @@ -420,7 +416,7 @@ def test_reject_author(self):
def test_restart_full_dagrun(self):
self.api_client.force_authenticate(user=self.curator)
url = reverse(
"api:workflows-authors-restart",
"api:authors-restart",
kwargs={"pk": self.workflow.id},
)
response = self.api_client.post(url)
Expand All @@ -431,7 +427,7 @@ def test_restart_full_dagrun(self):
def test_restart_a_task(self):
self.api_client.force_authenticate(user=self.curator)
url = reverse(
"api:workflows-authors-restart",
"api:authors-restart",
kwargs={"pk": self.workflow.id},
)
response = self.api_client.post(
Expand All @@ -443,7 +439,7 @@ def test_restart_a_task(self):
def test_restart_with_params(self):
self.api_client.force_authenticate(user=self.curator)
url = reverse(
"api:workflows-authors-restart",
"api:authors-restart",
kwargs={"pk": self.workflow.id},
)
response = self.api_client.post(
Expand All @@ -462,7 +458,7 @@ def test_validate_valid_record(self):
"$schema": "https://inspirehep.net/schemas/records/authors.json",
}
url = reverse(
"api:workflows-authors-validate",
"api:authors-validate",
)
response = self.api_client.post(url, format="json", data=data)
self.assertContains(response, "Record is valid.", status_code=200)
Expand All @@ -479,7 +475,7 @@ def test_validate_not_valid_record(self):
"_collections": ["Authors"],
}
url = reverse(
"api:workflows-authors-validate",
"api:authors-validate",
)
response = self.api_client.post(url, format="json", data=data)
expected_response = {
Expand All @@ -500,7 +496,7 @@ def test_validate_not_valid_record(self):
def test_validate_no_schema_record(self):
self.api_client.force_authenticate(user=self.curator)
url = reverse(
"api:workflows-authors-validate",
"api:authors-validate",
)
response = self.api_client.post(url, format="json", data={})
self.assertContains(
Expand All @@ -516,7 +512,7 @@ def test_validate_invalid_schema_record(self):
"$schema": "https://inspirehep.net/schemas/records/notajsonschema.json",
}
url = reverse(
"api:workflows-authors-validate",
"api:authors-validate",
)
response = self.api_client.post(url, format="json", data=data)
self.assertContains(
Expand All @@ -526,8 +522,8 @@ def test_validate_invalid_schema_record(self):
)


class TestWorkflowSearchFilterViewSet(BaseTransactionTestCase):
endpoint = "/api/workflows/search/"
class TestAuthorWorkflowSearchFilterViewSet(BaseTransactionTestCase):
endpoint = reverse("search:authors-list")
reset_sequences = True
fixtures = ["backoffice/fixtures/groups.json"]

Expand Down Expand Up @@ -572,15 +568,15 @@ def setUpClass(cls):
def test_facets(self):
self.api_client.force_authenticate(user=self.admin)

response = self.api_client.get(reverse("search:workflow-list"))
response = self.api_client.get(self.endpoint)

assert "_filter_status" in response.json()["facets"]
assert "_filter_workflow_type" in response.json()["facets"]

def test_search_data_name(self):
self.api_client.force_authenticate(user=self.admin)

url = reverse("search:workflow-list") + "?search=John"
url = self.endpoint + "?search=John"

response = self.api_client.get(url)
results = response.json()["results"]
Expand All @@ -593,7 +589,7 @@ def test_search_data_email(self, query_params):

email = "[email protected]"

url = reverse("search:workflow-list") + f"{query_params}{email}"
url = self.endpoint + f"{query_params}{email}"

response = self.api_client.get(url)
results = response.json()["results"]
Expand All @@ -603,7 +599,7 @@ def test_search_data_email(self, query_params):
def test_filter_status(self):
self.api_client.force_authenticate(user=self.admin)

url = reverse("search:workflow-list") + f"?status={StatusChoices.RUNNING}"
url = self.endpoint + f"?status={StatusChoices.RUNNING}"

response = self.api_client.get(url)

Expand All @@ -613,10 +609,7 @@ def test_filter_status(self):
def test_filter_workflow_type(self):
self.api_client.force_authenticate(user=self.admin)

url = (
reverse("search:workflow-list")
+ f'?workflow_type="={WorkflowType.AUTHOR_CREATE}'
)
url = self.endpoint + f'?workflow_type="={WorkflowType.AUTHOR_CREATE}'

response = self.api_client.get(url)

Expand All @@ -626,9 +619,7 @@ def test_filter_workflow_type(self):
def test_ordering_updated_at(self):
self.api_client.force_authenticate(user=self.admin)

base_url = reverse("search:workflow-list")

urls = [base_url, base_url + "?ordering=-_updated_at"]
urls = [self.endpoint, self.endpoint + "?ordering=-_updated_at"]

for url in urls:
response = self.api_client.get(url)
Expand All @@ -645,14 +636,14 @@ def test_ordering_score(self):

search_str = "search=Frank Castle^10 OR John^6"

url = reverse("search:workflow-list") + f"?ordering=_score&{search_str}"
url = self.endpoint + f"?ordering=_score&{search_str}"
response = self.api_client.get(url)
self.assertEqual(
response.json()["results"][0]["data"]["name"]["preferred_name"],
"John Smith",
)

url = reverse("search:workflow-list") + f"?ordering=-_score&{search_str}"
url = self.endpoint + f"?ordering=-_score&{search_str}"
response = self.api_client.get(url)
self.assertEqual(
response.json()["results"][0]["data"]["name"]["preferred_name"],
Expand All @@ -676,6 +667,6 @@ def test_create_decision(self):
"action": "accept",
}

url = reverse("api:decisions-list")
url = reverse("api:authors-decisions-list")
response = self.api_client.post(url, format="json", data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
18 changes: 0 additions & 18 deletions backoffice/backoffice/authors/urls.py

This file was deleted.

16 changes: 6 additions & 10 deletions backoffice/config/api_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
router.register("users", UserViewSet)

# Workflows
(
router.register(
"workflows/authors", AuthorWorkflowViewSet, basename="workflows-authors"
),
router.register(
"workflows/authors/tickets", AuthorWorkflowTicketViewSet, basename="authors-tickets"
)
router.register("workflows", AuthorWorkflowViewSet, basename="workflows")
(
router.register(
"workflow-ticket", AuthorWorkflowTicketViewSet, basename="workflow-ticket"
),
router.register(
"workflows/authors/decisions", AuthorDecisionViewSet, basename="authors-decisions"
)
router.register("decisions", AuthorDecisionViewSet, basename="decisions")
router.register("workflows/authors", AuthorWorkflowViewSet, basename="authors")

app_name = "api"
urlpatterns = router.urls
4 changes: 3 additions & 1 deletion backoffice/config/search_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


# Workflow
router.register("workflows/search", AuthorWorkflowDocumentView, basename="workflow")
router.register(
"workflows/authors/search", AuthorWorkflowDocumentView, basename="authors"
)

app_name = "search"
urlpatterns = router.urls
2 changes: 2 additions & 0 deletions backoffice/config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@
"BASE_DOMAIN": "sandbox.orcid.org",
}
}

CORS_ALLOW_ALL_ORIGINS = True
5 changes: 3 additions & 2 deletions docker-compose.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ services:
- cluster.name=opensearch-cluster
- node.name=opensearch-node1
- discovery.seed_hosts=opensearch-node1
- bootstrap.memory_lock=true
- bootstrap.memory_lock=false
- cluster.routing.allocation.disk.threshold_enabled=false
- discovery.type=single-node
- 'ES_JAVA_OPTS=-Xms1024m -Xms1024m'
- 'OPENSEARCH_JAVA_OPTS=-Xms1g -Xmx1g'
ulimits:
memlock:
soft: -1
Expand Down
Loading

0 comments on commit 3c9d5d3

Please sign in to comment.