Skip to content

Commit

Permalink
Test MasterReplicaOAuth2Validator fetches from Master DB
Browse files Browse the repository at this point in the history
Test that the MasterReplicaOAuth2Validator fetches from the Master
Database if it fails to retrieve the AccessToken from the Read Replica
  • Loading branch information
DavisRayM committed Oct 9, 2020
1 parent cb174ab commit d6dfcff
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions onadata/libs/tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase
from django.http.request import HttpRequest

from mock import patch, MagicMock
from oauth2_provider.models import AccessToken
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.test import APIRequestFactory

Expand All @@ -13,6 +16,7 @@
TempTokenAuthentication,
TempTokenURLParameterAuthentication,
check_lockout,
MasterReplicaOAuth2Validator
)


Expand Down Expand Up @@ -143,3 +147,28 @@ def test_exception_on_username_with_whitespaces(self):
# passed as a username
with self.assertRaises(AuthenticationFailed):
check_lockout(request)


class TestMasterReplicaOAuth2Validator(TestCase):

@patch('onadata.libs.authentication.AccessToken')
def test_reads_from_master(self, mock_token_class):
def is_valid_mock(*args, **kwargs):
return True
token = MagicMock()
token.is_valid = is_valid_mock
token.user = 'bob'
token.application = 'bob-test'
token.token = 'abc'
mock_token_class.DoesNotExist = AccessToken.DoesNotExist
mock_token_class.objects.select_related(
"application", "user").\
get.side_effect = [AccessToken.DoesNotExist, token]
req = HttpRequest()
self.assertTrue(
MasterReplicaOAuth2Validator().validate_bearer_token(
'abc', {}, req))
self.assertEqual(
mock_token_class.objects.select_related(
"application", "user").get.call_count, 2)
self.assertEqual(req.access_token, token)

0 comments on commit d6dfcff

Please sign in to comment.