1
- import json
2
-
3
- from django .conf import settings
4
1
from django .core .urlresolvers import reverse
5
- from django .test .client import Client , MULTIPART_CONTENT
6
2
from django .test import TestCase
7
3
from django .contrib .auth import get_user_model
8
4
from django .core import mail
9
5
from django .test .utils import override_settings
10
- from django .contrib .sites .models import Site
11
6
from django .utils .encoding import force_text
12
7
13
- from allauth .socialaccount .models import SocialApp
14
- from allauth .socialaccount .providers .facebook .provider import GRAPH_API_URL
15
- import responses
16
-
17
8
from rest_framework import status
18
9
19
-
20
- class APIClient (Client ):
21
-
22
- def patch (self , path , data = '' , content_type = MULTIPART_CONTENT , follow = False , ** extra ):
23
- return self .generic ('PATCH' , path , data , content_type , ** extra )
24
-
25
- def options (self , path , data = '' , content_type = MULTIPART_CONTENT , follow = False , ** extra ):
26
- return self .generic ('OPTIONS' , path , data , content_type , ** extra )
27
-
28
-
29
- class BaseAPITestCase (object ):
30
-
31
- """
32
- base for API tests:
33
- * easy request calls, f.e.: self.post(url, data), self.get(url)
34
- * easy status check, f.e.: self.post(url, data, status_code=200)
35
- """
36
- def send_request (self , request_method , * args , ** kwargs ):
37
- request_func = getattr (self .client , request_method )
38
- status_code = None
39
- if 'content_type' not in kwargs and request_method != 'get' :
40
- kwargs ['content_type' ] = 'application/json'
41
- if 'data' in kwargs and request_method != 'get' and kwargs ['content_type' ] == 'application/json' :
42
- data = kwargs .get ('data' , '' )
43
- kwargs ['data' ] = json .dumps (data ) # , cls=CustomJSONEncoder
44
- if 'status_code' in kwargs :
45
- status_code = kwargs .pop ('status_code' )
46
-
47
- # check_headers = kwargs.pop('check_headers', True)
48
- if hasattr (self , 'token' ):
49
- kwargs ['HTTP_AUTHORIZATION' ] = 'Token %s' % self .token
50
-
51
- self .response = request_func (* args , ** kwargs )
52
- is_json = bool (
53
- [x for x in self .response ._headers ['content-type' ] if 'json' in x ])
54
- if is_json and self .response .content :
55
- self .response .json = json .loads (force_text (self .response .content ))
56
- else :
57
- self .response .json = {}
58
- if status_code :
59
- self .assertEqual (self .response .status_code , status_code )
60
- return self .response
61
-
62
- def post (self , * args , ** kwargs ):
63
- return self .send_request ('post' , * args , ** kwargs )
64
-
65
- def get (self , * args , ** kwargs ):
66
- return self .send_request ('get' , * args , ** kwargs )
67
-
68
- def patch (self , * args , ** kwargs ):
69
- return self .send_request ('patch' , * args , ** kwargs )
70
-
71
- # def put(self, *args, **kwargs):
72
- # return self.send_request('put', *args, **kwargs)
73
-
74
- # def delete(self, *args, **kwargs):
75
- # return self.send_request('delete', *args, **kwargs)
76
-
77
- # def options(self, *args, **kwargs):
78
- # return self.send_request('options', *args, **kwargs)
79
-
80
- # def post_file(self, *args, **kwargs):
81
- # kwargs['content_type'] = MULTIPART_CONTENT
82
- # return self.send_request('post', *args, **kwargs)
83
-
84
- # def get_file(self, *args, **kwargs):
85
- # content_type = None
86
- # if 'content_type' in kwargs:
87
- # content_type = kwargs.pop('content_type')
88
- # response = self.send_request('get', *args, **kwargs)
89
- # if content_type:
90
- # self.assertEqual(
91
- # bool(filter(lambda x: content_type in x, response._headers['content-type'])), True)
92
- # return response
93
-
94
- def init (self ):
95
- settings .DEBUG = True
96
- self .client = APIClient ()
97
-
98
- self .login_url = reverse ('rest_login' )
99
- self .logout_url = reverse ('rest_logout' )
100
- self .password_change_url = reverse ('rest_password_change' )
101
- self .register_url = reverse ('rest_register' )
102
- self .password_reset_url = reverse ('rest_password_reset' )
103
- self .user_url = reverse ('rest_user_details' )
104
- self .veirfy_email_url = reverse ('rest_verify_email' )
105
- self .fb_login_url = reverse ('fb_login' )
106
-
107
- def _login (self ):
108
- payload = {
109
- "username" : self .USERNAME ,
110
- "password" : self .PASS
111
- }
112
- self .post (self .login_url , data = payload , status_code = status .HTTP_200_OK )
113
-
114
- def _logout (self ):
115
- self .post (self .logout_url , status = status .HTTP_200_OK )
116
-
117
-
118
- # -----------------------
119
- # T E S T H E R E
120
- # -----------------------
10
+ from .test_base import BaseAPITestCase
121
11
122
12
123
13
class APITestCase1 (TestCase , BaseAPITestCase ):
@@ -127,7 +17,7 @@ class APITestCase1(TestCase, BaseAPITestCase):
127
17
- custom registration: backend defined
128
18
"""
129
19
130
- urls = 'rest_auth.test_urls '
20
+ urls = 'tests.urls '
131
21
132
22
USERNAME = 'person'
133
23
PASS = 'person'
@@ -421,119 +311,3 @@ def test_registration_with_email_verification(self):
421
311
# try to login again
422
312
self ._login ()
423
313
self ._logout ()
424
-
425
-
426
- class TestSocialAuth (TestCase , BaseAPITestCase ):
427
-
428
- urls = 'rest_auth.test_urls'
429
-
430
- USERNAME = 'person'
431
- PASS = 'person'
432
-
433
- REGISTRATION_DATA = {
434
- "username" : USERNAME ,
435
- "password1" : PASS ,
436
- "password2" : PASS ,
437
- "email" : EMAIL
438
- }
439
-
440
- def setUp (self ):
441
- self .init ()
442
-
443
- social_app = SocialApp .objects .create (
444
- provider = 'facebook' ,
445
- name = 'Facebook' ,
446
- client_id = '123123123' ,
447
- secret = '321321321' ,
448
- )
449
- site = Site .objects .get_current ()
450
- social_app .sites .add (site )
451
- self .graph_api_url = GRAPH_API_URL + '/me'
452
-
453
- @responses .activate
454
- def test_failed_social_auth (self ):
455
- # fake response
456
- responses .add (
457
- responses .GET ,
458
- self .graph_api_url ,
459
- body = '' ,
460
- status = 400 ,
461
- content_type = 'application/json'
462
- )
463
-
464
- payload = {
465
- 'access_token' : 'abc123'
466
- }
467
- self .post (self .fb_login_url , data = payload , status_code = 400 )
468
-
469
- @responses .activate
470
- def test_social_auth (self ):
471
- # fake response for facebook call
472
- resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\ /\\ /www.facebook.com\\ /john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true}' # noqa
473
- responses .add (
474
- responses .GET ,
475
- self .graph_api_url ,
476
- body = resp_body ,
477
- status = 200 ,
478
- content_type = 'application/json'
479
- )
480
-
481
- users_count = get_user_model ().objects .all ().count ()
482
- payload = {
483
- 'access_token' : 'abc123'
484
- }
485
-
486
- self .post (self .fb_login_url , data = payload , status_code = 200 )
487
- self .assertIn ('key' , self .response .json .keys ())
488
- self .assertEqual (get_user_model ().objects .all ().count (), users_count + 1 )
489
-
490
- # make sure that second request will not create a new user
491
- self .post (self .fb_login_url , data = payload , status_code = 200 )
492
- self .assertIn ('key' , self .response .json .keys ())
493
- self .assertEqual (get_user_model ().objects .all ().count (), users_count + 1 )
494
-
495
- @responses .activate
496
- @override_settings (
497
- ACCOUNT_EMAIL_VERIFICATION = 'mandatory' ,
498
- ACCOUNT_EMAIL_REQUIRED = True ,
499
- REST_SESSION_LOGIN = False
500
- )
501
- def test_edge_case (self ):
502
- resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\ /\\ /www.facebook.com\\ /john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true,"email":"%s"}' # noqa
503
- responses .add (
504
- responses .GET ,
505
- self .graph_api_url ,
506
- body = resp_body % self .EMAIL ,
507
- status = 200 ,
508
- content_type = 'application/json'
509
- )
510
-
511
- # test empty payload
512
- self .post (self .register_url , data = {}, status_code = 400 )
513
-
514
- self .post (
515
- self .register_url ,
516
- data = self .REGISTRATION_DATA ,
517
- status_code = 201
518
- )
519
- new_user = get_user_model ().objects .latest ('id' )
520
- self .assertEqual (new_user .username , self .REGISTRATION_DATA ['username' ])
521
-
522
- # verify email
523
- email_confirmation = new_user .emailaddress_set .get (email = self .EMAIL )\
524
- .emailconfirmation_set .order_by ('-created' )[0 ]
525
- self .post (
526
- self .veirfy_email_url ,
527
- data = {"key" : email_confirmation .key },
528
- status_code = status .HTTP_200_OK
529
- )
530
-
531
- self ._login ()
532
- self ._logout ()
533
-
534
- payload = {
535
- 'access_token' : 'abc123'
536
- }
537
-
538
- self .post (self .fb_login_url , data = payload , status_code = 200 )
539
- self .assertIn ('key' , self .response .json .keys ())
0 commit comments