From 7277245334fed525179a02f6c17c0e8f5bbc11fb Mon Sep 17 00:00:00 2001 From: Jonas Carson Date: Fri, 25 Oct 2024 12:41:50 -0700 Subject: [PATCH] Fix missing coverage for login page and auth_error --- .../socialaccount/snippets/provider_list.html | 10 --- primed/users/tests/test_adapters.py | 85 +++++++++++++------ primed/users/tests/test_views.py | 19 +++++ 3 files changed, 80 insertions(+), 34 deletions(-) diff --git a/primed/templates/socialaccount/snippets/provider_list.html b/primed/templates/socialaccount/snippets/provider_list.html index d89feade..1fd9f5d3 100644 --- a/primed/templates/socialaccount/snippets/provider_list.html +++ b/primed/templates/socialaccount/snippets/provider_list.html @@ -3,16 +3,6 @@ {% get_providers as socialaccount_providers %} {% for provider in socialaccount_providers %} -{% if provider.id == "openid" %} -{% for brand in provider.get_brands %} -

- {{brand.name}} -

-{% endfor %} -{% endif %}

diff --git a/primed/users/tests/test_adapters.py b/primed/users/tests/test_adapters.py index 0ddf77e2..df8835a8 100644 --- a/primed/users/tests/test_adapters.py +++ b/primed/users/tests/test_adapters.py @@ -35,24 +35,11 @@ def setUp(self): secret="test-client-secret", ) self.social_app.sites.add(current_site) - - def test_social_login_success(self): - # Mock user - request = self.factory.get("/") - middleware = SessionMiddleware(lambda x: None) - middleware.process_request(request) - request.session.save() - middleware = AuthenticationMiddleware(lambda x: None) - middleware.process_request(request) - request.user = AnonymousUser() - user = User.objects.create(username="testuser", email="testuser@example.com") - - # # Mock social login - # Create a mock SocialAccount and link it to the user + self.user = User.objects.create(username="testuser", email="testuser@example.com") new_first_name = "Bob" new_last_name = "Rob" - social_account = SocialAccount.objects.create( - user=user, + self.social_account = SocialAccount.objects.create( + user=self.user, provider="drupal_oauth_provider", uid="12345", extra_data={ @@ -64,7 +51,37 @@ def test_social_login_success(self): ) # Create a mock SocialLogin object and associate the user and social account - sociallogin = SocialLogin(user=user, account=social_account) + self.sociallogin = SocialLogin(user=self.user, account=self.social_account) + + def test_social_login_success(self): + # Mock user + request = self.factory.get("/") + middleware = SessionMiddleware(lambda x: None) + middleware.process_request(request) + request.session.save() + middleware = AuthenticationMiddleware(lambda x: None) + middleware.process_request(request) + request.user = AnonymousUser() + # user = User.objects.create(username="testuser", email="testuser@example.com") + + # # # Mock social login + # # Create a mock SocialAccount and link it to the user + new_first_name = "Bob" + new_last_name = "Rob" + # social_account = SocialAccount.objects.create( + # user=user, + # provider="drupal_oauth_provider", + # uid="12345", + # extra_data={ + # "preferred_username": "testuser", + # "first_name": new_first_name, + # "last_name": new_last_name, + # "email": "testuser@example.com", + # }, + # ) + + # # Create a mock SocialLogin object and associate the user and social account + # sociallogin = SocialLogin(user=user, account=social_account) # Simulate social login from allauth.account.adapter import get_adapter @@ -72,18 +89,34 @@ def test_social_login_success(self): # adapter = SocialAccountAdapter() adapter = get_adapter(request) - adapter.login(request, user) + adapter.login(request, self.user) signals.user_logged_in.send( - sender=user.__class__, + sender=self.user.__class__, request=request, - user=user, - sociallogin=sociallogin, + user=self.user, + sociallogin=self.sociallogin, ) # Check if the login completed successfully - self.assertEqual(sociallogin.user, user) - self.assertEqual(request.user, user) - self.assertEqual(user.name, f"{new_first_name} {new_last_name}") + self.assertEqual(self.sociallogin.user, self.user) + self.assertEqual(request.user, self.user) + self.assertEqual(self.user.name, f"{new_first_name} {new_last_name}") + + def test_authentication_error_with_callback(self): + """Test authentication error during callback processing""" + from django.urls import reverse + + callback_url = reverse("drupal_oauth_provider_callback") + response = self.client.get(callback_url, {"error": "access_denied"}) + self.assertTemplateUsed( + response, + "socialaccount/authentication_error.%s" % getattr(settings, "ACCOUNT_TEMPLATE_EXTENSION", "html"), + ) + # # Check if the response redirects to the login error page + # #self.assertEqual(response.status_code, 302) + # import sys + # print(f"RESP {response} ", file=sys.stderr) + # self.assertIn('socialaccount/authentication_error', response.url) def test_update_user_info(self): adapter = SocialAccountAdapter() @@ -224,10 +257,14 @@ def test_update_user_groups_malformed(self): def test_account_is_open_for_signup(self): request = RequestFactory() adapter = AccountAdapter() + social_adapter = SocialAccountAdapter() assert adapter.is_open_for_signup(request) is True + assert social_adapter.is_open_for_signup(request=request, sociallogin=self.sociallogin) is True @override_settings(ACCOUNT_ALLOW_REGISTRATION=False) def test_account_is_not_open_for_signup(self): request = RequestFactory() adapter = AccountAdapter() + social_adapter = SocialAccountAdapter() assert adapter.is_open_for_signup(request) is False + assert social_adapter.is_open_for_signup(request=request, sociallogin=self.sociallogin) is False diff --git a/primed/users/tests/test_views.py b/primed/users/tests/test_views.py index a03da768..73d41a58 100644 --- a/primed/users/tests/test_views.py +++ b/primed/users/tests/test_views.py @@ -1,6 +1,7 @@ import json import pytest +from allauth.socialaccount.models import SocialApp from anvil_consortium_manager.models import AnVILProjectManagerAccess from anvil_consortium_manager.tests.factories import ( AccountFactory, @@ -11,6 +12,7 @@ from django.contrib.auth.models import AnonymousUser, Permission from django.contrib.messages.middleware import MessageMiddleware from django.contrib.sessions.middleware import SessionMiddleware +from django.contrib.sites.models import Site from django.http import HttpRequest from django.shortcuts import resolve_url from django.test import RequestFactory, TestCase @@ -22,6 +24,7 @@ NonDataAffiliateAgreementFactory, ) from primed.dbgap.tests.factories import dbGaPApplicationFactory +from primed.drupal_oauth_provider.provider import CustomProvider from primed.primed_anvil.tests.factories import StudySiteFactory from primed.users.forms import UserChangeForm from primed.users.models import User @@ -189,6 +192,22 @@ def test_view_links(self, client, user: User, rf: RequestFactory): assert account.get_absolute_url() not in str(response.content) +class LoginViewTest(TestCase): + def setUp(self): + current_site = Site.objects.get_current() + self.social_app = SocialApp.objects.create( + provider=CustomProvider.id, + name="DOA", + client_id="test-client-id", + secret="test-client-secret", + ) + self.social_app.sites.add(current_site) + + def test_basic_login_view_render(self): + response = self.client.get(reverse("account_login")) + assert response.status_code == 200 + + class UserDetailTest(TestCase): def setUp(self): self.factory = RequestFactory()