Skip to content
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

add test to sqlite example #208

Closed
wants to merge 3 commits into from
Closed

Conversation

thor314
Copy link

@thor314 thor314 commented Mar 26, 2024

Adding a login-logout test to the sqlite example.

Several questions arose while implementing the test:

  • How to build the login request, to include cookies(*) like AuthSession and Messages in the request
  • How to verify that the user is logged in
  • Why logout is a GET request instead of a POST in these examples

these questions are documented in the test:

#[sqlx::test]
async fn test_login_logout(pool: SqlitePool) {
    let app = setup_app(pool).await.unwrap();

    let login_request = {
        let credentials = Credentials {
            username: "ferris".to_string(),
            password: "password".to_string(),
            next: None,
        };
        // todo: unclear how to instatiate or add to request; no tests in axum-messages
        // let messages: Messages;
        let credentials = serde_urlencoded::to_string(credentials).unwrap();

        Request::builder()
            .uri("/login")
            .method("POST")
            .header(CONTENT_TYPE, "application/x-www-form-urlencoded")
            // todo: where do I put messages in the request?
            .body(credentials)
            .unwrap()
    };
    let login_response = app.clone().oneshot(login_request).await.unwrap();
    dbg!(&login_response);
    assert!(login_response.status().is_redirection());

    // todo: how to verify that the user is logged in?

    // why is logout a get request instead of a post in the example?
    let logout_request = Request::builder()
        .uri("/logout")
        .method("GET")
        .body(Body::empty())
        .unwrap();
    let logout_response = app.clone().oneshot(logout_request).await.unwrap();
    dbg!(&logout_response);
    assert!(logout_response.status().is_redirection());
}

@maxcountryman
Copy link
Owner

Thanks for putting this together!

I'm curious how this is different from the integration tests and why we wouldn't continue to colocate tests in that directory?

Copy link

codecov bot commented Mar 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.13%. Comparing base (e959752) to head (07236bf).

❗ Current head 07236bf differs from pull request most recent head af69d00. Consider uploading reports for the commit af69d00 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #208   +/-   ##
=======================================
  Coverage   80.13%   80.13%           
=======================================
  Files           5        5           
  Lines         151      151           
=======================================
  Hits          121      121           
  Misses         30       30           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@thor314
Copy link
Author

thor314 commented Mar 26, 2024

I think your question is why bother with unit tests for a server, as opposed to only integration tests, please correct me if otherwise.

Integration tests are useful for testing the overall function of a server. Unit tests are closer to sanity checks, that some component is functioning as expected.

I am currently debugging where axum-login is breaking within my own server, and these sanity checks help me reason with finer granularity as to whether I have made mistakes, or if something is broken in the library (I don't yet know which may be the case).

In either case, unit tests are at least an additionally helpful documentation, and especially helpful for the kind of sanity I currently feel to lack in my debugging.

@maxcountryman
Copy link
Owner

I'd prefer this kind of thing be done in a separate repo and that we continue to use the test harness that's already in place for axum-login and its examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants