|
| 1 | +import mock |
| 2 | +import pytest |
| 3 | + |
| 4 | +from addons.forward.tests.utils import ForwardAddonTestCase |
| 5 | +from tests.base import OsfTestCase |
| 6 | +from website import settings |
| 7 | +from tests.json_api_test_app import JSONAPITestApp |
| 8 | + |
| 9 | +pytestmark = pytest.mark.django_db |
| 10 | + |
| 11 | +class TestForward(ForwardAddonTestCase, OsfTestCase): |
| 12 | + """ |
| 13 | + Forward (the redirect url has two v2 routes, one is addon based `/v2/nodes/{}/addons/forward/` one is node settings |
| 14 | + based `/v2/nodes/{}/settings/` they both need to be checked for spam each time they are used to modify a redirect url. |
| 15 | + """ |
| 16 | + |
| 17 | + django_app = JSONAPITestApp() |
| 18 | + |
| 19 | + def setUp(self): |
| 20 | + super(TestForward, self).setUp() |
| 21 | + self.app.authenticate(*self.user.auth) |
| 22 | + |
| 23 | + @mock.patch.object(settings, 'SPAM_CHECK_ENABLED', True) |
| 24 | + @mock.patch('osf.models.node.Node.do_check_spam') |
| 25 | + def test_change_url_check_spam(self, mock_check_spam): |
| 26 | + self.project.is_public = True |
| 27 | + self.project.save() |
| 28 | + self.django_app.put_json_api( |
| 29 | + '/v2/nodes/{}/addons/forward/'.format(self.project._id), |
| 30 | + {'data': {'attributes': {'url': 'http://possiblyspam.com'}}}, |
| 31 | + auth=self.user.auth, |
| 32 | + ) |
| 33 | + |
| 34 | + assert mock_check_spam.called |
| 35 | + data, _ = mock_check_spam.call_args |
| 36 | + author, author_email, content, request_headers = data |
| 37 | + |
| 38 | + assert author == self.user.fullname |
| 39 | + assert author_email == self.user.username |
| 40 | + assert content == 'http://possiblyspam.com' |
| 41 | + |
| 42 | + @mock.patch.object(settings, 'SPAM_CHECK_ENABLED', True) |
| 43 | + @mock.patch('osf.models.node.Node.do_check_spam') |
| 44 | + def test_change_url_check_spam_node_settings(self, mock_check_spam): |
| 45 | + self.project.is_public = True |
| 46 | + self.project.save() |
| 47 | + |
| 48 | + payload = { |
| 49 | + 'data': { |
| 50 | + 'type': 'node-settings', |
| 51 | + 'attributes': { |
| 52 | + 'access_requests_enabled': False, |
| 53 | + 'redirect_link_url': 'http://possiblyspam.com', |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + self.django_app.put_json_api( |
| 59 | + '/v2/nodes/{}/settings/'.format(self.project._id), |
| 60 | + payload, |
| 61 | + auth=self.user.auth, |
| 62 | + ) |
| 63 | + |
| 64 | + assert mock_check_spam.called |
| 65 | + data, _ = mock_check_spam.call_args |
| 66 | + author, author_email, content, request_headers = data |
| 67 | + |
| 68 | + assert author == self.user.fullname |
| 69 | + assert author_email == self.user.username |
| 70 | + assert content == 'http://possiblyspam.com' |
| 71 | + |
| 72 | + def test_invalid_url(self): |
| 73 | + res = self.django_app.put_json_api( |
| 74 | + '/v2/nodes/{}/addons/forward/'.format(self.project._id), |
| 75 | + {'data': {'attributes': {'url': 'bad url'}}}, |
| 76 | + auth=self.user.auth, expect_errors=True, |
| 77 | + ) |
| 78 | + assert res.status_code == 400 |
| 79 | + error = res.json['errors'][0] |
| 80 | + |
| 81 | + assert error['detail'] == 'Enter a valid URL.' |
0 commit comments