Skip to content

Log assertions: add assertions for API log errors #1152

Closed

Description

@josecelano

Parent issue: #1150

Some API endpoints produce log errors:

cargo test -- --test-threads=1 --nocapture | grep "ERROR" | grep "API"

Write assertions in the tests producing those errors.

Tests expecting a 500 response are tests using these asserts:

pub async fn assert_token_not_valid(response: Response) {
    assert_unhandled_rejection(response, "token not valid").await;
}

pub async fn assert_unauthorized(response: Response) {
    assert_unhandled_rejection(response, "unauthorized").await;
}

pub async fn assert_failed_to_remove_torrent_from_whitelist(response: Response) {
    assert_unhandled_rejection(response, "failed to remove torrent from whitelist").await;
}

pub async fn assert_failed_to_whitelist_torrent(response: Response) {
    assert_unhandled_rejection(response, "failed to whitelist torrent").await;
}

pub async fn assert_failed_to_reload_whitelist(response: Response) {
    assert_unhandled_rejection(response, "failed to reload whitelist").await;
}

pub async fn assert_failed_to_generate_key(response: Response) {
    assert_unhandled_rejection(response, "failed to generate key").await;
}

pub async fn assert_failed_to_delete_key(response: Response) {
    assert_unhandled_rejection(response, "failed to delete key").await;
}

pub async fn assert_failed_to_reload_keys(response: Response) {
    assert_unhandled_rejection(response, "failed to reload keys").await;
}

async fn assert_unhandled_rejection(response: Response, reason: &str) {
    assert_eq!(response.status(), 500);
    assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");

    let reason_text = format!("Unhandled rejection: Err {{ reason: \"{reason}");
    let response_text = response.text().await.unwrap();
    assert!(
        response_text.contains(&reason_text),
        ":\n  response: `\"{response_text}\"`\n  does not contain: `\"{reason_text}\"`."
    );
}

We only need to identify tests using these assert functions:

assert_token_not_valid
assert_unauthorized
assert_failed_to_remove_torrent_from_whitelist
assert_failed_to_whitelist_torrent
assert_failed_to_reload_whitelist
assert_failed_to_generate_key
assert_failed_to_delete_key
assert_failed_to_reload_keys

We need to use the request-id in the logs to match the test with the log line.

