Пример #1
0
def test_get_environment(env, expected):
    if expected:
        env_obj = get_environment(env)

        assert env_obj.aws_profile == expected["aws_profile"]
        assert env_obj.bucket == expected["bucket"]
        assert env_obj.table == expected["table"]

    else:
        with pytest.raises(HTTPException) as exc_info:
            get_environment(env)

        assert exc_info.value.status_code == 404
        assert exc_info.value.detail == "Invalid environment='bad'"
Пример #2
0
def test_commit_publish_unresolved_links(mock_commit, fake_publish, db):
    """Ensure commit_publish raises for unresolved links."""

    # Add an item with link to a non-existent item.
    ln_item = Item(
        web_uri="/alternate/route/to/bad/path",
        object_key="",
        link_to="/bad/path",
        publish_id=fake_publish.id,
    )
    fake_publish.items.append(ln_item)

    db.add(fake_publish)
    db.commit()

    with pytest.raises(HTTPException) as exc_info:
        routers.publish.commit_publish(
            env=get_environment("test"),
            publish_id=fake_publish.id,
            db=db,
            settings=Settings(),
        )

    assert exc_info.value.status_code == 400
    assert (exc_info.value.detail ==
            "Unable to resolve item object_key:\n\tURI: '%s'\n\tLink: '%s'" %
            (ln_item.web_uri, ln_item.link_to))

    mock_commit.assert_not_called()
Пример #3
0
def test_sign_url_without_private_key():
    env = get_environment("test")

    with pytest.raises(HTTPException) as exc_info:
        cdn.sign_url("some/uri", 60, env)

    assert "Missing private key for CDN access" in str(exc_info)
Пример #4
0
def test_commit_publish_linked_items(mock_commit, fake_publish, db):
    """Ensure commit_publish correctly resolves links."""

    # Whole items
    item1 = Item(
        web_uri="/some/path",
        object_key="1" * 64,
        publish_id=fake_publish.id,
        link_to=None,  # It should be able to handle None/NULL link_to values...
    )
    item2 = Item(
        web_uri="/another/path",
        object_key="2" * 64,
        publish_id=fake_publish.id,
        link_to="",  # ...and empty string link_to values...
    )
    item3 = Item(
        web_uri="/some/different/path",
        object_key="3" * 64,
        publish_id=fake_publish.id,
    )
    # Linked items
    ln_item1 = Item(
        web_uri="/alternate/route/to/some/path",
        link_to="/some/path",
        publish_id=fake_publish.id,
    )
    ln_item2 = Item(
        web_uri="/alternate/route/to/another/path",
        link_to="/another/path",
        publish_id=fake_publish.id,
    )
    fake_publish.items.extend([item1, item2, item3, ln_item1, ln_item2])

    db.add(fake_publish)
    db.commit()

    publish_task = routers.publish.commit_publish(
        env=get_environment("test"),
        publish_id=fake_publish.id,
        db=db,
        settings=Settings(),
    )

    # Should've filled ln_item1's object_key with that of item1.
    assert ln_item1.object_key == "1" * 64
    # Should've filled ln_item2's object_key with that of item2.
    assert ln_item2.object_key == "2" * 64
    # Should've created and sent task.
    assert isinstance(publish_task, Task)

    mock_commit.assert_has_calls(calls=[
        mock.call.send(
            publish_id="123e4567-e89b-12d3-a456-426614174000",
            env="test",
            from_date=mock.ANY,
        )
    ], )
Пример #5
0
def test_sign_url_without_cdn_url(monkeypatch, dummy_private_key):
    monkeypatch.setenv("EXODUS_GW_CDN_PRIVATE_KEY_TEST", dummy_private_key)

    env = get_environment("test")
    env.cdn_url = None

    with pytest.raises(HTTPException) as exc_info:
        cdn.sign_url("some/uri", 60, env)

    assert "Missing cdn_url, nowhere to redirect request" in str(exc_info)
Пример #6
0
def test_sign_url_without_key_id(monkeypatch, dummy_private_key):
    monkeypatch.setenv("EXODUS_GW_CDN_PRIVATE_KEY_TEST", dummy_private_key)

    env = get_environment("test")
    env.cdn_key_id = None

    with pytest.raises(HTTPException) as exc_info:
        cdn.sign_url("some/uri", 60, env)

    assert "Missing key ID for CDN access" in str(exc_info)
Пример #7
0
def test_batch_write_item_limit(mock_boto3_client, fake_publish, caplog):
    items = fake_publish.items * 9
    env = get_environment("test")

    request = dynamodb.create_request(env.table, items, NOW_UTC)

    with pytest.raises(ValueError) as exc_info:
        dynamodb.batch_write(env, request)

    assert "Cannot process more than 25 items per request" in caplog.text
    assert str(exc_info.value) == "Request contains too many items (27)"
Пример #8
0
def test_sign_url_with_query(monkeypatch, dummy_private_key):
    monkeypatch.setenv("EXODUS_GW_CDN_PRIVATE_KEY_TEST", dummy_private_key)

    env = get_environment("test")

    # URL-parameter separator should be "&" when a query string is given.
    signed_url = cdn.sign_url("?cdest=some-file&ckey=a1bc3d4", 60, env)
    expected_url = (
        "https://test.cloudfront.net/?cdest=some-file&ckey=a1bc3d4"
        "&Expires=1644969660"
        "&Signature=Me7zBg4~P-0i3R46S9zW1QkRDNSpAxVXa1tjWThcaAOO262UJV"
        "YQoszT5j4~p~cozcVK3qahaFe9~~lW5ODaGLLR6NXflaKzLDiDF9ITfmn8V8S"
        "yGEhYynPZ7aFjxgPZalIWBu0nBMTn~MZwWyOraQVLIgJEpjfdNrGD7908V6I_"
        "&Key-Pair-Id=XXXXXXXXXXXXXX")

    assert signed_url == expected_url
Пример #9
0
def test_batch_write(mock_boto3_client, fake_publish, delete,
                     expected_request):
    env = get_environment("test")

    request = dynamodb.create_request(env.table,
                                      fake_publish.items,
                                      NOW_UTC,
                                      delete=delete)

    # Represent successful write/delete of all items to the table.
    mock_boto3_client.batch_write_item.return_value = {"UnprocessedItems": {}}

    dynamodb.batch_write(env, request)

    # Should've requested write of all items.
    mock_boto3_client.batch_write_item.assert_called_once_with(
        RequestItems=expected_request)
Пример #10
0
def test_commit_publish_prev_completed(mock_commit, fake_publish, db):
    """Ensure commit_publish fails for publishes in invalid state."""

    db.add(fake_publish)
    # Simulate that this publish was published.
    fake_publish.state = schemas.PublishStates.committed
    db.commit()

    with pytest.raises(HTTPException) as exc_info:
        routers.publish.commit_publish(
            env=get_environment("test"),
            publish_id=fake_publish.id,
            db=db,
            settings=Settings(),
        )

    assert exc_info.value.status_code == 409
    assert (exc_info.value.detail ==
            "Publish %s in unexpected state, 'COMMITTED'" % fake_publish.id)

    mock_commit.assert_not_called()
Пример #11
0
def test_commit_publish(mock_commit, fake_publish, db):
    """Ensure commit_publish delegates to worker correctly and creates task."""

    db.add(fake_publish)
    db.commit()

    publish_task = routers.publish.commit_publish(
        env=get_environment("test"),
        publish_id=fake_publish.id,
        db=db,
        settings=Settings(),
    )

    assert isinstance(publish_task, Task)

    mock_commit.assert_has_calls(calls=[
        mock.call.send(
            publish_id="123e4567-e89b-12d3-a456-426614174000",
            env="test",
            from_date=mock.ANY,
        )
    ], )