|
| 1 | +use crate::tests::util::{MockTokenUser, RequestHelper, TestApp}; |
| 2 | +use chrono::{TimeDelta, Utc}; |
| 3 | +use crates_io_database::models::trustpub::NewToken; |
| 4 | +use crates_io_database::schema::trustpub_tokens; |
| 5 | +use crates_io_trustpub::access_token::AccessToken; |
| 6 | +use diesel::prelude::*; |
| 7 | +use diesel_async::{AsyncPgConnection, RunQueryDsl}; |
| 8 | +use http::StatusCode; |
| 9 | +use insta::assert_compact_debug_snapshot; |
| 10 | +use insta::assert_snapshot; |
| 11 | +use secrecy::ExposeSecret; |
| 12 | +use sha2::Sha256; |
| 13 | +use sha2::digest::Output; |
| 14 | + |
| 15 | +const URL: &str = "/api/v1/trusted_publishing/tokens"; |
| 16 | + |
| 17 | +fn generate_token() -> (String, Output<Sha256>) { |
| 18 | + let token = AccessToken::generate(); |
| 19 | + (token.finalize().expose_secret().to_string(), token.sha256()) |
| 20 | +} |
| 21 | + |
| 22 | +async fn new_token(conn: &mut AsyncPgConnection, crate_id: i32) -> QueryResult<String> { |
| 23 | + let (token, hashed_token) = generate_token(); |
| 24 | + |
| 25 | + let new_token = NewToken { |
| 26 | + expires_at: Utc::now() + TimeDelta::minutes(30), |
| 27 | + hashed_token: hashed_token.as_slice(), |
| 28 | + crate_ids: &[crate_id], |
| 29 | + }; |
| 30 | + |
| 31 | + new_token.insert(conn).await?; |
| 32 | + |
| 33 | + Ok(token) |
| 34 | +} |
| 35 | + |
| 36 | +async fn all_crate_ids(conn: &mut AsyncPgConnection) -> QueryResult<Vec<Vec<Option<i32>>>> { |
| 37 | + trustpub_tokens::table |
| 38 | + .select(trustpub_tokens::crate_ids) |
| 39 | + .load(conn) |
| 40 | + .await |
| 41 | +} |
| 42 | + |
| 43 | +#[tokio::test(flavor = "multi_thread")] |
| 44 | +async fn test_happy_path() -> anyhow::Result<()> { |
| 45 | + let (app, _client) = TestApp::full().empty().await; |
| 46 | + let mut conn = app.db_conn().await; |
| 47 | + |
| 48 | + let token1 = new_token(&mut conn, 1).await?; |
| 49 | + let _token2 = new_token(&mut conn, 2).await?; |
| 50 | + assert_compact_debug_snapshot!(all_crate_ids(&mut conn).await?, @"[[Some(1)], [Some(2)]]"); |
| 51 | + |
| 52 | + let header = format!("Bearer {}", token1); |
| 53 | + let token_client = MockTokenUser::with_auth_header(header, app.clone()); |
| 54 | + |
| 55 | + let response = token_client.delete::<()>(URL).await; |
| 56 | + assert_eq!(response.status(), StatusCode::NO_CONTENT); |
| 57 | + assert_eq!(response.text(), ""); |
| 58 | + |
| 59 | + // Check that the token is deleted |
| 60 | + assert_compact_debug_snapshot!(all_crate_ids(&mut conn).await?, @"[[Some(2)]]"); |
| 61 | + |
| 62 | + Ok(()) |
| 63 | +} |
| 64 | + |
| 65 | +#[tokio::test(flavor = "multi_thread")] |
| 66 | +async fn test_missing_authorization_header() -> anyhow::Result<()> { |
| 67 | + let (_app, client) = TestApp::full().empty().await; |
| 68 | + |
| 69 | + let response = client.delete::<()>(URL).await; |
| 70 | + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); |
| 71 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Missing authorization header"}]}"#); |
| 72 | + |
| 73 | + Ok(()) |
| 74 | +} |
| 75 | + |
| 76 | +#[tokio::test(flavor = "multi_thread")] |
| 77 | +async fn test_invalid_authorization_header_format() -> anyhow::Result<()> { |
| 78 | + let (app, _client) = TestApp::full().empty().await; |
| 79 | + |
| 80 | + // Create a client with an invalid authorization header (missing "Bearer " prefix) |
| 81 | + let header = "invalid-format".to_string(); |
| 82 | + let token_client = MockTokenUser::with_auth_header(header, app.clone()); |
| 83 | + |
| 84 | + let response = token_client.delete::<()>(URL).await; |
| 85 | + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); |
| 86 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Invalid authorization header"}]}"#); |
| 87 | + |
| 88 | + Ok(()) |
| 89 | +} |
| 90 | + |
| 91 | +#[tokio::test(flavor = "multi_thread")] |
| 92 | +async fn test_invalid_token_format() -> anyhow::Result<()> { |
| 93 | + let (app, _client) = TestApp::full().empty().await; |
| 94 | + |
| 95 | + // Create a client with an invalid token format |
| 96 | + let header = "Bearer invalid-token".to_string(); |
| 97 | + let token_client = MockTokenUser::with_auth_header(header, app.clone()); |
| 98 | + |
| 99 | + let response = token_client.delete::<()>(URL).await; |
| 100 | + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); |
| 101 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Invalid authorization header"}]}"#); |
| 102 | + |
| 103 | + Ok(()) |
| 104 | +} |
| 105 | + |
| 106 | +#[tokio::test(flavor = "multi_thread")] |
| 107 | +async fn test_non_existent_token() -> anyhow::Result<()> { |
| 108 | + let (app, _client) = TestApp::full().empty().await; |
| 109 | + |
| 110 | + // Generate a valid token format, but it doesn't exist in the database |
| 111 | + let (token, _) = generate_token(); |
| 112 | + let header = format!("Bearer {}", token); |
| 113 | + let token_client = MockTokenUser::with_auth_header(header, app.clone()); |
| 114 | + |
| 115 | + // The request should succeed with 204 No Content even though the token doesn't exist |
| 116 | + let response = token_client.delete::<()>(URL).await; |
| 117 | + assert_eq!(response.status(), StatusCode::NO_CONTENT); |
| 118 | + assert_eq!(response.text(), ""); |
| 119 | + |
| 120 | + Ok(()) |
| 121 | +} |
0 commit comments