Skip to content

Require a verified email after 2019-03-01 00:00 UTC #1629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 102 additions & 7 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use std::collections::HashMap;
use std::sync::Arc;

use chrono::offset::TimeZone;
use chrono::{DateTime, Utc};
use hex::ToHex;

use crate::git;
Expand Down Expand Up @@ -67,13 +69,12 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {

let mut other_warnings = vec![];
let verified_email_address = user.verified_email(&conn)?;
if verified_email_address.is_none() {
other_warnings.push(String::from(
"You do not currently have a verified email address associated with your crates.io \
account. Starting 2019-02-28, a verified email will be required to publish crates. \
Visit https://crates.io/me to set and verify your email address.",
));
}

// This function can be inlined (with only the error-returning functionality) and its unit
// tests deleted after 2019-02-28; it was created to make injecting the date for tests easier.
// The integration tests in src/tests/krate.rs cover the current production behavior (and will
// need to be updated at that time)
verified_email_check(&mut other_warnings, &verified_email_address, Utc::now())?;

// Create a transaction on the database, if there are no errors,
// commit the transactions to record a new or updated crate.
Expand Down Expand Up @@ -267,3 +268,97 @@ fn parse_new_headers(req: &mut dyn Request) -> CargoResult<(EncodableCrateUpload
let user = req.user()?;
Ok((new, user.clone()))
}

fn verified_email_check(
other_warnings: &mut Vec<String>,
verified_email_address: &Option<String>,
now: DateTime<Utc>,
) -> CargoResult<()> {
match verified_email_address {
Some(_) => Ok(()),
None => {
if now < Utc.ymd(2019, 3, 1).and_hms(0, 0, 0) {
other_warnings.push(String::from(
"You do not currently have a verified email address associated with your \
crates.io account. Starting 2019-02-28, a verified email will be required to \
publish crates. Visit https://crates.io/me to set and verify your email \
address.",
));
Ok(())
} else {
Err(human(
"A verified email address is required to publish crates to crates.io. \
Visit https://crates.io/me to set and verify your email address.",
))
}
}
}
}

// These tests should be deleted after 2018-02-28; this functionality will then be covered by
// integration tests in src/tests/krate.rs.
#[cfg(test)]
mod tests {
use super::*;
use chrono::offset::TimeZone;

#[test]
fn allow_publish_with_verified_email_without_warning_before_2018_02_28() {
let mut warnings = vec![];

let fake_current_date = Utc.ymd(2019, 2, 27).and_hms(0, 0, 0);
let result = verified_email_check(
&mut warnings,
&Some("[email protected]".into()),
fake_current_date,
);

assert!(result.is_ok());
assert_eq!(warnings.len(), 0);
}

#[test]
fn allow_publish_with_verified_email_without_error_after_2018_02_28() {
let mut warnings = vec![];

let fake_current_date = Utc.ymd(2019, 3, 1).and_hms(0, 0, 0);
let result = verified_email_check(
&mut warnings,
&Some("[email protected]".into()),
fake_current_date,
);

assert!(result.is_ok());
assert_eq!(warnings.len(), 0);
}

#[test]
fn warn_without_verified_email_before_2018_02_28() {
let mut warnings = vec![];

let fake_current_date = Utc.ymd(2019, 2, 27).and_hms(0, 0, 0);
let result = verified_email_check(&mut warnings, &None, fake_current_date);

assert!(result.is_ok());
assert_eq!(warnings.len(), 1);
assert_eq!(warnings[0], "You do not currently have a verified email address associated \
with your crates.io account. Starting 2019-02-28, a verified email will be required to \
publish crates. Visit https://crates.io/me to set and verify your email address.");
}

#[test]
fn error_without_verified_email_after_2018_02_28() {
let mut warnings = vec![];

let fake_current_date = Utc.ymd(2019, 3, 1).and_hms(0, 0, 0);
let result = verified_email_check(&mut warnings, &None, fake_current_date);

assert!(result.is_err());
assert_eq!(
result.err().unwrap().description(),
"A verified email address is required to \
publish crates to crates.io. Visit https://crates.io/me to set and verify your email \
address."
);
}
}
4 changes: 2 additions & 2 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ fn new_krate_with_readme() {
assert_eq!(json.krate.max_version, "1.0.0");
}

// This warning will soon become a hard error.
// This test will start failing on 2019-02-28 and should be updated at that time.
// See https://github.com/rust-lang/crates-io-cargo-teams/issues/8
#[test]
fn new_krate_without_any_email_warns() {
Expand All @@ -1096,7 +1096,7 @@ fn new_krate_without_any_email_warns() {
});
}

// This warning will soon become a hard error.
// This test will start failing on 2019-02-28 and should be updated at that time.
// See https://github.com/rust-lang/crates-io-cargo-teams/issues/8
#[test]
fn new_krate_with_unverified_email_warns() {
Expand Down