def test_str(self): iframe = Iframe( position=2, region="test", content_type=self.content_type, content_id=1, url="https://news.bbc.co.uk/" ) iframe.full_clean() iframe.save() self.assertEqual(force_text(iframe), "https://news.bbc.co.uk/")
def test_continue_editing_parent_object(self): """ if continue editing is hit, it should go back to the parent URL, I think? """ user = User(username='******', is_staff=True, is_superuser=True, is_active=True) user.set_password('test') user.full_clean() user.save() admin_instance = get_modeladmin(Iframe) self.assertIsInstance(admin_instance, RealishAdmin) request = RequestFactory().get('/', { '_continue': 1, }) request.user = user iframe_admin = reverse('admin:embeds_iframe_add') response_301 = HttpResponsePermanentRedirect(redirect_to=iframe_admin) 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() new_response = admin_instance.maybe_fix_redirection( request=request, response=response_301, obj=iframe) self.assertEqual(new_response['X-Chunkadmin-Response'], 'redirect-to-parent') self.assertEqual(301, new_response.status_code) self.assertEqual('/admin_mountpoint/auth/user/1/?_data_changed=1', new_response['Location'])
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_move_view(self): ct = get_content_type(User) user = User(username='******', is_staff=True, is_superuser=True, is_active=True) user.set_password('test') user.full_clean() user.save() for x in range(1, 10): obj = Iframe(position=x, region='test', content_id=user.pk, content_type=ct, url='http://example.com/') obj.full_clean() obj.save() first_obj = EditRegionChunk.objects.all()[0] request = RequestFactory().get('/', {'position': 3, 'pk': first_obj.pk, 'region': 'test'}) request.user = user response = self.admin.move_view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-type'], 'application/json') json_data = json.loads(force_text(response.content)) self.assertIn('action', json_data) self.assertIn('html', json_data) self.assertEqual('move', json_data['action']) self.assertIn('<div class="region-inline-wrapper"', json_data['html'])
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_usage(self): tmpl = Template(""" output: {% load editregion %} {% editregion "test" obj %}fallback{% endeditregion %} {% get_editregion "test" obj as exposed %} {% for chunk in exposed %} {% endfor %} """) user = User(username='******', is_staff=True, is_active=True, is_superuser=True) user.set_password('test') user.full_clean() user.save() user_content_type = get_content_type(User) # attach some chunks to the region in test for x in range(1, 11): iframe = Iframe(region='test', content_id=user.pk, content_type=user_content_type, url='https://news.bbc.co.uk/{0!s}'.format(x), position=x) iframe.full_clean() iframe.save() # fix the admin in test try: admin.site.unregister(User) except NotRegistered: pass admin.site.register(User, TestUserAdmin) request = RequestFactory().get('/') request.user = user ctx = RequestContext(request) ctx.update({'obj': user}) rendered = tmpl.render(ctx).strip() for x in range(1, 11): self.assertIn('name="chunk-iframe-{0}" data-pk="{0}" ' 'data-position="{0}" data-region="{1}"'.format( x, 'test'), rendered) tmpl = Template(""" output: {% load editregion %} {% get_editregion "test" obj as exposed %} {% for chunk in exposed %}xxx:{{ chunk.output }}{% endfor %} """) rendered = tmpl.render(ctx).strip() self.assertIn('output:', rendered) for x in range(1, 11): self.assertIn('xxx:<iframe '.format(x), rendered) self.assertIn('src="https://news.bbc.co.uk/{0}"'.format(x), rendered) self.assertIn('name="chunk-iframe-{0}"'.format(x), rendered)
def _test_changelist_display_methods(self, func, expected, extra): ct = get_content_type(User) user = User(username='******', is_staff=True, is_superuser=True, is_active=True) user.set_password('test') user.full_clean() user.save() iframe = Iframe(position=2, region='test', content_type=ct, content_id=user.pk, url='https://news.bbc.co.uk/') iframe.full_clean() iframe.save() render_html = getattr(self.admin, func) received = render_html(obj=iframe, **extra) for x in expected: self.assertIn(x, received)
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_render_changelists_for_object(self): request = RequestFactory().get('/') ct = get_content_type(User) user = User(username='******', is_staff=True, is_superuser=True, is_active=True) user.set_password('test') user.full_clean() user.save() iframe = Iframe(position=1, region='test', content_type=ct, content_id=user.pk, url='https://news.bbc.co.uk/') iframe.full_clean() iframe.save() received = self.admin.render_changelists_for_object(request=request, obj=user) self.assertIn('<div class="region-inline-wrapper"', received) self.assertIn('<h3>Embeds</h3>', received) self.assertIn('<div class="region-inline-progress-wrapper">', received)
def test_changelists_as_context_data(self): request = RequestFactory().get('/') ct = get_content_type(User) user = User(username='******', is_staff=True, is_superuser=True, is_active=True) user.set_password('test') user.full_clean() user.save() iframe = Iframe(position=1, region='test', content_type=ct, content_id=user.pk, url='https://news.bbc.co.uk/') iframe.full_clean() iframe.save() received = self.admin.changelists_as_context_data(request=request, obj=user) self.assertTrue('inline_admin_formset' in received) self.assertTrue('formset' in received['inline_admin_formset']) self.assertTrue('region_changelists' in received['inline_admin_formset']['formset']) # noqa self.assertEqual( 2, len(received['inline_admin_formset']['formset']['region_changelists'])) # noqa
def test_queryset(self): ct = get_content_type(User) user = User(username='******', is_staff=True, is_superuser=True, is_active=True) user.set_password('test') user.full_clean() user.save() data = [] for x in range(1, 10): iframe = Iframe(position=x, region='test', content_type=ct, content_id=user.pk, url='https://news.bbc.co.uk/') iframe.full_clean() iframe.save() data.append(iframe) try: self.assertEqual(data, list(self.admin.get_queryset('pk'))) except AttributeError: self.assertEqual(data, list(self.admin.queryset('pk')))
def test_inheritance_with_get_ancestors(self): user = User(username='******', is_staff=True, is_active=True, is_superuser=True) user.set_password('test') user.full_clean() user.save() parent_user = User(username='******', is_staff=True, is_active=True, is_superuser=True) parent_user.set_password('test') parent_user.full_clean() parent_user.save() for x in range(1, 11): iframe = Iframe(region='test', content_id=parent_user.pk, content_type=get_content_type(User), url='https://news.bbc.co.uk/{0!s}'.format(x), position=x) iframe.full_clean() iframe.save() user.get_ancestors = lambda: [parent_user] try: admin.site.unregister(User) except NotRegistered: pass admin.site.register(User, TestUserAdmin) request = RequestFactory().get('/') request.user = user ctx = RequestContext(request) ctx.update({'obj': user}) tmpl = Template(""" output: {% load editregion %} {% editregion "test" obj inherit %}fallback{% endeditregion %} """) rendered = tmpl.render(ctx).strip() for x in range(1, 11): self.assertIn('name="chunk-iframe-{0}" data-pk="{0}" ' 'data-position="{0}" data-region="{1}"'.format( x, 'test'), rendered)
def test_render_one_chunk(self): iframe = Iframe(region='test', content_id=1, content_type=self.ct, url='https://news.bbc.co.uk/', position=1) iframe.full_clean() iframe.pk = 1 request = RequestFactory().get('/') ctx = RequestContext(request) iterdata = chunk_iteration_context( index=0, value=iframe, iterable=[iframe]) ctx.update(iterdata) output = render_one_chunk(context=ctx, chunk=iframe, extra=iterdata['chunkloop'], renderer=None).strip() self.assertIn('<iframe ', output) self.assertIn('src="https://news.bbc.co.uk/"', output) self.assertIn('data-position="1"', output) self.assertIn('data-pk="1"', output) self.assertIn('data-region="test"', output) self.assertIn('</iframe>', output)
def test_render_all_chunks(self): objs = [] for x in range(0, 10): iframe = Iframe(region='test', content_id=1, content_type=self.ct, url='https://news.bbc.co.uk/{0!s}'.format(x), position=x) iframe.full_clean() iframe.pk = x objs.append(iframe) ctx = Context() chunks = render_all_chunks(context=ctx, found_chunks=objs) converted_chunks = list(chunks) self.assertEqual(10, len(converted_chunks)) for num, rendered_chunk in enumerate(converted_chunks): testable = rendered_chunk.output.strip() pos = num + 1 self.assertIn('src="https://news.bbc.co.uk/{0}"'.format(num), testable) self.assertIn('data-position="{0}"'.format(pos), testable) self.assertIn('data-region="test"', testable)
def test_get_safe_name_fallback(self): iframe = Iframe( position=2, region="test", content_type=self.content_type, content_id=1, url="https://news.bbc.co.uk/" ) iframe.full_clean() iframe.save() self.assertEqual(iframe.get_safe_name(), "chunk-iframe-1")
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_autoclose_chunkadmin(self): """ If `_autoclose` is in the URL, that + `_data_changed` should propagate to the next redirect URL for the purposes of our adminlinks JS. """ user = User(username='******', is_staff=True, is_superuser=True, is_active=True) user.set_password('test') user.full_clean() user.save() admin_instance = get_modeladmin(Iframe) self.assertIsInstance(admin_instance, RealishAdmin) request = RequestFactory().get('/', { '_autoclose': 1, }) request.user = user iframe_admin = reverse('admin:embeds_iframe_add') response_301 = HttpResponsePermanentRedirect(redirect_to=iframe_admin) 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() new_response = admin_instance.maybe_fix_redirection( request=request, response=response_301, obj=iframe) self.assertEqual(new_response['X-Chunkadmin-Response'], 'autoclose') self.assertEqual(301, new_response.status_code) location, querystring = new_response['Location'].split('?') self.assertEqual('/admin_mountpoint/embeds/iframe/add/', location) self.assertIn('region=test', querystring) self.assertIn('_data_changed=1', querystring) self.assertIn('_autoclose=1', querystring) self.assertIn('content_type={0}'.format(ct.pk), querystring) self.assertIn('content_id={0}'.format(iframe.pk), querystring)
def setUp(self): user = User(username="******") user.set_password("user") user.full_clean() user.save() ct = get_content_type(user) obj = EditRegionChunk(position=1, region="test", content_type=ct, content_id=user.pk) obj.full_clean() obj.save() iframe = Iframe(position=2, region="test", content_type=ct, content_id=user.pk, url="https://news.bbc.co.uk/") iframe.full_clean() iframe.save() try: admin.site.unregister(Iframe) except NotRegistered: pass admin.site.register(Iframe, IframeAdmin) self.base_obj = obj self.obj = iframe self.user = user self.user_content_type = user
def test_get_safe_name(self): iframe = Iframe( position=2, region="test", content_type=self.content_type, content_id=1, url="https://news.bbc.co.uk/", name="BBC News", ) iframe.full_clean() iframe.save() self.assertEqual(iframe.get_safe_name(), "bbc-news")