Repository: aipotheosis-labs/aci
File: aipolabs/server/tests/routes/auth/test_auth.py (Python)

Code before:
            return_value=MOCK_USER_GOOGLE_AUTH_DATA,
        ),
        patch("aipolabs.server.oauth2.authorize_redirect", return_value=None),
    ):
        test_client.get(
            f"{config.ROUTER_PREFIX_AUTH}/signup/google?signup_code={VALID_SIGNUP_CODE}"
        )
        response = test_client.get(
            f"{config.ROUTER_PREFIX_AUTH}/signup/callback/google?signup_code={VALID_SIGNUP_CODE}"
        )

    assert response.status_code == status.HTTP_307_TEMPORARY_REDIRECT
    assert (
        response.headers["location"] == f"{config.DEV_PORTAL_URL}"
    ), "should redirect to dev portal if user already signed up"


def test_logout(test_client: TestClient, db_session: Session) -> None:
    test_client.cookies.set("accessToken", "randomToken")

    response = test_client.post(f"{config.ROUTER_PREFIX_AUTH}/logout/")

    assert response.status_code == 200

    assert "Set-Cookie" in response.headers
    assert "accessToken=" in response.headers["Set-Cookie"]
    assert "Max-Age=0" in response.headers["Set-Cookie"]


def _create_mock_user(db_session: Session) -> User:
    identity_provider_user_info = IdentityProviderUserInfo.model_validate(
        MOCK_USER_GOOGLE_AUTH_DATA["userinfo"]
    )
    user = crud.users.create_user(
        db_session,
        UserCreate(
            identity_provider=identity_provider_user_info.iss,
            user_id_by_provider=identity_provider_user_info.sub,
            name=identity_provider_user_info.name,
            email=identity_provider_user_info.email,
            profile_picture=identity_provider_user_info.picture,
        ),
    )
    db_session.commit()
    return user


Review comment (bug):
this will still pass if the `accessToken` is not deleted right? e.g, `accessToken=abc` should fail but it won't in this test because `response.headers["Set-Cookie"]` returns string and  `accessToken=` will be `in` regardless it's empty or not.

Code after:
            return_value=MOCK_USER_GOOGLE_AUTH_DATA,
        ),
        patch("aipolabs.server.oauth2.authorize_redirect", return_value=None),
    ):
        test_client.get(
            f"{config.ROUTER_PREFIX_AUTH}/signup/google?signup_code={VALID_SIGNUP_CODE}"
        )
        response = test_client.get(
            f"{config.ROUTER_PREFIX_AUTH}/signup/callback/google?signup_code={VALID_SIGNUP_CODE}"
        )

    assert response.status_code == status.HTTP_307_TEMPORARY_REDIRECT
    assert (
        response.headers["location"] == f"{config.DEV_PORTAL_URL}"
    ), "should redirect to dev portal if user already signed up"


def test_logout(test_client: TestClient, db_session: Session) -> None:
    test_client.cookies.set(config.COOKIE_KEY_FOR_AUTH_TOKEN, "randomToken")

    response = test_client.post(f"{config.ROUTER_PREFIX_AUTH}/logout/")

    assert response.status_code == 200

    assert "Set-Cookie" in response.headers
    assert f'{config.COOKIE_KEY_FOR_AUTH_TOKEN}=""' in response.headers["Set-Cookie"]
    assert f"Domain={config.AIPOLABS_ROOT_DOMAIN}" in response.headers["Set-Cookie"]
    assert "Max-Age=0" in response.headers["Set-Cookie"]


def _create_mock_user(db_session: Session) -> User:
    identity_provider_user_info = IdentityProviderUserInfo.model_validate(
        MOCK_USER_GOOGLE_AUTH_DATA["userinfo"]
    )
    user = crud.users.create_user(
        db_session,
        UserCreate(
            identity_provider=identity_provider_user_info.iss,
            user_id_by_provider=identity_provider_user_info.sub,
            name=identity_provider_user_info.name,
            email=identity_provider_user_info.email,
            profile_picture=identity_provider_user_info.picture,
        ),
    )
    db_session.commit()
    return user