Skip to content

Commit

Permalink
apps/comments: make sure comment can only be added with egreement and…
Browse files Browse the repository at this point in the history
… add tests
  • Loading branch information
fuzzylogic2000 committed Jul 4, 2022
1 parent 9918201 commit 4352257
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 11 deletions.
22 changes: 16 additions & 6 deletions adhocracy4/comments_async/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.apps import apps
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.urls import reverse
from django.urls.exceptions import NoReverseMatch
from django.utils.translation import gettext_lazy as _
from django_filters import rest_framework as filters
from rest_framework import mixins
from rest_framework import status
Expand Down Expand Up @@ -113,7 +116,7 @@ def get_serializer_class(self):

def _save_terms_agreement(self):
if hasattr(settings, 'A4_USE_ORGANISATION_TERMS_OF_USE') \
and settings.A4_USE_ORGANISATION_TERMS_OF_USE:
and settings.A4_USE_ORGANISATION_TERMS_OF_USE:
if 'agreed_terms_of_use' in self.request.data and \
self.request.data['agreed_terms_of_use']:
organisation_model = apps.get_model(
Expand All @@ -130,6 +133,10 @@ def _save_terms_agreement(self):
self.request.data['agreed_terms_of_use']
}
)
else:
raise PermissionDenied(
_("Please agree to the organisation's terms of use.")
)

def perform_create(self, serializer):
self._save_terms_agreement()
Expand Down Expand Up @@ -192,11 +199,14 @@ def list(self, request, *args, **kwargs):
user_has_agreed = None
use_org_terms_of_use = True
organisation = self.content_object.project.organisation
org_terms_url = reverse(
'organisation-terms-of-use', kwargs={
'organisation_slug': organisation.slug
}
)
try:
org_terms_url = reverse(
'organisation-terms-of-use', kwargs={
'organisation_slug': organisation.slug
}
)
except NoReverseMatch:
raise NotImplementedError('Add org terms of use view.')
if hasattr(request, 'user'):
user = request.user
if user.is_authenticated:
Expand Down
9 changes: 9 additions & 0 deletions tests/apps/organisations/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from adhocracy4.test.factories import UserFactory
from tests.apps.organisations.models import Member
from tests.apps.organisations.models import Organisation
from tests.apps.organisations.models import OrganisationTermsOfUse


class OrganisationFactory(factory.django.DjangoModelFactory):
Expand Down Expand Up @@ -33,3 +34,11 @@ class Meta:

member = factory.SubFactory(USER_FACTORY)
organisation = factory.SubFactory(ORGANISATION_FACTORY)


class OrganisationTermsOfUseFactory(factory.django.DjangoModelFactory):
class Meta:
model = OrganisationTermsOfUse

user = factory.SubFactory(UserFactory)
organisation = factory.SubFactory(OrganisationFactory)
28 changes: 28 additions & 0 deletions tests/apps/organisations/migrations/0003_organisationtermsofuse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.2.13 on 2022-06-21 14:14

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('a4test_organisations', '0002_member'),
]

operations = [
migrations.CreateModel(
name='OrganisationTermsOfUse',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('has_agreed', models.BooleanField(default=False)),
('organisation', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.A4_ORGANISATIONS_MODEL)),
('user', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'unique_together': {('user', 'organisation')},
},
),
]
33 changes: 28 additions & 5 deletions tests/apps/organisations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Organisation(models.Model):
)
groups = models.ManyToManyField(
Group,
blank=True
blank=True,
)

def __str__(self):
Expand All @@ -38,10 +38,33 @@ def get_absolute_url(self):


class Member(models.Model):
member = models.ForeignKey(User,
on_delete=models.CASCADE)
organisation = models.ForeignKey(settings.A4_ORGANISATIONS_MODEL,
on_delete=models.CASCADE)
member = models.ForeignKey(
User,
on_delete=models.CASCADE,
)
organisation = models.ForeignKey(
settings.A4_ORGANISATIONS_MODEL,
on_delete=models.CASCADE,
)

class Meta:
unique_together = [('member', 'organisation')]


class OrganisationTermsOfUse(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
editable=False,
)
organisation = models.ForeignKey(
settings.A4_ORGANISATIONS_MODEL,
on_delete=models.CASCADE,
editable=False,
)
has_agreed = models.BooleanField(
default=False,
)

class Meta:
unique_together = [('user', 'organisation')]
2 changes: 2 additions & 0 deletions tests/comments/test_async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ def test_fields(user, apiclient, question_ct, question):
assert 'would_have_commenting_permission' in response.data
assert 'project_is_public' in response.data
assert response.data['count'] == 1
assert 'use_org_terms_of_use' in response.data
assert not response.data['use_org_terms_of_use']

commentDict = response.data['results'][0]
assert len(commentDict) == 21
Expand Down
Loading

0 comments on commit 4352257

Please sign in to comment.