Skip to content

Add checkcookie redirect handling#1

Merged
frwickst merged 4 commits intofrwickst:mainfrom
taskinen:checkcookie_redirect
Aug 13, 2025
Merged

Add checkcookie redirect handling#1
frwickst merged 4 commits intofrwickst:mainfrom
taskinen:checkcookie_redirect

Conversation

@taskinen
Copy link
Copy Markdown
Contributor

@taskinen taskinen commented Aug 7, 2025

Checkcookie redirect handling

This fixes frwickst/wilma_ha#1

  1. Problem: After login, the Pirkkala instance redirects to https://pirkkala.inschool.fi/?checkcookie instead of directly providing a URL with the user ID format like https://server/!userid
  2. Root Cause: The original code expected the Location header to contain an exclamation mark (!) with the user ID, but Pirkkala's checkcookie redirect goes to the root page without exposing the user ID in the URL
  3. Solution: Modified ./pywilma/wilhelmina/client.py to handle the checkcookie redirect by:
  • Following the redirect to see where it leads
  • When it redirects to the root page, fetching the home page content
  • Using regex patterns to extract the user ID from the page HTML content
  • Successfully extracting user ID from the home page

Added Tests:

  1. test_login_checkcookie_style_checkcookie_redirect - Tests the main scenario where:
  • Login redirects to ?checkcookie (without user ID)
  • Checkcookie redirects to root page
  • User ID is extracted from home page content
  1. test_login_checkcookie_style_no_redirect_from_checkcookie - Tests when checkcookie returns status 200 instead of redirecting
  2. test_login_checkcookie_style_user_id_extraction_patterns - Tests different regex patterns for extracting user ID from HTML:
  • var url = "/!1234567/messages" (URL in quotes)
  • userId = "!1234567" (variable assignment)
  1. test_login_checkcookie_style_extraction_failure - Tests error handling when user ID cannot be found in home page

Result:

  • Login to Pirkkala Wilma works:
❯ poetry run wilhelmina login
Login successful!
  • Messages can be listed:
❯ poetry run wilhelmina messages
                                 Messages
┏━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┓
┃      ID ┃ Subject                    ┃ Date             ┃ Sender               ┃
┡━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━┩
... real messages follow
  • Messages can be read:
❯ poetry run wilhelmina message 1520946
Message subject here
From: Sender Name (S.N)
Date: 2025-08-07 12:23

Message text here.
  • All tests pass:
collected 40 items

 wilhelmina/tests/test_client.py ✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓    52% █████▍
 wilhelmina/tests/test_error_handling.py ✓✓✓✓             62% ██████▍
 wilhelmina/tests/test_init.py ✓✓✓✓✓                      75% ███████▌
 wilhelmina/tests/test_models.py ✓✓✓✓✓                    88% ████████▊
 wilhelmina/tests/test_unread_messages.py ✓✓✓✓            98% █████████▊
 wilhelmina/tests/test_utils.py ✓                        100% ██████████

Results (0.56s):
      40 passed

@frwickst
Copy link
Copy Markdown
Owner

Thank you for this. I will try to find the time to confirm that this does not break the Turku workflow today, and then we can merge this 👍

@frwickst
Copy link
Copy Markdown
Owner

Looks good, and works for me still, so we can continue with merging this after some of the tests are passing. There were some issues already in main that I noticed, but there are some in your code.

Could you rebase on main and then run ruff check . && ruff format and you will see some issues still, but just fix those and we can merge this.

Fixed 3 ruff B023 errors in wilhelmina/tests/test_client.py where the
get_side_effect function was referencing loop variables that could change
during iteration. Resolved by capturing the loop variables as default
function parameters to properly bind them in the closure.
@taskinen
Copy link
Copy Markdown
Contributor Author

Fixed the ruff check and ruff format errors.

@frwickst frwickst merged commit 8d22fb8 into frwickst:main Aug 13, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Authentication to Wilma failed: Invalid Location header

2 participants