from unittest.mock import patch from fastapi.testclient import TestClient from main import app from routers.github_sync import ( format_problem_readme, format_solution_file, generate_root_readme, slugify_problem, ) client = TestClient(app) def test_slugify_problem(): assert slugify_problem("Two Sum", 1) == "0001-two-sum" assert slugify_problem("3Sum Closest", 16) == "0016-3sum-closest" assert slugify_problem("Special Characters #$% Test!", 100) == "0100-special-characters-test" assert slugify_problem("Virtual Problem", None) == "algo-virtual-problem" def test_format_solution_file(): code = "def twoSum(nums, target):\n return [0, 1]" res = format_solution_file( problem_name="Two Sum", problem_number=1, difficulty="Easy", pattern="Arrays & Hashing", optimal_time="O(N)", optimal_space="O(N)", code=code, ) assert "Problem: #1 Two Sum" in res assert "Difficulty: Easy" in res assert "Pattern: Arrays & Hashing" in res assert "Time Complexity: O(N)" in res assert "Space Complexity: O(N)" in res assert code in res def test_format_problem_readme(): res = format_problem_readme( problem_name="Two Sum", problem_number=1, difficulty="Easy", pattern="Arrays & Hashing", optimal_time="O(N)", optimal_space="O(N)", description="Given an array of integers nums and an integer target...", examples=[{"input": "nums = [2,7,11,15], target = 9", "output": "[0,1]"}], constraints=["2 <= nums.length <= 10^4"], ) assert "# #1 Two Sum" in res assert "Given an array of integers" in res assert "nums = [2,7,11,15]" in res assert "2 <= nums.length <= 10^4" in res assert "Complexity Analysis" in res def test_generate_root_readme(): problems = [ { "problem_number": 1, "name": "Two Sum", "difficulty": "Easy", "pattern": "Arrays", "optimal_time": "O(N)", "optimal_space": "O(N)", }, { "problem_number": 15, "name": "3Sum", "difficulty": "Medium", "pattern": "Two Pointers", "optimal_time": "O(N^2)", "optimal_space": "O(1)", }, ] res = generate_root_readme("testuser", "algospaced-solutions", problems) assert "# ⚡ algospaced-solutions — AlgoSpaced Solutions" in res assert "Total_Solved-2" in res assert "| #1 | **Two Sum** | `Easy` | Arrays | `O(N)` | `O(N)` |" in res assert "| #15 | **3Sum** | `Medium` | Two Pointers | `O(N^2)` | `O(1)` |" in res @patch("routers.github_sync.github_api_request") def test_verify_github_token_endpoint(mock_api): # Success case mock_api.return_value = ( 200, { "login": "octocat", "name": "The Octocat", "avatar_url": "https://github.com/images/error/octocat_happy.gif", "html_url": "https://github.com/octocat", }, ) response = client.post("/api/github/verify", json={"token": "ghp_validtoken123"}) assert response.status_code == 200 data = response.json() assert data["valid"] is True assert data["user"]["login"] == "octocat" assert data["user"]["name"] == "The Octocat" # Invalid token case mock_api.return_value = (401, {"message": "Bad credentials"}) response_bad = client.post("/api/github/verify", json={"token": "ghp_badtoken"}) assert response_bad.status_code == 200 data_bad = response_bad.json() assert data_bad["valid"] is False assert "Bad credentials" in data_bad["error"] @patch("routers.github_sync.commit_file_to_github") @patch("routers.github_sync.github_api_request") def test_push_solution_endpoint(mock_api, mock_commit): mock_api.return_value = (200, {"login": "octocat"}) mock_commit.return_value = ( True, "Committed successfully", { "html_url": "https://github.com/octocat/algospaced-solutions/blob/main/solutions/0001-two-sum/solution.py" }, ) payload = { "token": "ghp_validtoken123", "repo": "octocat/algospaced-solutions", "branch": "main", "problem_number": 1, "problem_name": "Two Sum", "pattern": "Arrays & Hashing", "difficulty": "Easy", "optimal_time": "O(N)", "optimal_space": "O(N)", "description": "Find indices summing to target", "code": "def twoSum(nums, target): return [0, 1]", } response = client.post("/api/github/push-solution", json=payload) assert response.status_code == 200 data = response.json() assert data["success"] is True assert "0001-two-sum" in data["slug"] assert "https://github.com/octocat/algospaced-solutions" in data["html_url"] def test_oauth_status_endpoint(monkeypatch): monkeypatch.delenv("GITHUB_CLIENT_ID", raising=False) monkeypatch.delenv("GITHUB_CLIENT_SECRET", raising=False) res = client.get("/api/github/oauth/status") assert res.status_code == 200 assert res.json() == {"configured": False, "client_id": None} monkeypatch.setenv("GITHUB_CLIENT_ID", "test_client_id_123") monkeypatch.setenv("GITHUB_CLIENT_SECRET", "test_client_secret_abc") res2 = client.get("/api/github/oauth/status") assert res2.status_code == 200 assert res2.json() == {"configured": True, "client_id": "test_client_id_123"} def test_oauth_login_endpoint(monkeypatch): # Unconfigured case -> returns error HTML monkeypatch.delenv("GITHUB_CLIENT_ID", raising=False) monkeypatch.delenv("GITHUB_CLIENT_SECRET", raising=False) res = client.get("/api/github/oauth/login") assert res.status_code == 200 assert "GITHUB_OAUTH_ERROR" in res.text # Configured case -> 302 redirect to GitHub authorize monkeypatch.setenv("GITHUB_CLIENT_ID", "gh_client_123") monkeypatch.setenv("GITHUB_CLIENT_SECRET", "gh_secret_456") res2 = client.get("/api/github/oauth/login", follow_redirects=False) assert res2.status_code == 302 assert "github.com/login/oauth/authorize" in res2.headers["location"] assert "client_id=gh_client_123" in res2.headers["location"] assert "scope=repo" in res2.headers["location"] @patch("urllib.request.urlopen") def test_oauth_callback_endpoint_success(mock_urlopen, monkeypatch): monkeypatch.setenv("GITHUB_CLIENT_ID", "gh_client_123") monkeypatch.setenv("GITHUB_CLIENT_SECRET", "gh_secret_456") # Mock token exchange response import io mock_resp = io.BytesIO(b'{"access_token": "gho_testtoken999", "scope": "repo"}') mock_resp.status = 200 mock_urlopen.return_value.__enter__.return_value = mock_resp res = client.get("/api/github/oauth/callback?code=mock_code_abc") assert res.status_code == 200 assert "GITHUB_OAUTH_SUCCESS" in res.text assert "gho_testtoken999" in res.text def test_oauth_callback_endpoint_error(): res = client.get( "/api/github/oauth/callback?error=access_denied&error_description=User+cancelled" ) assert res.status_code == 200 assert "GITHUB_OAUTH_ERROR" in res.text assert "User cancelled" in res.text