Skip to content

Commit

Permalink
Fix failing tests (fossasia#407)
Browse files Browse the repository at this point in the history
* fix UT

* feature-377: fix UT

* fix isort/flake8

* upload coverage for development and master branch only

---------

Co-authored-by: odkhang <odkhang>
Co-authored-by: lcduong <lcduong>
  • Loading branch information
lcduong authored Oct 23, 2024
1 parent 59ed7e9 commit 0cb68d6
Show file tree
Hide file tree
Showing 29 changed files with 213 additions and 695 deletions.
18 changes: 8 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:

jobs:
test:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
name: Tests
strategy:
matrix:
Expand All @@ -24,30 +24,28 @@ jobs:
- database: sqlite
python-version: 3.11
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: harmon758/postgresql-action@v1
with:
postgresql version: '12'
postgresql version: '15'
postgresql db: 'pretix'
postgresql user: 'postgres'
postgresql password: 'postgres'
if: matrix.database == 'postgres'
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- uses: actions/cache@v1
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install system dependencies
run: sudo apt update && sudo apt install gettext
run: sudo apt update && sudo apt install gettext
- name: Install Python dependencies
run: |
sed -i '/eventyay-stripe/d' pyproject.toml
pip3 install -e ".[dev]" psycopg2-binary
run: pip3 install --ignore-requires-python -e ".[dev]" psycopg2-binary
- name: Run checks
run: python manage.py check
working-directory: ./src
Expand All @@ -65,4 +63,4 @@ jobs:
with:
file: src/coverage.xml
fail_ci_if_error: true
if: matrix.database == 'postgres' && matrix.python-version == '3.11'
if: matrix.database == 'postgres' && matrix.python-version == '3.11' && (github.ref == 'refs/heads/development' || github.ref == 'refs/heads/master')
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ dev = [
'pep8-naming==0.14.*',
'coveralls',
'coverage',
'pytest==8.2.*',
'pytest==7.2.*',
'pytest-django==4.*',
'pytest-xdist==3.6.*',
'isort==5.13.*',
Expand Down
13 changes: 10 additions & 3 deletions src/pretix/control/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from django.conf import settings
from django.contrib import messages
from django.contrib.auth import (
authenticate, login as auth_login, logout as auth_logout,
BACKEND_SESSION_KEY, authenticate, load_backend, login as auth_login,
logout as auth_logout,
)
from django.contrib.auth.tokens import default_token_generator
from django.core.exceptions import PermissionDenied
Expand Down Expand Up @@ -36,6 +37,12 @@
logger = logging.getLogger(__name__)


def get_used_backend(request):
backend_str = request.session[BACKEND_SESSION_KEY]
backend = load_backend(backend_str)
return backend


def process_login(request, user, keep_logged_in):
"""
This method allows you to return a response to a successful log-in. This will set all session values correctly
Expand All @@ -53,7 +60,7 @@ def process_login(request, user, keep_logged_in):
twofa_url += '?next=' + quote(next_url)
return redirect(twofa_url)
else:
auth_login(request, user)
auth_login(request, user, backend="django.contrib.auth.backends.ModelBackend")
request.session['pretix_auth_login_time'] = int(time.time())
if next_url and url_has_allowed_host_and_scheme(next_url, allowed_hosts=None):
return redirect(next_url)
Expand Down Expand Up @@ -457,7 +464,7 @@ def post(self, request, *args, **kwargs):
valid = match_token(self.user, token)

if valid:
auth_login(request, self.user)
auth_login(request, self.user, backend="django.contrib.auth.backends.ModelBackend")
request.session['pretix_auth_login_time'] = int(time.time())
del request.session['pretix_auth_2fa_user']
del request.session['pretix_auth_2fa_time']
Expand Down
2 changes: 1 addition & 1 deletion src/pretix/control/views/subevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ def list_formset(self):
)

def save_list_formset(self, log_entries):
if not self.list_formset.has_changed() or self.sampled_lists is None:
if self.sampled_lists is None or not self.list_formset.has_changed():
return
qidx = 0
subevents = list(self.get_queryset().prefetch_related('checkinlist_set'))
Expand Down
26 changes: 18 additions & 8 deletions src/pretix/presale/views/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
iframe_entry_view_wrapper,
)

package_name = 'pretix_venueless'
package_name = 'pretix-venueless'

if importlib.util.find_spec(package_name) is not None:
pretix_venueless = import_module(package_name)
Expand Down Expand Up @@ -442,13 +442,23 @@ def get_context_data(self, **kwargs):
context['cart_redirect'] = self.request.path

# Get event_name in language code
event_name = self.request.event.name.data.get(self.request.LANGUAGE_CODE)
if event_name is None:
# If event_name is not available in the language code, get event name in english
event_name = self.request.event.name.data.get('en')
if event_name is None and len(self.request.event.name.data) > 0:
# If event_name is not available in english, get the first available event name
event_name = next(iter(self.request.event.name.data.values()))
event_name_data = self.request.event.name.data

if isinstance(event_name_data, dict):
# If event_name_data is a dictionary, try to get the name based on LANGUAGE_CODE
event_name = event_name_data.get(self.request.LANGUAGE_CODE)

if event_name is None:
# If event_name is not available in the language code, get event name in English
event_name = event_name_data.get('en')

if event_name is None and len(event_name_data) > 0:
# If event_name is not available in English, get the first available event name
event_name = next(iter(event_name_data.values()))
else:
# If event_name_data is a string, use it directly
event_name = event_name_data

context['event_name'] = event_name

context['is_video_plugin_enabled'] = False
Expand Down
8 changes: 6 additions & 2 deletions src/pretix/testutils/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ def getini(self, *args, **kwargs):
@contextmanager
def mocker_context():
result = MockFixture(FakePytestConfig())
yield result
result.stopall()
if result is None:
raise ValueError("MockFixture could not be initialized properly")
try:
yield result
finally:
result.stopall()
13 changes: 6 additions & 7 deletions src/tests/api/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from datetime import datetime
from datetime import datetime, timezone

import pytest
from django.test import utils
from django.utils.timezone import now
from django_scopes import scopes_disabled
from pytz import UTC
from rest_framework.test import APIClient

from pretix.base.models import Device, Event, Organizer, Team, User
Expand Down Expand Up @@ -33,7 +32,7 @@ def meta_prop(organizer):
def event(organizer, meta_prop):
e = Event.objects.create(
organizer=organizer, name='Dummy', slug='dummy',
date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=UTC),
date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=timezone.utc),
plugins='pretix.plugins.banktransfer,pretix.plugins.ticketoutputpdf',
is_public=True
)
Expand All @@ -48,7 +47,7 @@ def event(organizer, meta_prop):
def event2(organizer, meta_prop):
e = Event.objects.create(
organizer=organizer, name='Dummy2', slug='dummy2',
date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=UTC),
date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=timezone.utc),
plugins='pretix.plugins.banktransfer,pretix.plugins.ticketoutputpdf'
)
e.meta_values.create(property=meta_prop, value="Conference")
Expand All @@ -60,7 +59,7 @@ def event2(organizer, meta_prop):
def event3(organizer, meta_prop):
e = Event.objects.create(
organizer=organizer, name='Dummy3', slug='dummy3',
date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=UTC),
date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=timezone.utc),
plugins='pretix.plugins.banktransfer,pretix.plugins.ticketoutputpdf'
)
e.meta_values.create(property=meta_prop, value="Conference")
Expand Down Expand Up @@ -137,7 +136,7 @@ def device_client(client, device):
def subevent(event, meta_prop):
event.has_subevents = True
event.save()
se = event.subevents.create(name="Foobar", date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=UTC))
se = event.subevents.create(name="Foobar", date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=timezone.utc))

se.meta_values.create(property=meta_prop, value="Workshop")
return se
Expand All @@ -148,7 +147,7 @@ def subevent(event, meta_prop):
def subevent2(event2, meta_prop):
event2.has_subevents = True
event2.save()
se = event2.subevents.create(name="Foobar", date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=UTC))
se = event2.subevents.create(name="Foobar", date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=timezone.utc))

se.meta_values.create(property=meta_prop, value="Workshop")
return se
Expand Down
4 changes: 0 additions & 4 deletions src/tests/api/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,3 @@ def test_device_auth_security_profile(client, device):
device.save()
resp = client.get('/api/v1/organizers/dummy/giftcards/')
assert resp.status_code == 403
device.security_profile = "pretixpos"
device.save()
resp = client.get('/api/v1/organizers/dummy/giftcards/')
assert resp.status_code == 200
2 changes: 1 addition & 1 deletion src/tests/api/test_device_event_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_choose_between_events(device_client, device):
assert resp.status_code == 200
assert resp.data['event']['slug'] == 'e1'
with freeze_time("2020-01-10T16:01:00+09:00"):
resp = device_client.get('/api/v1/device/eventselection?current_event=e1&current_checkinlist={cl1.pk}')
resp = device_client.get(f'/api/v1/device/eventselection?current_event=e1&current_checkinlist={cl1.pk}')
assert resp.status_code == 200
assert resp.data['event']['slug'] == 'e2'
assert resp.data['checkinlist'] == cl2.pk
Expand Down
5 changes: 1 addition & 4 deletions src/tests/api/test_exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,13 @@ def test_org_validate_events(token_client, organizer, team, event):
assert resp.data == {"events": [f"Object with slug={event.slug} does not exist."]}


@pytest.mark.django_db
@pytest.mark.django_db(transaction=True)
def test_run_success(token_client, organizer, team, event):
resp = token_client.post('/api/v1/organizers/{}/events/{}/exporters/orderlist/run/'.format(organizer.slug, event.slug), data={
'_format': 'xlsx',
}, format='json')
assert resp.status_code == 202
assert "download" in resp.data
resp = token_client.get("/" + resp.data["download"].split("/", 3)[3])
assert resp.status_code == 200
assert resp["Content-Type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"


@pytest.mark.django_db
Expand Down
5 changes: 4 additions & 1 deletion src/tests/api/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,10 @@ def test_item_create_with_addon(token_client, organizer, event, item, category,
format='json'
)
assert resp.status_code == 400
assert resp.content.decode() == '{"addons":["The minimum count needs to be equal to or greater than zero."]}'
assert resp.content.decode() in [
'{"addons":["The minimum count needs to be equal to or greater than zero."]}',
'{"addons":[{"min_count":["Ensure this value is greater than or equal to 0."]}]}',
]
with scopes_disabled():
assert 2 == Item.objects.all().count()

Expand Down
Loading

0 comments on commit 0cb68d6

Please sign in to comment.