Пример #1
0
def test_template_map_key_values():
    """This test checks whether the keys holds the correct values"""
    with patch("h.notification.reply_template.Annotation") as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()
        annotation = store_fake_data[1]

        parent = rt.parent_values(annotation)
        tmap = rt.create_template_map(request, annotation, parent)

        parent = store_fake_data[0]

        # Document properties
        assert tmap["document_title"] == annotation["document"]["title"]
        assert tmap["document_path"] == parent["uri"]

        # Parent properties
        assert tmap["parent_text"] == parent["text"]
        assert tmap["parent_user"] == user_name(parent["user"])
        assert tmap["parent_user_profile"] == user_profile_url(request, parent["user"])
        assert tmap["parent_path"] == standalone_url(request, parent["id"])

        # Annotation properties
        assert tmap["reply_text"] == annotation["text"]
        assert tmap["reply_user"] == user_name(annotation["user"])
        assert tmap["reply_user_profile"] == user_profile_url(request, annotation["user"])
        assert tmap["reply_path"] == standalone_url(request, annotation["id"])

        assert tmap["parent_timestamp"] == "27 October 2013 at 19:40"
        assert tmap["reply_timestamp"] == "27 October 2014 at 19:50"

        assert tmap["unsubscribe"] == "UNSUBSCRIBE_URL"
Пример #2
0
def test_template_map_key_values():
    """This test checks whether the keys holds the correct values"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()
        annotation = store_fake_data[1]

        parent = rt.parent_values(annotation)
        tmap = rt.create_template_map(request, annotation, parent)

        parent = store_fake_data[0]

        # Document properties
        assert tmap['document_title'] == annotation['document']['title']
        assert tmap['document_path'] == parent['uri']

        # Parent properties
        assert tmap['parent_text'] == parent['text']
        assert tmap['parent_user'] == user_name(parent['user'])
        assert tmap['parent_user_profile'] == user_profile_url(request, parent['user'])
        assert tmap['parent_path'] == standalone_url(request, parent['id'])

        # Annotation properties
        assert tmap['reply_text'] == annotation['text']
        assert tmap['reply_user'] == user_name(annotation['user'])
        assert tmap['reply_user_profile'] == user_profile_url(request, annotation['user'])
        assert tmap['reply_path'] == standalone_url(request, annotation['id'])

        assert tmap['parent_timestamp'] == '27 October 2013 at 19:40'
        assert tmap['reply_timestamp'] == '27 October 2014 at 19:50'

        assert tmap['unsubscribe'] == 'UNSUBSCRIBE_URL'
Пример #3
0
def test_all_keys_are_there():
    """Checks for the existence of every needed key for the template"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = DummyRequest()
        request.domain = 'www.howtoreachtheark.now'
        annotation = store_fake_data[1]

        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {'id': 1}
        }
        tmap = rt.create_template_map(request, annotation, data)

        assert 'document_title' in tmap
        assert 'document_path' in tmap
        assert 'parent_text' in tmap
        assert 'parent_user' in tmap
        assert 'parent_timestamp' in tmap
        assert 'parent_user_profile' in tmap
        assert 'parent_path' in tmap
        assert 'reply_text' in tmap
        assert 'reply_user' in tmap
        assert 'reply_timestamp' in tmap
        assert 'reply_user_profile' in tmap
        assert 'reply_path' in tmap
        assert 'unsubscribe' in tmap
Пример #4
0
def test_all_keys_are_there():
    """Checks for the existence of every needed key for the template"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()
        annotation = store_fake_data[1]

        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {
                'id': 1
            }
        }
        tmap = rt.create_template_map(request, annotation, data)

        assert 'document_title' in tmap
        assert 'document_path' in tmap
        assert 'parent_text' in tmap
        assert 'parent_user' in tmap
        assert 'parent_timestamp' in tmap
        assert 'parent_user_profile' in tmap
        assert 'parent_path' in tmap
        assert 'reply_text' in tmap
        assert 'reply_user' in tmap
        assert 'reply_timestamp' in tmap
        assert 'reply_user_profile' in tmap
        assert 'reply_path' in tmap
        assert 'unsubscribe' in tmap
Пример #5
0
def test_parent_values_root_annotation():
    """Test if it gives back an empty dict for root annotations"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        annotation = store_fake_data[0]
        parent = rt.parent_values(annotation)

        assert len(parent.items()) == 0
