| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (C) 2017-2023 by the Free Software Foundation, Inc. |
| 3 |
# |
| 4 |
# This file is part of Django-Mailman3. |
| 5 |
# |
| 6 |
# HyperKitty is free software: you can redistribute it and/or modify it under |
| 7 |
# the terms of the GNU General Public License as published by the Free |
| 8 |
# Software Foundation, either version 3 of the License, or (at your option) |
| 9 |
# any later version. |
| 10 |
# |
| 11 |
# HyperKitty is distributed in the hope that it will be useful, but WITHOUT |
| 12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 |
# more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along with |
| 17 |
# Django-Mailman3. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
|
| 20 |
from unittest.mock import Mock, patch |
| 21 |
|
| 22 |
from django.test import RequestFactory, TestCase |
| 23 |
from django.urls import reverse |
| 24 |
|
| 25 |
from openid.consumer import consumer |
| 26 |
|
| 27 |
from django_mailman3.lib.auth.fedora.provider import ( |
| 28 |
FedoraAccount, FedoraProvider) |
| 29 |
|
| 30 |
|
| 31 |
class TestFedoraAccount(TestCase): |
| 32 |
""" |
| 33 |
Test FedoraAccount social account. |
| 34 |
""" |
| 35 |
|
| 36 |
def setUp(self): |
| 37 |
self.account = FedoraAccount('') |
| 38 |
|
| 39 |
def test_self_brand(self): |
| 40 |
self.assertEqual(self.account.get_brand(), |
| 41 |
{'id': 'fedora', 'name': 'Fedora'}) |
| 42 |
|
| 43 |
|
| 44 |
class TestFedoraProvider(TestCase): |
| 45 |
""" |
| 46 |
Test FedoraProvider openid authentication. |
| 47 |
""" |
| 48 |
|
| 49 |
@patch('allauth.socialaccount.providers.openid.views._openid_consumer') |
| 50 |
def setUp(self, consumer_mock): |
| 51 |
self.factory = RequestFactory() |
| 52 |
client = Mock() |
| 53 |
complete = Mock() |
| 54 |
consumer_mock.return_value = client |
| 55 |
client.complete = complete |
| 56 |
self.complete_response = Mock() |
| 57 |
complete.return_value = self.complete_response |
| 58 |
self.complete_response.status = consumer.SUCCESS |
| 59 |
self.complete_response.identity_url = 'http://bob.id.fedoraproject.org' |
| 60 |
|
| 61 |
def test_get_login_url(self): |
| 62 |
req = self.factory.get('/') |
| 63 |
login_url = FedoraProvider(req).get_login_url(req) |
| 64 |
self.assertEqual(login_url, reverse('fedora_login')) |
| 65 |
login_url = FedoraProvider(req).get_login_url(req, query1='value1') |
| 66 |
new_url = reverse('fedora_login') + '?query1=value1' |
| 67 |
self.assertEqual(login_url, new_url) |
| 68 |
|
| 69 |
def test_extract_username(self): |
| 70 |
req = self.factory.get('/') |
| 71 |
username = FedoraProvider(req).extract_username(self.complete_response) |
| 72 |
self.assertEqual(username, 'bob') |
| 73 |
|
| 74 |
def test_extract_email_addresses(self): |
| 75 |
with patch('django_mailman3.lib.auth.fedora.provider' |
| 76 |
'.get_email_from_response') as email_mock: |
| 77 |
email_mock.return_value = '[email protected]' |
| 78 |
req = self.factory.get('/') |
| 79 |
emails = FedoraProvider(req).extract_email_addresses( |
| 80 |
self.complete_response) |
| 81 |
self.assertEqual(len(emails), 2) |
| 82 |
self.assertEqual(sorted([x.email for x in emails]), |
| 83 |
['[email protected]', '[email protected]']) |