diff --git a/src/tests/http-data/routes_crates_versions_yank_unyank_auth_cookie_user b/src/tests/http-data/routes_crates_versions_yank_unyank_auth_cookie_user new file mode 100644 index 00000000000..2a4ad0e250e --- /dev/null +++ b/src/tests/http-data/routes_crates_versions_yank_unyank_auth_cookie_user @@ -0,0 +1,122 @@ +[ + { + "request": { + "uri": "http://alexcrichton-test.s3.amazonaws.com/crates/fyk/fyk-1.0.0.crate", + "method": "PUT", + "headers": [ + [ + "accept", + "*/*" + ], + [ + "accept-encoding", + "gzip" + ], + [ + "content-length", + "35" + ], + [ + "content-type", + "application/gzip" + ] + ], + "body": "H4sIAAAAAAAA/+3AAQEAAACCIP+vbkhQwKsBLq+17wAEAAA=" + }, + "response": { + "status": 200, + "headers": [], + "body": "" + } + }, + { + "request": { + "uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk", + "method": "PUT", + "headers": [ + [ + "accept", + "*/*" + ], + [ + "accept-encoding", + "gzip" + ], + [ + "content-length", + "157" + ], + [ + "content-type", + "text/plain" + ] + ], + "body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg==" + }, + "response": { + "status": 200, + "headers": [], + "body": "" + } + }, + { + "request": { + "uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk", + "method": "PUT", + "headers": [ + [ + "accept", + "*/*" + ], + [ + "accept-encoding", + "gzip" + ], + [ + "content-length", + "156" + ], + [ + "content-type", + "text/plain" + ] + ], + "body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjp0cnVlLCJsaW5rcyI6bnVsbH0K" + }, + "response": { + "status": 200, + "headers": [], + "body": "" + } + }, + { + "request": { + "uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk", + "method": "PUT", + "headers": [ + [ + "accept", + "*/*" + ], + [ + "accept-encoding", + "gzip" + ], + [ + "content-length", + "157" + ], + [ + "content-type", + "text/plain" + ] + ], + "body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg==" + }, + "response": { + "status": 200, + "headers": [], + "body": "" + } + } +] diff --git a/src/tests/http-data/routes_crates_versions_yank_unyank_auth_token_user b/src/tests/http-data/routes_crates_versions_yank_unyank_auth_token_user new file mode 100644 index 00000000000..2a4ad0e250e --- /dev/null +++ b/src/tests/http-data/routes_crates_versions_yank_unyank_auth_token_user @@ -0,0 +1,122 @@ +[ + { + "request": { + "uri": "http://alexcrichton-test.s3.amazonaws.com/crates/fyk/fyk-1.0.0.crate", + "method": "PUT", + "headers": [ + [ + "accept", + "*/*" + ], + [ + "accept-encoding", + "gzip" + ], + [ + "content-length", + "35" + ], + [ + "content-type", + "application/gzip" + ] + ], + "body": "H4sIAAAAAAAA/+3AAQEAAACCIP+vbkhQwKsBLq+17wAEAAA=" + }, + "response": { + "status": 200, + "headers": [], + "body": "" + } + }, + { + "request": { + "uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk", + "method": "PUT", + "headers": [ + [ + "accept", + "*/*" + ], + [ + "accept-encoding", + "gzip" + ], + [ + "content-length", + "157" + ], + [ + "content-type", + "text/plain" + ] + ], + "body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg==" + }, + "response": { + "status": 200, + "headers": [], + "body": "" + } + }, + { + "request": { + "uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk", + "method": "PUT", + "headers": [ + [ + "accept", + "*/*" + ], + [ + "accept-encoding", + "gzip" + ], + [ + "content-length", + "156" + ], + [ + "content-type", + "text/plain" + ] + ], + "body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjp0cnVlLCJsaW5rcyI6bnVsbH0K" + }, + "response": { + "status": 200, + "headers": [], + "body": "" + } + }, + { + "request": { + "uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk", + "method": "PUT", + "headers": [ + [ + "accept", + "*/*" + ], + [ + "accept-encoding", + "gzip" + ], + [ + "content-length", + "157" + ], + [ + "content-type", + "text/plain" + ] + ], + "body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg==" + }, + "response": { + "status": 200, + "headers": [], + "body": "" + } + } +] diff --git a/src/tests/http-data/routes_crates_versions_yank_unyank_auth_unauthenticated b/src/tests/http-data/routes_crates_versions_yank_unyank_auth_unauthenticated new file mode 100644 index 00000000000..8c9445c2f5a --- /dev/null +++ b/src/tests/http-data/routes_crates_versions_yank_unyank_auth_unauthenticated @@ -0,0 +1,62 @@ +[ + { + "request": { + "uri": "http://alexcrichton-test.s3.amazonaws.com/crates/fyk/fyk-1.0.0.crate", + "method": "PUT", + "headers": [ + [ + "accept", + "*/*" + ], + [ + "accept-encoding", + "gzip" + ], + [ + "content-length", + "35" + ], + [ + "content-type", + "application/gzip" + ] + ], + "body": "H4sIAAAAAAAA/+3AAQEAAACCIP+vbkhQwKsBLq+17wAEAAA=" + }, + "response": { + "status": 200, + "headers": [], + "body": "" + } + }, + { + "request": { + "uri": "http://alexcrichton-test.s3.amazonaws.com/3/f/fyk", + "method": "PUT", + "headers": [ + [ + "accept", + "*/*" + ], + [ + "accept-encoding", + "gzip" + ], + [ + "content-length", + "157" + ], + [ + "content-type", + "text/plain" + ] + ], + "body": "eyJuYW1lIjoiZnlrIiwidmVycyI6IjEuMC4wIiwiZGVwcyI6W10sImNrc3VtIjoiYWNiNTYwNGIxMjZhYzg5NGMxZWIxMWM0NTc1YmYyMDcyZmVhNjEyMzJhODg4ZTQ1Mzc3MGM3OWQ3ZWQ1NjQxOSIsImZlYXR1cmVzIjp7fSwieWFua2VkIjpmYWxzZSwibGlua3MiOm51bGx9Cg==" + }, + "response": { + "status": 200, + "headers": [], + "body": "" + } + } +] diff --git a/src/tests/routes/crates/versions/yank_unyank.rs b/src/tests/routes/crates/versions/yank_unyank.rs index 5a89bb9786a..bd5e4282a5d 100644 --- a/src/tests/routes/crates/versions/yank_unyank.rs +++ b/src/tests/routes/crates/versions/yank_unyank.rs @@ -91,3 +91,66 @@ fn unyank_records_an_audit_action() { assert_eq!(action.action, "unyank"); assert_eq!(action.user.id, token.as_model().user_id); } + +mod auth { + use super::*; + use crate::util::{MockAnonymousUser, MockCookieUser}; + + const CRATE_NAME: &str = "fyk"; + const CRATE_VERSION: &str = "1.0.0"; + + fn prepare() -> (TestApp, MockAnonymousUser, MockCookieUser) { + let (app, anon, cookie) = TestApp::full().with_user(); + + let pb = PublishBuilder::new(CRATE_NAME).version(CRATE_VERSION); + cookie.publish_crate(pb).good(); + + (app, anon, cookie) + } + + #[test] + fn unauthenticated() { + let (_, client, _) = prepare(); + + let response = client.yank(CRATE_NAME, CRATE_VERSION); + assert_eq!(response.status(), StatusCode::FORBIDDEN); + assert_eq!( + response.into_json(), + json!({ "errors": [{ "detail": "must be logged in to perform that action" }] }) + ); + + let response = client.unyank(CRATE_NAME, CRATE_VERSION); + assert_eq!(response.status(), StatusCode::FORBIDDEN); + assert_eq!( + response.into_json(), + json!({ "errors": [{ "detail": "must be logged in to perform that action" }] }) + ); + } + + #[test] + fn cookie_user() { + let (_, _, client) = prepare(); + + let response = client.yank(CRATE_NAME, CRATE_VERSION); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.into_json(), json!({ "ok": true })); + + let response = client.unyank(CRATE_NAME, CRATE_VERSION); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.into_json(), json!({ "ok": true })); + } + + #[test] + fn token_user() { + let (_, _, client) = prepare(); + let client = client.db_new_token("test-token"); + + let response = client.yank(CRATE_NAME, CRATE_VERSION); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.into_json(), json!({ "ok": true })); + + let response = client.unyank(CRATE_NAME, CRATE_VERSION); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.into_json(), json!({ "ok": true })); + } +}