Activity

  1. self-assigned this
    on Dec 23, 2024
  2. added
    Testing
    on Dec 23, 2024
  3. added a commit that references this issue on Dec 23, 2024
  4. josecelano commented on Dec 23, 2024

    @josecelano
    MemberAuthor

    Test producing errors (from here):

    test servers::api::v1::contract::authentication::should_not_authenticate_requests_when_the_token_is_empty ... 2024-12-23T18:46:10.685125Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=c0c0d658-489c-460c-ac2f-b9a65c99b1eb
    test servers::api::v1::contract::authentication::should_not_authenticate_requests_when_the_token_is_invalid ... 2024-12-23T18:46:10.699268Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=c24022e9-a659-4879-a4e1-c6cb2c123887
    test servers::api::v1::contract::authentication::should_not_authenticate_requests_when_the_token_is_missing ... 2024-12-23T18:46:10.713313Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=a1dcb071-6baf-4c51-954a-25721e508467
    test servers::api::v1::contract::context::auth_key::deprecated_generate_key_endpoint::should_fail_when_the_auth_key_cannot_be_generated ... 2024-12-23T18:46:10.769691Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=d6b5222b-d892-4f97-bc4e-46db875e3c99
    test servers::api::v1::contract::context::auth_key::deprecated_generate_key_endpoint::should_not_allow_generating_a_new_auth_key_for_unauthenticated_users ... 2024-12-23T18:46:10.783818Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=1335bfe3-816d-4c68-a0a1-87b805bccd88
    test servers::api::v1::contract::context::auth_key::should_fail_when_keys_cannot_be_reloaded ... 2024-12-23T18:46:10.997966Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=6c7b0672-d323-4fa2-9ef8-b6f918a6f422
    test servers::api::v1::contract::context::auth_key::should_fail_when_the_auth_key_cannot_be_deleted ... 2024-12-23T18:46:11.016038Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=6e7ea98d-f2da-4e34-9989-14c8af0e54c0
    test servers::api::v1::contract::context::auth_key::should_fail_when_the_auth_key_cannot_be_generated ... 2024-12-23T18:46:11.033056Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=aea1d68b-e3b2-45c7-91c9-3987a9934a2c
    test servers::api::v1::contract::context::auth_key::should_not_allow_deleting_an_auth_key_for_unauthenticated_users ... 2024-12-23T18:46:11.048427Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=dbda8097-f3ca-4c56-8a94-cd24adcc58a1
    test servers::api::v1::contract::context::auth_key::should_not_allow_generating_a_new_auth_key_for_unauthenticated_users ... 2024-12-23T18:46:11.074044Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=046b60d2-0e20-4b6d-9d44-a86aa4520911
    test servers::api::v1::contract::context::auth_key::should_not_allow_reloading_keys_for_unauthenticated_users ... 2024-12-23T18:46:11.099531Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=94a8e2ab-b4a8-40a3-8063-c2a136445f6a
    test servers::api::v1::contract::context::stats::should_not_allow_getting_tracker_statistics_for_unauthenticated_users ... 2024-12-23T18:46:11.152930Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=e0da13a9-7d4e-4d0e-94c3-6203cd346617
    test servers::api::v1::contract::context::torrent::should_not_allow_getting_a_torrent_info_for_unauthenticated_users ... 2024-12-23T18:46:11.484403Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=dbe035c1-3345-45b9-9d25-0495cca99c84
    test servers::api::v1::contract::context::torrent::should_not_allow_getting_torrents_for_unauthenticated_users ... 2024-12-23T18:46:11.508950Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=3cc74693-a589-46b5-93ae-bea37a5072d0
    test servers::api::v1::contract::context::whitelist::should_fail_when_the_torrent_cannot_be_removed_from_the_whitelist ... 2024-12-23T18:46:11.695539Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=15fee3e8-9717-464c-909d-63620145aaf4
    test servers::api::v1::contract::context::whitelist::should_fail_when_the_torrent_cannot_be_whitelisted ... 2024-12-23T18:46:11.713213Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=d1d8087b-0f16-408f-9e60-a8a4c8106d89
    test servers::api::v1::contract::context::whitelist::should_fail_when_the_whitelist_cannot_be_reloaded_from_the_database ... 2024-12-23T18:46:11.731415Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=c23f7e5a-d5ab-4ccf-bbda-2e0d4685b33a
    test servers::api::v1::contract::context::whitelist::should_not_allow_removing_a_torrent_from_the_whitelist_for_unauthenticated_users ... 2024-12-23T18:46:11.831375Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=aee93b39-3e05-49d9-8529-98e635e39a73
    test servers::api::v1::contract::context::whitelist::should_not_allow_whitelisting_a_torrent_for_unauthenticated_users ... 2024-12-23T18:46:11.855691Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=7891d303-f970-465b-b9b0-b4fc7a559dfa
    
  5. josecelano commented on Dec 23, 2024

    @josecelano
    MemberAuthor

    You can run a single test with:

    cargo test -- --nocapture servers::api::v1::contract::authentication::should_not_authenticate_requests_when_the_token_is_empty

    The output:

    2024-12-23T18:59:44.062852Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error request_id=02530e91-836b-403b-8ef9-85afa69bc096
    

    Now we have the request_id, and we can write asserts for logs containing the request_id sent in the request.

  6. added 2 commits that reference this issue on Dec 26, 2024
  7. josecelano commented on Dec 26, 2024

    @josecelano
    MemberAuthor

    Closed via #1161

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Testing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions