File size: 10,176 Bytes
cf09ddc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""Integration tests for WallSection API endpoints."""

from __future__ import annotations

import pytest
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient


@pytest.mark.django_db
@pytest.mark.integration
class TestWallSectionAPI:
    """Test WallSection CRUD operations via REST API."""

    def test_create_wall_section_success(self, api_client: APIClient) -> None:
        """Test creating a wall section for a profile returns 201."""
        profile_url = reverse("profile-list")
        profile = api_client.post(
            profile_url,
            {"name": "Northern Watch", "team_lead": "Jon Snow"},
            format="json",
        ).data

        url = reverse("wallsection-list")
        payload = {
            "profile": profile["id"],
            "section_name": "Tower 1-2",
        }

        response = api_client.post(url, payload, format="json")

        assert response.status_code == status.HTTP_201_CREATED
        assert response.data["section_name"] == "Tower 1-2"
        assert response.data["profile"] == profile["id"]
        assert "id" in response.data
        assert "created_at" in response.data

    def test_create_wall_section_duplicate_name_for_profile_fails(self, api_client: APIClient) -> None:
        """Test duplicate section name for same profile returns 400."""
        profile_url = reverse("profile-list")
        profile = api_client.post(
            profile_url,
            {"name": "Northern Watch", "team_lead": "Jon Snow"},
            format="json",
        ).data

        url = reverse("wallsection-list")
        payload = {
            "profile": profile["id"],
            "section_name": "Tower 1-2",
        }

        api_client.post(url, payload, format="json")
        response = api_client.post(url, payload, format="json")

        assert response.status_code == status.HTTP_400_BAD_REQUEST

    def test_create_wall_section_same_name_different_profiles_succeeds(self, api_client: APIClient) -> None:
        """Test same section name for different profiles is allowed."""
        profile_url = reverse("profile-list")
        profile1 = api_client.post(
            profile_url,
            {"name": "Northern Watch", "team_lead": "Jon Snow"},
            format="json",
        ).data
        profile2 = api_client.post(
            profile_url,
            {"name": "Eastern Defense", "team_lead": "Tormund"},
            format="json",
        ).data

        url = reverse("wallsection-list")
        payload1 = {
            "profile": profile1["id"],
            "section_name": "Tower 1-2",
        }
        payload2 = {
            "profile": profile2["id"],
            "section_name": "Tower 1-2",
        }

        response1 = api_client.post(url, payload1, format="json")
        response2 = api_client.post(url, payload2, format="json")

        assert response1.status_code == status.HTTP_201_CREATED
        assert response2.status_code == status.HTTP_201_CREATED

    def test_list_wall_sections(self, api_client: APIClient) -> None:
        """Test listing wall sections returns all sections."""
        profile_url = reverse("profile-list")
        profile = api_client.post(
            profile_url,
            {"name": "Northern Watch", "team_lead": "Jon Snow"},
            format="json",
        ).data

        section_url = reverse("wallsection-list")
        api_client.post(
            section_url,
            {
                "profile": profile["id"],
                "section_name": "Tower 1-2",
            },
            format="json",
        )
        api_client.post(
            section_url,
            {
                "profile": profile["id"],
                "section_name": "Tower 2-3",
            },
            format="json",
        )

        response = api_client.get(section_url)

        assert response.status_code == status.HTTP_200_OK
        assert response.data["count"] == 2
        assert len(response.data["results"]) == 2

    def test_filter_wall_sections_by_profile(self, api_client: APIClient) -> None:
        """Test filtering sections by profile ID."""
        profile_url = reverse("profile-list")
        profile1 = api_client.post(
            profile_url,
            {"name": "Northern Watch", "team_lead": "Jon Snow"},
            format="json",
        ).data
        profile2 = api_client.post(
            profile_url,
            {"name": "Eastern Defense", "team_lead": "Tormund"},
            format="json",
        ).data

        section_url = reverse("wallsection-list")
        api_client.post(
            section_url,
            {
                "profile": profile1["id"],
                "section_name": "Tower 1-2",
            },
            format="json",
        )
        api_client.post(
            section_url,
            {
                "profile": profile2["id"],
                "section_name": "Tower 3-4",
            },
            format="json",
        )

        response = api_client.get(section_url, {"profile": profile1["id"]})

        assert response.status_code == status.HTTP_200_OK
        assert response.data["count"] == 1
        assert response.data["results"][0]["section_name"] == "Tower 1-2"

    def test_retrieve_wall_section(self, api_client: APIClient) -> None:
        """Test retrieving a specific wall section."""
        profile_url = reverse("profile-list")
        profile = api_client.post(
            profile_url,
            {"name": "Northern Watch", "team_lead": "Jon Snow"},
            format="json",
        ).data

        section_url = reverse("wallsection-list")
        section = api_client.post(
            section_url,
            {
                "profile": profile["id"],
                "section_name": "Tower 1-2",
            },
            format="json",
        ).data

        detail_url = reverse("wallsection-detail", kwargs={"pk": section["id"]})
        response = api_client.get(detail_url)

        assert response.status_code == status.HTTP_200_OK
        assert response.data["id"] == section["id"]
        assert response.data["section_name"] == "Tower 1-2"

    def test_update_wall_section(self, api_client: APIClient) -> None:
        """Test updating a wall section."""
        profile_url = reverse("profile-list")
        profile = api_client.post(
            profile_url,
            {"name": "Northern Watch", "team_lead": "Jon Snow"},
            format="json",
        ).data

        section_url = reverse("wallsection-list")
        section = api_client.post(
            section_url,
            {
                "profile": profile["id"],
                "section_name": "Tower 1-2",
            },
            format="json",
        ).data

        detail_url = reverse("wallsection-detail", kwargs={"pk": section["id"]})
        updated_payload = {
            "profile": profile["id"],
            "section_name": "Tower 1-2 Extended",
        }
        response = api_client.put(detail_url, updated_payload, format="json")

        assert response.status_code == status.HTTP_200_OK
        assert response.data["section_name"] == "Tower 1-2 Extended"

    def test_delete_wall_section(self, api_client: APIClient) -> None:
        """Test deleting a wall section."""
        profile_url = reverse("profile-list")
        profile = api_client.post(
            profile_url,
            {"name": "Northern Watch", "team_lead": "Jon Snow"},
            format="json",
        ).data

        section_url = reverse("wallsection-list")
        section = api_client.post(
            section_url,
            {
                "profile": profile["id"],
                "section_name": "Tower 1-2",
            },
            format="json",
        ).data

        detail_url = reverse("wallsection-detail", kwargs={"pk": section["id"]})
        response = api_client.delete(detail_url)

        assert response.status_code == status.HTTP_204_NO_CONTENT

        retrieve_response = api_client.get(detail_url)
        assert retrieve_response.status_code == status.HTTP_404_NOT_FOUND

    def test_delete_profile_cascades_to_sections(self, api_client: APIClient) -> None:
        """Test deleting a profile also deletes associated wall sections."""
        profile_url = reverse("profile-list")
        profile = api_client.post(
            profile_url,
            {"name": "Northern Watch", "team_lead": "Jon Snow"},
            format="json",
        ).data

        section_url = reverse("wallsection-list")
        section = api_client.post(
            section_url,
            {
                "profile": profile["id"],
                "section_name": "Tower 1-2",
            },
            format="json",
        ).data

        profile_detail_url = reverse("profile-detail", kwargs={"pk": profile["id"]})
        api_client.delete(profile_detail_url)

        section_detail_url = reverse("wallsection-detail", kwargs={"pk": section["id"]})
        response = api_client.get(section_detail_url)
        assert response.status_code == status.HTTP_404_NOT_FOUND

    def test_wall_section_requires_profile(self, api_client: APIClient) -> None:
        """Test creating section without profile returns 400."""
        url = reverse("wallsection-list")
        payload = {
            "section_name": "Tower 1-2",
        }

        response = api_client.post(url, payload, format="json")

        assert response.status_code == status.HTTP_400_BAD_REQUEST
        assert "profile" in response.data

    def test_wall_section_requires_section_name(self, api_client: APIClient) -> None:
        """Test creating section without section_name returns 400."""
        profile_url = reverse("profile-list")
        profile = api_client.post(
            profile_url,
            {"name": "Northern Watch", "team_lead": "Jon Snow"},
            format="json",
        ).data

        url = reverse("wallsection-list")
        payload = {
            "profile": profile["id"],
        }

        response = api_client.post(url, payload, format="json")

        assert response.status_code == status.HTTP_400_BAD_REQUEST
        assert "section_name" in response.data