Skip to content

Commit

Permalink
Fix missing coverage for login page and auth_error
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarson committed Oct 25, 2024
1 parent 32da68c commit 7277245
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 34 deletions.
10 changes: 0 additions & 10 deletions primed/templates/socialaccount/snippets/provider_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@
{% get_providers as socialaccount_providers %}

{% for provider in socialaccount_providers %}
{% if provider.id == "openid" %}
{% for brand in provider.get_brands %}
<p>
<a title="{{brand.name}}"
class="socialaccount_provider {{provider.id}} {{brand.id}}"
href="{% provider_login_url provider.id openid=brand.openid_url process=process %}"
>{{brand.name}}</a>
</p>
{% endfor %}
{% endif %}
<p>
<form title="{{provider.name}}" class="socialaccount_provider {{provider.id}}"
action="{% provider_login_url provider.id process=process scope=scope auth_params=auth_params %}" method="POST">
Expand Down
85 changes: 61 additions & 24 deletions primed/users/tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]")

# # Mock social login
# Create a mock SocialAccount and link it to the user
self.user = User.objects.create(username="testuser", email="[email protected]")
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={
Expand All @@ -64,26 +51,72 @@ 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="[email protected]")

# # # 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": "[email protected]",
# },
# )

# # 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

# 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()
Expand Down Expand Up @@ -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
19 changes: 19 additions & 0 deletions primed/users/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7277245

Please sign in to comment.