Skip to content

Commit 6289db2

Browse files
committed
Refactor test fixtures to use valid authentication headers and remove unused community fixture
1 parent b05e4de commit 6289db2

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
lines changed

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async def community(session: AsyncSession):
9696

9797

9898
@pytest_asyncio.fixture()
99-
async def token(async_client: AsyncGenerator[AsyncClient, None]) -> str:
99+
async def token(async_client: AsyncClient) -> str:
100100
form_data = {
101101
"grant_type": "password",
102102
"username": CommunityCredentials.username,

tests/test_libraries.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import date
2+
from typing import Mapping
23

34
import pytest
4-
import pytest_asyncio
55
from httpx import AsyncClient
66
from sqlmodel import select
77
from sqlmodel.ext.asyncio.session import AsyncSession
@@ -11,15 +11,6 @@
1111
from app.services.database.models import Community, Library
1212

1313

14-
@pytest_asyncio.fixture
15-
async def community(session: AsyncSession):
16-
community = Community(username="admin", email="[email protected]", password="123")
17-
session.add(community)
18-
await session.commit()
19-
await session.refresh(community)
20-
return community
21-
22-
2314
@pytest.mark.asyncio
2415
async def test_insert_libraries(session: AsyncSession, community: Community):
2516
library = Library(
@@ -60,7 +51,9 @@ async def test_insert_libraries(session: AsyncSession, community: Community):
6051

6152
@pytest.mark.asyncio
6253
async def test_post_libraries_endpoint(
63-
async_client: AsyncClient, session: AsyncSession
54+
session: AsyncSession,
55+
async_client: AsyncClient,
56+
valid_auth_headers: Mapping[str, str],
6457
):
6558
body = {
6659
"library_name": "FastAPI",
@@ -79,7 +72,7 @@ async def test_post_libraries_endpoint(
7972
response = await async_client.post(
8073
"/api/libraries",
8174
json=body,
82-
headers={"Content-Type": "application/json"},
75+
headers=valid_auth_headers,
8376
)
8477

8578
assert response.status_code == 200
@@ -104,7 +97,10 @@ async def test_post_libraries_endpoint(
10497

10598

10699
@pytest.mark.asyncio
107-
async def test_get_libraries_by_language(async_client: AsyncClient):
100+
async def test_get_libraries_by_language(
101+
async_client: AsyncClient,
102+
valid_auth_headers: Mapping[str, str],
103+
):
108104
body = {
109105
"library_name": "FastAPI",
110106
"news": [
@@ -122,7 +118,7 @@ async def test_get_libraries_by_language(async_client: AsyncClient):
122118
responsePOST = await async_client.post(
123119
"/api/libraries",
124120
json=body,
125-
headers={"Content-Type": "application/json"},
121+
headers=valid_auth_headers,
126122
)
127123

128124
assert responsePOST.status_code == 200
@@ -131,6 +127,7 @@ async def test_get_libraries_by_language(async_client: AsyncClient):
131127
response = await async_client.get(
132128
"/api/libraries",
133129
params={"language": "Python"},
130+
headers=valid_auth_headers,
134131
)
135132

136133
assert response.status_code == 200
@@ -153,7 +150,10 @@ async def test_get_libraries_by_language(async_client: AsyncClient):
153150

154151

155152
@pytest.mark.asyncio
156-
async def test_get_libraries_by_inexistent_language(async_client: AsyncClient):
153+
async def test_get_libraries_by_inexistent_language(
154+
async_client: AsyncClient,
155+
valid_auth_headers: Mapping[str, str],
156+
):
157157
body = {
158158
"library_name": "FastAPI",
159159
"news": [
@@ -171,7 +171,7 @@ async def test_get_libraries_by_inexistent_language(async_client: AsyncClient):
171171
responsePOST = await async_client.post(
172172
"/api/libraries",
173173
json=body,
174-
headers={"Content-Type": "application/json"},
174+
headers=valid_auth_headers,
175175
)
176176

177177
assert responsePOST.status_code == 200
@@ -180,6 +180,7 @@ async def test_get_libraries_by_inexistent_language(async_client: AsyncClient):
180180
response = await async_client.get(
181181
"/api/libraries",
182182
params={"language": "NodeJS"},
183+
headers=valid_auth_headers,
183184
)
184185

185186
assert response.status_code == 200

tests/test_libraries_request.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1+
from typing import Mapping
2+
13
import pytest
2-
import pytest_asyncio
34
from httpx import AsyncClient
45
from sqlmodel import select
56
from sqlmodel.ext.asyncio.session import AsyncSession
67

78
from app.services.database.models import Community, LibraryRequest
89

910

10-
@pytest_asyncio.fixture
11-
async def community(session: AsyncSession):
12-
community = Community(username="admin", email="[email protected]", password="123")
13-
session.add(community)
14-
await session.commit()
15-
await session.refresh(community)
16-
return community
17-
18-
1911
@pytest.mark.asyncio
2012
async def test_insert_libraries(session: AsyncSession, community: Community):
2113
library = LibraryRequest(
@@ -41,14 +33,17 @@ async def test_insert_libraries(session: AsyncSession, community: Community):
4133

4234
@pytest.mark.asyncio
4335
async def test_post_libraries_endpoint(
44-
async_client: AsyncClient, session: AsyncSession
36+
async_client: AsyncClient,
37+
session: AsyncSession,
38+
community: Community,
39+
valid_auth_headers: Mapping[str, str],
4540
):
4641
body = {"library_name": "FastAPI", "library_home_page": "http://teste.com/"}
4742

4843
response = await async_client.post(
4944
"/api/libraries/request",
5045
json=body,
51-
headers={"Content-Type": "application/json", "user-email": "[email protected]"},
46+
headers=valid_auth_headers,
5247
)
5348

5449
assert response.status_code == 200
@@ -61,5 +56,5 @@ async def test_post_libraries_endpoint(
6156
created_request = result.first()
6257

6358
assert created_request is not None
64-
assert created_request.user_email == "[email protected]"
59+
assert created_request.user_email == community.email
6560
assert created_request.library_home_page == "http://teste.com/"

tests/test_subscriptions.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from typing import Mapping
2+
13
import pytest
2-
import pytest_asyncio
34
from httpx import AsyncClient
45
from sqlmodel import select
56
from sqlmodel.ext.asyncio.session import AsyncSession
@@ -8,15 +9,6 @@
89
from app.services.database.models import Community, Subscription
910

1011

11-
@pytest_asyncio.fixture
12-
async def community(session: AsyncSession):
13-
community = Community(username="admin", email="[email protected]", password="123")
14-
session.add(community)
15-
await session.commit()
16-
await session.refresh(community)
17-
return community
18-
19-
2012
@pytest.mark.asyncio
2113
async def test_insert_subscription(session: AsyncSession, community: Community):
2214
subscription = Subscription(
@@ -45,7 +37,10 @@ async def test_insert_subscription(session: AsyncSession, community: Community):
4537
assert found.community_id == community.id
4638

4739

48-
async def preset_libraries_with_http_post(async_client: AsyncClient):
40+
async def preset_libraries_with_http_post(
41+
async_client: AsyncClient,
42+
valid_auth_headers: Mapping[str, str],
43+
):
4944
body1 = {
5045
"library_name": "Flask",
5146
"news": [
@@ -63,7 +58,7 @@ async def preset_libraries_with_http_post(async_client: AsyncClient):
6358
response1 = await async_client.post(
6459
"/api/libraries",
6560
json=body1,
66-
headers={"Content-Type": "application/json"},
61+
headers=valid_auth_headers,
6762
)
6863

6964
assert response1.status_code == 200
@@ -85,17 +80,22 @@ async def preset_libraries_with_http_post(async_client: AsyncClient):
8580
response2 = await async_client.post(
8681
"/api/libraries",
8782
json=body2,
88-
headers={"Content-Type": "application/json"},
83+
headers=valid_auth_headers,
8984
)
9085

9186
assert response2.status_code == 200
9287

9388

9489
@pytest.mark.asyncio
9590
async def test_post_subscribe_endpoint(
96-
async_client: AsyncClient, session: AsyncSession
91+
async_client: AsyncClient,
92+
session: AsyncSession,
93+
community: Community,
94+
valid_auth_headers: Mapping[str, str],
9795
):
98-
await preset_libraries_with_http_post(async_client=async_client)
96+
await preset_libraries_with_http_post(
97+
async_client=async_client, valid_auth_headers=valid_auth_headers
98+
)
9999

100100
body = {
101101
"tags": ["bug_fix", "updates"],
@@ -105,13 +105,15 @@ async def test_post_subscribe_endpoint(
105105
response = await async_client.post(
106106
"/api/libraries/subscribe",
107107
json=body,
108-
headers={"Content-Type": "application/json", "user-email": "[email protected]"},
108+
headers=valid_auth_headers,
109109
)
110110

111111
assert response.status_code == 200
112112
assert response.json()["status"] == "Subscribed in libraries successfully"
113113

114-
statement = select(Subscription).where(Subscription.user_email == "[email protected]")
114+
statement = select(Subscription).where(
115+
Subscription.user_email == community.email
116+
)
115117
result = await session.exec(statement)
116118
created_subscriptions = result.all()
117119

@@ -130,8 +132,11 @@ async def test_post_subscribe_endpoint(
130132
@pytest.mark.asyncio
131133
async def test_post_subscribe_endpoint_with_unexistents_libraries(
132134
async_client: AsyncClient,
135+
valid_auth_headers: Mapping[str, str],
133136
):
134-
await preset_libraries_with_http_post(async_client=async_client)
137+
await preset_libraries_with_http_post(
138+
async_client=async_client, valid_auth_headers=valid_auth_headers
139+
)
135140

136141
body = {
137142
"tags": ["bug_fix", "updates"],
@@ -141,7 +146,7 @@ async def test_post_subscribe_endpoint_with_unexistents_libraries(
141146
response = await async_client.post(
142147
"/api/libraries/subscribe",
143148
json=body,
144-
headers={"Content-Type": "application/json", "user-email": "[email protected]"},
149+
headers=valid_auth_headers,
145150
)
146151

147152
assert response.status_code == 404

0 commit comments

Comments
 (0)