-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #385 from terencehonles/fix-collecting-users-on-dj…
…ango fix #373 breaking collecting user information on *not* starlette
- Loading branch information
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -327,6 +327,76 @@ def test_fastapi_request_data_empty_values(self): | |
self.assertEqual(data['user_ip'], scope['client'][0]) | ||
self.assertEqual(data['method'], scope['method']) | ||
|
||
def test_django_build_person_data(self): | ||
try: | ||
import django | ||
from django.conf import settings | ||
except ImportError: | ||
self.skipTest('Requires Django to be installed') | ||
else: | ||
settings.configure( | ||
INSTALLED_APPS=['django.contrib.auth', 'django.contrib.contenttypes'] | ||
) | ||
if django.VERSION >= (1, 7): | ||
django.setup() | ||
|
||
from django.contrib.auth.models import User | ||
from django.http.request import HttpRequest | ||
|
||
request = HttpRequest() | ||
request.user = User() | ||
request.user.id = 123 | ||
request.user.username = 'admin' | ||
request.user.email = '[email protected]' | ||
|
||
data = rollbar._build_person_data(request) | ||
|
||
self.assertDictEqual( | ||
data, {'id': '123', 'username': 'admin', 'email': '[email protected]'} | ||
) | ||
|
||
def test_starlette_build_person_data_if_user_authenticated(self): | ||
try: | ||
from starlette.authentication import SimpleUser | ||
from starlette.requests import Request | ||
except ImportError: | ||
self.skipTest('Requires Starlette to be installed') | ||
|
||
# Implement interface with the id attribute | ||
class User(SimpleUser): | ||
counter = 0 | ||
|
||
def __init__(self, username, email): | ||
super().__init__(username) | ||
self.email = email | ||
|
||
User.counter += 1 | ||
self.id = User.counter | ||
|
||
scope = {'type': 'http'} | ||
request = Request(scope) | ||
# Make the user authenticated | ||
request.scope['user'] = User('admin', '[email protected]') | ||
|
||
data = rollbar._build_person_data(request) | ||
|
||
self.assertDictEqual( | ||
data, {'id': '1', 'username': 'admin', 'email': '[email protected]'} | ||
) | ||
|
||
def test_starlette_failsafe_build_person_data_if_user_not_authenticated(self): | ||
try: | ||
from starlette.requests import Request | ||
except ImportError: | ||
self.skipTest('Requires Starlette to be installed') | ||
|
||
scope = {'type': 'http'} | ||
request = Request(scope) | ||
|
||
data = rollbar._build_person_data(request) | ||
|
||
self.assertIsNone(data) | ||
|
||
@unittest.skipUnless(sys.version_info >= (3, 6), 'Python3.6+ required') | ||
def test_get_request_starlette_middleware(self): | ||
try: | ||
|