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

iplweb / bpp / 18634744198

19 Oct 2025 07:00PM UTC coverage: 31.618% (-29.9%) from 61.514%
18634744198

push

github

mpasternak
Merge branch 'release/v202510.1270'

657 of 9430 branches covered (6.97%)

Branch coverage included in aggregate %.

229 of 523 new or added lines in 42 files covered. (43.79%)

11303 existing lines in 316 files now uncovered.

14765 of 39346 relevant lines covered (37.53%)

0.38 hits per line

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

21.92
src/bpp_setup_wizard/tests.py
1
import pytest
1✔
2
from django.contrib.auth import get_user_model
1✔
3
from django.test import Client
1✔
4
from django.urls import reverse
1✔
5

6
from bpp.models import Uczelnia
1✔
7

8
BppUser = get_user_model()
1✔
9

10

11
@pytest.fixture(autouse=True)
1✔
12
def enable_setup_wizard_middleware(settings):
1✔
13
    """Enable SetupWizardMiddleware for all tests in this module."""
UNCOV
14
    middleware = list(settings.MIDDLEWARE)
×
UNCOV
15
    if "bpp_setup_wizard.middleware.SetupWizardMiddleware" not in middleware:
×
16
        # Add the middleware after AuthenticationMiddleware to ensure user is available
UNCOV
17
        try:
×
UNCOV
18
            auth_index = middleware.index(
×
19
                "django.contrib.auth.middleware.AuthenticationMiddleware"
20
            )
UNCOV
21
            middleware.insert(
×
22
                auth_index + 1, "bpp_setup_wizard.middleware.SetupWizardMiddleware"
23
            )
24
        except ValueError:
×
25
            # If AuthenticationMiddleware is not found, add at the beginning
26
            middleware.insert(0, "bpp_setup_wizard.middleware.SetupWizardMiddleware")
×
UNCOV
27
        settings.MIDDLEWARE = middleware
×
28

29

30
@pytest.mark.django_db
1✔
31
def test_setup_wizard_redirect_when_no_users():
1✔
32
    """Test that the middleware redirects to setup when no users exist."""
UNCOV
33
    client = Client()
×
34

35
    # Verify no users exist
UNCOV
36
    assert BppUser.objects.count() == 0
×
37

38
    # Try to access the home page
UNCOV
39
    response = client.get("/")
×
40

41
    # Should redirect to setup wizard
UNCOV
42
    assert response.status_code == 302
×
UNCOV
43
    assert response.url == reverse("bpp_setup_wizard:setup")
×
44

45

46
@pytest.mark.django_db
1✔
47
def test_setup_wizard_form_display():
1✔
48
    """Test that the setup wizard form is displayed correctly."""
UNCOV
49
    client = Client()
×
50

51
    # Verify no users exist
UNCOV
52
    assert BppUser.objects.count() == 0
×
53

54
    # Access the setup wizard page
UNCOV
55
    response = client.get(reverse("bpp_setup_wizard:setup"))
×
56

57
    # Should display the form
UNCOV
58
    assert response.status_code == 200
×
UNCOV
59
    assert "Kreator konfiguracji BPP" in response.content.decode("utf-8")
×
UNCOV
60
    assert "Nazwa użytkownika" in response.content.decode("utf-8")
×
UNCOV
61
    assert "Adres email" in response.content.decode("utf-8")
×
UNCOV
62
    assert "Hasło" in response.content.decode("utf-8")
×
63

64

65
@pytest.mark.django_db
1✔
66
def test_setup_wizard_create_admin_user():
1✔
67
    """Test that the setup wizard creates an admin user correctly."""
UNCOV
68
    client = Client()
×
69

70
    # Verify no users exist
UNCOV
71
    assert BppUser.objects.count() == 0
×
72

73
    # Submit the form
UNCOV
74
    response = client.post(
×
75
        reverse("bpp_setup_wizard:setup"),
76
        {
77
            "username": "testadmin",
78
            "email": "admin@test.com",
79
            "password1": "TestPassword123!",
80
            "password2": "TestPassword123!",
81
        },
82
    )
83

84
    # Should redirect to main page after successful creation
UNCOV
85
    assert response.status_code == 302
×
UNCOV
86
    assert response.url == "/"
×
87

88
    # Verify the user was created
UNCOV
89
    assert BppUser.objects.count() == 1
×
UNCOV
90
    user = BppUser.objects.first()
×
UNCOV
91
    assert user.username == "testadmin"
×
UNCOV
92
    assert user.email == "admin@test.com"
×
UNCOV
93
    assert user.is_staff is True
×
UNCOV
94
    assert user.is_superuser is True
×
UNCOV
95
    assert user.is_active is True
×
96

97

