Skip to content

Commit fbed26f

Browse files
committed
smoke_test: Add "publish with invalid authentication" check
This should check a) if publishing without valid authentication fails and b) if HTTP error codes and messages are successfully received and shown by cargo.
1 parent dbdd2cd commit fbed26f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

crates/crates_io_smoke_test/src/cargo.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::exit_status_ext::ExitStatusExt;
22
use secrecy::{ExposeSecret, SecretString};
33
use std::path::Path;
4+
use std::process::Output;
45
use tokio::process::Command;
56

67
#[allow(unstable_name_collisions)]
@@ -43,3 +44,21 @@ pub async fn publish(project_path: &Path, token: &SecretString) -> anyhow::Resul
4344
.exit_ok()
4445
.map_err(Into::into)
4546
}
47+
48+
pub async fn publish_with_output(
49+
project_path: &Path,
50+
token: &SecretString,
51+
) -> anyhow::Result<Output> {
52+
Command::new("cargo")
53+
.args(["publish", "--registry", "staging"])
54+
.current_dir(project_path)
55+
.env("CARGO_TERM_COLOR", "always")
56+
.env(
57+
"CARGO_REGISTRIES_STAGING_INDEX",
58+
"https://github.com/rust-lang/staging.crates.io-index",
59+
)
60+
.env("CARGO_REGISTRIES_STAGING_TOKEN", token.expose_secret())
61+
.output()
62+
.await
63+
.map_err(Into::into)
64+
}

crates/crates_io_smoke_test/src/main.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod git;
77
extern crate tracing;
88

99
use crate::api::ApiClient;
10-
use anyhow::{anyhow, Context};
10+
use anyhow::{anyhow, bail, Context};
1111
use clap::Parser;
1212
use secrecy::SecretString;
1313
use std::path::{Path, PathBuf};
@@ -65,6 +65,20 @@ async fn main() -> anyhow::Result<()> {
6565
.await
6666
.context("Failed to create project")?;
6767

68+
info!("Checking publish with invalid authentication…");
69+
let invalid_token = "invalid-token".into();
70+
let output = cargo::publish_with_output(&project_path, &invalid_token).await?;
71+
if output.status.success() {
72+
bail!("Expected `cargo publish` to fail with invalid token");
73+
} else {
74+
let stderr = String::from_utf8_lossy(&output.stderr);
75+
if !stderr.contains("401 Unauthorized")
76+
|| !stderr.contains("The given API token does not match the format used by crates.io")
77+
{
78+
bail!("Expected `cargo publish` to fail with an `401 Unauthorized` error, but got: {stderr}");
79+
}
80+
}
81+
6882
if options.skip_publish {
6983
info!("Packaging crate file…");
7084
cargo::package(&project_path)

0 commit comments

Comments
 (0)