|
| 1 | +from datetime import datetime, timezone |
| 2 | +import pytest |
| 3 | +from fastapi.testclient import TestClient |
| 4 | +from unittest.mock import MagicMock |
| 5 | +from main import app |
| 6 | +from api.db.database import get_db |
| 7 | +from api.v1.models.blog import Blog |
| 8 | + |
| 9 | +client = TestClient(app) |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def db_session_mock(): |
| 14 | + return MagicMock() |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(autouse=True) |
| 18 | +def override_get_db(db_session_mock): |
| 19 | + app.dependency_overrides[get_db] = lambda: db_session_mock |
| 20 | + yield |
| 21 | + app.dependency_overrides[get_db] = None |
| 22 | + |
| 23 | + |
| 24 | +def test_create_blog_success(db_session_mock): |
| 25 | + """Checks if a new blog post can be created successfully and |
| 26 | + validates the response data.""" |
| 27 | + new_blog_data = { |
| 28 | + "title": "New Blog Post", |
| 29 | + "excerpt": "A summary of the blog post...", |
| 30 | + "content": "The content of the blog post...", |
| 31 | + "image_url": "image-url-link", |
| 32 | + } |
| 33 | + |
| 34 | + # Mocking the behavior of the database session |
| 35 | + db_session_mock.query.return_value.filter.return_value.first.return_value = None |
| 36 | + db_session_mock.add.side_effect = lambda x: setattr(x, "id", 1) |
| 37 | + db_session_mock.commit.side_effect = lambda: None |
| 38 | + db_session_mock.refresh.side_effect = lambda blog: setattr( |
| 39 | + blog, "created_at", datetime.now(timezone.utc) |
| 40 | + ) or setattr(blog, "updated_at", datetime.now(timezone.utc)) |
| 41 | + |
| 42 | + # Creating the mock blog object |
| 43 | + blog_mock = Blog(**new_blog_data) |
| 44 | + blog_mock.id = 1 |
| 45 | + blog_mock.created_at = datetime.now(timezone.utc) |
| 46 | + blog_mock.updated_at = datetime.now(timezone.utc) |
| 47 | + |
| 48 | + db_session_mock.query.return_value.filter.return_value.first.return_value = None |
| 49 | + db_session_mock.add.side_effect = lambda x: setattr(x, "id", blog_mock.id) |
| 50 | + db_session_mock.commit.side_effect = lambda: None |
| 51 | + db_session_mock.refresh.side_effect = lambda x: setattr( |
| 52 | + x, "created_at", blog_mock.created_at |
| 53 | + ) or setattr(x, "updated_at", blog_mock.updated_at) |
| 54 | + |
| 55 | + response = client.post("/api/v1/blogs", json=new_blog_data) |
| 56 | + |
| 57 | + assert response.status_code == 201 |
| 58 | + response_data = response.json() |
| 59 | + assert response_data["title"] == new_blog_data["title"] |
| 60 | + assert response_data["excerpt"] == new_blog_data["excerpt"] |
| 61 | + assert response_data["content"] == new_blog_data["content"] |
| 62 | + assert response_data["image_url"] == new_blog_data["image_url"] |
| 63 | + assert "created_at" in response_data |
| 64 | + assert "updated_at" in response_data |
| 65 | + |
| 66 | + |
| 67 | +def test_create_blog_conflict(db_session_mock): |
| 68 | + """Simulates a conflict scenario by creating a blog post with a title that |
| 69 | + already exists, then checks for the correct status code and error message.""" |
| 70 | + # Arrange |
| 71 | + new_blog_data = { |
| 72 | + "title": "Existing Blog Post", |
| 73 | + "excerpt": "A summary of the blog post...", |
| 74 | + "content": "The content of the blog post...", |
| 75 | + "image_url": "image-url-link", |
| 76 | + } |
| 77 | + |
| 78 | + # Mock the database query for checking existing blog |
| 79 | + db_session_mock.query.return_value.filter.return_value.first.return_value = Blog( |
| 80 | + title="Existing Blog Post", excerpt="...", content="...", image_url="..." |
| 81 | + ) |
| 82 | + |
| 83 | + # Act |
| 84 | + response = client.post("/api/v1/blogs", json=new_blog_data) |
| 85 | + |
| 86 | + # Assert |
| 87 | + assert response.status_code == 409 |
| 88 | + assert response.json()["detail"] == "A blog post with this title already exists." |
| 89 | + |
| 90 | + |
| 91 | +def test_create_blog_internal_server_error(db_session_mock): |
| 92 | + """Simulates an internal server error and checks for the |
| 93 | + correct status code and error message.""" |
| 94 | + new_blog_data = { |
| 95 | + "title": "New Blog Post", |
| 96 | + "excerpt": "A summary of the blog post...", |
| 97 | + "content": "The content of the blog post...", |
| 98 | + "image_url": "image-url-link", |
| 99 | + } |
| 100 | + |
| 101 | + db_session_mock.query.side_effect = Exception("Unexpected error") |
| 102 | + |
| 103 | + response = client.post("/api/v1/blogs", json=new_blog_data) |
| 104 | + |
| 105 | + assert response.status_code == 500 |
| 106 | + assert response.json()["detail"] == "Internal server error." |
| 107 | + |
| 108 | + |
| 109 | +def test_create_blog_invalid_data(db_session_mock): |
| 110 | + """Sends invalid data (e.g., empty title) and checks for validation errors.""" |
| 111 | + invalid_blog_data = { |
| 112 | + "title": "", # Title is required and cannot be empty |
| 113 | + "excerpt": "A summary of the blog post...", |
| 114 | + "content": "The content of the blog post...", |
| 115 | + "image_url": "image-url-link", |
| 116 | + } |
| 117 | + |
| 118 | + response = client.post("/api/v1/blogs", json=invalid_blog_data) |
| 119 | + |
| 120 | + # Unprocessable Entity (validation error) |
| 121 | + assert response.status_code == 422 |
| 122 | + response_data = response.json() |
| 123 | + assert "detail" in response_data |
| 124 | + |
| 125 | + |
| 126 | +def test_create_blog_boundary_testing(db_session_mock): |
| 127 | + """Tests the maximum length constraints for the title and excerpt fields, |
| 128 | + ensuring the API handles boundary conditions correctly.""" |
| 129 | + |
| 130 | + boundary_blog_data = { |
| 131 | + "title": "T" * 255, # Maximum allowed length for title |
| 132 | + "excerpt": "E" * 300, # Maximum allowed length for excerpt |
| 133 | + "content": "Content of the blog post...", |
| 134 | + "image_url": "image-url-link", |
| 135 | + } |
| 136 | + |
| 137 | + # Mocking the behavior of the database session |
| 138 | + db_session_mock.query.return_value.filter.return_value.first.return_value = None |
| 139 | + db_session_mock.add.side_effect = lambda x: setattr(x, "id", 1) |
| 140 | + db_session_mock.commit.side_effect = lambda: None |
| 141 | + db_session_mock.refresh.side_effect = lambda x: setattr( |
| 142 | + x, "created_at", datetime.now(timezone.utc) |
| 143 | + ) or setattr(x, "updated_at", datetime.now(timezone.utc)) |
| 144 | + |
| 145 | + response = client.post("/api/v1/blogs", json=boundary_blog_data) |
| 146 | + |
| 147 | + assert response.status_code == 201 |
| 148 | + response_data = response.json() |
| 149 | + assert response_data["title"] == boundary_blog_data["title"] |
| 150 | + assert response_data["excerpt"] == boundary_blog_data["excerpt"] |
| 151 | + assert response_data["content"] == boundary_blog_data["content"] |
| 152 | + assert response_data["image_url"] == boundary_blog_data["image_url"] |
| 153 | + assert "created_at" in response_data |
| 154 | + assert "updated_at" in response_data |
| 155 | + assert isinstance( |
| 156 | + response_data["created_at"], str |
| 157 | + ) # Check if it's a string representation of a datetime |
| 158 | + assert isinstance(response_data["updated_at"], str) |
0 commit comments