test_items.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from app.core.config import settings
  2. from apps.tests.utils.item import create_random_item
  3. from sqlmodel import Session
  4. from fastapi.testclient import TestClient
  5. def test_create_item(
  6. client: TestClient, superuser_token_headers: dict[str, str], db: Session
  7. ) -> None:
  8. data = {"title": "Foo", "description": "Fighters"}
  9. response = client.post(
  10. f"{settings.API_V1_STR}/items/",
  11. headers=superuser_token_headers,
  12. json=data,
  13. )
  14. assert response.status_code == 200
  15. content = response.json()
  16. assert content["title"] == data["title"]
  17. assert content["description"] == data["description"]
  18. assert "id" in content
  19. assert "owner_id" in content
  20. def test_read_item(
  21. client: TestClient, superuser_token_headers: dict[str, str], db: Session
  22. ) -> None:
  23. item = create_random_item(db)
  24. response = client.get(
  25. f"{settings.API_V1_STR}/items/{item.id}",
  26. headers=superuser_token_headers,
  27. )
  28. assert response.status_code == 200
  29. content = response.json()
  30. assert content["title"] == item.title
  31. assert content["description"] == item.description
  32. assert content["id"] == item.id
  33. assert content["owner_id"] == item.owner_id
  34. def test_read_item_not_found(
  35. client: TestClient, superuser_token_headers: dict[str, str], db: Session
  36. ) -> None:
  37. response = client.get(
  38. f"{settings.API_V1_STR}/items/999",
  39. headers=superuser_token_headers,
  40. )
  41. assert response.status_code == 404
  42. content = response.json()
  43. assert content["detail"] == "Item not found"
  44. def test_read_item_not_enough_permissions(
  45. client: TestClient, normal_user_token_headers: dict[str, str], db: Session
  46. ) -> None:
  47. item = create_random_item(db)
  48. response = client.get(
  49. f"{settings.API_V1_STR}/items/{item.id}",
  50. headers=normal_user_token_headers,
  51. )
  52. assert response.status_code == 400
  53. content = response.json()
  54. assert content["detail"] == "Not enough permissions"
  55. def test_read_items(
  56. client: TestClient, superuser_token_headers: dict[str, str], db: Session
  57. ) -> None:
  58. create_random_item(db)
  59. create_random_item(db)
  60. response = client.get(
  61. f"{settings.API_V1_STR}/items/",
  62. headers=superuser_token_headers,
  63. )
  64. assert response.status_code == 200
  65. content = response.json()
  66. assert len(content["data"]) >= 2
  67. def test_update_item(
  68. client: TestClient, superuser_token_headers: dict[str, str], db: Session
  69. ) -> None:
  70. item = create_random_item(db)
  71. data = {"title": "Updated title", "description": "Updated description"}
  72. response = client.put(
  73. f"{settings.API_V1_STR}/items/{item.id}",
  74. headers=superuser_token_headers,
  75. json=data,
  76. )
  77. assert response.status_code == 200
  78. content = response.json()
  79. assert content["title"] == data["title"]
  80. assert content["description"] == data["description"]
  81. assert content["id"] == item.id
  82. assert content["owner_id"] == item.owner_id
  83. def test_update_item_not_found(
  84. client: TestClient, superuser_token_headers: dict[str, str], db: Session
  85. ) -> None:
  86. data = {"title": "Updated title", "description": "Updated description"}
  87. response = client.put(
  88. f"{settings.API_V1_STR}/items/999",
  89. headers=superuser_token_headers,
  90. json=data,
  91. )
  92. assert response.status_code == 404
  93. content = response.json()
  94. assert content["detail"] == "Item not found"
  95. def test_update_item_not_enough_permissions(
  96. client: TestClient, normal_user_token_headers: dict[str, str], db: Session
  97. ) -> None:
  98. item = create_random_item(db)
  99. data = {"title": "Updated title", "description": "Updated description"}
  100. response = client.put(
  101. f"{settings.API_V1_STR}/items/{item.id}",
  102. headers=normal_user_token_headers,
  103. json=data,
  104. )
  105. assert response.status_code == 400
  106. content = response.json()
  107. assert content["detail"] == "Not enough permissions"
  108. def test_delete_item(
  109. client: TestClient, superuser_token_headers: dict[str, str], db: Session
  110. ) -> None:
  111. item = create_random_item(db)
  112. response = client.delete(
  113. f"{settings.API_V1_STR}/items/{item.id}",
  114. headers=superuser_token_headers,
  115. )
  116. assert response.status_code == 200
  117. content = response.json()
  118. assert content["message"] == "Item deleted successfully"
  119. def test_delete_item_not_found(
  120. client: TestClient, superuser_token_headers: dict[str, str], db: Session
  121. ) -> None:
  122. response = client.delete(
  123. f"{settings.API_V1_STR}/items/999",
  124. headers=superuser_token_headers,
  125. )
  126. assert response.status_code == 404
  127. content = response.json()
  128. assert content["detail"] == "Item not found"
  129. def test_delete_item_not_enough_permissions(
  130. client: TestClient, normal_user_token_headers: dict[str, str], db: Session
  131. ) -> None:
  132. item = create_random_item(db)
  133. response = client.delete(
  134. f"{settings.API_V1_STR}/items/{item.id}",
  135. headers=normal_user_token_headers,
  136. )
  137. assert response.status_code == 400
  138. content = response.json()
  139. assert content["detail"] == "Not enough permissions"