Beispiel #1
0
def test_get_s3_key(root_doc):
    locale, slug = root_doc.locale, root_doc.slug
    expected_key = 'api/v1/doc/{}/{}'.format(locale, slug)
    assert (get_s3_key(root_doc) == get_s3_key(locale=locale, slug=slug) ==
            expected_key)
    assert (get_s3_key(root_doc, for_redirect=True) == get_s3_key(
        locale=locale, slug=slug, for_redirect=True) == '/' + expected_key)
Beispiel #2
0
def test_get_s3_key(root_doc):
    locale, slug = root_doc.locale, root_doc.slug
    expected_key = 'api/v1/doc/{}/{}'.format(locale, slug)
    assert (get_s3_key(root_doc) == get_s3_key(locale=locale, slug=slug) ==
            expected_key)
    assert (get_s3_key(root_doc, for_redirect=True) ==
            get_s3_key(locale=locale, slug=slug, for_redirect=True) ==
            '/' + expected_key)
Beispiel #3
0
def test_get_s3_key(root_doc):
    locale, slug = root_doc.locale, root_doc.slug
    expected_key = "api/v1/doc/{}/{}.json".format(locale, slug)
    assert get_s3_key(root_doc) == get_s3_key(locale=locale,
                                              slug=slug) == expected_key
    assert (get_s3_key(root_doc, prefix_with_forward_slash=True) == get_s3_key(
        locale=locale, slug=slug, prefix_with_forward_slash=True) ==
            "/" + expected_key)
Beispiel #4
0
def test_publish_multiple(get_s3_bucket_mock, root_doc, redirect_doc,
                          redirect_to_home, trans_doc):
    """
    Test the publish task for multiple documents of various kinds, including
    standard documents and redirects.
    """
    trans_doc.delete()
    log_mock = mock.Mock()
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    publish(
        [trans_doc.pk, root_doc.pk, redirect_doc.pk, redirect_to_home.pk],
        log=log_mock,
        completion_message="Done!",
    )
    s3_bucket_mock.put_object.assert_has_calls([
        mock.call(
            ACL="public-read",
            Key=get_s3_key(root_doc),
            Body=json.dumps(document_api_data(root_doc)),
            ContentType="application/json",
            ContentLanguage=root_doc.locale,
        ),
        mock.call(
            ACL="public-read",
            Key=get_s3_key(redirect_doc),
            WebsiteRedirectLocation=get_s3_key(
                root_doc,
                prefix_with_forward_slash=True,
                suffix_file_extension=False,
            ),
            ContentType="application/json",
            ContentLanguage=redirect_doc.locale,
            Body=json.dumps(
                document_api_data(
                    redirect_url=get_content_based_redirect(redirect_doc)[0])),
        ),
        mock.call(
            ACL="public-read",
            Key=get_s3_key(redirect_to_home),
            Body=json.dumps(document_api_data(redirect_url="/en-US/")),
            ContentType="application/json",
            ContentLanguage=redirect_to_home.locale,
        ),
    ])
    log_mock.error.assert_called_once_with(
        "Document with pk={} does not exist".format(trans_doc.pk))
    log_mock.info.assert_has_calls([
        mock.call("Published S3 Object #1"),
        mock.call("Published S3 Object #2"),
        mock.call("Published S3 Object #3"),
        mock.call("Done!"),
    ])
Beispiel #5
0
def test_get_content_based_redirect(root_doc, redirect_doc, redirect_to_self,
                                    redirect_to_home, redirect_to_macros,
                                    case):
    if case == "normal":
        doc = root_doc
        expected = None
    elif case == "redirect":
        doc = redirect_doc
        expected = (
            get_s3_key(root_doc,
                       prefix_with_forward_slash=True,
                       suffix_file_extension=False),
            True,
        )
    elif case == "redirect-to-self":
        doc = redirect_to_self
        expected = None
    elif case == "redirect-to-home":
        doc = redirect_to_home
        expected = ("/en-US/", False)
    else:
        doc = redirect_to_macros
        expected = (absolutify("/en-US/dashboards/macros",
                               for_wiki_site=True), False)
    assert get_content_based_redirect(doc) == expected
Beispiel #6
0
def test_publish_redirect(get_s3_bucket_mock, root_doc, redirect_doc):
    """
    Test the publish task for a document that redirects to another document
    within the S3 bucket.
    """
    log_mock = mock.Mock()
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    publish([redirect_doc.pk], log=log_mock)
    s3_bucket_mock.put_object.assert_called_once_with(
        ACL='public-read',
        Key=get_s3_key(redirect_doc),
        WebsiteRedirectLocation=get_s3_key(root_doc, for_redirect=True),
        ContentType='application/json',
        ContentLanguage=redirect_doc.locale
    )
    log_mock.info.assert_called_once_with('Published S3 Object #1')
Beispiel #7
0
def test_unpublish_multiple_chunked(get_s3_bucket_mock, root_doc, redirect_doc,
                                    redirect_to_home):
    """
    Test the unpublish task for multiple documents where the deletes are
    broken-up into chunks.
    """
    log_mock = mock.Mock()
    docs = (root_doc, redirect_doc, redirect_to_home)
    doc_locale_slug_pairs = [(doc.locale, doc.slug) for doc in docs]
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    unpublish(doc_locale_slug_pairs, log=log_mock, completion_message='Done!')
    s3_keys = tuple(get_s3_key(doc) for doc in docs)
    s3_bucket_mock.delete_objects.assert_has_calls([
        mock.call(Delete={'Objects': [{
            'Key': key
        } for key in s3_keys[:2]]}),
        mock.call(Delete={'Objects': [{
            'Key': key
        } for key in s3_keys[2:]]})
    ])
    log_mock.error.assert_has_calls([
        mock.call(
            'Unable to unpublish {}: (InternalError) Some error'.format(key))
        for key in s3_keys[:2]
    ])
    log_mock.info.assert_has_calls(
        [mock.call('Unpublished {}'.format(s3_keys[-1])),
         mock.call('Done!')])
Beispiel #8
0
def test_unpublish_multiple(get_s3_bucket_mock, root_doc, redirect_doc,
                            redirect_to_home):
    """
    Test the unpublish task for multiple documents of various kinds, including
    standard documents and redirects.
    """
    log_mock = mock.Mock()
    docs = (root_doc, redirect_doc, redirect_to_home)
    doc_locale_slug_pairs = [(doc.locale, doc.slug) for doc in docs]
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    unpublish(doc_locale_slug_pairs, log=log_mock, completion_message='Done!')
    s3_keys = tuple(get_s3_key(doc) for doc in docs)
    s3_bucket_mock.delete_objects.assert_called_once_with(
        Delete={
            'Objects': [
                {
                    'Key': key
                }
                for key in s3_keys
            ]
        }
    )
    log_mock.error.assert_called_once_with(
        'Unable to unpublish {}: (InternalError) Some error'.format(s3_keys[0])
    )
    log_mock.info.assert_has_calls(
        [mock.call('Unpublished {}'.format(key)) for key in s3_keys[1:]] +
        [mock.call('Done!')]
    )
