1
1
from unittest .case import skip
2
2
3
- from django .conf import settings
4
- from django .contrib .auth import get_user_model
5
- from django .contrib .auth .models import Permission
6
- from django .contrib .messages .storage .fallback import FallbackStorage
7
- from django .http .request import HttpRequest
8
3
from django .test import TestCase
9
4
from django .views .generic .base import ContextMixin
10
5
from edc_action_item import site_action_items
11
6
from edc_action_item .models import ActionItem
12
7
from edc_registration .models import RegisteredSubject
13
- from edc_sites .utils import get_site_model_cls
14
8
from edc_sites .view_mixins import SiteViewMixin
9
+ from edc_test_utils .get_httprequest_for_tests import get_request_object_for_tests
10
+ from edc_test_utils .get_user_for_tests import get_user_for_tests
15
11
16
12
from ..action_items import SUBJECT_LOCATOR_ACTION
17
13
from ..view_mixins import SubjectLocatorViewMixin , SubjectLocatorViewMixinError
@@ -24,30 +20,9 @@ def __init__(self, **kwargs):
24
20
25
21
class TestViewMixins (TestCase ):
26
22
def setUp (self ):
23
+ self .user = get_user_for_tests ()
27
24
self .subject_identifier = "12345"
28
25
RegisteredSubject .objects .create (subject_identifier = self .subject_identifier )
29
- self .user = get_user_model ().objects .create_superuser (
30
- "user_login" ,
"[email protected] " ,
"pass"
31
- )
32
- self .user .is_active = True
33
- self .user .is_staff = True
34
- self .user .save ()
35
- self .user .refresh_from_db ()
36
- self .user .userprofile .sites .add (get_site_model_cls ().objects .get (id = settings .SITE_ID ))
37
- self .user .user_permissions .add (
38
- Permission .objects .get (
39
- codename = "view_appointment" , content_type__app_label = "edc_appointment"
40
- )
41
- )
42
-
43
- def get_request_object (self ) -> HttpRequest :
44
- request = HttpRequest ()
45
- setattr (request , "session" , "session" )
46
- messages = FallbackStorage (request )
47
- setattr (request , "_messages" , messages )
48
- setattr (request , "user" , self .user )
49
- setattr (request , "site" , get_site_model_cls ().objects .get (id = settings .SITE_ID ))
50
- return request
51
26
52
27
def test_subject_locator_raises_on_bad_model (self ):
53
28
class MySubjectLocatorViewMixin (SiteViewMixin , SubjectLocatorViewMixin , ContextMixin ):
@@ -56,7 +31,7 @@ class MySubjectLocatorViewMixin(SiteViewMixin, SubjectLocatorViewMixin, ContextM
56
31
57
32
mixin = MySubjectLocatorViewMixin ()
58
33
mixin .kwargs = {"subject_identifier" : self .subject_identifier }
59
- mixin .request = self .get_request_object ( )
34
+ mixin .request = get_request_object_for_tests ( self .user )
60
35
self .assertRaises (SubjectLocatorViewMixinError , mixin .get_context_data )
61
36
62
37
def test_subject_locator_raisesmissing_wrapper_cls (self ):
@@ -73,7 +48,7 @@ class MySubjectLocatorViewMixin(SiteViewMixin, SubjectLocatorViewMixin, ContextM
73
48
74
49
mixin = MySubjectLocatorViewMixin ()
75
50
mixin .kwargs = {"subject_identifier" : self .subject_identifier }
76
- mixin .request = self .get_request_object ( )
51
+ mixin .request = get_request_object_for_tests ( self .user )
77
52
self .assertGreater (len (mixin .request ._messages ._queued_messages ), 0 )
78
53
79
54
def test_subject_locator_view_ok (self ):
@@ -82,7 +57,7 @@ class MySubjectLocatorViewMixin(SiteViewMixin, SubjectLocatorViewMixin, ContextM
82
57
subject_locator_model = "edc_locator.subjectlocator"
83
58
84
59
mixin = MySubjectLocatorViewMixin ()
85
- mixin .request = self .get_request_object ( )
60
+ mixin .request = get_request_object_for_tests ( self .user )
86
61
mixin .kwargs = {"subject_identifier" : self .subject_identifier }
87
62
try :
88
63
mixin .get_context_data ()
@@ -95,7 +70,7 @@ class MySubjectLocatorViewMixin(SiteViewMixin, SubjectLocatorViewMixin, ContextM
95
70
subject_locator_model = "edc_locator.subjectlocator"
96
71
97
72
mixin = MySubjectLocatorViewMixin ()
98
- mixin .request = self .get_request_object ( )
73
+ mixin .request = get_request_object_for_tests ( self .user )
99
74
mixin .kwargs = {"subject_identifier" : self .subject_identifier }
100
75
try :
101
76
mixin .get_context_data ()
0 commit comments