Skip to content

Commit

Permalink
Record the verified email of the version publisher
Browse files Browse the repository at this point in the history
If there is one, because we're still in the optional stage, but we can
start recording them now if possible.
  • Loading branch information
carols10cents committed Feb 10, 2019
1 parent 8cfa142 commit 623cfe7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/bin/update-downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ mod test {
None,
0,
user_id,
Some("someone@example.com".into()),
)
.unwrap();
let version = version.save(conn, &[]).unwrap();
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
let conn = app.diesel_database.get()?;

let mut other_warnings = vec![];
if !user.has_verified_email(&conn)? {
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. \
Expand Down Expand Up @@ -147,6 +148,7 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
// to get here, and max upload sizes are way less than i32 max
file_length as i32,
user.id,
verified_email_address,
)?
.save(&conn, &new_crate.authors)?;

Expand Down
17 changes: 7 additions & 10 deletions src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::borrow::Cow;
use crate::app::App;
use crate::util::CargoResult;

use crate::models::{Crate, CrateOwner, NewEmail, Owner, OwnerKind, Rights};
use crate::models::{Crate, CrateOwner, Email, NewEmail, Owner, OwnerKind, Rights};
use crate::schema::{crate_owners, emails, users};
use crate::views::{EncodablePrivateUser, EncodablePublicUser};

Expand Down Expand Up @@ -162,15 +162,12 @@ impl User {
Ok(best)
}

pub fn has_verified_email(&self, conn: &PgConnection) -> CargoResult<bool> {
use diesel::dsl::exists;
let email_exists = diesel::select(exists(
emails::table
.filter(emails::user_id.eq(self.id))
.filter(emails::verified.eq(true)),
))
.get_result(&*conn)?;
Ok(email_exists)
pub fn verified_email(&self, conn: &PgConnection) -> CargoResult<Option<String>> {
Ok(Email::belonging_to(self)
.select(emails::email)
.filter(emails::verified.eq(true))
.first::<String>(&*conn)
.optional()?)
}

/// Converts this `User` model into an `EncodablePrivateUser` for JSON serialization.
Expand Down
5 changes: 5 additions & 0 deletions src/models/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct NewVersion {
license: Option<String>,
crate_size: Option<i32>,
published_by: i32,
published_by_email: Option<String>,
}

impl Version {
Expand Down Expand Up @@ -160,6 +161,7 @@ impl Version {

impl NewVersion {
#[allow(clippy::new_ret_no_self)]
#[allow(clippy::too_many_arguments)]
pub fn new(
crate_id: i32,
num: &semver::Version,
Expand All @@ -168,6 +170,7 @@ impl NewVersion {
license_file: Option<&str>,
crate_size: i32,
published_by: i32,
verified_email_address: Option<String>, // TODO: change to just `String` after 2019-02-28
) -> CargoResult<Self> {
let features = serde_json::to_value(features)?;

Expand All @@ -178,6 +181,8 @@ impl NewVersion {
license,
crate_size: Some(crate_size),
published_by,
// TODO: wrap `String` in `Some` after 2019-02-28
published_by_email: verified_email_address,
};

new_version.validate_license(license_file)?;
Expand Down
1 change: 1 addition & 0 deletions src/tests/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl<'a> VersionBuilder<'a> {
self.license_file,
self.size,
published_by,
Some("someone@example.com".into()),
)?
.save(connection, &[])?;

Expand Down
29 changes: 28 additions & 1 deletion src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ fn new_krate_with_readme() {
// See https://github.com/rust-lang/crates-io-cargo-teams/issues/8
#[test]
fn new_krate_without_any_email_warns() {
let (_, _, _, token) = TestApp::with_proxy().with_token();
let (app, _, _, token) = TestApp::with_proxy().with_token();

let crate_to_publish = PublishBuilder::new("foo_no_email");

Expand All @@ -1086,6 +1086,15 @@ fn new_krate_without_any_email_warns() {
assert_eq!(json.warnings.other[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.");

// Don't record a verified email if there isn't one
app.db(|conn| {
let email = versions::table
.select(versions::published_by_email)
.first::<Option<String>>(conn)
.unwrap();
assert!(email.is_none());
});
}

// This warning will soon become a hard error.
Expand All @@ -1112,6 +1121,15 @@ fn new_krate_with_unverified_email_warns() {
assert_eq!(json.warnings.other[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.");

// Don't record a verified email if there isn't one
app.db(|conn| {
let email = versions::table
.select(versions::published_by_email)
.first::<Option<String>>(conn)
.unwrap();
assert!(email.is_none());
});
}

#[test]
Expand All @@ -1137,6 +1155,15 @@ fn new_krate_with_verified_email_doesnt_warn() {

let json = token.publish(crate_to_publish).good();
assert_eq!(json.warnings.other.len(), 0);

// Record a verified email because there is one
app.db(|conn| {
let email = versions::table
.select(versions::published_by_email)
.first::<Option<String>>(conn)
.unwrap();
assert_eq!(email.unwrap(), "something@example.com");
});
}

#[test]
Expand Down

0 comments on commit 623cfe7

Please sign in to comment.