• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

iplweb / bpp / 2e69921e-aa42-40fa-b731-e454f5df7d43

24 Aug 2025 10:25PM UTC coverage: 41.915% (-0.5%) from 42.367%
2e69921e-aa42-40fa-b731-e454f5df7d43

push

circleci

mpasternak
Merge tag 'v202508.1205' into dev

Nowa wersja: v202508.1205 v202508.1205

1 of 1 new or added line in 1 file covered. (100.0%)

1503 existing lines in 86 files now uncovered.

16348 of 39003 relevant lines covered (41.91%)

0.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
src/pbn_api/tests/test_admin.py
1
from unittest.mock import patch
×
2

3
import pytest
×
4
from django.urls import reverse
×
UNCOV
5
from model_bakery import baker
×
6
from selenium.webdriver.support.wait import WebDriverWait
×
7

8
from pbn_api.admin import OswiadczeniaInstytucjiAdmin, PBN_Export_QueueAdmin
×
9
from pbn_api.client import PBN_DELETE_PUBLICATION_STATEMENT
×
UNCOV
10
from pbn_api.models import OswiadczenieInstytucji, PBN_Export_Queue, SentData
×
11
from pbn_api.tests.utils import middleware
×
12

UNCOV
13
from django.contrib.admin.sites import AdminSite
×
14
from django.contrib.contenttypes.models import ContentType
×
UNCOV
15
from django.contrib.messages import get_messages
×
16

17
from django_bpp.selenium_util import LONG_WAIT_TIME, wait_for, wait_for_page_load
×
18

19

20
def test_SentDataAdmin_list_filter_works(admin_client):
×
UNCOV
21
    url = reverse("admin:pbn_api_sentdata_changelist")
×
UNCOV
22
    res = admin_client.get(url + "?q=123")
×
23
    assert res.status_code == 200
×
24

25

26
def test_PublisherAdmin_search_works(admin_client):
×
UNCOV
27
    url = reverse("admin:pbn_api_publisher_changelist")
×
UNCOV
28
    res = admin_client.get(url + "?q=123")
×
29
    assert res.status_code == 200
×
30

31

32
@pytest.mark.django_db
×
UNCOV
33
def test_OswiadczenieInstytucji_delete_model(pbn_uczelnia, pbn_client, rf):
×
34
    oi = baker.make(OswiadczenieInstytucji)
×
UNCOV
35
    req = rf.get("/")
×
36

UNCOV
37
    pbn_client.transport.return_values[
×
38
        PBN_DELETE_PUBLICATION_STATEMENT.format(publicationId=oi.publicationId_id)
39
    ] = {"1": "2"}
40

UNCOV
41
    with middleware(req):
×
UNCOV
42
        OswiadczeniaInstytucjiAdmin(OswiadczenieInstytucji, None).delete_model(
×
43
            req, oi, pbn_client=pbn_client
44
        )
45

46
    try:
×
47
        OswiadczenieInstytucji.objects.get(pk=oi.pk)
×
48
        msg = list(get_messages(req))
×
49
        if msg:
×
50
            raise Exception(str(msg))
×
UNCOV
51
        raise Exception("Nie został skasowany")
×
UNCOV
52
    except OswiadczenieInstytucji.DoesNotExist:
×
53
        assert True  # good
×
54

55

UNCOV
56
@pytest.mark.django_db
×
57
def test_pbn_api_admin_SentDataAdmin_wyslij_ponownie(
×
58
    wydawnictwo_zwarte, admin_browser, channels_live_server
59
):
UNCOV
60
    s = SentData.objects.create(
×
61
        object_id=wydawnictwo_zwarte.pk,
62
        content_type=ContentType.objects.get_for_model(wydawnictwo_zwarte),
63
        data_sent={"foo": "bar"},
64
    )
65

UNCOV
66
    with wait_for_page_load(admin_browser):
×
UNCOV
67
        admin_browser.visit(
×
68
            channels_live_server.url + f"/admin/pbn_api/sentdata/{s.pk}/change"
69
        )
70

UNCOV
71
    wait_for(lambda: len(admin_browser.find_by_id("wyslij-ponownie")) > 0)
×
72

73
    elem = admin_browser.find_by_id("wyslij-ponownie")
×
74

75
    with wait_for_page_load(admin_browser):
×
UNCOV
76
        elem.click()
×
77

78
    WebDriverWait(admin_browser, LONG_WAIT_TIME).until(
×
79
        lambda *args, **kw: "nie będzie eksportowany" in admin_browser.html
80
    )
UNCOV
81
    assert True
×
82

83

84
# Tests for PBN_Export_QueueAdmin
85

86

UNCOV
87
@pytest.mark.django_db
×
UNCOV
88
def test_pbn_export_queue_admin_changelist_loads(admin_client):
×
89
    """Test that PBN_Export_Queue changelist loads without errors"""
