diff --git a/kepi/trilby_api/tests/__init__.py b/kepi/trilby_api/tests/__init__.py index fe14089..5d99687 100644 --- a/kepi/trilby_api/tests/__init__.py +++ b/kepi/trilby_api/tests/__init__.py @@ -3,6 +3,57 @@ from rest_framework.test import force_authenticate, APIClient from kepi.trilby_api.models import * 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): def setUp(self): @@ -11,6 +62,49 @@ class TrilbyTestCase(TestCase): 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'): from kepi.trilby_api.models import TrilbyUser @@ -48,7 +142,7 @@ def create_local_status(content, return result def _client_request( - url, data, + path, data, as_user, is_post, ): @@ -64,30 +158,28 @@ def _client_request( if is_post: result = c.post( - url, + path, data, format = 'json', ) else: result = c.get( - url, + path, format = 'json', ) return result -def post( - url, +def post(path, data, as_user = None): - return _client_request(url, data, as_user, + return _client_request(path, data, as_user, is_post = True) -def get( - url, +def get(path, as_user = None): - return _client_request(url, {}, as_user, + return _client_request(path, {}, as_user, is_post = False) diff --git a/kepi/trilby_api/tests/test_account.py b/kepi/trilby_api/tests/test_account.py index 2e405d8..3cc9504 100644 --- a/kepi/trilby_api/tests/test_account.py +++ b/kepi/trilby_api/tests/test_account.py @@ -8,46 +8,14 @@ from django.conf import settings # Tests for accounts. API docs are here: # 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): # Getting the list of an account's statuses is handled in test_timeline. def test_verify_credentials_anonymous(self): - request = self.factory.get( + result = self.get( '/api/v1/accounts/verify_credentials', ) - view = Verify_Credentials.as_view() - - result = view(request) self.assertEqual( result.status_code, @@ -67,14 +35,10 @@ class TestAccountCredentials(TrilbyTestCase): def _user_test(self, name): alice = create_local_person(name='alice') - request = self.factory.get( + result = self.get( '/api/v1/accounts/'+name, + as_user = alice, ) - force_authenticate(request, user=alice.local_user) - - view = Verify_Credentials.as_view() - - result = view(request) self.assertEqual( result.status_code, diff --git a/kepi/trilby_api/tests/test_instance.py b/kepi/trilby_api/tests/test_instance.py index 304d93f..2d22a71 100644 --- a/kepi/trilby_api/tests/test_instance.py +++ b/kepi/trilby_api/tests/test_instance.py @@ -11,12 +11,9 @@ from django.conf import settings class TestInstance(TrilbyTestCase): def test_instance_query(self): - request = self.factory.get( + result = self.get( '/api/v1/instance', ) - view = Instance.as_view() - - result = view(request) self.assertEqual( result.status_code, @@ -39,3 +36,21 @@ class TestInstance(TrilbyTestCase): @skip("Not yet implemented") def test_list_weekly_activity(self): 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, + [], + ) diff --git a/kepi/trilby_api/tests/test_status.py b/kepi/trilby_api/tests/test_status.py index 02d8733..385f5cc 100644 --- a/kepi/trilby_api/tests/test_status.py +++ b/kepi/trilby_api/tests/test_status.py @@ -40,15 +40,10 @@ class TestStatus(TrilbyTestCase): posted_by = self._alice, ) - request = self.factory.get( - '/api/v1/statuses/'+str(self._status.id), + result = self.get( + 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( result.status_code, @@ -94,14 +89,10 @@ class TestStatus(TrilbyTestCase): posted_by = self._alice, ) - request = self.factory.get( + result = self.get( '/api/v1/statuses/', + as_user = self._alice, ) - force_authenticate(request, user=self._alice.local_user) - - view = Statuses.as_view() - - result = view(request) self.assertEqual( result.status_code, @@ -641,74 +632,17 @@ class TestStatus(TrilbyTestCase): def test_unpin(self): 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): self._create_alice() - request = self.factory.post( + result = self.post( '/api/v1/statuses/', { '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( result.status_code, @@ -723,42 +657,6 @@ class TestStatus(TrilbyTestCase): '

Hello world

', ) - @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): def test_publish_simple(self):