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

Disable authentication/permission classes on AB testing API views #35

Merged
merged 1 commit into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion wagtail_ab_testing/test/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import datetime

from django.contrib.auth import get_user_model
from django.urls import reverse
from freezegun import freeze_time
from rest_framework.test import APITestCase
from rest_framework.test import APIClient, APITestCase
from wagtail.core.models import Page

from wagtail_ab_testing.models import AbTest
Expand Down Expand Up @@ -73,6 +74,26 @@ def test_register_participant_finish(self):
self.ab_test.refresh_from_db()
self.assertEqual(self.ab_test.status, AbTest.STATUS_FINISHED)

def test_register_participant_authenticated_user(self):
# By default, Django REST framework will enforce CSRF checks on authenticated users
# We disable these by removing all authentication/permission classes from the view
client = APIClient(enforce_csrf_checks=True)

User = get_user_model()
User.objects.create_user('foo', '[email protected]', 'bar')
client.login(username='foo', password='bar')

response = client.post(
reverse('wagtail_ab_testing:register_participant'),
{
'test_id': self.ab_test.id,
'version': 'control',
}
)

# Shouldn't give 403 error
self.assertEqual(response.status_code, 200)


@freeze_time('2020-11-04T22:37:00Z')
class TestGoalReached(APITestCase):
Expand Down Expand Up @@ -146,3 +167,23 @@ def test_log_conversion_for_something_else(self):

# This shouldn't create a history log
self.assertFalse(self.ab_test.hourly_logs.exists())

def test_log_conversion_authenticated_user(self):
# By default, Django REST framework will enforce CSRF checks on authenticated users
# We disable these by removing all authentication/permission classes from the view
client = APIClient(enforce_csrf_checks=True)

User = get_user_model()
User.objects.create_user('foo', '[email protected]', 'bar')
client.login(username='foo', password='bar')

response = client.post(
reverse('wagtail_ab_testing:goal_reached', args=[]),
{
'test_id': self.ab_test.id,
'version': 'control'
}
)

# Shouldn't give 403 error
self.assertEqual(response.status_code, 200)
6 changes: 5 additions & 1 deletion wagtail_ab_testing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import django_filters
from django_filters.constants import EMPTY_VALUES
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.response import Response
from wagtail.admin import messages
from wagtail.admin.action_menu import ActionMenuItem
Expand Down Expand Up @@ -435,6 +435,8 @@ def get_queryset(self):

@csrf_exempt
@api_view(['POST'])
@authentication_classes([])
@permission_classes([])
def register_participant(request):
test_id = request.data.get('test_id', None)
if test_id is None:
Expand Down Expand Up @@ -467,6 +469,8 @@ def register_participant(request):

@csrf_exempt
@api_view(['POST'])
@authentication_classes([])
@permission_classes([])
def goal_reached(request):
test_id = request.data.get('test_id', None)
if test_id is None:
Expand Down