test: increase coverage for celery_app.py and google_drive.py to 100%
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
"""Tests for app/views/google_drive.py module."""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
import urllib.parse
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@@ -26,3 +28,105 @@ class TestGoogleDriveViews:
|
||||
"""Test the Google Drive OAuth callback with auth code."""
|
||||
response = client.get("/google-drive-callback?code=test_code")
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_google_drive_callback_with_code_and_state(self, client):
|
||||
"""Test the Google Drive OAuth callback with code and state."""
|
||||
response = client.get("/google-drive-callback?code=test_code&state=test_state")
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_google_drive_auth_start_with_redirect_uri(self, client):
|
||||
"""Test starting Google Drive OAuth flow with explicit redirect_uri."""
|
||||
client_id = "test_client_id_123"
|
||||
redirect_uri = "https://example.com/callback"
|
||||
response = client.get(
|
||||
f"/google-drive-auth-start?client_id={client_id}&redirect_uri={redirect_uri}",
|
||||
follow_redirects=False
|
||||
)
|
||||
|
||||
assert response.status_code in [302, 307] # Redirect status codes
|
||||
|
||||
# Verify redirect location
|
||||
location = response.headers.get("location")
|
||||
assert location is not None
|
||||
assert "accounts.google.com/o/oauth2/auth" in location
|
||||
assert f"client_id={client_id}" in location
|
||||
assert urllib.parse.quote(redirect_uri) in location
|
||||
assert "response_type=code" in location
|
||||
assert "access_type=offline" in location
|
||||
assert "prompt=consent" in location
|
||||
# Verify scope includes drive.file
|
||||
assert "scope=" in location
|
||||
|
||||
def test_google_drive_auth_start_without_redirect_uri(self, client):
|
||||
"""Test starting Google Drive OAuth flow without explicit redirect_uri."""
|
||||
client_id = "test_client_id_456"
|
||||
response = client.get(
|
||||
f"/google-drive-auth-start?client_id={client_id}",
|
||||
follow_redirects=False
|
||||
)
|
||||
|
||||
assert response.status_code in [302, 307] # Redirect status codes
|
||||
|
||||
# Verify redirect location
|
||||
location = response.headers.get("location")
|
||||
assert location is not None
|
||||
assert "accounts.google.com/o/oauth2/auth" in location
|
||||
assert f"client_id={client_id}" in location
|
||||
# Should use default redirect_uri based on request host
|
||||
assert "redirect_uri=" in location
|
||||
|
||||
def test_google_drive_auth_start_scope_configuration(self, client):
|
||||
"""Test that Google Drive auth start uses correct OAuth scope."""
|
||||
client_id = "test_client_id_789"
|
||||
response = client.get(
|
||||
f"/google-drive-auth-start?client_id={client_id}",
|
||||
follow_redirects=False
|
||||
)
|
||||
|
||||
location = response.headers.get("location")
|
||||
assert location is not None
|
||||
|
||||
# The scope should be URL encoded, so check for the encoded version
|
||||
# drive.file scope: https://www.googleapis.com/auth/drive.file
|
||||
expected_scope = urllib.parse.quote("https://www.googleapis.com/auth/drive.file")
|
||||
assert expected_scope in location
|
||||
|
||||
@patch("app.views.google_drive.settings")
|
||||
def test_google_drive_setup_page_with_folder_id_empty(self, mock_settings, client):
|
||||
"""Test setup page when folder_id is None/empty - should show not configured."""
|
||||
mock_settings.google_drive_use_oauth = False
|
||||
mock_settings.google_drive_client_id = "test_client_id"
|
||||
mock_settings.google_drive_client_secret = "test_secret"
|
||||
mock_settings.google_drive_refresh_token = "test_token"
|
||||
mock_settings.google_drive_credentials_json = '{"test": "creds"}'
|
||||
mock_settings.google_drive_folder_id = None # Empty folder ID
|
||||
|
||||
response = client.get("/google-drive-setup")
|
||||
assert response.status_code == 200
|
||||
# The page should indicate not fully configured due to missing folder_id
|
||||
|
||||
@patch("app.views.google_drive.settings")
|
||||
def test_google_drive_setup_page_oauth_mode(self, mock_settings, client):
|
||||
"""Test setup page in OAuth mode."""
|
||||
mock_settings.google_drive_use_oauth = True
|
||||
mock_settings.google_drive_client_id = "oauth_client_id"
|
||||
mock_settings.google_drive_client_secret = "oauth_secret"
|
||||
mock_settings.google_drive_refresh_token = "oauth_token"
|
||||
mock_settings.google_drive_folder_id = "test_folder_id"
|
||||
mock_settings.google_drive_credentials_json = None
|
||||
|
||||
response = client.get("/google-drive-setup")
|
||||
assert response.status_code == 200
|
||||
|
||||
@patch("app.views.google_drive.settings")
|
||||
def test_google_drive_setup_page_service_account_mode(self, mock_settings, client):
|
||||
"""Test setup page in service account mode."""
|
||||
mock_settings.google_drive_use_oauth = False
|
||||
mock_settings.google_drive_credentials_json = '{"type": "service_account"}'
|
||||
mock_settings.google_drive_folder_id = "test_folder_id"
|
||||
mock_settings.google_drive_client_id = None
|
||||
mock_settings.google_drive_client_secret = None
|
||||
mock_settings.google_drive_refresh_token = None
|
||||
|
||||
response = client.get("/google-drive-setup")
|
||||
assert response.status_code == 200
|
||||
|
||||
Reference in New Issue
Block a user