kopia lustrzana https://gitlab.com/marnanel/chapeau
test_validation() renamed to test_local_lookup(). test_remote_user_known() added.
Old cruft removed.2019-08-17
rodzic
e2e7d8fa34
commit
a92c5fc3c7
|
@ -12,12 +12,6 @@ import json
|
|||
|
||||
logger = logging.getLogger(name='django_kepi')
|
||||
|
||||
POLLY_URL = 'https://remote.example.net/users/polly'
|
||||
SUKI_URL = 'https://remote.example.net/services/suki/kettle'
|
||||
PUT_THE_KETTLE_ON = "Let's put the kettle on"
|
||||
SUKI_TAKE_IT_OFF = "Suki, take it off again"
|
||||
RESULT_URL = 'https://localhost/async_result'
|
||||
|
||||
ACTIVITY_ID = "https://example.com/04b065f8-81c4-408e-bec3-9fb1f7c06408"
|
||||
INBOX_HOST = 'europa.example.com'
|
||||
INBOX_PATH = '/inbox'
|
||||
|
@ -98,6 +92,34 @@ def _test_message(secret='', **fields):
|
|||
result.save()
|
||||
return result
|
||||
|
||||
def _remote_user(url, name, public_key):
|
||||
result = {
|
||||
'@context': MESSAGE_CONTEXT,
|
||||
'id': url,
|
||||
'type': 'Person',
|
||||
'following': '',
|
||||
'followers': '',
|
||||
'inbox': '',
|
||||
'outbox': '',
|
||||
'featured': '',
|
||||
'preferredUsername': name,
|
||||
'url': url,
|
||||
'publicKey': {
|
||||
'id': url+'#main-key',
|
||||
'owner': url,
|
||||
'publicKeyPem': public_key,
|
||||
},
|
||||
}
|
||||
return result
|
||||
|
||||
class ResultWrapper(object):
|
||||
def __init__(self,
|
||||
text,
|
||||
status_code=200,
|
||||
):
|
||||
self.text = json.dumps(text)
|
||||
self.status_code = status_code
|
||||
|
||||
class TestTasks(TestCase):
|
||||
|
||||
def _mock_remote_object(self,
|
||||
|
@ -159,7 +181,7 @@ class TestTasks(TestCase):
|
|||
body=local_endpoint)
|
||||
|
||||
@patch('requests.get')
|
||||
def test_validation(self, mock_get):
|
||||
def test_local_lookup(self, mock_get):
|
||||
keys = json.load(open('tests/keys/keys-0000.json', 'r'))
|
||||
|
||||
alice = ThingUser(
|
||||
|
@ -189,77 +211,26 @@ class TestTasks(TestCase):
|
|||
self.assertIsNotNone(result)
|
||||
mock_get.assert_not_called()
|
||||
|
||||
@httpretty.activate
|
||||
def test_request_success(self):
|
||||
@patch('requests.get')
|
||||
def test_remote_user_known(self, mock_get):
|
||||
|
||||
self._mock_remote_object(
|
||||
url = POLLY_URL,
|
||||
content = PUT_THE_KETTLE_ON,
|
||||
)
|
||||
self._mock_local_endpoint()
|
||||
|
||||
fetch(
|
||||
fetch_url = POLLY_URL,
|
||||
post_data = None,
|
||||
result_url = RESULT_URL,
|
||||
keys = json.load(open('tests/keys/keys-0001.json', 'r'))
|
||||
mock_get.return_value = ResultWrapper(
|
||||
text = _remote_user(
|
||||
url = REMOTE_FRED,
|
||||
name = 'Fred',
|
||||
public_key=keys['public']),
|
||||
)
|
||||
|
||||
self.assertEqual(self._received_code, 200)
|
||||
self.assertEqual(self._received_url, POLLY_URL)
|
||||
self.assertEqual(self._received_body, PUT_THE_KETTLE_ON)
|
||||
|
||||
@httpretty.activate
|
||||
def test_request_failure(self):
|
||||
|
||||
self._mock_remote_object(
|
||||
url = POLLY_URL,
|
||||
content = 'no idea, mate',
|
||||
status = 404,
|
||||
)
|
||||
self._mock_local_endpoint()
|
||||
|
||||
fetch(
|
||||
fetch_url = POLLY_URL,
|
||||
post_data = None,
|
||||
result_url = RESULT_URL,
|
||||
message = _test_message(
|
||||
f_id=ACTIVITY_ID,
|
||||
f_type="Follow",
|
||||
f_actor=REMOTE_FRED,
|
||||
f_object=LOCAL_ALICE,
|
||||
secret = keys['private'],
|
||||
)
|
||||
validate(message.id)
|
||||
|
||||
self.assertEqual(self._received_code, 404)
|
||||
self.assertEqual(self._received_url, POLLY_URL)
|
||||
|
||||
@httpretty.activate
|
||||
def test_request_gone(self):
|
||||
|
||||
self._mock_remote_object(
|
||||
url = POLLY_URL,
|
||||
content = "they've all gone away",
|
||||
status = 410,
|
||||
)
|
||||
self._mock_local_endpoint()
|
||||
|
||||
fetch(
|
||||
fetch_url = POLLY_URL,
|
||||
post_data = None,
|
||||
result_url = RESULT_URL,
|
||||
)
|
||||
|
||||
self.assertEqual(self._received_code, 410)
|
||||
self.assertEqual(self._received_url, POLLY_URL)
|
||||
|
||||
@httpretty.activate
|
||||
def test_submit_success(self):
|
||||
|
||||
self._mock_remote_service(
|
||||
url = SUKI_URL,
|
||||
)
|
||||
self._mock_local_endpoint()
|
||||
|
||||
fetch(
|
||||
fetch_url = SUKI_URL,
|
||||
post_data = SUKI_TAKE_IT_OFF,
|
||||
result_url = None,
|
||||
)
|
||||
|
||||
self.assertEqual(self._sent_body, SUKI_TAKE_IT_OFF)
|
||||
mock_get.assert_called_once_with(REMOTE_FRED)
|
||||
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue