def test_write_key__to_stdout(isatty_cleanup):
    """When sys.stdout is being piped/redicrected, print contents to it."""

    download.sys.stdout.isatty = mock.Mock(return_value=False)
    key = mock.Mock()
    download.write_key(key)
    key.get_contents_to_file.assert_called_once_with(download.sys.stdout)
def test_write_key__generate_url(flag, capfd, argv_cleanup):
    """Ensure that we print a url when asked."""

    key = mock.Mock()
    download.sys.argv = ["download", flag]
    download.write_key(key)
    key.generate_url.assert_called_once_with(300)  # 5 minute URLs
    out, err = capfd.readouterr()
    assert not err
    assert str(key.generate_url(300)) in out
def test_write_key__to_file(capfd, isatty_cleanup):
    """Verify the calls made to write a S3 key to file."""

    download.sys.stdout.isatty = mock.Mock(return_value=True)
    key = mock.Mock()
    key.name = "mock_pkg/mock_pkg-1.0.2-dev1.tar.gz"

    with mock.patch.object(builtins, "open") as open_patch:
        download.write_key(key)

    key.get_contents_to_file.assert_called_once_with(open_patch().__enter__())
    out, err = capfd.readouterr()
    assert not err
    assert "mock_pkg-1.0.2-dev1.tar.gz" in out