Skip to content

Commit

Permalink
fix(test): Fix & add tests after phasing out first_round methods & se…
Browse files Browse the repository at this point in the history
…ssion creation in get_block
  • Loading branch information
drikusroor committed Jul 19, 2024
1 parent 9c23696 commit a24e859
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
10 changes: 10 additions & 0 deletions backend/experiment/rules/tests/test_beat_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.test import TestCase
from experiment.models import Block
from result.models import Result
from participant.models import Participant
from participant.utils import PARTICIPANT_KEY
from section.models import Playlist
from session.models import Session
import json
Expand Down Expand Up @@ -43,7 +45,15 @@ def load_json(self, response):
return json.loads(response.content)

def test_block(self):
participant = Participant.objects.create()
participant.save()

session = self.client.session
session.update({PARTICIPANT_KEY: participant.id})
session.save()

response = self.client.get('/experiment/block/ba/')

response_json = self.load_json(response)
self.assertTrue({'id', 'slug', 'name', 'class_name', 'rounds',
'playlists', 'next_round', 'loading_text'} <= response_json.keys())
Expand Down
9 changes: 9 additions & 0 deletions backend/experiment/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from experiment.rules.hooked import Hooked
from participant.models import Participant
from participant.utils import PARTICIPANT_KEY
from session.models import Session
from theme.models import ThemeConfig, FooterConfig, HeaderConfig

Expand Down Expand Up @@ -268,6 +269,14 @@ def test_get_block(self):
bonus_points=42,
)
participant = Participant.objects.create()
participant.save()

# Request session (not to be confused with experiment block session)
request_session = self.client.session
request_session.update({PARTICIPANT_KEY: participant.id})
request_session.save()

# Experiment block session
Session.objects.bulk_create([
Session(block=block, participant=participant, finished_at=timezone.now()) for index in range(3)
])
Expand Down
2 changes: 1 addition & 1 deletion backend/experiment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
logger = logging.getLogger(__name__)


def get_block(request, slug):
def get_block(request: HttpRequest, slug: str) -> JsonResponse:
"""Get block data from active block with given :slug
DO NOT modify session data here, it will break participant_id system
(/participant and /block/<slug> are called at the same time by the frontend)"""
Expand Down
2 changes: 1 addition & 1 deletion backend/image/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.test import TestCase, RequestFactory
from django.test import TestCase
from django.contrib.admin.sites import AdminSite
from django.utils.safestring import mark_safe

Expand Down
39 changes: 38 additions & 1 deletion backend/participant/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json

from django.test import Client, TestCase

from .models import Participant
from .utils import get_participant, PARTICIPANT_KEY
from experiment.models import Block
from session.models import Session
from result.models import Result
Expand Down Expand Up @@ -38,6 +38,43 @@ def setUp(self):
self.session['country_code'] = 'BLA'
self.session.save()

def test_get_participant(self):
participant = Participant.objects.create()
participant.save()

session = self.client.session
session.update({PARTICIPANT_KEY: participant.id})
session.save()

request = self.client.request().wsgi_request
request.session = session

self.assertEqual(get_participant(request), participant)

def test_get_participant_no_participant_in_session(self):
request = self.client.request().wsgi_request
request.session = self.client.session

with self.assertRaisesMessage(
Participant.DoesNotExist,
'No participant in session'
):
get_participant(request)

def test_get_participant_no_participant(self):
session = self.client.session
session.update({PARTICIPANT_KEY: 1234567890})
session.save()

request = self.client.request().wsgi_request
request.session = session

with self.assertRaisesMessage(
Participant.DoesNotExist,
'Participant matching query does not exist.'
):
get_participant(request)

def set_participant(self):
self.session['participant_id'] = self.participant.id
self.session.save()
Expand Down
8 changes: 7 additions & 1 deletion backend/participant/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import urllib
import urllib.request
from django.conf import settings
from django.http import HttpRequest


from .models import Participant
PARTICIPANT_KEY = 'participant_id'
Expand Down Expand Up @@ -71,9 +73,13 @@ def visitor_ip_address(request):
return request.META.get('REMOTE_ADDR')


def get_participant(request) -> Participant:
def get_participant(request: HttpRequest) -> Participant:
# get participant from session
participant_id = request.session.get(PARTICIPANT_KEY, -1)

if not participant_id or participant_id == -1:
raise Participant.DoesNotExist("No participant in session")

try:
return Participant.objects.get(
pk=int(participant_id))
Expand Down

0 comments on commit a24e859

Please sign in to comment.