Skip to content

Commit

Permalink
Add trademark search and alert system
Browse files Browse the repository at this point in the history
Fixes #1025

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/OWASP-BLT/BLT/issues/1025?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
DonnieBLT committed Oct 13, 2024
1 parent 9f45721 commit bc3bdda
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions blt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"captcha",
"dj_rest_auth",
"dj_rest_auth.registration",
"website.management.commands.search_trademarks",
)


Expand Down
19 changes: 19 additions & 0 deletions website/management/commands/search_trademarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import requests
from django.core.management.base import BaseCommand
from django.core.mail import send_mail
from website.models import Company, Domain
from website.utils import search_uspto_database, send_email_alert

class Command(BaseCommand):
help = "Search the USPTO database for trademarks and alert companies if their brand name is at risk"

def handle(self, *args, **options):
companies = Company.objects.all()
for company in companies:
domains = Domain.objects.filter(company=company)
for domain in domains:
search_terms = [company.name, domain.name]
for term in search_terms:
results = search_uspto_database(term)
if results:
send_email_alert(company, results)
52 changes: 52 additions & 0 deletions website/tests/test_search_trademarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import unittest
from unittest.mock import patch, MagicMock
from django.core.management import call_command
from django.core.mail import send_mail
from website.models import Company, Domain
from website.utils import search_uspto_database, send_email_alert

class SearchTrademarksCommandTest(unittest.TestCase):

@patch('website.utils.search_uspto_database')
@patch('website.utils.send_email_alert')
def test_handle(self, mock_send_email_alert, mock_search_uspto_database):
# Setup mock data
company = Company(name="TestCompany", email="[email protected]")
domain = Domain(name="testdomain", company=company)
company.save()
domain.save()

mock_search_uspto_database.return_value = [{"trademark": "TestTrademark"}]

# Call the management command
call_command('search_trademarks')

# Assertions
mock_search_uspto_database.assert_called_with("TestCompany")
mock_search_uspto_database.assert_called_with("testdomain")
mock_send_email_alert.assert_called_with(company, [{"trademark": "TestTrademark"}])

class UtilsTest(unittest.TestCase):

@patch('requests.get')
def test_search_uspto_database(self, mock_get):
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"results": "test"}
mock_get.return_value = mock_response

result = search_uspto_database("test")
self.assertEqual(result, {"results": "test"})

@patch('django.core.mail.send_mail')
def test_send_email_alert(self, mock_send_mail):
company = Company(name="TestCompany", email="[email protected]")
results = {"results": "test"}

send_email_alert(company, results)
mock_send_mail.assert_called_with(
"Trademark Alert for TestCompany",
"Potential trademark matches found:\n\n{'results': 'test'}",
"[email protected]",
["[email protected]"]
)
22 changes: 22 additions & 0 deletions website/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from bs4 import BeautifulSoup
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator
from django.core.mail import send_mail
from website.models import Company, Domain

WHITELISTED_IMAGE_TYPES = {
"jpeg": "image/jpeg",
Expand Down Expand Up @@ -117,3 +119,23 @@ def get_github_issue_title(github_issue_url):
return f"Issue #{issue_number}"
except Exception:
return "No Title"


def search_uspto_database(term):
url = f"https://api.uspto.gov/trademark/v1/trademarkSearch/{term}"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {settings.USPTO_API_KEY}",
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
return None


def send_email_alert(company, results):
subject = f"Trademark Alert for {company.name}"
message = f"Potential trademark matches found:\n\n{results}"
from_email = settings.DEFAULT_FROM_EMAIL
recipient_list = [company.email]
send_mail(subject, message, from_email, recipient_list)

0 comments on commit bc3bdda

Please sign in to comment.