Пример #6
0
def test_parent_values_reply():
    """Test if the function gives back the correct parent_value"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        annotation = store_fake_data[1]
        parent = rt.parent_values(annotation)

        assert parent['id'] == '0'
Пример #7
0
def test_parent_values_reply():
    """Test if the function gives back the correct parent_value"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        annotation = store_fake_data[1]
        parent = rt.parent_values(annotation)

        assert parent['id'] == '0'
Пример #8
0
def test_parent_values_root_annotation():
    """Test if it gives back an empty dict for root annotations"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        annotation = store_fake_data[0]
        parent = rt.parent_values(annotation)

        assert len(parent.items()) == 0
Пример #9
0
def test_parent_values_second_level_reply():
    """Test if it the grandparent values are filled for a second level reply"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        annotation = store_fake_data[2]
        parent = rt.parent_values(annotation)

        assert parent['id'] == '1'
        assert parent['quote'] == store_fake_data[0]['text']
Пример #10
0
def test_parent_values_second_level_reply():
    """Test if it the grandparent values are filled for a second level reply"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        annotation = store_fake_data[2]
        parent = rt.parent_values(annotation)

        assert parent['id'] == '1'
        assert parent['quote'] == store_fake_data[0]['text']
Пример #11
0
def test_dont_send_if_parent_is_missing():
    """Tests that this function returns False if the annotations parent's user is missing"""
    with patch("h.notification.reply_template.Annotation") as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)

        annotation = store_fake_data[3]
        data = {"parent": rt.parent_values(annotation), "subscription": {"id": 1}}

        send = rt.check_conditions(annotation, data)
        assert send is False
Пример #12
0
def test_fallback_title():
    """Checks that the title falls back to using the url"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()
        annotation = store_fake_data[4]

        parent = rt.parent_values(annotation)
        tmap = rt.create_template_map(request, annotation, parent)
        assert tmap['document_title'] == annotation['uri']
Пример #13
0
def test_good_conditions():
    """If conditions match, this function returns with a True value"""
    with patch("h.notification.reply_template.Annotation") as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)

        annotation = store_fake_data[1]
        data = {"parent": rt.parent_values(annotation), "subscription": {"id": 1, "uri": "acct:[email protected]"}}

        send = rt.check_conditions(annotation, data)
        assert send is True
Пример #14
0
def test_dont_send_to_the_same_user():
    """Tests that if the parent user and the annotation user is the same
    then this function returns False"""
    with patch("h.notification.reply_template.Annotation") as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)

        annotation = store_fake_data[0]
        data = {"parent": rt.parent_values(annotation), "subscription": {"id": 1}}

        send = rt.check_conditions(annotation, data)
        assert send is False
Пример #15
0
def test_unsubscribe_url_generation():
    """ensures that a serialized token is generated for the unsubscribe url"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()
        annotation = store_fake_data[4]

        parent = rt.parent_values(annotation)
        rt.create_template_map(request, annotation, parent)

        request.route_url.assert_called_with('unsubscribe', token='TOKEN')
Пример #16
0
def test_unsubscribe_token_generation():
    """ensures that a serialized token is generated for the unsubscribe url"""
    with patch("h.notification.reply_template.Annotation") as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()
        annotation = store_fake_data[4]

        parent = rt.parent_values(annotation)
        rt.create_template_map(request, annotation, parent)

        notification_serializer = request.registry.notification_serializer
        notification_serializer.dumps.assert_called_with({"type": REPLY_TYPE, "uri": parent["user"]})