Beispiel #9
0
def test_publish_multiple(get_s3_bucket_mock, root_doc, redirect_doc,
                          redirect_to_home, trans_doc):
    """
    Test the publish task for multiple documents of various kinds, including
    standard documents and redirects.
    """
    trans_doc.delete()
    log_mock = mock.Mock()
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    publish([trans_doc.pk, root_doc.pk, redirect_doc.pk, redirect_to_home.pk],
            log=log_mock, completion_message='Done!')
    s3_bucket_mock.put_object.assert_has_calls([
        mock.call(
            ACL='public-read',
            Key=get_s3_key(root_doc),
            Body=json.dumps(
                document_api_data(root_doc, ensure_contributors=True)),
            ContentType='application/json',
            ContentLanguage=root_doc.locale
        ),
        mock.call(
            ACL='public-read',
            Key=get_s3_key(redirect_doc),
            WebsiteRedirectLocation=get_s3_key(root_doc, for_redirect=True),
            ContentType='application/json',
            ContentLanguage=redirect_doc.locale
        ),
        mock.call(
            ACL='public-read',
            Key=get_s3_key(redirect_to_home),
            Body=json.dumps(document_api_data(redirect_url='/en-US/')),
            ContentType='application/json',
            ContentLanguage=redirect_to_home.locale
        ),
    ])
    log_mock.error.assert_called_once_with(
        'Document with pk={} does not exist'.format(trans_doc.pk))
    log_mock.info.assert_has_calls([
        mock.call('Published S3 Object #1'),
        mock.call('Published S3 Object #2'),
        mock.call('Published S3 Object #3'),
        mock.call('Done!'),
    ])
Beispiel #10
0
def test_publish_redirect(get_s3_bucket_mock, root_doc, redirect_doc):
    """
    Test the publish task for a document that redirects to another document
    within the S3 bucket.
    """
    log_mock = mock.Mock()
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    publish([redirect_doc.pk], log=log_mock)

    s3_bucket_mock.put_object.assert_called_once_with(
        ACL='public-read',
        Key=get_s3_key(redirect_doc),
        WebsiteRedirectLocation=get_s3_key(
            root_doc, prefix_with_forward_slash=True, suffix_file_extension=False),
        ContentType='application/json',
        ContentLanguage=redirect_doc.locale,
        Body=json.dumps(document_api_data(None, redirect_url=get_content_based_redirect(redirect_doc)[0]))
    )
    log_mock.info.assert_called_once_with('Published S3 Object #1')
Beispiel #11
0
def test_publish_standard(get_s3_bucket_mock, root_doc):
    """Test the publish task for a standard (non-redirect) document."""
    log_mock = mock.Mock()
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    publish.get_logger = mock.Mock(return_value=log_mock)
    publish([root_doc.pk])
    s3_bucket_mock.put_object.assert_called_once_with(
        ACL='public-read',
        Key=get_s3_key(root_doc),
        Body=json.dumps(document_api_data(root_doc, ensure_contributors=True)),
        ContentType='application/json',
        ContentLanguage=root_doc.locale
    )
    log_mock.info.assert_called_once_with('Published S3 Object #1')
Beispiel #12
0
def test_publish_redirect_to_home(get_s3_bucket_mock, redirect_to_home):
    """
    Test the publish task for a document that redirects to a URL outside the
    S3 bucket, in this case the home page.
    """
    log_mock = mock.Mock()
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    publish([redirect_to_home.pk], log=log_mock)
    s3_bucket_mock.put_object.assert_called_once_with(
        ACL='public-read',
        Key=get_s3_key(redirect_to_home),
        Body=json.dumps(document_api_data(redirect_url='/en-US/')),
        ContentType='application/json',
        ContentLanguage=redirect_to_home.locale)
    log_mock.info.assert_called_once_with('Published S3 Object #1')
Beispiel #13
0
def test_publish_redirect_to_home(get_s3_bucket_mock, redirect_to_home):
    """
    Test the publish task for a document that redirects to a URL outside the
    S3 bucket, in this case the home page.
    """
    log_mock = mock.Mock()
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    publish([redirect_to_home.pk], log=log_mock)
    s3_bucket_mock.put_object.assert_called_once_with(
        ACL='public-read',
        Key=get_s3_key(redirect_to_home),
        Body=json.dumps(document_api_data(redirect_url='/en-US/')),
        ContentType='application/json',
        ContentLanguage=redirect_to_home.locale
    )
    log_mock.info.assert_called_once_with('Published S3 Object #1')
