kopia lustrzana https://github.com/snarfed/bridgy-fed
65 wiersze
2.0 KiB
Python
65 wiersze
2.0 KiB
Python
"""Common test utility code.
|
|
"""
|
|
import copy
|
|
import datetime
|
|
import unittest
|
|
from unittest.mock import ANY, call
|
|
|
|
from app import app, cache
|
|
from oauth_dropins.webutil import testutil, util
|
|
from oauth_dropins.webutil.appengine_config import ndb_client
|
|
import requests
|
|
|
|
import common
|
|
|
|
NOW = datetime.datetime(2022, 11, 23, 22, 29, 19)
|
|
|
|
|
|
class TestCase(unittest.TestCase, testutil.Asserts):
|
|
maxDiff = None
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
app.testing = True
|
|
cache.clear()
|
|
self.client = app.test_client()
|
|
common.utcnow = lambda: NOW
|
|
|
|
# clear datastore
|
|
requests.post('http://%s/reset' % ndb_client.host)
|
|
self.ndb_context = ndb_client.context()
|
|
self.ndb_context.__enter__()
|
|
|
|
def tearDown(self):
|
|
self.ndb_context.__exit__(None, None, None)
|
|
super().tearDown()
|
|
|
|
def req(self, url, **kwargs):
|
|
"""Returns a mock requests call."""
|
|
kwargs.setdefault('headers', {}).update({
|
|
'User-Agent': util.user_agent,
|
|
})
|
|
kwargs.setdefault('timeout', util.HTTP_TIMEOUT)
|
|
kwargs.setdefault('stream', True)
|
|
return call(url, **kwargs)
|
|
|
|
def as2_req(self, url, **kwargs):
|
|
# TODO: these aren't currently getting checked?!
|
|
headers = {
|
|
'Date': 'Wed, 23 Nov 2022 22:29:19 GMT',
|
|
'Host': util.domain_from_link(url, minimize=False),
|
|
'Content-Type': 'application/activity+json',
|
|
'Digest': ANY,
|
|
**common.CONNEG_HEADERS_AS2_HTML,
|
|
**kwargs.pop('headers', {}),
|
|
}
|
|
return self.req(url, auth=ANY, headers=headers, **kwargs)
|
|
|
|
def assert_req(self, mock, url, **kwargs):
|
|
"""Checks a mock requests call."""
|
|
kwargs.setdefault('headers', {}).setdefault(
|
|
'User-Agent', 'Bridgy Fed (https://fed.brid.gy/)')
|
|
kwargs.setdefault('stream', True)
|
|
kwargs.setdefault('timeout', util.HTTP_TIMEOUT)
|
|
mock.assert_any_call(url, **kwargs)
|