Пример #17
0
def test_dont_send_if_parent_is_missing():
    """Tests that this function returns False if the annotations parent's user is missing"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)

        annotation = store_fake_data[3]
        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {'id': 1}
        }

        send = rt.check_conditions(annotation, data)
        assert send is False
Пример #18
0
def test_different_subscription():
    """If subscription.uri is different from user, do not send!"""
    with patch("h.notification.reply_template.Annotation") as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)

        annotation = store_fake_data[1]
        data = {
            "parent": rt.parent_values(annotation),
            "subscription": {"id": 1, "uri": "acct:[email protected]"},
        }

        send = rt.check_conditions(annotation, data)
        assert send is False
Пример #19
0
def test_dont_send_to_the_same_user():
    """Tests that if the parent user and the annotation user is the same
    then this function returns False"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)

        annotation = store_fake_data[0]
        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {'id': 1}
        }

        send = rt.check_conditions(annotation, data)
        assert send is False
Пример #20
0
def test_fallback_title():
    """Checks that the title falls back to using the url"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = DummyRequest()
        request.domain = 'www.howtoreachtheark.now'
        annotation = store_fake_data[4]

        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {'id': 1}
        }
        tmap = rt.create_template_map(request, annotation, data)
        assert tmap['document_title'] == annotation['uri']
Пример #21
0
def test_unsubscribe_token_generation():
    """ensures that a serialized token is generated for the unsubscribe url"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()
        annotation = store_fake_data[4]

        parent = rt.parent_values(annotation)
        rt.create_template_map(request, annotation, parent)

        notification_serializer = request.registry.notification_serializer
        notification_serializer.dumps.assert_called_with({
            'type': REPLY_TYPE,
            'uri': parent['user'],
        })
Пример #22
0
def test_get_email():
    """Tests whether it gives back the user.email property"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        with patch('h.notification.reply_template.get_user_by_name') as mock_user_db:
            user = Mock()
            user.email = '*****@*****.**'
            mock_user_db.return_value = user
            request = _create_request()

            annotation = store_fake_data[1]
            parent = rt.parent_values(annotation)

            email = rt.get_recipients(request, parent)
            assert email[0] == user.email
Пример #23
0
def test_good_conditions():
    """If conditions match, this function returns with a True value"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)

        annotation = store_fake_data[1]
        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {
                'id': 1,
                'uri': 'acct:[email protected]'
            }
        }

        send = rt.check_conditions(annotation, data)
        assert send is True
Пример #24
0
def test_good_conditions():
    """If conditions match, this function returns with a True value"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)

        annotation = store_fake_data[1]
        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {
                'id': 1,
                'uri': 'acct:[email protected]'
            }
        }

        send = rt.check_conditions(annotation, data)
        assert send is True
Пример #25
0
def test_different_subscription():
    """If subscription.uri is different from user, do not send!"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)

        annotation = store_fake_data[1]
        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {
                'id': 1,
                'uri': 'acct:[email protected]'
            }
        }

        send = rt.check_conditions(annotation, data)
        assert send is False
Пример #26
0
def test_different_subscription():
    """If subscription.uri is different from user, do not send!"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)

        annotation = store_fake_data[1]
        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {
                'id': 1,
                'uri': 'acct:[email protected]'
            }
        }

        send = rt.check_conditions(annotation, data)
        assert send is False
Пример #27
0
def test_dont_send_to_the_same_user():
    """Tests that if the parent user and the annotation user is the same
    then this function returns False"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = DummyRequest()
        request.domain = 'www.howtoreachtheark.now'

        annotation = store_fake_data[0]
        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {'id': 1}
        }

        send = rt.check_conditions(annotation, data)
        assert send is False
