from django.core.exceptions import ImproperlyConfigured
from django.views.generic import View
from django.test import TestCase, RequestFactory

from oauthlib.oauth2 import Server

from ..mixins import OAuthLibMixin
from ..oauth2_validators import OAuth2Validator


class TestOAuthLibMixin(TestCase):
    @classmethod
    def setUpClass(cls):
        cls.request_factory = RequestFactory()

    def test_missing_server_class(self):
        class TestView(OAuthLibMixin, View):
            validator_class = OAuth2Validator

        request = self.request_factory.get("/fake-req")
        test_view = TestView()

        self.assertRaises(ImproperlyConfigured, test_view.get_server, request)

    def test_missing_validator_class(self):
        class TestView(OAuthLibMixin, View):
            server_class = Server

        request = self.request_factory.get("/fake-req")
        test_view = TestView()

        self.assertRaises(ImproperlyConfigured, test_view.get_server, request)

    def test_correct_server(self):
        class TestView(OAuthLibMixin, View):
            server_class = Server
            validator_class = OAuth2Validator

        request = self.request_factory.get("/fake-req")
        request.user = "fake"
        test_view = TestView()

        self.assertIsInstance(test_view.get_server(request), Server)

@@ -207,16 +207,37 @@
 LibMixin
+, ScopedResourceMixin
 %0Afrom ..
@@ -1345,8 +1345,722 @@
 Server)%0A
+%0A%0Aclass TestScopedResourceMixin(TestCase):%0A    @classmethod%0A    def setUpClass(cls):%0A        cls.request_factory = RequestFactory()%0A%0A    def test_missing_requested_scopes(self):%0A        class TestView(ScopedResourceMixin, View):%0A            pass%0A%0A        request = self.request_factory.get(%22/fake-req%22)%0A        test_view = TestView()%0A%0A        self.assertRaises(ImproperlyConfigured, test_view.get_scopes)%0A%0A    def test_correct_requested_scopes(self):%0A        class TestView(ScopedResourceMixin, View):%0A            requested_scopes = %5B'scope1', 'scope2'%5D%0A%0A        request = self.request_factory.get(%22/fake-req%22)%0A        test_view = TestView()%0A%0A        self.assertEqual(test_view.get_scopes(), %5B'scope1', 'scope2'%5D)%0A

Add ScopedResourceMixin tests