98
@pytest.mark.django_db
1✔
99
def test_setup_wizard_not_accessible_when_users_exist(normal_django_user):
1✔
100
    """Test that the setup wizard is not accessible when users already exist."""
UNCOV
101
    client = Client()
×
102

103
    # Verify users exist
UNCOV
104
    assert BppUser.objects.count() > 0
×
105

106
    # Try to access the setup wizard
UNCOV
107
    response = client.get(reverse("bpp_setup_wizard:setup"))
×
108

109
    # Should redirect away from setup
UNCOV
110
    assert response.status_code == 302
×
UNCOV
111
    assert response.url == "/"
×
112

113

114
@pytest.mark.django_db
1✔
115
def test_setup_wizard_status_view_needs_setup():
1✔
116
    """Test that the status view correctly shows setup is needed."""
UNCOV
117
    client = Client()
×
118

119
    # Verify no users exist
UNCOV
120
    assert BppUser.objects.count() == 0
×
121

122
    # Access the status page
UNCOV
123
    response = client.get(reverse("bpp_setup_wizard:status"))
×
124

125
    # Should show setup is needed
UNCOV
126
    assert response.status_code == 200
×
UNCOV
127
    assert "Wymagana konfiguracja" in response.content.decode("utf-8")
×
UNCOV
128
    assert "Uruchom kreator konfiguracji" in response.content.decode("utf-8")
×
129

130

131
@pytest.mark.django_db
1✔
132
def test_setup_wizard_status_view_already_configured(normal_django_user):
1✔
133
    """Test that the status view correctly shows system is configured."""
UNCOV
134
    client = Client()
×
135

136
    # Verify users exist
UNCOV
137
    user_count = BppUser.objects.count()
×
UNCOV
138
    assert user_count > 0
×
139

140
    # Access the status page
UNCOV
141
    response = client.get(reverse("bpp_setup_wizard:status"))
×
142

143
    # Should show system is configured
UNCOV
144
    assert response.status_code == 200
×
UNCOV
145
    assert "System skonfigurowany" in response.content.decode("utf-8")
×
UNCOV
146
    assert str(user_count) in response.content.decode("utf-8")
×
147

148

149
@pytest.mark.django_db
1✔
150
def test_uczelnia_setup_requires_login():
1✔
151
    """Test that Uczelnia setup requires authentication."""
UNCOV
152
    client = Client()
×
153

154
    # Clean up any existing Uczelnia
UNCOV
155
    Uczelnia.objects.all().delete()
×
156

157
    # Try to access without login
UNCOV
158
    response = client.get(reverse("bpp_setup_wizard:uczelnia_setup"))
×
159

160
    # Should redirect to login
UNCOV
161
    assert response.status_code == 302
×
UNCOV
162
    assert "/accounts/login/" in response.url
×
163

164

165
@pytest.mark.django_db
1✔
166
def test_uczelnia_setup_requires_superuser(normal_django_user):
1✔
167
    """Test that Uczelnia setup requires superuser privileges."""
UNCOV
168
    client = Client()
×
169

170
    # Clean up any existing Uczelnia
UNCOV
171
    Uczelnia.objects.all().delete()
×
172

173
    # Login as normal user
UNCOV
174
    client.force_login(normal_django_user)
×
175

176
    # Try to access Uczelnia setup
UNCOV
177
    response = client.get(reverse("bpp_setup_wizard:uczelnia_setup"))
×
178

179
    # Should redirect away
UNCOV
180
    assert response.status_code == 302
×
UNCOV
181
    assert response.url == "/"
×
182

183

184
@pytest.mark.django_db
1✔
185
def test_uczelnia_setup_form_display(admin_user):
1✔
186
    """Test that the Uczelnia setup form is displayed correctly."""
UNCOV
187
    client = Client()
×
188

189
    # Clean up any existing Uczelnia
UNCOV
190
    Uczelnia.objects.all().delete()
×
191

192
    # Login as admin
UNCOV
193
    client.force_login(admin_user)
×
194

195
    # Access the Uczelnia setup page
UNCOV
196
    response = client.get(reverse("bpp_setup_wizard:uczelnia_setup"))
×
197

198
    # Should display the form
UNCOV
199
    assert response.status_code == 200
×
UNCOV
200
    content = response.content.decode("utf-8")
×
UNCOV
201
    assert "Konfiguracja uczelni" in content
×
UNCOV
202
    assert "Nazwa uczelni" in content
×
UNCOV
203
    assert "Nazwa w dopełniaczu" in content
×
UNCOV
204
    assert "Skrót uczelni" in content
×
UNCOV
205
    assert "Środowisko PBN" in content
×
UNCOV
206
    assert "Używaj wydziałów" in content
×
207

208

209
@pytest.mark.django_db
1✔
210
def test_uczelnia_setup_create_uczelnia(admin_user):
1✔
211
    """Test that the Uczelnia setup creates a university configuration correctly."""
