Coverage for test_backends.py: 100%

1 from plain.oauth.providers import OAuthProvider, OAuthToken, OAuthUser

2

3

4 class DummyProvider(OAuthProvider):

5 def get_oauth_token(self, *, code, request):

6 return OAuthToken(

7 access_token="dummy_token",

8 )

9

10 def get_oauth_user(self, *, oauth_token):

11 return OAuthUser(

12 id="dummy_user_id",

13 username="dummy_username",

14 email="dummy@example.com",

15 )

16

17 def check_request_state(self, *, request):

18 """Don't check the state"""

19 return

20

21

22 def test_single_backend(db, client, settings):

23 settings.OAUTH_LOGIN_PROVIDERS = {

24 "dummy": {

25 "class": "test_backends.DummyProvider",

26 "kwargs": {

27 "client_id": "dummy_client_id",

28 "client_secret": "dummy_client_secret",

29 "scope": "dummy_scope",

30 },

31 }

32 }

33

34 response = client.get("/oauth/dummy/callback/?code=test_code&state=dummy_state")

35 assert response.status_code == 302

36 assert response.url == "/"

37

38 # Now logged in

39 response = client.get("/")

40 assert response.user

41

42

43 def test_multiple_backends(db, client, settings):

44 settings.OAUTH_LOGIN_PROVIDERS = {

45 "dummy": {

46 "class": "test_backends.DummyProvider",

47 "kwargs": {

48 "client_id": "dummy_client_id",

49 "client_secret": "dummy_client_secret",

50 "scope": "dummy_scope",

51 },

52 }

53 }

54

55 response = client.get("/oauth/dummy/callback/?code=test_code&state=dummy_state")

56 assert response.status_code == 302

57 assert response.url == "/"

58

59 # Now logged in

60 response = client.get("/")

61 assert response.user