Пример #28
0
def test_no_email():
    """If user has no email we must throw an exception"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        with patch('h.notification.reply_template.get_user_by_name') as mock_user_db:
            mock_user_db.return_value = {}
            request = _create_request()

            annotation = store_fake_data[1]
            parent = rt.parent_values(annotation)

            exc = False
            try:
                rt.get_recipients(request, parent)
            except:
                exc = True
            assert exc
Пример #29
0
def test_template_map_key_values():
    """This test checks whether the keys holds the correct values"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = DummyRequest()
        request.domain = 'www.howtoreachtheark.now'
        annotation = store_fake_data[1]

        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {'id': 1}
        }
        tmap = rt.create_template_map(request, annotation, data)

        parent = store_fake_data[0]

        # Document properties
        assert tmap['document_title'] == annotation['document']['title']
        assert tmap['document_path'] == parent['uri']

        # Parent properties
        assert tmap['parent_text'] == parent['text']
        assert tmap['parent_user'] == user_name(parent['user'])
        assert tmap['parent_user_profile'] == user_profile_url(request, parent['user'])
        assert tmap['parent_path'] == standalone_url(request, parent['id'])

        # Annotation properties
        assert tmap['reply_text'] == annotation['text']
        assert tmap['reply_user'] == user_name(annotation['user'])
        assert tmap['reply_user_profile'] == user_profile_url(request, annotation['user'])
        assert tmap['reply_path'] == standalone_url(request, annotation['id'])

        # Timestamps
        date_format = '%Y-%m-%dT%H:%M:%S.%f'
        parent_timestamp = datetime.strptime(parent['created'][:-6], date_format)
        reply_timestamp = datetime.strptime(annotation['created'][:-6], date_format)

        assert tmap['parent_timestamp'] == parent_timestamp
        assert tmap['reply_timestamp'] == reply_timestamp

        # Unsubscribe link
        seq = ('http://', str(request.domain), '/app?__formid__=unsubscribe&subscription_id=', str(data['subscription']['id']))
        unsubscribe = "".join(seq)

        assert tmap['unsubscribe'] == unsubscribe
Пример #30
0
def test_unsubscribe_token_generation():
    """ensures that a serialized token is generated for the unsubscribe url"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()
        annotation = store_fake_data[4]

        data = {
            'parent': rt.parent_values(annotation),
            'subscription': {'id': 1}
        }
        rt.create_template_map(request, annotation, data)

        notification_serializer = request.registry.notification_serializer
        notification_serializer.dumps.assert_called_with({
            'type': REPLY_TYPE,
            'uri': data['parent']['user'],
        })
Пример #31
0
def test_get_email():
    """Tests whether it gives back the user.email property"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        with patch('h.notification.reply_template.get_user_by_name') as mock_user_db:
            user = Mock()
            user.email = '*****@*****.**'
            mock_user_db.return_value = user
            request = DummyRequest()
            request.domain = 'www.howtoreachtheark.now'

            annotation = store_fake_data[1]
            data = {
                'parent': rt.parent_values(annotation),
                'subscription': {'id': 1}
            }

            email = rt.get_recipients(request, data)
            assert email[0] == user.email
Пример #32
0
def test_no_email():
    """If user has no email we must throw an exception"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        with patch('h.notification.reply_template.get_user_by_name') as mock_user_db:
            mock_user_db.return_value = {}
            request = DummyRequest()
            request.domain = 'www.howtoreachtheark.now'

            annotation = store_fake_data[1]
            data = {
                'parent': rt.parent_values(annotation),
                'subscription': {'id': 1}
            }

            exc = False
            try:
                rt.get_recipients(request, data)
            except:
                exc = True
            assert exc
Пример #33
0
def test_all_keys_are_there():
    """Checks for the existence of every needed key for the template"""
    with patch('h.notification.reply_template.Annotation') as mock_annotation:
        mock_annotation.fetch = MagicMock(side_effect=fake_fetch)
        request = _create_request()
        annotation = store_fake_data[1]

        parent = rt.parent_values(annotation)
        tmap = rt.create_template_map(request, annotation, parent)

        assert 'document_title' in tmap
        assert 'document_path' in tmap
        assert 'parent_text' in tmap
        assert 'parent_user' in tmap
        assert 'parent_timestamp' in tmap
        assert 'parent_user_profile' in tmap
        assert 'parent_path' in tmap
        assert 'reply_text' in tmap
        assert 'reply_user' in tmap
        assert 'reply_timestamp' in tmap
        assert 'reply_user_profile' in tmap
        assert 'reply_path' in tmap
        assert 'unsubscribe' in tmap