Skip to content

Commit

Permalink
hack together fix to send redirect url to akismet
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnetordoff committed Jul 9, 2019
1 parent 299abdb commit 1db904b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
41 changes: 39 additions & 2 deletions addons/forward/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import mock
import pytest

from nose.tools import assert_equal

from addons.forward.tests.utils import ForwardAddonTestCase
from tests.base import OsfTestCase
from website import settings
from tests.json_api_test_app import JSONAPITestApp

pytestmark = pytest.mark.django_db

class TestForwardLogs(ForwardAddonTestCase, OsfTestCase):
class TestForward(ForwardAddonTestCase, OsfTestCase):

django_app = JSONAPITestApp()

def setUp(self):
super(TestForwardLogs, self).setUp()
super(TestForward, self).setUp()
self.app.authenticate(*self.user.auth)

def test_change_url_log_added(self):
Expand All @@ -27,6 +32,38 @@ def test_change_url_log_added(self):
log_count + 1
)

@mock.patch.object(settings, 'SPAM_CHECK_ENABLED', True)
@mock.patch('osf.models.node.Node.do_check_spam')
def test_change_url_check_spam_v1(self, mock_check_spam):
self.project.is_public = True
self.project.save()
self.app.put_json(self.project.api_url_for('forward_config_put'), {'url': 'http://possiblyspam.com'})

assert mock_check_spam.called
data, _ = mock_check_spam.call_args
author, author_email, content, request_headers = data

assert author == self.user.fullname
assert author_email == self.user.username
assert content == 'http://possiblyspam.com'

@mock.patch.object(settings, 'SPAM_CHECK_ENABLED', True)
@mock.patch('osf.models.node.Node.do_check_spam')
def test_change_url_check_spam_v2(self, mock_check_spam):
self.project.is_public = True
self.project.save()
self.django_app.put_json_api('/v2/nodes/{}/addons/forward/'.format(self.project._id),
{'data': {'attributes': {'url': 'http://possiblyspam.com'}}},
auth=self.user.auth)

assert mock_check_spam.called
data, _ = mock_check_spam.call_args
author, author_email, content, request_headers = data

assert author == self.user.fullname
assert author_email == self.user.username
assert content == 'http://possiblyspam.com'

def test_change_timeout_log_not_added(self):
log_count = self.project.logs.count()
self.app.put_json(
Expand Down
1 change: 1 addition & 0 deletions addons/forward/views/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ def forward_config_put(auth, node_addon, **kwargs):
auth=auth,
save=True,
)
node_addon.owner.check_spam(auth.user, {'addons_forward_node_settings__url'}, request.headers)

return {}
7 changes: 5 additions & 2 deletions api/nodes/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from website.project.metadata.utils import is_prereg_admin_not_project_admin
from website.project.model import NodeUpdateError
from osf.utils import permissions as osf_permissions

from osf.utils.requests import get_headers_from_request

class RegistrationProviderRelationshipField(RelationshipField):
def get_object(self, _id):
Expand Down Expand Up @@ -890,7 +890,9 @@ def create(self, validated_data):
class ForwardNodeAddonSettingsSerializer(NodeAddonSettingsSerializerBase):

def update(self, instance, validated_data):
auth = Auth(self.context['request'].user)
request = self.context['request']
user = request.user
auth = Auth(user)
set_url = 'url' in validated_data
set_label = 'label' in validated_data

Expand Down Expand Up @@ -935,6 +937,7 @@ def update(self, instance, validated_data):
auth=auth,
save=True,
)
instance.owner.check_spam(user, {'addons_forward_node_settings__url'}, get_headers_from_request(request))

return instance

Expand Down
3 changes: 2 additions & 1 deletion osf/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,8 @@ def _get_spam_content(self, saved_fields):
spam_fields = self.get_spam_fields(saved_fields)
content = []
for field in spam_fields:
content.append((getattr(self, field, None) or '').encode('utf-8'))
values = list(self.__class__.objects.filter(id=self.id).values_list(field, flat=True))
content.append((' '.join(values) or '').encode('utf-8'))
if self.all_tags.exists():
content.extend([name.encode('utf-8') for name in self.all_tags.values_list('name', flat=True)])
if not content:
Expand Down
1 change: 1 addition & 0 deletions osf/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ class AbstractNode(DirtyFieldsMixin, TypedModel, AddonModelMixin, IdentifierMixi
SPAM_CHECK_FIELDS = {
'title',
'description',
'addons_forward_node_settings__url' # the often spammed redirect URL
}

# Fields that are writable by Node.update
Expand Down

0 comments on commit 1db904b

Please sign in to comment.