UNCOV
90
    url = reverse("admin:pbn_api_pbn_export_queue_changelist")
×
UNCOV
91
    res = admin_client.get(url)
×
UNCOV
92
    assert res.status_code == 200
×
93

94

UNCOV
95
@pytest.mark.django_db
×
UNCOV
96
def test_pbn_export_queue_admin_search_works(admin_client):
×
97
    """Test that PBN_Export_Queue admin search works"""
UNCOV
98
    url = reverse("admin:pbn_api_pbn_export_queue_changelist")
×
UNCOV
99
    res = admin_client.get(url + "?q=testuser")
×
UNCOV
100
    assert res.status_code == 200
×
101

102

UNCOV
103
@pytest.mark.django_db
×
UNCOV
104
def test_pbn_export_queue_admin_resend_single_item(wydawnictwo_ciagle, admin_user):
×
105
    """Test the _resend_single_item method"""
UNCOV
106
    queue_item = baker.make(
×
107
        PBN_Export_Queue,
108
        rekord_do_wysylki=wydawnictwo_ciagle,
109
        zamowil=admin_user,
110
        wysylke_zakonczono="2023-01-01 00:00:00",
111
        zakonczono_pomyslnie=True,
112
        retry_after_user_authorised=True,
113
    )
114

UNCOV
115
    admin_instance = PBN_Export_QueueAdmin(PBN_Export_Queue, AdminSite())
×
116

UNCOV
117
    with patch(
×
118
        "pbn_api.admin.pbn_export_queue.task_sprobuj_wyslac_do_pbn.delay"
119
    ) as mock_task:
UNCOV
120
        admin_instance._resend_single_item(queue_item, admin_user, " (test)")
×
121

122
        # Check that status fields were reset
UNCOV
123
        queue_item.refresh_from_db()
×
UNCOV
124
        assert queue_item.wysylke_zakonczono is None
×
UNCOV
125
        assert queue_item.zakonczono_pomyslnie is None
×
UNCOV
126
        assert queue_item.retry_after_user_authorised is None
×
127

128
        # Check that message was added to komunikat
UNCOV
129
        assert (
×
130
            f"Ponownie wysłano przez użytkownika: {admin_user} (test)"
131
            in queue_item.komunikat
132
        )
133

134
        # Check that task was called
UNCOV
135
        mock_task.assert_called_once_with(queue_item.pk)
×
136

137

UNCOV
138
@pytest.mark.django_db
×
UNCOV
139
def test_pbn_export_queue_admin_resend_action(wydawnictwo_ciagle, admin_user, rf):
×
140
    """Test the resend_to_pbn_action bulk action"""
UNCOV
141
    queue_item1 = baker.make(
×
142
        PBN_Export_Queue,
143
        rekord_do_wysylki=wydawnictwo_ciagle,
144
        zamowil=admin_user,
145
        wysylke_zakonczono="2023-01-01 00:00:00",
146
        zakonczono_pomyslnie=True,
147
    )
148

UNCOV
149
    queue_item2 = baker.make(
×
150
        PBN_Export_Queue,
151
        rekord_do_wysylki=wydawnictwo_ciagle,
152
        zamowil=admin_user,
153
        wysylke_zakonczono="2023-01-01 00:00:00",
154
        zakonczono_pomyslnie=False,
155
    )
156

UNCOV
157
    queryset = PBN_Export_Queue.objects.filter(pk__in=[queue_item1.pk, queue_item2.pk])
×
158

UNCOV
159
    request = rf.post("/")
×
UNCOV
160
    request.user = admin_user
×
161

UNCOV
162
    admin_instance = PBN_Export_QueueAdmin(PBN_Export_Queue, AdminSite())
×
163

UNCOV
164
    with middleware(request):
×
UNCOV
165
        with patch(
×
166
            "pbn_api.admin.pbn_export_queue.task_sprobuj_wyslac_do_pbn.delay"
167
        ) as mock_task:
UNCOV
168
            admin_instance.resend_to_pbn_action(request, queryset)
×
169

170
            # Check that both items were processed
UNCOV
171
            queue_item1.refresh_from_db()
×
UNCOV
172
            queue_item2.refresh_from_db()
×
173

UNCOV
174
            assert queue_item1.wysylke_zakonczono is None
×
UNCOV
175
            assert queue_item1.zakonczono_pomyslnie is None
×
UNCOV
176
            assert queue_item2.wysylke_zakonczono is None
×
UNCOV
177
            assert queue_item2.zakonczono_pomyslnie is None
×
178

179
            # Check that messages were added
UNCOV
180
            assert (
×
181
                f"Ponownie wysłano przez użytkownika: {admin_user} (akcja masowa)"
182
                in queue_item1.komunikat
183
            )
