Skip to content

tests/yank_unyank: Add authentication tests #5583

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
Dec 3, 2022
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
Original file line number Diff line number Diff line change
@@ -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": ""
}
}
]
122 changes: 122 additions & 0 deletions src/tests/http-data/routes_crates_versions_yank_unyank_auth_token_user
Original file line number Diff line number Diff line change
@@ -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": ""
}
}
]
Original file line number Diff line number Diff line change
@@ -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": ""
}
}
]
63 changes: 63 additions & 0 deletions src/tests/routes/crates/versions/yank_unyank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
}
}