This repository has been archived by the owner on May 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from open-craft/paulo/purge-notifications
Add API to purge notifications for specific users
- Loading branch information
Showing
8 changed files
with
137 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
All in-proc API endpoints for acting as a Notifications Admin | ||
""" | ||
from edx_notifications.stores.store import notification_store | ||
|
||
|
||
def purge_user_data(user_ids): | ||
""" | ||
This will purge the notifications and preferences for the given user IDs | ||
:param user_ids: and iterable of user IDs | ||
""" | ||
store = notification_store() | ||
store.purge_notifications_for_users(user_ids) | ||
|
||
|
||
def purge_notifications_with_payload(payload): | ||
""" | ||
This will purge the notifications and containing the given payload | ||
:payload: string content contained in notifications payload | ||
""" | ||
store = notification_store() | ||
store.purge_notifications_containing(payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
""" | ||
Administration endpoints | ||
""" | ||
from django.http import HttpResponseBadRequest, HttpResponse, HttpResponseForbidden | ||
|
||
from edx_notifications.lib.admin import purge_user_data | ||
from edx_notifications.server.api.api_utils import AuthenticatedAPIView | ||
|
||
|
||
class DeleteUsersData(AuthenticatedAPIView): | ||
""" | ||
POST removes all data for given user IDs | ||
""" | ||
|
||
def post(self, request): | ||
""" | ||
HTTP POST Handler | ||
""" | ||
if 'user_ids' not in request.data: | ||
return HttpResponseBadRequest('Missing user ids') | ||
if not request.user.is_staff: | ||
return HttpResponseForbidden() | ||
user_ids = request.data.get('user_ids') | ||
purge_user_data(user_ids) | ||
return HttpResponse(status=204) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
Tests for the administration endpoints | ||
""" | ||
from django.contrib.auth.models import User | ||
from django.core.urlresolvers import reverse | ||
from django.test import TestCase | ||
|
||
from edx_notifications.server.api.tests.utils import TestClient | ||
|
||
|
||
class AdminAPITests(TestCase): | ||
""" | ||
Tests for the admin.py | ||
""" | ||
|
||
def setUp(self): | ||
""" | ||
Create clients | ||
""" | ||
|
||
self.admin_client = TestClient() | ||
self.admin_user = User(username='admin', is_staff=True) | ||
self.admin_user.save() | ||
self.admin_client.login_user(self.admin_user) | ||
|
||
self.client = TestClient() | ||
self.user = User(username='user', is_staff=False) | ||
self.user.save() | ||
self.client.login_user(self.user) | ||
|
||
def test_purge_user_data(self): | ||
""" | ||
Make sure purging user data works | ||
""" | ||
|
||
response = self.admin_client.post( | ||
reverse('edx_notifications.admin.delete_users_data'), | ||
{'user_ids': [self.admin_user.id]} | ||
) | ||
self.assertEqual(response.status_code, 204) | ||
|
||
def test_admin_required_to_purge(self): | ||
""" | ||
Make sure purging is only available to staff users | ||
""" | ||
|
||
response = self.client.post( | ||
reverse('edx_notifications.admin.delete_users_data'), | ||
{'user_ids': [self.admin_user.id]} | ||
) | ||
self.assertEqual(response.status_code, 403) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters