-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3704 from webcompat/issue/3703/1
Fixes #3703 - Automatically label nsfw issues
- Loading branch information
Showing
9 changed files
with
273 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
"""Tests for Siterank class.""" | ||
|
||
import unittest | ||
import webcompat | ||
from webcompat.nsfw import moderate_screenshot | ||
|
||
|
||
class TestNSFW(unittest.TestCase): | ||
"""Tests for Top Sites Alexa class.""" | ||
|
||
def setUp(self): | ||
"""Set up the tests.""" | ||
webcompat.app.config['TESTING'] = True | ||
|
||
self.issue_body_with_screenshot = """ | ||
<!-- @browser: Firefox 94.0 --> | ||
<!-- @ua_header: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:94.0) Gecko/20100101 Firefox/94.0 --> | ||
<!-- @reported_with: desktop-reporter --> | ||
<!-- @public_url: https://github.com/webcompat/webcompat-tests/issues/2710 --> | ||
<!-- @extra_labels: type-webrender-enabled --> | ||
**URL**: http://aturemlaguerra.org/ | ||
**Browser / Version**: Firefox 94.0 | ||
**Operating System**: Mac OS X 10.15 | ||
**Tested Another Browser**: Yes Chrome | ||
**Problem type**: Site is not usable | ||
**Description**: Buttons or links not working | ||
**Steps to Reproduce**: | ||
gawrhs agrhsrthse sethsrthserhserhser | ||
<details> | ||
<summary>View the screenshot</summary> | ||
<img alt="Screenshot" src="https://staging.webcompat.com/uploads/2021/9/31053f87-a71a-4241-8b47-b2c388545d14.jpeg"> | ||
</details> | ||
""" # noqa | ||
|
||
self.issue_body_moderated = """ | ||
<!-- @browser: Firefox 94.0 --> | ||
<!-- @ua_header: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:94.0) Gecko/20100101 Firefox/94.0 --> | ||
<!-- @reported_with: desktop-reporter --> | ||
<!-- @public_url: https://github.com/webcompat/webcompat-tests/issues/2710 --> | ||
<!-- @extra_labels: type-webrender-enabled --> | ||
**URL**: http://aturemlaguerra.org/ | ||
**Browser / Version**: Firefox 94.0 | ||
**Operating System**: Mac OS X 10.15 | ||
**Tested Another Browser**: Yes Chrome | ||
**Problem type**: Site is not usable | ||
**Description**: Buttons or links not working | ||
**Steps to Reproduce**: | ||
gawrhs agrhsrthse sethsrthserhserhser | ||
<details> | ||
<summary>View the screenshot</summary> | ||
Screenshot removed - possible explicit content. | ||
</details> | ||
""" # noqa | ||
|
||
self.issue_body_no_screenshot = """ | ||
<!-- @browser: Firefox 94.0 --> | ||
<!-- @ua_header: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:94.0) Gecko/20100101 Firefox/94.0 --> | ||
<!-- @reported_with: desktop-reporter --> | ||
<!-- @public_url: https://github.com/webcompat/webcompat-tests/issues/2710 --> | ||
<!-- @extra_labels: type-webrender-enabled --> | ||
**URL**: http://aturemlaguerra.org/ | ||
**Browser / Version**: Firefox 94.0 | ||
**Operating System**: Mac OS X 10.15 | ||
**Tested Another Browser**: Yes Chrome | ||
**Problem type**: Site is not usable | ||
**Description**: Buttons or links not working | ||
**Steps to Reproduce**: | ||
gawrhs agrhsrthse sethsrthserhserhser | ||
""" # noqa | ||
|
||
def tearDown(self): | ||
"""Tear down the tests.""" | ||
pass | ||
|
||
def test_moderate_screenshot(self): | ||
"""Moderate screenshot and remove image tag.""" | ||
body = moderate_screenshot(self.issue_body_with_screenshot) | ||
self.assertEqual(body, self.issue_body_moderated) | ||
|
||
def test_no_screenshot_unchanged(self): | ||
"""Body remains unchanged if there is no screenshot.""" | ||
body = moderate_screenshot(self.issue_body_no_screenshot) | ||
self.assertEqual(body, self.issue_body_no_screenshot) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
"""Helpers methods for nsfw detection.""" | ||
|
||
import re | ||
|
||
from webcompat.db import SiteNSFW | ||
from webcompat.db import site_nsfw_db | ||
from webcompat.helpers import get_domains | ||
|
||
NSFW_LABEL = 'nsfw' | ||
|
||
|
||
def is_url_nsfw(url): | ||
return site_nsfw_db.query(SiteNSFW).filter_by(url=url).first() | ||
|
||
|
||
def get_nsfw_label(hostname): | ||
"""Query the nsfw DB for hostname or domain.""" | ||
if hostname: | ||
if is_url_nsfw(hostname): | ||
return NSFW_LABEL | ||
|
||
# If hostname not found, try less-level domain (>2) | ||
# If hostname is lv4.lv3.example.com, find lv3.example.com/example.com | ||
domains = get_domains(hostname) | ||
for domain in domains: | ||
if is_url_nsfw(domain): | ||
return NSFW_LABEL | ||
|
||
return None | ||
|
||
|
||
def moderate_screenshot(body): | ||
pattern = re.compile(r'<img alt="Screenshot" [^>]*src="([^"]+)"[^>]*>') | ||
clean_body = re.sub( | ||
pattern, | ||
'Screenshot removed - possible explicit content.', | ||
body | ||
) | ||
return clean_body |
Oops, something went wrong.