Skip to content

Commit

Permalink
(inclusion_connect) make logout compliant with new django Inclusion C…
Browse files Browse the repository at this point in the history
…onnect
  • Loading branch information
vincentporte committed Oct 23, 2023
1 parent dcb8e58 commit 595be63
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
20 changes: 7 additions & 13 deletions lacommunaute/inclusion_connect/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,15 @@ def test_normal_signin(self):

class InclusionConnectLogoutTest(InclusionConnectBaseTestCase):
@respx.mock
def test_simple_logout(self):
def test_logout_with_redirection(self):
mock_oauth_dance(self)
params = {
"id_token_hint": 123456,
"post_logout_redirect_uri": f'http://testserver{reverse("pages:home")}',
}
expected_redirection = f"{constants.INCLUSION_CONNECT_ENDPOINT_LOGOUT}?{urlencode(params)}"
respx.get(constants.INCLUSION_CONNECT_ENDPOINT_LOGOUT).respond(200)
logout_url = reverse("inclusion_connect:logout")
response = self.client.get(logout_url)
self.assertRedirects(response, reverse("pages:home"))
self.assertFalse(auth.get_user(self.client).is_authenticated)

@respx.mock
def test_logout_with_redirection(self):
mock_oauth_dance(self)
expected_redirection = reverse("pages:home")
respx.get(constants.INCLUSION_CONNECT_ENDPOINT_LOGOUT).respond(200)

params = {"redirect_url": expected_redirection}
logout_url = f"{reverse('inclusion_connect:logout')}?{urlencode(params)}"
response = self.client.get(logout_url)
self.assertRedirects(response, expected_redirection)
self.assertRedirects(response, expected_redirection, fetch_redirect_response=False)
14 changes: 3 additions & 11 deletions lacommunaute/inclusion_connect/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,21 @@ def inclusion_connect_callback(request): # pylint: disable=too-many-return-stat

def inclusion_connect_logout(request):
token = request.GET.get("token")
state = request.GET.get("state")
post_logout_redirect_url = request.GET.get("redirect_url", reverse("pages:home"))
post_logout_redirect_uri = request.GET.get("redirect_url", reverse("pages:home"))

# Fallback on session data.
if not token:
ic_session = request.session.get(constants.INCLUSION_CONNECT_SESSION_KEY)
if not ic_session:
raise KeyError("Missing session key.")
token = ic_session["token"]
state = ic_session["state"]

params = {
"id_token_hint": token,
"state": state,
"post_logout_redirect_uri": request.build_absolute_uri(post_logout_redirect_uri),
}
complete_url = f"{constants.INCLUSION_CONNECT_ENDPOINT_LOGOUT}?{urlencode(params)}"
# Logout user from IC with HTTPX to benefit from respx in tests
# and to handle post logout redirection more easily.
response = httpx.get(complete_url)
if response.status_code != 200:
logger.error("Error during IC logout. Status code: %s", response.status_code)

# Logout user from Django
logout(request)

return HttpResponseRedirect(post_logout_redirect_url)
return HttpResponseRedirect(complete_url)

0 comments on commit 595be63

Please sign in to comment.