Skip to content

Log assertions: add assertions for API log errors #1152

@josecelano

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.

Metadata

Metadata

Assignees

Labels

TestingChecking Torrust

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions