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

iplweb / bpp / #820

19 Oct 2025 06:59PM UTC coverage: 65.093% (+5.3%) from 59.791%
#820

push

coveralls-python

Michał Pasternak
Fixes

4215 of 9430 branches covered (44.7%)

Branch coverage included in aggregate %.

27562 of 39388 relevant lines covered (69.98%)

0.7 hits per line

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

97.95
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."""
14
    middleware = list(settings.MIDDLEWARE)
1✔
15
    if "bpp_setup_wizard.middleware.SetupWizardMiddleware" not in middleware:
1!
16
        # Add the middleware after AuthenticationMiddleware to ensure user is available
17
        try:
1✔
18
            auth_index = middleware.index(
1✔
19
                "django.contrib.auth.middleware.AuthenticationMiddleware"
20
            )
21
            middleware.insert(
1✔
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")
×
27
        settings.MIDDLEWARE = middleware
1✔
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."""
33
    client = Client()
1✔
34

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

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

41
    # Should redirect to setup wizard
42
    assert response.status_code == 302
1✔
43
    assert response.url == reverse("bpp_setup_wizard:setup")
1✔
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."""
49
    client = Client()
1✔
50

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

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

57
    # Should display the form
58
    assert response.status_code == 200
1✔
59
    assert "Kreator konfiguracji BPP" in response.content.decode("utf-8")
1✔
60
    assert "Nazwa użytkownika" in response.content.decode("utf-8")
1✔
61
    assert "Adres email" in response.content.decode("utf-8")
1✔
62
    assert "Hasło" in response.content.decode("utf-8")
1✔
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."""
68
    client = Client()
1✔
69

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

73
    # Submit the form
74
    response = client.post(
1✔
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
85
    assert response.status_code == 302
1✔
86
    assert response.url == "/"
1✔
87

88
    # Verify the user was created
89
    assert BppUser.objects.count() == 1
1✔
90
    user = BppUser.objects.first()
1✔
91
    assert user.username == "testadmin"
1✔
92
    assert user.email == "admin@test.com"
1✔
93
    assert user.is_staff is True
1✔
94
    assert user.is_superuser is True
1✔
95
    assert user.is_active is True
1✔
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."""
101
    client = Client()
1✔
102

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

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

109
    # Should redirect away from setup
110
    assert response.status_code == 302
1✔
111
    assert response.url == "/"
1✔
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."""
117
    client = Client()
1✔
118

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

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

125
    # Should show setup is needed
126
    assert response.status_code == 200
1✔
127
    assert "Wymagana konfiguracja" in response.content.decode("utf-8")
1✔
128
    assert "Uruchom kreator konfiguracji" in response.content.decode("utf-8")
1✔
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."""
134
    client = Client()
1✔
135

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

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

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

148

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

154
    # Clean up any existing Uczelnia
155
    Uczelnia.objects.all().delete()
1✔
156

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

160
    # Should redirect to login
161
    assert response.status_code == 302
1✔
162
    assert "/accounts/login/" in response.url
1✔
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."""
168
    client = Client()
1✔
169

170
    # Clean up any existing Uczelnia
171
    Uczelnia.objects.all().delete()
1✔
172

173
    # Login as normal user
174
    client.force_login(normal_django_user)
1✔
175

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

179
    # Should redirect away
180
    assert response.status_code == 302
1✔
181
    assert response.url == "/"
1✔
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."""
187
    client = Client()
1✔
188

189
    # Clean up any existing Uczelnia
190
    Uczelnia.objects.all().delete()
1✔
191

192
    # Login as admin
193
    client.force_login(admin_user)
1✔
194

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

198
    # Should display the form
199
    assert response.status_code == 200
1✔
200
    content = response.content.decode("utf-8")
1✔
201
    assert "Konfiguracja uczelni" in content
1✔
202
    assert "Nazwa uczelni" in content
1✔
203
    assert "Nazwa w dopełniaczu" in content
1✔
204
    assert "Skrót uczelni" in content
1✔
205
    assert "Środowisko PBN" in content
1✔
206
    assert "Używaj wydziałów" in content
1✔
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."""
212
    client = Client()
1✔
213

214
    # Clean up any existing Uczelnia
215
    Uczelnia.objects.all().delete()
1✔
216

217
    # Login as admin
218
    client.force_login(admin_user)
1✔
219

220
    # Submit the form
221
    response = client.post(
1✔
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
235
    assert response.status_code == 302
1✔
236
    assert response.url == "/"
1✔
237

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

252
    # Verify the automatically set fields
253
    assert uczelnia.pbn_api_kasuj_przed_wysylka is True
1✔
254
    assert uczelnia.pbn_api_nie_wysylaj_prac_bez_pk is True
1✔
255
    assert uczelnia.pbn_api_afiliacja_zawsze_na_uczelnie is True
1✔
256
    assert uczelnia.pbn_wysylaj_bez_oswiadczen is True
1✔
257
    assert uczelnia.pbn_integracja is True
1✔
258
    assert uczelnia.pbn_aktualizuj_na_biezaco is True
1✔
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."""
264
    client = Client()
1✔
265

266
    # Login as admin
267
    client.force_login(admin_user)
1✔
268

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

272
    # Should redirect away from setup
273
    assert response.status_code == 302
1✔
274
    assert response.url == "/"
1✔
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."""
280
    client = Client()
1✔
281

282
    # Clean up
283
    BppUser.objects.all().delete()
1✔
284
    Uczelnia.objects.all().delete()
1✔
285

286
    # First create an admin user
287
    response = client.post(
1✔
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
298
    assert response.status_code == 302
1✔
299
    assert response.url == "/"
1✔
300

301
    # Login as the admin user we just created
302
    admin = BppUser.objects.get(username="testadmin")
1✔
303
    client.force_login(admin)
1✔
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)
307
    response = client.get("/")
1✔
308
    assert response.status_code == 302
1✔
309
    assert response.url == reverse("bpp_setup_wizard:uczelnia_setup")
1✔
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