UNCOV
212
    client = Client()
×
213

214
    # Clean up any existing Uczelnia
UNCOV
215
    Uczelnia.objects.all().delete()
×
216

217
    # Login as admin
UNCOV
218
    client.force_login(admin_user)
×
219

220
    # Submit the form
UNCOV
221
    response = client.post(
×
222
        reverse("bpp_setup_wizard:uczelnia_setup"),
223
        {
224
            "nazwa": "Uniwersytet Testowy",
225
            "nazwa_dopelniacz_field": "Uniwersytetu Testowego",
226
            "skrot": "UT",
227
            "pbn_api_root": "https://pbn-micro-alpha.opi.org.pl",
228
            "pbn_app_name": "test_app",
229
            "pbn_app_token": "test_token",
230
            "uzywaj_wydzialow": True,
231
        },
232
    )
233

234
    # Should redirect to main page after successful creation
UNCOV
235
    assert response.status_code == 302
×
UNCOV
236
    assert response.url == "/"
×
237

238
    # Verify the Uczelnia was created with correct data
UNCOV
239
    assert Uczelnia.objects.count() == 1
×
UNCOV
240
    uczelnia = Uczelnia.objects.first()
×
UNCOV
241
    assert uczelnia.nazwa == "Uniwersytet Testowy"
×
UNCOV
242
    assert uczelnia.nazwa_dopelniacz_field == "Uniwersytetu Testowego"
×
UNCOV
243
    assert (
×
244
        uczelnia.nazwa_dopelniacz() == "Uniwersytetu Testowego"
245
    )  # Method should return the field value
UNCOV
246
    assert uczelnia.skrot == "UT"
×
UNCOV
247
    assert uczelnia.pbn_api_root == "https://pbn-micro-alpha.opi.org.pl"
×
UNCOV
248
    assert uczelnia.pbn_app_name == "test_app"
×
UNCOV
249
    assert uczelnia.pbn_app_token == "test_token"
×
UNCOV
250
    assert uczelnia.uzywaj_wydzialow is True
×
251

252
    # Verify the automatically set fields
UNCOV
253
    assert uczelnia.pbn_api_kasuj_przed_wysylka is True
×
UNCOV
254
    assert uczelnia.pbn_api_nie_wysylaj_prac_bez_pk is True
×
UNCOV
255
    assert uczelnia.pbn_api_afiliacja_zawsze_na_uczelnie is True
×
UNCOV
256
    assert uczelnia.pbn_wysylaj_bez_oswiadczen is True
×
UNCOV
257
    assert uczelnia.pbn_integracja is True
×
UNCOV
258
    assert uczelnia.pbn_aktualizuj_na_biezaco is True
×
259

260

261
@pytest.mark.django_db
1✔
262
def test_uczelnia_setup_not_accessible_when_exists(admin_user, uczelnia):
1✔
263
    """Test that Uczelnia setup is not accessible when Uczelnia already exists."""
UNCOV
264
    client = Client()
×
265

266
    # Login as admin
UNCOV
267
    client.force_login(admin_user)
×
268

269
    # Try to access the Uczelnia setup
UNCOV
270
    response = client.get(reverse("bpp_setup_wizard:uczelnia_setup"))
×
271

272
    # Should redirect away from setup
UNCOV
273
    assert response.status_code == 302
×
UNCOV
274
    assert response.url == "/"
×
275

276

277
@pytest.mark.django_db
1✔
278
def test_middleware_redirects_to_uczelnia_setup_after_user_setup():
1✔
279
    """Test that middleware redirects to Uczelnia setup after user is created."""
UNCOV
280
    client = Client()
×
281

282
    # Clean up
UNCOV
283
    BppUser.objects.all().delete()
×
UNCOV
284
    Uczelnia.objects.all().delete()
×
285

286
    # First create an admin user
UNCOV
287
    response = client.post(
×
288
        reverse("bpp_setup_wizard:setup"),
289
        {
290
            "username": "testadmin",
291
            "email": "admin@test.com",
292
            "password1": "TestPassword123!",
293
            "password2": "TestPassword123!",
294
        },
295
    )
296

297
    # Should redirect to main page
UNCOV
298
    assert response.status_code == 302
×
UNCOV
299
    assert response.url == "/"
×
300

301
    # Login as the admin user we just created
UNCOV
302
    admin = BppUser.objects.get(username="testadmin")
×
UNCOV
303
    client.force_login(admin)
×
304

305
    # Now trying to access main page should redirect to Uczelnia setup
306
    # (since user is logged in as superuser and no Uczelnia exists)
UNCOV
307
    response = client.get("/")
×
UNCOV
308
    assert response.status_code == 302
×
UNCOV
309
    assert response.url == reverse("bpp_setup_wizard:uczelnia_setup")
×
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