UNCOV
184
            assert (
×
185
                f"Ponownie wysłano przez użytkownika: {admin_user} (akcja masowa)"
186
                in queue_item2.komunikat
187
            )
188

189
            # Check that tasks were called for both items
UNCOV
190
            assert mock_task.call_count == 2
×
191

192

UNCOV
193
@pytest.mark.django_db
×
UNCOV
194
def test_pbn_export_queue_admin_response_change_resend(
×
195
    wydawnictwo_ciagle, admin_user, rf
196
):
197
    """Test the response_change method with _resend_to_pbn POST parameter"""
UNCOV
198
    queue_item = baker.make(
×
199
        PBN_Export_Queue,
200
        rekord_do_wysylki=wydawnictwo_ciagle,
201
        zamowil=admin_user,
202
        wysylke_zakonczono="2023-01-01 00:00:00",
203
        zakonczono_pomyslnie=True,
204
    )
205

UNCOV
206
    request = rf.post("/", {"_resend_to_pbn": "true"})
×
UNCOV
207
    request.user = admin_user
×
208

UNCOV
209
    admin_instance = PBN_Export_QueueAdmin(PBN_Export_Queue, AdminSite())
×
210

UNCOV
211
    with middleware(request):
×
UNCOV
212
        with patch(
×
213
            "pbn_api.admin.pbn_export_queue.task_sprobuj_wyslac_do_pbn.delay"
214
        ) as mock_task:
UNCOV
215
            response = admin_instance.response_change(request, queue_item)
×
216

217
            # Check that we get a redirect response
UNCOV
218
            assert response.status_code == 302
×
UNCOV
219
            assert (
×
220
                f"/admin/pbn_api/pbn_export_queue/{queue_item.pk}/change/"
221
                in response.url
222
            )
223

224
            # Check that item was reset
UNCOV
225
            queue_item.refresh_from_db()
×
UNCOV
226
            assert queue_item.wysylke_zakonczono is None
×
UNCOV
227
            assert queue_item.zakonczono_pomyslnie is None
×
228

229
            # Check that task was called
UNCOV
230
            mock_task.assert_called_once_with(queue_item.pk)
×
231

232

UNCOV
233
@pytest.mark.django_db
×
UNCOV
234
def test_pbn_export_queue_admin_response_change_normal(
×
235
    wydawnictwo_ciagle, admin_user, rf
236
):
237
    """Test the response_change method with normal POST (no resend)"""
UNCOV
238
    queue_item = baker.make(
×
239
        PBN_Export_Queue,
240
        rekord_do_wysylki=wydawnictwo_ciagle,
241
        zamowil=admin_user,
242
    )
243

UNCOV
244
    request = rf.post("/", {"_save": "true"})  # Normal save, not resend
×
UNCOV
245
    request.user = admin_user
×
246

UNCOV
247
    admin_instance = PBN_Export_QueueAdmin(PBN_Export_Queue, AdminSite())
×
248

UNCOV
249
    with middleware(request):
×
UNCOV
250
        with patch(
×
251
            "pbn_api.admin.pbn_export_queue.task_sprobuj_wyslac_do_pbn.delay"
252
        ) as mock_task:
UNCOV
253
            with patch.object(
×
254
                admin_instance.__class__.__bases__[0], "response_change"
255
            ) as mock_super:
UNCOV
256
                mock_super.return_value = "super_response"
×
257

UNCOV
258
                response = admin_instance.response_change(request, queue_item)
×
259

260
                # Check that super().response_change was called
UNCOV
261
                mock_super.assert_called_once_with(request, queue_item)
×
UNCOV
262
                assert response == "super_response"
×
263

264
                # Check that task was NOT called
UNCOV
265
                mock_task.assert_not_called()
×
266

267

UNCOV
268
@pytest.mark.django_db
×
UNCOV
269
def test_pbn_export_queue_admin_save_form(admin_user, rf):
×
270
    """Test save_form method returns form.save(commit=False)"""
UNCOV
271
    from django import forms
×
272

UNCOV
273
    class MockForm(forms.ModelForm):
×
UNCOV
274
        class Meta:
×
UNCOV
275
            model = PBN_Export_Queue
×
UNCOV
276
            fields = []
×
277

UNCOV
278
        def save(self, commit=True):
×
UNCOV
279
            return f"saved_with_commit_{commit}"
×
280

UNCOV
281
    request = rf.post("/")
×
UNCOV
282
    request.user = admin_user
×
283

UNCOV
284
    admin_instance = PBN_Export_QueueAdmin(PBN_Export_Queue, AdminSite())
×
UNCOV
285
    form = MockForm()
×
286

UNCOV
287
    result = admin_instance.save_form(request, form, change=True)
×
UNCOV
288
    assert result == "saved_with_commit_False"
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc