def test_register_admin_widget(self): """Testing register_admin_widget""" message = ( 'reviewboard.admin.widgets.register_admin_widget() is ' 'deprecated and will be removed in Review Board 5.0. Use ' 'reviewboard.admin.widgets.admin_widgets_registry.register() ' 'to register %r instead.' % MyLegacyWidget) with self.assertWarns(RemovedInReviewBoard50Warning, message): register_admin_widget(MyLegacyWidget) self.assertIn(MyLegacyWidget, admin_widgets_registry) self.assertIs(admin_widgets_registry.get_widget('my-legacy-widget'), MyLegacyWidget) admin_widgets_registry.unregister(MyLegacyWidget)
def __init__(self, extension, widget_cls, primary=False): super(AdminWidgetHook, self).__init__(extension) self.widget_cls = widget_cls register_admin_widget(widget_cls, primary)
def test_new_widget_render(self): """Testing that a new widget renders in the admin dashboard""" self.client.login(username='******', password='******') response = self.client.get('/admin/') self.assertEqual(response.status_code, 200) total_inital_widgets = ( len(response.context['selected_secondary_widgets']) + len(response.context['selected_primary_widgets'])) # Since admin/views.py widget_select() does not get run in testing, # we must do this instead to set up the data. profile = response.context['user'].get_profile() profile.extra_data.update({ 'primary_widget_selections': {widget.widget_id: '1' for widget in primary_widgets}, 'secondary_widget_selections': {widget.widget_id: '1' for widget in secondary_widgets}, 'primary_widget_positions': {widget.widget_id: i for i, widget in enumerate(primary_widgets)}, 'secondary_widget_positions': { widget.widget_id: i for i, widget in enumerate(secondary_widgets) }, }) profile.save(update_fields=('extra_data', )) class TestPrimaryWidget(Widget): widget_id = 'test-primary-widget' class TestSecondaryWidget(Widget): widget_id = 'test-secondary-widget' # If either new widget doesn't render correctly, the page will break. try: register_admin_widget(TestPrimaryWidget, True) register_admin_widget(TestSecondaryWidget) # We must also add TestPrimaryWidget to primary_widget_selections # and TestSecondaryWidget to secondary_widget_selections, so that # they are selected to display on the page, but have no position. primary_selections = ( profile.extra_data['primary_widget_selections']) secondary_selections = ( profile.extra_data['secondary_widget_selections']) primary_selections[TestPrimaryWidget.widget_id] = '1' secondary_selections[TestSecondaryWidget.widget_id] = '1' profile.save(update_fields=('extra_data', )) response = self.client.get('/admin/') self.assertEqual(response.status_code, 200) total_tested_widgets = ( len(response.context['selected_secondary_widgets']) + len(response.context['selected_primary_widgets'])) self.assertTrue(total_tested_widgets == total_inital_widgets + 2) self.assertIn(TestPrimaryWidget, response.context['selected_primary_widgets']) self.assertIn(TestSecondaryWidget, response.context['selected_secondary_widgets']) finally: # If an error was encountered above, the widgets will not be # registered. Ignore any errors in that case. try: unregister_admin_widget(TestPrimaryWidget) except KeyError: pass try: unregister_admin_widget(TestSecondaryWidget) except KeyError: pass
def test_new_widget_render(self): """Testing that a new widget renders in the admin dashboard""" self.client.login(username='******', password='******') response = self.client.get('/admin/') self.assertEqual(response.status_code, 200) total_inital_widgets = ( len(response.context['selected_secondary_widgets']) + len(response.context['selected_primary_widgets'])) # Since admin/views.py widget_select() does not get run in testing, # we must do this instead to set up the data. profile = response.context['user'].get_profile() profile.extra_data.update({ 'primary_widget_selections': { widget.widget_id: '1' for widget in primary_widgets }, 'secondary_widget_selections': { widget.widget_id: '1' for widget in secondary_widgets }, 'primary_widget_positions': { widget.widget_id: i for i, widget in enumerate(primary_widgets) }, 'secondary_widget_positions': { widget.widget_id: i for i, widget in enumerate(secondary_widgets) }, }) profile.save(update_fields=('extra_data',)) class TestPrimaryWidget(Widget): widget_id = 'test-primary-widget' class TestSecondaryWidget(Widget): widget_id = 'test-secondary-widget' # If either new widget doesn't render correctly, the page will break. try: register_admin_widget(TestPrimaryWidget, True) register_admin_widget(TestSecondaryWidget) # We must also add TestPrimaryWidget to primary_widget_selections # and TestSecondaryWidget to secondary_widget_selections, so that # they are selected to display on the page, but have no position. primary_selections = ( profile.extra_data['primary_widget_selections']) secondary_selections = ( profile.extra_data['secondary_widget_selections']) primary_selections[TestPrimaryWidget.widget_id] = '1' secondary_selections[TestSecondaryWidget.widget_id] = '1' profile.save(update_fields=('extra_data',)) response = self.client.get('/admin/') self.assertEqual(response.status_code, 200) total_tested_widgets = ( len(response.context['selected_secondary_widgets']) + len(response.context['selected_primary_widgets'])) self.assertTrue(total_tested_widgets == total_inital_widgets + 2) self.assertIn(TestPrimaryWidget, response.context['selected_primary_widgets']) self.assertIn(TestSecondaryWidget, response.context['selected_secondary_widgets']) finally: # If an error was encountered above, the widgets will not be # registered. Ignore any errors in that case. try: unregister_admin_widget(TestPrimaryWidget) except KeyError: pass try: unregister_admin_widget(TestSecondaryWidget) except KeyError: pass