def test_display_context_body_template(note, user, load_cfg):
    """Test display context for a notification with body template."""
    note.body_template = 'invalid-template.html'
    note.save()

    context = Notification.get_display_context(user)
    assert context['notifications'][0]['body'] == {
        'content': b'Template invalid-template.html does not exist.'
    }

    note.body_template = 'test-notification.html'
    note.save()

    context = Notification.get_display_context(user)
    context_note = context['notifications'][0]
    assert context_note['body'].content == b'Test notification body\n'
Exemple #2
0
def common(request):
    """Add additional context values to RequestContext for use in templates.

    Any resources referenced in the return value are expected to have been
    initialized or configured externally beforehand.
    """
    # Allow a value in configuration file to be translated.  Allow
    # the brand name 'FreedomBox' itself to be translated.
    gettext_noop('FreedomBox')

    from plinth.notification import Notification
    notifications_context = Notification.get_display_context(request,
                                                             user=request.user)

    slash_indices = [match.start() for match in re.finditer('/', request.path)]
    active_menu_urls = [request.path[:index + 1] for index in slash_indices]
    return {
        'cfg': cfg,
        'submenu': menu.main_menu.active_item(request),
        'active_menu_urls': active_menu_urls,
        'box_name': _(cfg.box_name),
        'user_is_admin': is_user_admin(request, True),
        'notifications': notifications_context['notifications'],
        'notifications_max_severity': notifications_context['max_severity']
    }
Exemple #3
0
def test_display_context(gettext, note, user, rf):
    """Test display context for a notification."""
    request = rf.get('/plinth/help/about/')

    data = {
        'test-key1': 'test-value1',
        'test-key2': 'translate:test-value2',
        'test-key3': {
            'test-key4': 'translate:test-value4',
            'test-key5': 'test-value5'
        }
    }
    expected_data = {
        'test-key1': 'test-value1',
        'test-key2': 'translated test-value2',
        'test-key3': {
            'test-key4': 'translated test-value4',
            'test-key5': 'test-value5'
        }
    }
    actions = [{'type': 'link', 'text': 'Test text', 'url': 'Test url'}]
    expected_actions = [{
        'type': 'link',
        'text': 'translated Test text',
        'url': 'Test url'
    }]
    gettext.side_effect = lambda string: 'translated ' + string

    note.severity = 'error'
    note.title = 'Test Title {test-key1}'
    note.message = 'Test message {test-key1}'
    note.actions = actions
    note.user = '******'
    note.group = 'test-group'
    note.data = data
    note.save()

    context = Notification.get_display_context(request, user)
    assert len(context['notifications']) == 1
    assert context['max_severity'] == 'error'

    context_note = context['notifications'][0]
    assert context_note['id'] == 'test-notification'
    assert context_note['app_id'] == 'test-app'
    assert context_note['severity'] == 'error'
    assert context_note['title'] == 'translated Test Title test-value1'
    assert context_note['message'] == 'translated Test message test-value1'
    assert context_note['body'] is None
    assert context_note['actions'] == expected_actions
    assert context_note['data'] == expected_data
    assert note.data == data
    now = datetime.datetime.now()
    assert (now - context_note['created_time']).seconds < 60
    assert (now - context_note['last_update_time']).seconds < 60
    assert context_note['user'] == 'test-user'
    assert context_note['group'] == 'test-group'
    assert not context_note['dismissed']
Exemple #4
0
def test_display_context_body_template(note, user, load_cfg, rf):
    """Test display context for a notification with body template."""
    request = rf.get('/plinth/help/about/')

    note.body_template = 'invalid-template.html'
    note.save()

    context = Notification.get_display_context(request, user)
    assert context['notifications'][0]['body'] == {
        'content': b'Template invalid-template.html does not exist.'
    }

    note.body_template = 'test-notification.html'
    note.save()

    context = Notification.get_display_context(request, user)
    context_note = context['notifications'][0]
    assert context_note['body'].content == \
        b'Test notification body /plinth/help/about/\n'