def setUp(self): self.chunk_admin = ChunkAdmin() self.chunk_admin.admin_site = admin.site try: admin.site.unregister(Iframe) except NotRegistered: pass try: admin.site.unregister(User) except NotRegistered: pass admin.site.register(User, TestUserAdmin) admin.site.register(Iframe, RealishAdmin)
class ChunkAdminTestCase(DjangoTestCase): def setUp(self): self.chunk_admin = ChunkAdmin() self.chunk_admin.admin_site = admin.site try: admin.site.unregister(Iframe) except NotRegistered: pass try: admin.site.unregister(User) except NotRegistered: pass admin.site.register(User, TestUserAdmin) admin.site.register(Iframe, RealishAdmin) def test_get_model_perms(self): request = RequestFactory().get('/') results = self.chunk_admin.get_model_perms(request=request) self.assertEqual({}, results) def test_response_max(self): request = RequestFactory().get('/') context = { 'found': 3, 'limit': 1, 'region': 'test', 'me': '', 'parent': '', } results = self.chunk_admin.response_max(request=request, context=context).content self.assertIn(force_text('<h2>Limit reached</h2>'), force_text(results)) # this doesn't work. No idea why. Stupid Django. # self.assertIn('Unable to add more than <b>1</b> to this region.', # results) def test_get_response_add_context(self): request = RequestFactory().get('/') user = User(username='******') user.set_password('test') user.full_clean() user.save() ct = get_content_type(User) iframe = Iframe(position=2, region='test', content_type=ct, content_id=user.pk, url='https://news.bbc.co.uk/') iframe.full_clean() iframe.save() results = self.chunk_admin.get_response_add_context(request=request, obj=iframe) html = results.pop('html') self.assertEqual(results, {'action': {'add': True, 'change': False, 'delete': False}, 'object': {'pk': 1, 'id': 1} }) # self.assertIn('Please wait, saving changes', html) # self.assertIn('Add new content', html) # self.assertIn('Iframe', html) self.assertIn('<h3>Embeds</h3>', html) self.assertIn('<b>whee!:</b>', html) def test_get_response_change_context(self): request = RequestFactory().get('/') user = User(username='******') user.set_password('test') user.full_clean() user.save() ct = get_content_type(User) iframe = Iframe(position=2, region='test', content_type=ct, content_id=user.pk, url='https://news.bbc.co.uk/') iframe.full_clean() iframe.save() results = self.chunk_admin.get_response_change_context(request=request, obj=iframe) html = results.pop('html') self.assertEqual(results, {'action': {'add': False, 'change': True, 'delete': False}, 'object': {'pk': 1, 'id': 1} }) self.assertIn('<h3>Embeds</h3>', html) self.assertIn('<b>whee!:</b>', html) def test_get_response_delete_context_keyerror(self): request = RequestFactory().get('/') extra_context = {} results = self.chunk_admin.get_response_delete_context( request=request, obj_id=1, extra_context=extra_context) self.assertEqual(results, {'action': {'add': False, 'change': False, 'delete': True}, 'object': {'pk': 1, 'id': 1} }) def test_get_response_delete_context(self): request = RequestFactory().get('/') extra_context = { 'gfk': { 'content_object': User(username='******'), } } results = self.chunk_admin.get_response_delete_context( request=request, obj_id=1, extra_context=extra_context) html = results.pop('html') self.assertEqual(results, {'action': {'add': False, 'change': False, 'delete': True}, 'object': {'pk': 1, 'id': 1} }) self.assertIn('<h3>Embeds</h3>', html) self.assertIn('<b>whee!:</b>', html) def test_render_into_region(self): with self.settings(DEBUG=True): with self.assertRaises(NotImplementedError): self.chunk_admin.render_into_region(obj={}, context={}) with self.settings(DEBUG=False): data = self.chunk_admin.render_into_region(obj={}, context={}) self.assertIsNone(data) def test_render_into_summary(self): with self.settings(DEBUG=True): with self.assertRaises(NotImplementedError): self.chunk_admin.render_into_summary(obj={}, context={}) with self.settings(DEBUG=False): data = self.chunk_admin.render_into_summary(obj={}, context={}) self.assertIsNone(data) def test_save_model(self): user = User(username='******') user.set_password('test') user.full_clean() user.save() ct = get_content_type(User) iframe = Iframe(position=2, region='test', content_type=ct, content_id=user.pk, url='https://news.bbc.co.uk/') iframe.full_clean() iframe.save() request = RequestFactory().get('/', data={ 'region': 'test', 'content_type': ct.pk, 'content_id': user.pk, }) admin_instance = get_modeladmin(Iframe) expected_query_count = 2 if is_django_16plus() else 4 with self.assertNumQueries(expected_query_count): result = admin_instance.save_model(request=request, obj=iframe, form=ModelForm, change=True) self.assertIsNone(result) def test_logging(self): user = User(username='******') user.set_password('test') user.full_clean() user.save() ct = get_content_type(User) iframe = Iframe(position=2, region='test', content_type=ct, content_id=user.pk, url='https://news.bbc.co.uk/') iframe.full_clean() iframe.save() request = RequestFactory().get('/') request.user = user admin_instance = get_modeladmin(Iframe) admin_instance.log_addition(request, iframe) admin_instance.log_change(request, iframe, "we changed a thing!") admin_instance.log_deletion(request, iframe, "we deleted a thing!") # find them on the user logs = LogEntry.objects.get(content_type=ct, object_id=user.pk, user=user, action_flag=ADDITION) self.assertEqual(force_text(logs), 'Added "test".') logs = LogEntry.objects.get(content_type=ct, object_id=user.pk, user=user, action_flag=CHANGE) self.assertEqual(force_text(logs), 'Changed "test" - we changed a ' 'thing!') # can't check for deletions properly, see # https://code.djangoproject.com/ticket/21771#ticket # logs = LogEntry.objects.get(content_type=ct, object_id=user.pk, # user=user, action_flag=DELETION) # self.assertEqual(force_text(logs), 'Changed "test" - we changed a ' # 'thing!') # find them on the iframe ct = get_content_type(Iframe) logs = LogEntry.objects.get(content_type=ct, object_id=iframe.pk, user=user, action_flag=ADDITION) self.assertEqual(force_text(logs), 'Added "https://news.bbc.co.uk/".') logs = LogEntry.objects.get(content_type=ct, object_id=iframe.pk, user=user, action_flag=CHANGE) self.assertEqual(force_text(logs), 'Changed "https://news.bbc.co.uk/" ' '- we changed a thing!') # can't check for deletions properly, see # https://code.djangoproject.com/ticket/21771#ticket logs = LogEntry.objects.filter(content_type=ct, object_id=user.pk, user=user, action_flag=DELETION) self.assertEqual(force_text(logs[0]), 'Deleted "we deleted a thing!."') def _test_view(self, func='add_view', generate_chunks=1): user = User(username='******', is_staff=True, is_superuser=True, is_active=True) user.set_password('test') user.full_clean() user.save() ct = get_content_type(User) request = RequestFactory().get('/', { REQUEST_VAR_CT: ct.pk, REQUEST_VAR_ID: user.pk, REQUEST_VAR_REGION: 'test' }) request.user = user admin_instance = get_modeladmin(Iframe) for x in range(0, generate_chunks): iframe = Iframe(position=2, region='test', content_type=ct, content_id=user.pk, url='https://news.bbc.co.uk/') iframe.full_clean() iframe.save() kwargs = {'request': request} if func != 'add_view': kwargs.update({'object_id': force_text(iframe.pk)}) view = getattr(admin_instance, func) view(**kwargs) # now do the view again without the fields required by the decorator request = RequestFactory().get('/') request.user = user kwargs.update({'request': request}) with self.assertRaises(SuspiciousOperation): view(**kwargs) def test_change_view(self): self._test_view(func='change_view') def test_delete_view(self): self._test_view(func='delete_view') def test_add_view(self): self._test_view(func='add_view') def test_add_view_hitting_limit(self): self._test_view(func='add_view', generate_chunks=3)