Beispiel #14
0
def test_publish_redirect_to_other(get_s3_bucket_mock, redirect_to_macros):
    """
    Test the publish task for a document that redirects to a URL outside the
    S3 bucket, in this case someting other than the home page.
    """
    log_mock = mock.Mock()
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    publish([redirect_to_macros.pk], log=log_mock)
    s3_bucket_mock.put_object.assert_called_once_with(
        ACL='public-read',
        Key=get_s3_key(redirect_to_macros),
        Body=json.dumps(
            document_api_data(redirect_url=absolutify(
                '/en-US/dashboards/macros', for_wiki_site=True))),
        ContentType='application/json',
        ContentLanguage=redirect_to_macros.locale)
    log_mock.info.assert_called_once_with('Published S3 Object #1')
Beispiel #15
0
def test_publish_redirect_to_other(get_s3_bucket_mock, redirect_to_macros):
    """
    Test the publish task for a document that redirects to a URL outside the
    S3 bucket, in this case someting other than the home page.
    """
    log_mock = mock.Mock()
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    publish([redirect_to_macros.pk], log=log_mock)
    s3_bucket_mock.put_object.assert_called_once_with(
        ACL='public-read',
        Key=get_s3_key(redirect_to_macros),
        Body=json.dumps(document_api_data(
            redirect_url=absolutify('/en-US/dashboards/macros',
                                    for_wiki_site=True))),
        ContentType='application/json',
        ContentLanguage=redirect_to_macros.locale
    )
    log_mock.info.assert_called_once_with('Published S3 Object #1')
Beispiel #16
0
def test_unpublish_multiple_chunked(get_s3_bucket_mock, root_doc, redirect_doc,
                                    redirect_to_home):
    """
    Test the unpublish task for multiple documents where the deletes are
    broken-up into chunks.
    """
    log_mock = mock.Mock()
    docs = (root_doc, redirect_doc, redirect_to_home)
    doc_locale_slug_pairs = [(doc.locale, doc.slug) for doc in docs]
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    unpublish(doc_locale_slug_pairs, log=log_mock, completion_message='Done!')
    s3_keys = tuple(get_s3_key(doc) for doc in docs)
    s3_bucket_mock.delete_objects.assert_has_calls([
        mock.call(
            Delete={
                'Objects': [
                    {
                        'Key': key
                    }
                    for key in s3_keys[:2]
                ]
            }
        ),
        mock.call(
            Delete={
                'Objects': [
                    {
                        'Key': key
                    }
                    for key in s3_keys[2:]
                ]
            }
        )
    ])
    log_mock.error.assert_has_calls([
        mock.call(
            'Unable to unpublish {}: (InternalError) Some error'.format(key))
        for key in s3_keys[:2]
    ])
    log_mock.info.assert_has_calls([
        mock.call('Unpublished {}'.format(s3_keys[-1])),
        mock.call('Done!')
    ])
Beispiel #17
0
def test_get_content_based_redirect(root_doc, redirect_doc, redirect_to_self,
                                    redirect_to_home, redirect_to_macros, case):
    if case == 'normal':
        doc = root_doc
        expected = None
    elif case == 'redirect':
        doc = redirect_doc
        expected = (get_s3_key(root_doc, for_redirect=True), True)
    elif case == 'redirect-to-self':
        doc = redirect_to_self
        expected = None
    elif case == 'redirect-to-home':
        doc = redirect_to_home
        expected = ('/en-US/', False)
    else:
        doc = redirect_to_macros
        expected = (
            absolutify('/en-US/dashboards/macros', for_wiki_site=True), False)
    assert get_content_based_redirect(doc) == expected
Beispiel #18
0
def test_publish_standard(get_s3_bucket_mock, root_doc, invalidate_cdn_cache):
    """Test the publish task for a standard (non-redirect) document."""
    log_mock = mock.Mock()
    get_s3_bucket_mock.return_value = s3_bucket_mock = get_mocked_s3_bucket()
    publish.get_logger = mock.Mock(return_value=log_mock)
    with mock.patch('kuma.api.tasks.request_cdn_cache_invalidation') as mocked:
        publish([root_doc.pk], invalidate_cdn_cache=invalidate_cdn_cache)
        if invalidate_cdn_cache:
            mocked.delay.assert_called_once_with([(root_doc.locale,
                                                   root_doc.slug)])
        else:
            mocked.delay.assert_not_called()
    s3_bucket_mock.put_object.assert_called_once_with(
        ACL='public-read',
        Key=get_s3_key(root_doc),
        Body=json.dumps(document_api_data(root_doc)),
        ContentType='application/json',
        ContentLanguage=root_doc.locale)
    log_mock.info.assert_called_once_with('Published S3 Object #1')
Beispiel #19
0
def test_get_content_based_redirect(root_doc, redirect_doc, redirect_to_self,
                                    redirect_to_home, redirect_to_macros, case):
    if case == 'normal':
        doc = root_doc
        expected = None
    elif case == 'redirect':
        doc = redirect_doc
        expected = (get_s3_key(root_doc, prefix_with_forward_slash=True), True)
    elif case == 'redirect-to-self':
        doc = redirect_to_self
        expected = None
    elif case == 'redirect-to-home':
        doc = redirect_to_home
        expected = ('/en-US/', False)
    else:
        doc = redirect_to_macros
        expected = (
            absolutify('/en-US/dashboards/macros', for_wiki_site=True), False)
    assert get_content_based_redirect(doc) == expected
Beispiel #20
0
def test_unpublish(get_s3_bucket_mock, root_doc, case):
    """Test the unpublish task for a single document."""
    if case in ('deleted', 'purged'):
        root_doc.deleted = True
        root_doc.save()
        if case == 'purged':
            root_doc.purge()
    log_mock = mock.Mock()
    s3_bucket_mock = get_mocked_s3_bucket()
    get_s3_bucket_mock.return_value = s3_bucket_mock
    unpublish.get_logger = mock.Mock(return_value=log_mock)
    unpublish([(root_doc.locale, root_doc.slug)])
    s3_key = get_s3_key(root_doc)
    s3_bucket_mock.delete_objects.assert_called_once_with(
        Delete={
            'Objects': [
                {
                    'Key': s3_key
                }
            ]
        }
    )
    log_mock.info.assert_called_once_with('Unpublished {}'.format(s3_key))
Beispiel #21
0
        root_doc.save()
        if case == 'purged':
            root_doc.purge()
    log_mock = mock.Mock()
    s3_bucket_mock = get_mocked_s3_bucket()
    get_s3_bucket_mock.return_value = s3_bucket_mock
    unpublish.get_logger = mock.Mock(return_value=log_mock)
    with mock.patch('kuma.api.tasks.request_cdn_cache_invalidation') as mocked:
        unpublish([(root_doc.locale, root_doc.slug)],
                  invalidate_cdn_cache=invalidate_cdn_cache)
        if invalidate_cdn_cache:
            mocked.delay.assert_called_once_with([(root_doc.locale,
                                                   root_doc.slug)])
        else:
            mocked.delay.assert_not_called()
    s3_key = get_s3_key(root_doc)
    s3_bucket_mock.delete_objects.assert_called_once_with(
        Delete={'Objects': [{
            'Key': s3_key
        }]})
    log_mock.info.assert_called_once_with('Unpublished {}'.format(s3_key))


@mock.patch('kuma.api.tasks.get_s3_bucket')
def test_unpublish_multiple(get_s3_bucket_mock, root_doc, redirect_doc,
                            redirect_to_home):
    """
    Test the unpublish task for multiple documents of various kinds, including
    standard documents and redirects.
    """
    log_mock = mock.Mock()