- ACCOUNT_EXPECTED etc moved to __init__
 - the path param to get, post, etc is called "path" not "url"
 - _create_alice in TrilbyTestCase for now
 - get, post, etc methods in TrilbyTestCase
     (the existing standalone functions are still there for now
      to avoid breaking stuff we're going to refactor soon)
 - test_post_multiple_statuses removed bc pointless
 - test for custom emojis moved to test_instance and
      its path fixed to the correct one
trilby-heavy
Marnanel Thurman 2020-04-16 17:25:17 +01:00
rodzic d80ae73e5c
commit a8186fa585
4 zmienionych plików z 130 dodań i 161 usunięć

Wyświetl plik

@ -3,6 +3,57 @@ from rest_framework.test import force_authenticate, APIClient
from kepi.trilby_api.models import * from kepi.trilby_api.models import *
from django.conf import settings from django.conf import settings
ACCOUNT_EXPECTED = [
('id', '@alice'),
('username', 'alice'),
('acct', 'alice@testserver'),
('display_name', 'alice'),
('locked', False),
('followers_count', 0),
('following_count', 0),
('statuses_count', 0),
('note', ''),
('url', 'https://testserver/users/alice'),
('fields', []),
('emojis', []),
('avatar', 'https://testserver/static/defaults/avatar_1.jpg'),
('header', 'https://testserver/static/defaults/header.jpg'),
('avatar_static', 'https://testserver/static/defaults/avatar_1.jpg'),
('header_static', 'https://testserver/static/defaults/header.jpg'),
('bot', False),
]
ACCOUNT_SOURCE_EXPECTED = [
('privacy', 'A'),
('sensitive', False),
('language', settings.KEPI['LANGUAGES'][0]), # FIXME
]
STATUS_EXPECTED = [
('in_reply_to_account_id', None),
('content', 'Hello world.'),
('emojis', []),
('reblogs_count', 0),
('favourites_count', 0),
('reblogged', False),
('favourited', False),
('muted', False),
('sensitive', False),
('spoiler_text', ''),
('visibility', 'A'),
('media_attachments', []),
('mentions', []),
('tags', []),
('card', None),
('poll', None),
('application', None),
('language', 'en'),
('pinned', False),
]
class TrilbyTestCase(TestCase): class TrilbyTestCase(TestCase):
def setUp(self): def setUp(self):
@ -11,6 +62,49 @@ class TrilbyTestCase(TestCase):
super().setUp() super().setUp()
def _create_alice(self):
# TODO: this should be replaced with a general-case "_create_user()"
# that then gets used everywhere
result = create_local_person('alice')
self._alice = result
return result
def request(self, verb, path,
data={},
as_user=None,
*args, **kwargs,
):
c = APIClient()
if as_user:
c.force_authenticate(as_user.local_user)
command = getattr(c, verb)
result = command(
path,
*args,
format = 'json',
**kwargs,
)
return result
def get(self, *args, **kwargs):
return self.request('get', *args, **kwargs)
def post(self, *args, **kwargs):
return self.request('post', *args, **kwargs)
def patch(self, *args, **kwargs):
return self.request('patch', *args, **kwargs)
def delete(self, *args, **kwargs):
return self.request('delete', *args, **kwargs)
def create_local_person(name='jemima'): def create_local_person(name='jemima'):
from kepi.trilby_api.models import TrilbyUser from kepi.trilby_api.models import TrilbyUser
@ -48,7 +142,7 @@ def create_local_status(content,
return result return result
def _client_request( def _client_request(
url, data, path, data,
as_user, as_user,
is_post, is_post,
): ):
@ -64,30 +158,28 @@ def _client_request(
if is_post: if is_post:
result = c.post( result = c.post(
url, path,
data, data,
format = 'json', format = 'json',
) )
else: else:
result = c.get( result = c.get(
url, path,
format = 'json', format = 'json',
) )
return result return result
def post( def post(path,
url,
data, data,
as_user = None): as_user = None):
return _client_request(url, data, as_user, return _client_request(path, data, as_user,
is_post = True) is_post = True)
def get( def get(path,
url,
as_user = None): as_user = None):
return _client_request(url, {}, as_user, return _client_request(path, {}, as_user,
is_post = False) is_post = False)

Wyświetl plik

@ -8,46 +8,14 @@ from django.conf import settings
# Tests for accounts. API docs are here: # Tests for accounts. API docs are here:
# https://docs.joinmastodon.org/methods/accounts/ # https://docs.joinmastodon.org/methods/accounts/
ACCOUNT_EXPECTED = [
('id', '@alice'),
('username', 'alice'),
('acct', 'alice@testserver'),
('display_name', 'alice'),
('locked', False),
('followers_count', 0),
('following_count', 0),
('statuses_count', 0),
('note', ''),
('url', 'https://testserver/users/alice'),
('fields', []),
('emojis', []),
('avatar', 'https://testserver/static/defaults/avatar_1.jpg'),
('header', 'https://testserver/static/defaults/header.jpg'),
('avatar_static', 'https://testserver/static/defaults/avatar_1.jpg'),
('header_static', 'https://testserver/static/defaults/header.jpg'),
('bot', False),
]
ACCOUNT_SOURCE_EXPECTED = [
('privacy', 'A'),
('sensitive', False),
('language', settings.KEPI['LANGUAGES'][0]), # FIXME
]
class TestAccountCredentials(TrilbyTestCase): class TestAccountCredentials(TrilbyTestCase):
# Getting the list of an account's statuses is handled in test_timeline. # Getting the list of an account's statuses is handled in test_timeline.
def test_verify_credentials_anonymous(self): def test_verify_credentials_anonymous(self):
request = self.factory.get( result = self.get(
'/api/v1/accounts/verify_credentials', '/api/v1/accounts/verify_credentials',
) )
view = Verify_Credentials.as_view()
result = view(request)
self.assertEqual( self.assertEqual(
result.status_code, result.status_code,
@ -67,14 +35,10 @@ class TestAccountCredentials(TrilbyTestCase):
def _user_test(self, name): def _user_test(self, name):
alice = create_local_person(name='alice') alice = create_local_person(name='alice')
request = self.factory.get( result = self.get(
'/api/v1/accounts/'+name, '/api/v1/accounts/'+name,
as_user = alice,
) )
force_authenticate(request, user=alice.local_user)
view = Verify_Credentials.as_view()
result = view(request)
self.assertEqual( self.assertEqual(
result.status_code, result.status_code,

Wyświetl plik

@ -11,12 +11,9 @@ from django.conf import settings
class TestInstance(TrilbyTestCase): class TestInstance(TrilbyTestCase):
def test_instance_query(self): def test_instance_query(self):
request = self.factory.get( result = self.get(
'/api/v1/instance', '/api/v1/instance',
) )
view = Instance.as_view()
result = view(request)
self.assertEqual( self.assertEqual(
result.status_code, result.status_code,
@ -39,3 +36,21 @@ class TestInstance(TrilbyTestCase):
@skip("Not yet implemented") @skip("Not yet implemented")
def test_list_weekly_activity(self): def test_list_weekly_activity(self):
pass pass
def test_get_emojis(self):
result = self.get(
'/api/v1/custom_emojis',
)
self.assertEqual(
result.status_code,
200,
msg = result.content,
)
content = json.loads(result.content.decode())
self.assertEqual(
content,
[],
)

Wyświetl plik

@ -40,15 +40,10 @@ class TestStatus(TrilbyTestCase):
posted_by = self._alice, posted_by = self._alice,
) )
request = self.factory.get( result = self.get(
'/api/v1/statuses/'+str(self._status.id), path = '/api/v1/statuses/'+str(self._status.id),
as_user = self._alice,
) )
force_authenticate(request, user=self._alice.local_user)
view = Statuses.as_view()
result = view(request,
id=str(self._status.id))
self.assertEqual( self.assertEqual(
result.status_code, result.status_code,
@ -94,14 +89,10 @@ class TestStatus(TrilbyTestCase):
posted_by = self._alice, posted_by = self._alice,
) )
request = self.factory.get( result = self.get(
'/api/v1/statuses/', '/api/v1/statuses/',
as_user = self._alice,
) )
force_authenticate(request, user=self._alice.local_user)
view = Statuses.as_view()
result = view(request)
self.assertEqual( self.assertEqual(
result.status_code, result.status_code,
@ -641,74 +632,17 @@ class TestStatus(TrilbyTestCase):
def test_unpin(self): def test_unpin(self):
self.fail("Test not yet implemented") self.fail("Test not yet implemented")
def test_get_status_context(self):
self._create_alice()
self._create_status()
request = self.factory.get(
'/api/v1/statuses/'+str(self._status.id)+'/context',
)
force_authenticate(request, user=self._alice.local_user)
view = StatusContext.as_view()
result = view(request,
id=str(self._status.id))
self.assertEqual(
result.status_code,
200,
msg = result.content,
)
content = json.loads(result.content)
self.assertEqual(
content,
{
'ancestors': [],
'descendants': [],
})
def test_get_emojis(self):
request = self.factory.get(
'/api/v1/emojis/',
)
view = Emojis.as_view()
result = view(request)
self.assertEqual(
result.status_code,
200,
msg = result.content,
)
content = json.loads(result.content.decode())
self.assertEqual(
content,
[],
)
def test_post_status(self): def test_post_status(self):
self._create_alice() self._create_alice()
request = self.factory.post( result = self.post(
'/api/v1/statuses/', '/api/v1/statuses/',
{ {
'status': 'Hello world', 'status': 'Hello world',
}, },
format='json', as_user = self._alice,
) )
force_authenticate(request, user=self._alice.local_user)
view = Statuses.as_view()
result = view(request)
self.assertEqual( self.assertEqual(
result.status_code, result.status_code,
@ -723,42 +657,6 @@ class TestStatus(TrilbyTestCase):
'<p>Hello world</p>', '<p>Hello world</p>',
) )
@skip("serial numbers are not yet exposed")
def test_post_multiple_statuses(self):
self._create_alice()
previous_serial = 0
for i in range(0, 9):
request = self.factory.post(
'/api/v1/statuses/',
{
'status': 'Hello world %d' % (i,),
},
format='json',
)
force_authenticate(request, user=self._alice.local_user)
view = Statuses.as_view()
result = view(request)
self.assertEqual(
result.status_code,
200,
'Result code',
)
content = json.loads(result.content.decode())
self.assertLess(
previous_serial,
content['serial'])
previous_serial = content['serial']
class TestPublish(TrilbyTestCase): class TestPublish(TrilbyTestCase):
def test_publish_simple(self): def test_publish_simple(self):