Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add tests for the powersync CRUD endpoint
Roland Geider1e6c28f72cd3
diff --git a/wger/core/tests/powersync_base_test.py b/wger/core/tests/powersync_base_test.pynew file mode 100644index 000000000..c6f613359--- /dev/null+++ b/wger/core/tests/powersync_base_test.py@@ -0,0 +1,316 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# Standard Library+import json++# Django+from django.contrib.auth.models import User++# Third Party+from rest_framework import status+from rest_framework.test import APITestCase++# wger+from wger.core.tests.base_testcase import BaseTestCase+++POWERSYNC_URL = '/api/v2/upload-powersync-data'+++class PowerSyncBaseTestCase(BaseTestCase, APITestCase):+ """+ Base test case for the PowerSync push handlers.+ """++ url = POWERSYNC_URL++ table = None+ """The PowerSync table name"""++ resource = None+ """The model under test. Used for count and ownership assertions."""++ user_access = 'test'+ """Username of the owner who is expected to succeed."""++ user_fail = 'admin'+ """A different user who must be rejected on cross-user writes."""++ pk_owned = None+ """+ Lookup id (uuid string or integer pk) of an entry owned by ``user_access``.+ Used by the update and delete mixins.+ """++ create_payload = None+ """Body for handle_create_*. Set to None to skip create tests."""++ update_payload = None+ """+ Body for handle_update_*. Must contain an ``id`` field that resolves to an+ object owned by ``user_access``. Set to None to skip update tests.+ """++ fk_ownership = ()+ """+ Iterable of (field_name, foreign_value) tuples used to verify that the+ handler rejects payloads referencing FKs owned by another user. Empty if the+ handler doesn't run a check_fk_ownership step.+ """++ def authenticate(self, username=None):+ user = User.objects.get(username=username or self.user_access)+ self.client.force_authenticate(user=user)++ def push(self, verb, payload):+ """Send a PowerSync push as the currently authenticated user."""+ body = json.dumps({'table': self.table, 'data': payload})+ return self.client.generic(verb, self.url, data=body, content_type='application/json')++ def get_owner_username(self, obj):+ return obj.get_owner_object().user.username+++class PowerSyncCreateTestCase:+ """Mixin: ownership tests for handle_create_* (HTTP PUT)."""++ def _diff_new_object(self, pks_before):+ """Return the single object whose pk wasn't in ``pks_before``."""+ pks_after = set(self.resource.objects.values_list('pk', flat=True))+ new_pks = pks_after - pks_before+ self.assertEqual(+ len(new_pks), 1, f'Exactly one new {self.resource.__name__} row expected'+ )+ return self.resource.objects.get(pk=new_pks.pop())++ def test_create_anonymous_forbidden(self):+ if self.resource is None or self.create_payload is None:+ return+ before = self.resource.objects.count()+ response = self.push('PUT', self.create_payload)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(self.resource.objects.count(), before)++ def test_create_owner_succeeds(self):+ if self.resource is None or self.create_payload is None:+ return+ self.authenticate()+ pks_before = set(self.resource.objects.values_list('pk', flat=True))+ response = self.push('PUT', self.create_payload)+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)+ self.assertEqual(response.json(), {'status': 'ok!'})++ new_obj = self._diff_new_object(pks_before)+ self.assertEqual(self.get_owner_username(new_obj), self.user_access)++ def test_create_ignores_user_in_payload(self):+ """A user/user_id smuggled in the payload must not change the owner."""+ if self.resource is None or self.create_payload is None:+ return+ other_user = User.objects.get(username=self.user_fail)+ payload = {**self.create_payload, 'user': other_user.pk, 'user_id': other_user.pk}++ self.authenticate()+ pks_before = set(self.resource.objects.values_list('pk', flat=True))+ response = self.push('PUT', payload)+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)++ new_obj = self._diff_new_object(pks_before)+ self.assertEqual(+ self.get_owner_username(new_obj),+ self.user_access,+ f'Created object should belong to {self.user_access}, not {self.user_fail}',+ )++ def test_create_fk_ownership_rejected(self):+ if self.resource is None or self.create_payload is None or not self.fk_ownership:+ return+ self.authenticate()+ for field, foreign_value in self.fk_ownership:+ payload = {**self.create_payload, field: foreign_value}+ before = self.resource.objects.count()+ response = self.push('PUT', payload)+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ body = response.json()+ self.assertEqual(+ body.get('error'),+ 'Forbidden',+ f'FK theft via {field}={foreign_value} should be rejected, got {body}',+ )+ self.assertEqual(+ self.resource.objects.count(),+ before,+ f'No row should be created when FK {field} is rejected',+ )+++class PowerSyncUpdateTestCase:+ """Mixin: ownership tests for handle_update_* (HTTP PATCH)."""++ def _get_entry_for_update(self):+ """+ Return the model instance referenced by ``update_payload['id']``. The+ default tries pk first, then uuid. Subclasses with chained lookups+ (e.g. by ``plan__user_id``) can override.+ """+ ident = self.update_payload['id']+ try:+ return self.resource.objects.get(pk=ident)+ except (self.resource.DoesNotExist, ValueError, TypeError):+ return self.resource.objects.get(uuid=ident)++ def test_update_anonymous_forbidden(self):+ if self.resource is None or self.update_payload is None:+ return+ response = self.push('PATCH', self.update_payload)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)++ def test_update_owner_succeeds(self):+ if self.resource is None or self.update_payload is None:+ return+ self.authenticate()+ response = self.push('PATCH', self.update_payload)+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)+ self.assertEqual(response.json(), {'status': 'ok!'})++ def test_update_other_user_not_found(self):+ """A different user must not be able to PATCH someone else's row."""+ if self.resource is None or self.update_payload is None:+ return+ owned = self._get_entry_for_update()+ pk = owned.pk+ owner_before = self.get_owner_username(owned)++ self.authenticate(self.user_fail)+ response = self.push('PATCH', self.update_payload)+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ body = response.json()+ self.assertEqual(+ body.get('error'),+ 'Not found',+ f'PATCH by foreign user should return Not found, got {body}',+ )++ # Re-fetch a fresh instance instead of refresh_from_db: the latter+ # forces ImageField models (gallery.Image) to recompute width/height+ # from a file that doesn't exist in the test environment.+ fresh = self.resource.objects.get(pk=pk)+ self.assertEqual(+ self.get_owner_username(fresh),+ owner_before,+ 'Owner must not change after a rejected update',+ )++ def test_update_fk_ownership_rejected(self):+ if self.resource is None or self.update_payload is None or not self.fk_ownership:+ return+ self.authenticate()+ for field, foreign_value in self.fk_ownership:+ payload = {**self.update_payload, field: foreign_value}+ response = self.push('PATCH', payload)+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ body = response.json()+ self.assertEqual(+ body.get('error'),+ 'Forbidden',+ f'FK theft via {field}={foreign_value} should be rejected, got {body}',+ )+++class PowerSyncCreateNotAllowedTestCase:+ """+ Mixin for tables where PUT (create) is explicitly rejected by the dispatcher+ because creation must go through the REST API (Routine, NutritionPlan,+ GalleryImage). PATCH and DELETE still work via PowerSync.+ """++ def test_create_method_not_allowed(self):+ if self.resource is None:+ return+ self.authenticate()+ before = self.resource.objects.count()+ response = self.push('PUT', self.create_payload or {})+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ body = response.json()+ self.assertEqual(+ body.get('error'),+ 'Method not allowed',+ f'PUT to {self.table} should be rejected by the dispatcher, got {body}',+ )+ self.assertEqual(+ self.resource.objects.count(),+ before,+ 'A rejected PUT must not create any rows',+ )+++class PowerSyncDeleteTestCase:+ """Mixin: ownership tests for handle_delete_* (HTTP DELETE)."""++ def _delete_payload(self):+ return {'id': self.pk_owned}++ def test_delete_anonymous_forbidden(self):+ if self.resource is None or self.pk_owned is None:+ return+ before = self.resource.objects.count()+ response = self.push('DELETE', self._delete_payload())+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(self.resource.objects.count(), before)++ def test_delete_other_user_not_found(self):+ if self.resource is None or self.pk_owned is None:+ return+ before = self.resource.objects.count()+ self.authenticate(self.user_fail)+ response = self.push('DELETE', self._delete_payload())+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ body = response.json()+ self.assertEqual(+ body.get('error'),+ 'Not found',+ f'DELETE by foreign user should return Not found, got {body}',+ )+ self.assertEqual(+ self.resource.objects.count(),+ before,+ 'No row should be deleted on a rejected delete',+ )++ def test_delete_owner_succeeds(self):+ if self.resource is None or self.pk_owned is None:+ return+ before = self.resource.objects.count()+ self.authenticate()+ response = self.push('DELETE', self._delete_payload())+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)+ self.assertEqual(response.json(), {'status': 'ok!'})+ self.assertEqual(self.resource.objects.count(), before - 1)+++class PowerSyncResourceTestCase(+ PowerSyncBaseTestCase,+ PowerSyncCreateTestCase,+ PowerSyncUpdateTestCase,+ PowerSyncDeleteTestCase,+):+ """+ Full CRUD bundle for tables that support PUT/PATCH/DELETE.++ Tables that don't support all verbs (Routine, NutritionPlan, Image only do+ PATCH/DELETE) should mix in only the relevant cases instead.+ """++ passdiff --git a/wger/gallery/tests/test_powersync.py b/wger/gallery/tests/test_powersync.pynew file mode 100644index 000000000..e8e9ea845--- /dev/null+++ b/wger/gallery/tests/test_powersync.py@@ -0,0 +1,46 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# wger+from wger.core.tests import powersync_base_test+from wger.gallery.models import Image+++# Pinned in test-gallery-images.json+IMAGE_OWNED = 3 # user 'test'+++class GalleryImagePowerSyncTestCase(+ powersync_base_test.PowerSyncBaseTestCase,+ powersync_base_test.PowerSyncCreateNotAllowedTestCase,+ powersync_base_test.PowerSyncUpdateTestCase,+ powersync_base_test.PowerSyncDeleteTestCase,+):+ """+ PowerSync handlers for gallery.Image. Creation (and edits that swap the+ binary file) go through REST because PowerSync can't carry the upload,+ only metadata edits and deletes reach us via PowerSync.+ """++ table = 'gallery_image'+ resource = Image++ pk_owned = IMAGE_OWNED++ update_payload = {+ 'id': pk_owned,+ 'description': 'Renamed via PowerSync',+ }++ create_payload = {'id': 9999, 'description': 'should not be created'}diff --git a/wger/manager/tests/test_powersync.py b/wger/manager/tests/test_powersync.pynew file mode 100644index 000000000..0a2098368--- /dev/null+++ b/wger/manager/tests/test_powersync.py@@ -0,0 +1,119 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# wger+from wger.core.tests import powersync_base_test+from wger.manager.models import (+ Routine,+ WorkoutLog,+ WorkoutSession,+)+++# Pinned in test-routine-data.json+ROUTINE_OWNED = 2 # user 'test'+ROUTINE_OTHER = 1 # user 'admin'++# Pinned in test-workout-session.json (owned by 'test', linked to ROUTINE_OWNED's user)+SESSION_OWNED = 'bbbbbbbb-bbbb-bbbb-bbbb-000000000005'+SESSION_OTHER = 'bbbbbbbb-bbbb-bbbb-bbbb-000000000001' # owned by 'admin'++# Pinned in test-workout-log-data.json (owned by 'test')+LOG_OWNED = 'aaaaaaaa-aaaa-aaaa-aaaa-000000000005'++EXERCISE_PUBLIC = 2 # any base exercise; 'test' is allowed to reference it+++class WorkoutLogPowerSyncTestCase(powersync_base_test.PowerSyncResourceTestCase):+ """+ PowerSync handlers for manager.WorkoutLog. The handler runs+ check_fk_ownership against (Routine, 'routine') and (WorkoutSession, 'session').+ """++ table = 'manager_workoutlog'+ resource = WorkoutLog++ pk_owned = LOG_OWNED++ create_payload = {+ 'id': 'aaaaaaaa-aaaa-aaaa-aaaa-000000000099',+ 'date': '2030-01-15T10:00:00Z',+ 'routine': ROUTINE_OWNED,+ 'session': SESSION_OWNED,+ 'exercise': EXERCISE_PUBLIC,+ 'weight': '40.0',+ 'repetitions': 10,+ }+ update_payload = {+ 'id': pk_owned,+ 'weight': '42.5',+ }++ fk_ownership = (+ ('routine', ROUTINE_OTHER),+ ('session', SESSION_OTHER),+ )+++class WorkoutSessionPowerSyncTestCase(powersync_base_test.PowerSyncResourceTestCase):+ """+ PowerSync handlers for manager.WorkoutSession. The handler runs+ check_fk_ownership against (Routine, 'routine').+ """++ table = 'manager_workoutsession'+ resource = WorkoutSession++ pk_owned = SESSION_OWNED++ create_payload = {+ 'id': 'bbbbbbbb-bbbb-bbbb-bbbb-000000000099',+ 'date': '2030-01-15',+ 'routine': ROUTINE_OWNED,+ 'impression': '2',+ 'notes': 'created via PowerSync',+ }+ update_payload = {+ 'id': pk_owned,+ 'notes': 'edited via PowerSync',+ }++ fk_ownership = (+ ('routine', ROUTINE_OTHER),+ )+++class RoutinePowerSyncTestCase(+ powersync_base_test.PowerSyncBaseTestCase,+ powersync_base_test.PowerSyncCreateNotAllowedTestCase,+ powersync_base_test.PowerSyncUpdateTestCase,+ powersync_base_test.PowerSyncDeleteTestCase,+):+ """+ PowerSync handlers for manager.Routine. Creation is explicitly rejected+ by the dispatcher (must go through REST), so only PATCH/DELETE are tested.+ """++ table = 'manager_routine'+ resource = Routine++ pk_owned = ROUTINE_OWNED++ update_payload = {+ 'id': pk_owned,+ 'name': 'Renamed via PowerSync',+ }++ # Anything sent via PUT must be rejected; the body content doesn't matter.+ create_payload = {'id': 9999, 'name': 'should not be created'}diff --git a/wger/measurements/fixtures/test-measurement-categories.json b/wger/measurements/fixtures/test-measurement-categories.jsonindex e66b2c88b..aaf5f909c 100644--- a/wger/measurements/fixtures/test-measurement-categories.json+++ b/wger/measurements/fixtures/test-measurement-categories.json@@ -25,5 +25,14 @@"name": "The way I like it","unit": "rods to the hogshead"}+ },+ {+ "pk": "cccccccc-cccc-cccc-cccc-0000000000aa",+ "model": "measurements.category",+ "fields": {+ "user": 1,+ "name": "Admin's chest",+ "unit": "cm"+ }}]diff --git a/wger/measurements/tests/test_powersync.py b/wger/measurements/tests/test_powersync.pynew file mode 100644index 000000000..c81c60ab2--- /dev/null+++ b/wger/measurements/tests/test_powersync.py@@ -0,0 +1,78 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# wger+from wger.core.tests import powersync_base_test+from wger.measurements.models import (+ Category,+ Measurement,+)+++# Pinned in test-measurement-categories.json+CATEGORY_OWNED = 'cccccccc-cccc-cccc-cccc-000000000001' # user 'test'+CATEGORY_OTHER = 'cccccccc-cccc-cccc-cccc-0000000000aa' # user 'admin'++# Pinned in test-measurements.json (owned via CATEGORY_OWNED)+MEASUREMENT_OWNED = 'dddddddd-dddd-dddd-dddd-000000000001'+++class CategoryPowerSyncTestCase(powersync_base_test.PowerSyncResourceTestCase):+ """+ PowerSync handlers for measurements.Category. No check_fk_ownership step+ in the handler because Category only has a User FK.+ """++ table = 'measurements_category'+ resource = Category++ pk_owned = CATEGORY_OWNED++ create_payload = {+ 'id': 'cccccccc-cccc-cccc-cccc-000000000099',+ 'name': 'Calf',+ 'unit': 'cm',+ }+ update_payload = {+ 'id': pk_owned,+ 'name': 'Biceps (renamed)',+ }+++class MeasurementPowerSyncTestCase(powersync_base_test.PowerSyncResourceTestCase):+ """+ PowerSync handlers for measurements.Measurement. The handler runs+ check_fk_ownership against the `category` FK so payloads pointing at+ another user's category must be rejected.+ """++ table = 'measurements_measurement'+ resource = Measurement++ pk_owned = MEASUREMENT_OWNED++ create_payload = {+ 'id': 'dddddddd-dddd-dddd-dddd-000000000099',+ 'category': CATEGORY_OWNED,+ 'date': '2030-01-15T10:00:00Z',+ 'value': 22.5,+ }+ update_payload = {+ 'id': pk_owned,+ 'value': 23.5,+ }++ fk_ownership = (+ ('category', CATEGORY_OTHER),+ )diff --git a/wger/nutrition/fixtures/test-nutrition-diary.json b/wger/nutrition/fixtures/test-nutrition-diary.jsonindex 274b01e56..d07e5fe40 100644--- a/wger/nutrition/fixtures/test-nutrition-diary.json+++ b/wger/nutrition/fixtures/test-nutrition-diary.json@@ -6,7 +6,8 @@"ingredient": 5,"comment": "","amount": "2",- "weight_unit": 1+ "weight_unit": 1,+ "uuid": "ee000000-0000-0000-0000-000000000001"},"pk": 1,"model": "nutrition.logitem"diff --git a/wger/nutrition/tests/test_powersync.py b/wger/nutrition/tests/test_powersync.pynew file mode 100644index 000000000..b57a8e438--- /dev/null+++ b/wger/nutrition/tests/test_powersync.py@@ -0,0 +1,87 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# wger+from wger.core.tests import powersync_base_test+from wger.nutrition.models import (+ LogItem,+ NutritionPlan,+)+++# Pinned in test-nutrition-data.json+PLAN_OWNED = 1 # user 'test'+PLAN_OTHER = 2 # user 'admin'++# Pinned in test-nutrition-diary.json (linked to PLAN_OWNED)+LOG_OWNED_UUID = 'ee000000-0000-0000-0000-000000000001'++INGREDIENT_PUBLIC = 1+++class NutritionPlanPowerSyncTestCase(+ powersync_base_test.PowerSyncBaseTestCase,+ powersync_base_test.PowerSyncCreateNotAllowedTestCase,+ powersync_base_test.PowerSyncUpdateTestCase,+ powersync_base_test.PowerSyncDeleteTestCase,+):+ """+ PowerSync handlers for nutrition.NutritionPlan. Creation must go through+ REST (the dispatcher rejects PUT), so only PATCH/DELETE are tested.+ """++ table = 'nutrition_nutritionplan'+ resource = NutritionPlan++ pk_owned = PLAN_OWNED++ update_payload = {+ 'id': pk_owned,+ 'description': 'Renamed via PowerSync',+ }++ create_payload = {'id': 9999, 'description': 'should not be created'}+++class LogItemPowerSyncTestCase(powersync_base_test.PowerSyncResourceTestCase):+ """+ PowerSync handlers for nutrition.LogItem. The handler runs+ check_fk_ownership against (NutritionPlan, 'plan') and (Meal, 'meal').+ Lookup uses the uuid field, not the integer pk.+ """++ table = 'nutrition_logitem'+ resource = LogItem++ pk_owned = LOG_OWNED_UUID++ create_payload = {+ 'id': 'ee000000-0000-0000-0000-000000000099',+ 'plan': PLAN_OWNED,+ 'datetime': '2030-01-15T10:00:00Z',+ 'ingredient': INGREDIENT_PUBLIC,+ 'amount': '50',+ }+ update_payload = {+ 'id': pk_owned,+ 'amount': '99',+ }++ fk_ownership = (+ ('plan', PLAN_OTHER),+ )++ def _get_entry_for_update(self):+ # LogItem's PowerSync handler looks up by uuid, not pk+ return LogItem.objects.get(uuid=self.update_payload['id'])diff --git a/wger/weight/fixtures/test-weight-data.json b/wger/weight/fixtures/test-weight-data.jsonindex 0b93f4f9b..cc0b05083 100644--- a/wger/weight/fixtures/test-weight-data.json+++ b/wger/weight/fixtures/test-weight-data.json@@ -3,6 +3,7 @@"pk": 1,"model": "weight.weightentry","fields": {+ "uuid": "11111111-1111-1111-1111-000000000001","user": 2,"weight": 77,"date": "2012-10-01T14:30:21.592Z"@@ -12,6 +13,7 @@"pk": 2,"model": "weight.weightentry","fields": {+ "uuid": "11111111-1111-1111-1111-000000000002","user": 2,"weight": 77.2,"date": "2012-10-10T14:30:21.592Z"@@ -21,6 +23,7 @@"pk": 3,"model": "weight.weightentry","fields": {+ "uuid": "11111111-1111-1111-1111-000000000003","user": 2,"weight": 80.6,"date": "2012-11-01T14:30:21.592Z"@@ -30,6 +33,7 @@"pk": 4,"model": "weight.weightentry","fields": {+ "uuid": "11111111-1111-1111-1111-000000000004","user": 2,"weight": 80,"date": "2013-01-01T14:30:21.592Z"@@ -39,6 +43,7 @@"pk": 5,"model": "weight.weightentry","fields": {+ "uuid": "11111111-1111-1111-1111-000000000005","user": 2,"weight": 81,"date": "2013-01-10T14:30:21.592Z"@@ -48,6 +53,7 @@"pk": 6,"model": "weight.weightentry","fields": {+ "uuid": "11111111-1111-1111-1111-000000000006","user": 2,"weight": 82,"date": "2013-01-20T14:30:21.592Z"@@ -57,6 +63,7 @@"pk": 7,"model": "weight.weightentry","fields": {+ "uuid": "11111111-1111-1111-1111-000000000007","user": 2,"weight": 83,"date": "2013-01-30T14:30:21.592Z"diff --git a/wger/weight/tests/test_powersync.py b/wger/weight/tests/test_powersync.pynew file mode 100644index 000000000..9cee52158--- /dev/null+++ b/wger/weight/tests/test_powersync.py@@ -0,0 +1,43 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# wger+from wger.core.tests import powersync_base_test+from wger.weight.models import WeightEntry+++class WeightEntryPowerSyncTestCase(powersync_base_test.PowerSyncResourceTestCase):+ """+ Verify that the PowerSync handlers in wger/weight/powersync.py only let a+ user touch their own WeightEntry rows.+ """++ table = 'weight_weightentry'+ resource = WeightEntry++ # owned by user 'test'+ pk_owned = '11111111-1111-1111-1111-000000000003'++ create_payload = {+ 'date': '2030-01-15T10:00:00Z',+ 'weight': '95.5',+ }+ update_payload = {+ 'id': pk_owned,+ 'weight': '88.5',+ }++ def _get_entry_for_update(self):+ # WeightEntry's PowerSync handler looks up by uuid, not pk+ return WeightEntry.objects.get(uuid=self.update_payload['id'])