|
| 1 | +""" |
| 2 | +Unit Tests. |
| 3 | +""" |
| 4 | + |
| 5 | +import sys |
| 6 | +import os |
| 7 | + |
| 8 | +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 9 | + |
| 10 | +import json |
| 11 | +import pytest |
| 12 | +from unittest.mock import patch, AsyncMock |
| 13 | +from main import app |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def client(): |
| 18 | + """Fixture to create a test client for the Flask app.""" |
| 19 | + app.config["TESTING"] = True |
| 20 | + with app.test_client() as client: |
| 21 | + yield client |
| 22 | + |
| 23 | + |
| 24 | +def test_root_endpoint(client): |
| 25 | + """Test the root endpoint (/).""" |
| 26 | + response = client.get("/") |
| 27 | + assert response.status_code == 200 |
| 28 | + |
| 29 | + description = "A Telex integration/plugin that sends a coding challenge in a channel every morning to sharpen developer skills." |
| 30 | + |
| 31 | + data = response.get_json() |
| 32 | + assert "app_name" in data |
| 33 | + assert "description" in data |
| 34 | + assert "type" in data |
| 35 | + assert "category" in data |
| 36 | + assert data.get("app_name") == "Codex" |
| 37 | + assert data.get("description") == description |
| 38 | + assert data.get("type") == "Interval Integration" |
| 39 | + assert data.get("category") == "Development & Code Management" |
| 40 | + |
| 41 | + |
| 42 | +def test_get_coding_challenge(client): |
| 43 | + """Test the /integration.json endpoint.""" |
| 44 | + response = client.get("/integration.json") |
| 45 | + assert response.status_code == 200 |
| 46 | + |
| 47 | + data = response.get_json() |
| 48 | + assert "data" in data |
| 49 | + assert "descriptions" in data.get("data") |
| 50 | + assert "settings" in data.get("data") |
| 51 | + assert "tick_url" in data.get("data") |
| 52 | + assert data["data"]["descriptions"]["app_name"] == "Codex" |
| 53 | + |
| 54 | + |
| 55 | +@patch("main.coding_challenge", new_callable=AsyncMock) |
| 56 | +def test_tick_endpoint(mock_coding_challenge, client): |
| 57 | + """Test the /tick endpoint by mocking the async function.""" |
| 58 | + mock_coding_challenge.return_value = None # Simulate a successful async call |
| 59 | + |
| 60 | + payload = { |
| 61 | + "channel_id": "test-channel", |
| 62 | + "return_url": "https://test.return.url", |
| 63 | + "settings": [ |
| 64 | + { |
| 65 | + "label": "interval", |
| 66 | + "type": "text", |
| 67 | + "required": True, |
| 68 | + "default": "*/1 * * *", |
| 69 | + } |
| 70 | + ], |
| 71 | + } |
| 72 | + |
| 73 | + response = client.post( |
| 74 | + "/tick", data=json.dumps(payload), content_type="application/json" |
| 75 | + ) |
| 76 | + assert response.status_code == 202 |
| 77 | + |
| 78 | + data = response.get_json() |
| 79 | + assert "message" in data |
| 80 | + assert data.get("message") == "Coding Challenge delivered" |
| 81 | + |
| 82 | + # Ensure the async function was called with the correct payload |
| 83 | + mock_coding_challenge.assert_called_once() |
| 84 | + |
| 85 | + |
| 86 | +def test_tick_endpoint_invalid_payload(client): |
| 87 | + """Test the /tick endpoint with invalid payload (missing required fields).""" |
| 88 | + invalid_payload = {"channel_id": "test-channel"} # Missing return_url and settings |
| 89 | + |
| 90 | + response = client.post( |
| 91 | + "/tick", data=json.dumps(invalid_payload), content_type="application/json" |
| 92 | + ) |
| 93 | + assert response.status_code == 400 |
| 94 | + |
| 95 | + data = response.get_json() |
| 96 | + assert "error" in data # Error message should be returned |
0 commit comments