Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to force authentication when do testing? #612

Open
elcolie opened this issue Jun 28, 2018 · 2 comments
Open

How to force authentication when do testing? #612

elcolie opened this issue Jun 28, 2018 · 2 comments

Comments

@elcolie
Copy link

elcolie commented Jun 28, 2018

I have separated OAuth Provider and Resource Server. The token and scopes work smoothly with simple password type.

However, since the authentication_classes comes from 3rd party my ordinary force_authentication(user=user) fails.

class CustomerNewCaseIssueViewSet(viewsets.ModelViewSet):
    authentication_classes = [OAuth2Authentication]
    permission_classes = [TokenHasScope]
    required_scopes = ['mp_resource']
    queryset = CaseIssue.objects.all()
    serializer_class = NewCaseIssueSerializer
    def test_new_issue(self):
        """Test user shoot open an issue to MP"""
        machine_type = mommy.make(MachineType)
        data = {
            'machine_type': machine_type.id,
            'symptom': 'High pitch noise',
        }
        factory = APIRequestFactory()
        request = factory.post('', data, format='json')

        # By pass the OAuth logic
        viewset = CustomerNewCaseIssueViewSet
        viewset.authentication_classes = ()
        viewset.permission_classes = ()
        viewset.required_scopes = ()
        view = viewset.as_view({'post': 'create'})    # Ready
        force_authenticate(request, user=self.elcolie)
        res = view(request)
        assert status.HTTP_201_CREATED == res.status_code

Do you have any APIClient for testing?
Otherwise I have to override the class attribute before as_view() get called

Thanks

@broganross
Copy link

Did anyone figure this out? I'm trying to run unit tests on the resource server and haven't found a way to force authentication/authorization, or use a temp local OAuth provider.

@elcolie
Copy link
Author

elcolie commented Feb 21, 2020

Nope. I think django-oauth-toolkit authors are the one who can provide TestAuth framework for this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants