Skip to content

Commit

Permalink
[py-tx] Implement Matched/UpVote/DownVote in ThreatExchange Client API (
Browse files Browse the repository at this point in the history
  • Loading branch information
tcnguyen483 authored Oct 3, 2024
1 parent 09ea0c4 commit 505b431
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,45 @@ def remove_reaction_from_threat_descriptor(
dryRun=dryRun,
)

def react_matched_threat_descriptor(
self, descriptor_id, *, showURLs=False, dryRun=False
):
"""
Does a POST to the reactions API indicating that this descriptor's hash was
matched.
See: https://developers.facebook.com/docs/threat-exchange/reference/reacting
"""
return self.react_to_threat_descriptor(
descriptor_id, "SAW_THIS_TOO", showURLs=showURLs, dryRun=dryRun
)

def react_upvote_threat_descriptor(
self, descriptor_id, *, showURLs=False, dryRun=False
):
"""
Does a POST to the reactions API indicating that this descriptor's hash is
helpful for discovering harmful content
See: https://developers.facebook.com/docs/threat-exchange/reference/reacting
"""
return self.react_to_threat_descriptor(
descriptor_id, "HELPFUL", showURLs=showURLs, dryRun=dryRun
)

def react_downvote_threat_descriptor(
self, descriptor_id, *, showURLs=False, dryRun=False
):
"""
Does a POST to the reactions API indicating that this descriptor's hash is
NOT helpful for discovering harmful content
See: https://developers.facebook.com/docs/threat-exchange/reference/reacting
"""
return self.react_to_threat_descriptor(
descriptor_id, "NOT_HELPFUL", showURLs=showURLs, dryRun=dryRun
)

def upload_threat_descriptor(self, postParams, showURLs, dryRun):
"""
Does a single POST to the threat_descriptors endpoint. See also
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

from unittest.mock import Mock
import pytest
import requests
import typing as t

from threatexchange.exchanges.clients.fb_threatexchange.api import (
ThreatExchangeAPI,
)

POST_SUCCESS = """
{
"succcess": true
}
"""

POST_SUCCESS_JSON = {"succcess": True}


def mock_post_impl(url: str, data: str, **params):
content = POST_SUCCESS
# Void your warantee by messing with requests state
resp = requests.Response()
resp._content = content.encode()
resp.status_code = 200
resp.content # Set the rest of Request's internal state
return resp


def test_matched_upvote_downvote(monkeypatch: pytest.MonkeyPatch):
api = ThreatExchangeAPI("fake api token")
session = None
session = Mock(
strict_spec=["post", "__enter__", "__exit__"],
post=mock_post_impl,
__enter__=lambda _: session,
__exit__=lambda *args: None,
)
monkeypatch.setattr(api, "_get_session", lambda: session)

result = api.react_matched_threat_descriptor(1234, showURLs=False, dryRun=False)

assert result[2] == POST_SUCCESS_JSON
assert result[0] is None
assert result[1] is None

result = api.react_upvote_threat_descriptor(1234, showURLs=False, dryRun=False)

assert result[2] == POST_SUCCESS_JSON
assert result[0] is None
assert result[1] is None

result = api.react_downvote_threat_descriptor(1234, showURLs=False, dryRun=False)

assert result[2] == POST_SUCCESS_JSON
assert result[0] is None
assert result[1] is None

0 comments on commit 505b431

Please sign in to comment.