fixes mocks on tests

This commit is contained in:
Mouse Reeve 2021-02-07 11:29:24 -08:00
parent ad7a045582
commit bf6aca5122
5 changed files with 51 additions and 40 deletions

View file

@ -124,7 +124,7 @@ class GoodreadsImport(TestCase):
job_id=import_job.id, index=index, data=entry, book=self.book) job_id=import_job.id, index=index, data=entry, book=self.book)
break break
with patch('bookwyrm.broadcast.broadcast_task.delay'): with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
goodreads_import.handle_imported_book( goodreads_import.handle_imported_book(
self.user, import_item, False, 'public') self.user, import_item, False, 'public')
@ -144,9 +144,10 @@ class GoodreadsImport(TestCase):
def test_handle_imported_book_already_shelved(self): def test_handle_imported_book_already_shelved(self):
''' goodreads import added a book, this adds related connections ''' ''' goodreads import added a book, this adds related connections '''
shelf = self.user.shelf_set.filter(identifier='to-read').first() with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
models.ShelfBook.objects.create( shelf = self.user.shelf_set.filter(identifier='to-read').first()
shelf=shelf, user=self.user, book=self.book) models.ShelfBook.objects.create(
shelf=shelf, user=self.user, book=self.book)
import_job = models.ImportJob.objects.create(user=self.user) import_job = models.ImportJob.objects.create(user=self.user)
datafile = pathlib.Path(__file__).parent.joinpath('data/goodreads.csv') datafile = pathlib.Path(__file__).parent.joinpath('data/goodreads.csv')
@ -156,7 +157,7 @@ class GoodreadsImport(TestCase):
job_id=import_job.id, index=index, data=entry, book=self.book) job_id=import_job.id, index=index, data=entry, book=self.book)
break break
with patch('bookwyrm.broadcast.broadcast_task.delay'): with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
goodreads_import.handle_imported_book( goodreads_import.handle_imported_book(
self.user, import_item, False, 'public') self.user, import_item, False, 'public')
@ -185,7 +186,7 @@ class GoodreadsImport(TestCase):
job_id=import_job.id, index=index, data=entry, book=self.book) job_id=import_job.id, index=index, data=entry, book=self.book)
break break
with patch('bookwyrm.broadcast.broadcast_task.delay'): with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
goodreads_import.handle_imported_book( goodreads_import.handle_imported_book(
self.user, import_item, False, 'public') self.user, import_item, False, 'public')
goodreads_import.handle_imported_book( goodreads_import.handle_imported_book(
@ -214,7 +215,7 @@ class GoodreadsImport(TestCase):
import_item = models.ImportItem.objects.create( import_item = models.ImportItem.objects.create(
job_id=import_job.id, index=0, data=entry, book=self.book) job_id=import_job.id, index=0, data=entry, book=self.book)
with patch('bookwyrm.broadcast.broadcast_task.delay'): with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
goodreads_import.handle_imported_book( goodreads_import.handle_imported_book(
self.user, import_item, True, 'unlisted') self.user, import_item, True, 'unlisted')
review = models.Review.objects.get(book=self.book, user=self.user) review = models.Review.objects.get(book=self.book, user=self.user)
@ -235,7 +236,7 @@ class GoodreadsImport(TestCase):
import_item = models.ImportItem.objects.create( import_item = models.ImportItem.objects.create(
job_id=import_job.id, index=0, data=entry, book=self.book) job_id=import_job.id, index=0, data=entry, book=self.book)
with patch('bookwyrm.broadcast.broadcast_task.delay'): with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
goodreads_import.handle_imported_book( goodreads_import.handle_imported_book(
self.user, import_item, False, 'unlisted') self.user, import_item, False, 'unlisted')
self.assertFalse(models.Review.objects.filter( self.assertFalse(models.Review.objects.filter(

View file

@ -22,7 +22,7 @@ class Incoming(TestCase):
'mouse@example.com', 'mouse@mouse.com', 'mouseword', 'mouse@example.com', 'mouse@mouse.com', 'mouseword',
local=True, localname='mouse') local=True, localname='mouse')
self.local_user.remote_id = 'https://example.com/user/mouse' self.local_user.remote_id = 'https://example.com/user/mouse'
self.local_user.save() self.local_user.save(broadcast=False)
with patch('bookwyrm.models.user.set_remote_server.delay'): with patch('bookwyrm.models.user.set_remote_server.delay'):
self.remote_user = models.User.objects.create_user( self.remote_user = models.User.objects.create_user(
'rat', 'rat@rat.com', 'ratword', 'rat', 'rat@rat.com', 'ratword',
@ -31,11 +31,12 @@ class Incoming(TestCase):
inbox='https://example.com/users/rat/inbox', inbox='https://example.com/users/rat/inbox',
outbox='https://example.com/users/rat/outbox', outbox='https://example.com/users/rat/outbox',
) )
self.status = models.Status.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
user=self.local_user, self.status = models.Status.objects.create(
content='Test status', user=self.local_user,
remote_id='https://example.com/status/1', content='Test status',
) remote_id='https://example.com/status/1',
)
self.factory = RequestFactory() self.factory = RequestFactory()
@ -177,8 +178,9 @@ class Incoming(TestCase):
"object": "https://example.com/user/mouse" "object": "https://example.com/user/mouse"
} }
} }
models.UserFollows.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
user_subject=self.remote_user, user_object=self.local_user) models.UserFollows.objects.create(
user_subject=self.remote_user, user_object=self.local_user)
self.assertEqual(self.remote_user, self.local_user.followers.first()) self.assertEqual(self.remote_user, self.local_user.followers.first())
incoming.handle_unfollow(activity) incoming.handle_unfollow(activity)
@ -200,10 +202,11 @@ class Incoming(TestCase):
} }
} }
models.UserFollowRequest.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
user_subject=self.local_user, models.UserFollowRequest.objects.create(
user_object=self.remote_user user_subject=self.local_user,
) user_object=self.remote_user
)
self.assertEqual(models.UserFollowRequest.objects.count(), 1) self.assertEqual(models.UserFollowRequest.objects.count(), 1)
incoming.handle_follow_accept(activity) incoming.handle_follow_accept(activity)
@ -232,10 +235,11 @@ class Incoming(TestCase):
} }
} }
models.UserFollowRequest.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
user_subject=self.local_user, models.UserFollowRequest.objects.create(
user_object=self.remote_user user_subject=self.local_user,
) user_object=self.remote_user
)
self.assertEqual(models.UserFollowRequest.objects.count(), 1) self.assertEqual(models.UserFollowRequest.objects.count(), 1)
incoming.handle_follow_reject(activity) incoming.handle_follow_reject(activity)
@ -280,9 +284,10 @@ class Incoming(TestCase):
def test_handle_update_list(self): def test_handle_update_list(self):
''' a new list ''' ''' a new list '''
book_list = models.List.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
name='hi', remote_id='https://example.com/list/22', book_list = models.List.objects.create(
user=self.local_user) name='hi', remote_id='https://example.com/list/22',
user=self.local_user)
activity = { activity = {
'object': { 'object': {
"id": "https://example.com/list/22", "id": "https://example.com/list/22",
@ -539,7 +544,7 @@ class Incoming(TestCase):
activity = { activity = {
"id": "https://bookwyrm.social/shelfbook/6189#add", "id": "https://bookwyrm.social/shelfbook/6189#add",
"type": "Add", "type": "Add",
"actor": "hhttps://example.com/users/rat", "actor": "https://example.com/users/rat",
"object": "https://bookwyrm.social/book/37292", "object": "https://bookwyrm.social/book/37292",
"target": "https://bookwyrm.social/user/mouse/shelf/to-read", "target": "https://bookwyrm.social/user/mouse/shelf/to-read",
"@context": "https://www.w3.org/ns/activitystreams" "@context": "https://www.w3.org/ns/activitystreams"

View file

@ -65,8 +65,9 @@ class ReadingViews(TestCase):
def test_start_reading_reshelf(self): def test_start_reading_reshelf(self):
''' begin a book ''' ''' begin a book '''
to_read_shelf = self.local_user.shelf_set.get(identifier='to-read') to_read_shelf = self.local_user.shelf_set.get(identifier='to-read')
models.ShelfBook.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
shelf=to_read_shelf, book=self.book, user=self.local_user) models.ShelfBook.objects.create(
shelf=to_read_shelf, book=self.book, user=self.local_user)
shelf = self.local_user.shelf_set.get(identifier='reading') shelf = self.local_user.shelf_set.get(identifier='reading')
self.assertEqual(to_read_shelf.books.get(), self.book) self.assertEqual(to_read_shelf.books.get(), self.book)
self.assertFalse(shelf.books.exists()) self.assertFalse(shelf.books.exists())

View file

@ -57,8 +57,9 @@ class StatusViews(TestCase):
view = views.CreateStatus.as_view() view = views.CreateStatus.as_view()
user = models.User.objects.create_user( user = models.User.objects.create_user(
'rat', 'rat@rat.com', 'password', local=True) 'rat', 'rat@rat.com', 'password', local=True)
parent = models.Status.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
content='parent status', user=self.local_user) parent = models.Status.objects.create(
content='parent status', user=self.local_user)
form = forms.ReplyForm({ form = forms.ReplyForm({
'content': 'hi', 'content': 'hi',
'user': user.id, 'user': user.id,
@ -219,8 +220,9 @@ class StatusViews(TestCase):
def test_handle_delete_status(self): def test_handle_delete_status(self):
''' marks a status as deleted ''' ''' marks a status as deleted '''
view = views.DeleteStatus.as_view() view = views.DeleteStatus.as_view()
status = models.Status.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
user=self.local_user, content='hi') status = models.Status.objects.create(
user=self.local_user, content='hi')
self.assertFalse(status.deleted) self.assertFalse(status.deleted)
request = self.factory.post('') request = self.factory.post('')
request.user = self.local_user request.user = self.local_user

View file

@ -39,9 +39,10 @@ class TagViews(TestCase):
def test_tag_page(self): def test_tag_page(self):
''' there are so many views, this just makes sure it LOADS ''' ''' there are so many views, this just makes sure it LOADS '''
view = views.Tag.as_view() view = views.Tag.as_view()
tag = models.Tag.objects.create(name='hi there') with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
models.UserTag.objects.create( tag = models.Tag.objects.create(name='hi there')
tag=tag, user=self.local_user, book=self.book) models.UserTag.objects.create(
tag=tag, user=self.local_user, book=self.book)
request = self.factory.get('') request = self.factory.get('')
with patch('bookwyrm.views.tag.is_api_request') as is_api: with patch('bookwyrm.views.tag.is_api_request') as is_api:
is_api.return_value = False is_api.return_value = False
@ -82,9 +83,10 @@ class TagViews(TestCase):
def test_untag(self): def test_untag(self):
''' remove a tag from a book ''' ''' remove a tag from a book '''
view = views.RemoveTag.as_view() view = views.RemoveTag.as_view()
tag = models.Tag.objects.create(name='A Tag!?') with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
models.UserTag.objects.create( tag = models.Tag.objects.create(name='A Tag!?')
user=self.local_user, book=self.book, tag=tag) models.UserTag.objects.create(
user=self.local_user, book=self.book, tag=tag)
request = self.factory.post( request = self.factory.post(
'', { '', {
'user': self.local_user.id, 'user': self.local_user.id,