Eslonf commited on
Commit
ba2d0dc
·
1 Parent(s): 6261ac4

test: Add validation test for non-image file uploads

Browse files
Files changed (2) hide show
  1. tests/assets/fake_image.txt +1 -0
  2. tests/test_main.py +24 -2
tests/assets/fake_image.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ this is not an image
tests/test_main.py CHANGED
@@ -23,7 +23,6 @@ def test_read_main_page():
23
  # 3. Проверка содержимого:
24
  assert "Генератор объявлений" in response.text
25
 
26
-
27
  # --- Тест-кейс №2 ---
28
  def test_submit_image_successfully(mocker):
29
  """
@@ -53,4 +52,27 @@ def test_submit_image_successfully(mocker):
53
 
54
  # Утверждаем, что в JSON-ответе есть ключ "task_id".
55
  data = response.json()
56
- assert "task_id" in data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # 3. Проверка содержимого:
24
  assert "Генератор объявлений" in response.text
25
 
 
26
  # --- Тест-кейс №2 ---
27
  def test_submit_image_successfully(mocker):
28
  """
 
52
 
53
  # Утверждаем, что в JSON-ответе есть ключ "task_id".
54
  data = response.json()
55
+ assert "task_id" in data
56
+
57
+ # --- Тест-кейс №3 ---
58
+ def test_submit_wrong_file_type():
59
+ """
60
+ Тест проверяет, что сервер правильно отклоняет файлы, не являющиеся изображениями.
61
+ """
62
+ # 1. Действие:
63
+ # Открываем текстовый файл и отправляем его на сервер.
64
+ with open("tests/assets/fake_image.txt", "rb") as f:
65
+ response = client.post(
66
+ "/generate-ad",
67
+ data={"style": "brief"},
68
+ files={"image": ("fake_image.txt", f, "text/plain")}
69
+ )
70
+
71
+ # 2. Проверки (Asserts):
72
+ # Утверждаем, что сервер ответил кодом 400 (Bad Request).
73
+ assert response.status_code == 400
74
+
75
+ # Утверждаем, что в JSON-ответе содержится правильное сообщение об ошибке.
76
+ data = response.json()
77
+ assert "detail" in data
78
+ assert data["detail"] == "Недопустимый тип файла."