Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(accessLogsExport): create new endpoints TASK-1147  #5304

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

RuthShryock
Copy link
Member

@RuthShryock RuthShryock commented Nov 26, 2024

🗒️ Checklist

  • run linter locally
  • update all related docs (API, README, inline, etc.), if any
  • draft PR with a title <type>(<scope>)<!>: <title> TASK-1234
  • tag PR: at least frontend or backend unless it's global
  • fill in the template below and delete template comments
  • review thyself: read the diff and repro the preview as written
  • open PR & confirm that CI passes
  • request reviewers, if needed
  • delete this section before merging

👷 Description for instance maintainers

Create two new endpoints for access log exports which will return the status of the export job. One endpoint is restricted for superusers and allows them to access all logs while the other endpoint is for regular users to access their own logs.

👀 Preview steps

Test /api/v2/access-logs/export

  1. Login to kpi as a super user.
  2. Make a GET request to /api/v2/access-logs/export
  3. If this is your first time accessing the endpoint, then verify you receive a 404 Not Found with this response:
{
    "error": "No export task found for this user."
}
  1. If not, verify you receive a 200 Ok with the most status of the most recent created task:
{
    "uid": "uid string",
    "status": "complete"
}
  1. Make a POST request to /api/v2/access-logs/export and verify you receive a 202 Accepted with this response:
[
    "status: created"
]
  1. Logout and try to access the endpoint. Verify you get receive a 401 Unauthorized
    with this response:
{
    "detail": "Authentication credentials were not provided."
}
  1. Login as a regular user and access the endpoint. Verify you receive a 403 Forbidden with this response:
{
    "detail": "Only superusers can export all access logs."
}

Test /api/v2/access-logs/me/export

Follow the same testing instructions above but skip number 7 as both superusers and regular users have the same access to the endpoint.

🟢 Also verify that making a POST request on both endpoints should now generate an email with the follow format:

kpi_worker-1      | MIME-Version: 1.0
kpi_worker-1      | Content-Transfer-Encoding: 7bit
kpi_worker-1      | Subject: Access Log Report Complete
kpi_worker-1      | From: webmaster@localhost
kpi_worker-1      | To: [email protected]
kpi_worker-1      | Date: Wed, 27 Nov 2024 19:38:29 -0000
kpi_worker-1      | Message-ID: <173273630990.23.10232391233968451994@948bfe8772ce>
kpi_worker-1      | 
kpi_worker-1      | Hello test,
kpi_worker-1      | 
kpi_worker-1      | Your report is complete: http://kf.kobo.local:8080/private-media/test/exports/access_logs_export-test-2024-11-27T193829Z.csv
kpi_worker-1      | 
kpi_worker-1      | Regards,
kpi_worker-1      | KoboToolbox

@RuthShryock RuthShryock self-assigned this Nov 26, 2024
@RuthShryock RuthShryock changed the title feat(accessLogsExport): create new endpoints for access log exports TASK-1147  feat(accessLogsExport): create new endpoints TASK-1147  Nov 26, 2024
Base automatically changed from access-log-export-task-class to main December 2, 2024 19:06
Copy link
Contributor

@rgraber rgraber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make this more declarative/django-y if we use 2 different endpoints (like we do for viewing access logs).
We can use filter_backends and permission_classes like we do in the AllAccessLogViewSet and AccessLogViewSet to handle filtering down to the correct user and only allowing super users. That should allow us to get rid of a lot of the if-elses and having to check the request path, which is a little brittle (sometimes we might want to change the url of something without changing its functionality).

Also the endpoints need documentation :)

return Response(
{'uid': task.uid, 'status': task.status}, status=status.HTTP_200_OK
)
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No 404 here. An empty list is not an error response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants