Some more backend coverage
This commit is contained in:
parent
8e3d325938
commit
28a24c6aeb
@ -2,6 +2,7 @@ import json
|
|||||||
|
|
||||||
from django.contrib.auth.models import Permission
|
from django.contrib.auth.models import Permission
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from django.test import override_settings
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
|
|
||||||
@ -113,3 +114,20 @@ class TestApiUiSettings(DirectoriesMixin, APITestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
@override_settings(
|
||||||
|
PAPERLESS_OAUTH_CALLBACK_BASE_URL="http://localhost:8000",
|
||||||
|
PAPERLESS_GMAIL_OAUTH_CLIENT_ID="abc123",
|
||||||
|
PAPERLESS_GMAIL_OAUTH_CLIENT_SECRET="def456",
|
||||||
|
PAPERLESS_OUTLOOK_OAUTH_CLIENT_ID="ghi789",
|
||||||
|
PAPERLESS_OUTLOOK_OAUTH_CLIENT_SECRET="jkl012",
|
||||||
|
)
|
||||||
|
def test_settings_includes_oauth_urls_if_enabled(self):
|
||||||
|
response = self.client.get(self.ENDPOINT, format="json")
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
self.assertIsNotNone(
|
||||||
|
response.data["settings"]["gmail_oauth_url"],
|
||||||
|
)
|
||||||
|
self.assertIsNotNone(
|
||||||
|
response.data["settings"]["outlook_oauth_url"],
|
||||||
|
)
|
||||||
|
@ -10,6 +10,8 @@ from rest_framework import status
|
|||||||
|
|
||||||
from paperless_mail.mail import MailAccountHandler
|
from paperless_mail.mail import MailAccountHandler
|
||||||
from paperless_mail.models import MailAccount
|
from paperless_mail.models import MailAccount
|
||||||
|
from paperless_mail.oauth import generate_gmail_oauth_url
|
||||||
|
from paperless_mail.oauth import generate_outlook_oauth_url
|
||||||
from paperless_mail.oauth import get_oauth_callback_url
|
from paperless_mail.oauth import get_oauth_callback_url
|
||||||
from paperless_mail.oauth import get_oauth_redirect_url
|
from paperless_mail.oauth import get_oauth_redirect_url
|
||||||
|
|
||||||
@ -22,6 +24,7 @@ class TestMailOAuth(
|
|||||||
self.client.force_login(self.user)
|
self.client.force_login(self.user)
|
||||||
self.mail_account_handler = MailAccountHandler()
|
self.mail_account_handler = MailAccountHandler()
|
||||||
# Mock settings
|
# Mock settings
|
||||||
|
settings.OAUTH_CALLBACK_BASE_URL = "http://localhost:8000"
|
||||||
settings.GMAIL_OAUTH_CLIENT_ID = "test_gmail_client_id"
|
settings.GMAIL_OAUTH_CLIENT_ID = "test_gmail_client_id"
|
||||||
settings.GMAIL_OAUTH_CLIENT_SECRET = "test_gmail_client_secret"
|
settings.GMAIL_OAUTH_CLIENT_SECRET = "test_gmail_client_secret"
|
||||||
settings.OUTLOOK_OAUTH_CLIENT_ID = "test_outlook_client_id"
|
settings.OUTLOOK_OAUTH_CLIENT_ID = "test_outlook_client_id"
|
||||||
@ -73,6 +76,24 @@ class TestMailOAuth(
|
|||||||
"/mail",
|
"/mail",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_generate_oauth_urls(self):
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Mocked settings for Gmail and Outlook OAuth client IDs
|
||||||
|
WHEN:
|
||||||
|
- generate_gmail_oauth_url and generate_outlook_oauth_url are called
|
||||||
|
THEN:
|
||||||
|
- Correct URLs are generated
|
||||||
|
"""
|
||||||
|
self.assertEqual(
|
||||||
|
"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=test_gmail_client_id&redirect_uri=http://localhost:8000/api/oauth/callback/&scope=https://mail.google.com/&access_type=offline&prompt=consent",
|
||||||
|
generate_gmail_oauth_url(),
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=code&response_mode=query&client_id=test_outlook_client_id&redirect_uri=http://localhost:8000/api/oauth/callback/&scope=offline_access https://outlook.office.com/IMAP.AccessAsUser.All",
|
||||||
|
generate_outlook_oauth_url(),
|
||||||
|
)
|
||||||
|
|
||||||
@mock.patch("httpx.post")
|
@mock.patch("httpx.post")
|
||||||
def test_oauth_callback_view(self, mock_post):
|
def test_oauth_callback_view(self, mock_post):
|
||||||
"""
|
"""
|
||||||
@ -109,6 +130,27 @@ class TestMailOAuth(
|
|||||||
MailAccount.objects.filter(imap_server="outlook.office365.com").exists(),
|
MailAccount.objects.filter(imap_server="outlook.office365.com").exists(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_oauth_callback_view_no_code(self):
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- Mocked settings for Gmail and Outlook OAuth client IDs and secrets
|
||||||
|
WHEN:
|
||||||
|
- OAuth callback is called without a code
|
||||||
|
THEN:
|
||||||
|
- Error is logged
|
||||||
|
"""
|
||||||
|
|
||||||
|
response = self.client.get(
|
||||||
|
"/api/oauth/callback/",
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
self.assertFalse(
|
||||||
|
MailAccount.objects.filter(imap_server="imap.gmail.com").exists(),
|
||||||
|
)
|
||||||
|
self.assertFalse(
|
||||||
|
MailAccount.objects.filter(imap_server="outlook.office365.com").exists(),
|
||||||
|
)
|
||||||
|
|
||||||
@mock.patch("httpx.post")
|
@mock.patch("httpx.post")
|
||||||
def test_oauth_callback_view_error(self, mock_post):
|
def test_oauth_callback_view_error(self, mock_post):
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user