1212from datetime import timedelta , datetime
1313from django .conf import settings
1414from apps .authorization .models import update_grants
15+ from apps .authorization .models import ArchivedDataAccessGrant , DataAccessGrant
1516
17+ # Imports for quieting things during startup.
18+ from waffle .models import Switch
19+
20+ from uuid import uuid4
1621
17- def create_group (name = "BlueButton" ):
1822
23+ def create_group (name = "BlueButton" ):
1924 g , created = Group .objects .get_or_create (name = name )
2025 if created :
2126 print ("%s group created" % (name ))
@@ -24,41 +29,29 @@ def create_group(name="BlueButton"):
2429 return g
2530
2631
27- def create_user (group , usr ):
28- u_name = "fred "
32+ def create_user (the_group ):
33+ username = "rogersf "
2934 first_name = "Fred"
30- last_name = "Flinstone "
31- 32- password = "foobarfoobarfoobar"
35+ last_name = "Rogers "
36+ 37+ password = uuid4 ()
3338 user_type = "BEN"
3439
35- if usr is not None :
36- u_name = usr
37- first_name = "{}{}" .format (usr , "First" )
38- last_name = "{}{}" .format (usr , "Last" )
39- email = "{}.{}@example.com" .format (first_name , last_name )
40- user_type = "DEV"
41-
42- if User .objects .filter (username = u_name ).exists ():
43- User .objects .filter (username = u_name ).delete ()
44-
45- u = None
46-
47- if usr is not None :
48- u = User .objects .create_user (username = u_name ,
49- first_name = first_name ,
50- last_name = last_name ,
51- email = email )
52- u .set_unusable_password ()
53- else :
54- # create a sample user 'fred' for dev local that has a usable password
55- u = User .objects .create_user (username = u_name ,
56- first_name = first_name ,
57- last_name = last_name ,
58- email = email ,
59- password = password ,)
60-
61- UserProfile .objects .create (user = u ,
40+ # We will do this over-and-over.
41+ # If we don't already exist, then create the user.
42+ if User .objects .filter (username = username ).exists ():
43+ print (f"👟 { username } already exists. Skipping test user creation." )
44+ return User .objects .get (username = username )
45+
46+ # If the user didn't exist, it is our first time through.
47+ # Create the user.
48+ user_obj = User .objects .create (username = username ,
49+ first_name = first_name ,
50+ last_name = last_name ,
51+ email = email ,
52+ password = password ,)
53+ user_obj .set_unusable_password ()
54+ UserProfile .objects .create (user = user_obj ,
6255 user_type = user_type ,
6356 create_applications = True ,
6457 password_reset_question_1 = '1' ,
@@ -67,17 +60,17 @@ def create_user(group, usr):
6760 password_reset_answer_2 = 'Frank' ,
6861 password_reset_question_3 = '3' ,
6962 password_reset_answer_3 = 'Bentley' )
63+ user_obj .groups .add (the_group )
7064
71- u .groups .add (group )
72-
73- if usr is None :
74- if not Crosswalk .objects .filter (_user_id_hash = "ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536" ).exists ():
75- c , g_o_c = Crosswalk .objects .get_or_create (user = u ,
76- fhir_id_v2 = settings .DEFAULT_SAMPLE_FHIR_ID_V2 ,
77- _user_id_hash = "ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536" )
78- else :
79- print ("Skipping crosswalk creation; already exists." )
80- return u
65+ # CROSSWALK
66+ # Removing any existing crosswalks for this artificial user.
67+ # Why? Just in case.
68+ user_id_hash = "ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536"
69+ Crosswalk .objects .filter (_user_id_hash = user_id_hash ).delete ()
70+ Crosswalk .objects .get_or_create (user = user_obj ,
71+ fhir_id_v2 = settings .DEFAULT_SAMPLE_FHIR_ID_V2 ,
72+ _user_id_hash = user_id_hash )
73+ return user_obj
8174
8275
8376def create_application (user ):
@@ -110,47 +103,59 @@ def create_application(user):
110103 return the_app
111104
112105
113- def create_test_token (user , application ):
106+ def create_test_token (the_user , the_app ):
114107
108+ # Set expiration one day from now.
115109 now = timezone .now ()
116110 expires = now + timedelta (days = 1 )
117- token_value = "sample-token-string"
118111
119- scopes = application .scope .all ()
112+ scopes = the_app .scope .all ()
120113 scope = []
121114 for s in scopes :
122115 scope .append (s .slug )
123116
124- AccessToken .objects .filter (token = token_value ).delete ()
125- t = AccessToken .objects .create (user = user , application = application ,
126- token = token_value ,
117+ # We have to have a tokent with token="sample-token-string", because we
118+ # rely on it existing for unit tests. Which are actually integration tests.
119+ if AccessToken .objects .filter (token = "sample-token-string" ).exists ():
120+ t = AccessToken .objects .get (token = "sample-token-string" )
121+ t .expires = expires
122+ t .save ()
123+ else :
124+ AccessToken .objects .create (user = the_user ,
125+ application = the_app ,
126+ # This needs to be "sample-token-string", because
127+ # we have tests that rely on it.
128+ token = "sample-token-string" ,
127129 expires = expires ,
128- scope = ' ' .join (scope ))
129- return t
130+ scope = ' ' .join (scope ),)
131+
132+
133+ def get_switch (name ):
134+ try :
135+ sw = Switch .objects .get (name = name )
136+ return sw .active
137+ except Exception as e :
138+ print (f"Could not get switch { name } : { e } " )
139+
140+
141+ def set_switch (name , b ):
142+ sw , _ = Switch .objects .get_or_create (name = name )
143+ sw .active = b
144+ sw .save ()
130145
131146
132147class Command (BaseCommand ):
133148 help = 'Create a test user and application for the test client'
134149
135- def add_arguments (self , parser ):
136- parser .add_argument ("-u" , "--user" , help = "Name of the user to be created (unique)." )
137- parser .add_argument ("-a" , "--app" , help = "Name of the application to be created (unique)." )
138- parser .add_argument ("-r" , "--redirect" , help = "Redirect url of the application." )
139-
140150 def handle (self , * args , ** options ):
141- usr = options ["user" ]
142- app = options ["app" ]
143- redirect = options ["redirect" ]
144-
145- g = create_group ()
146- u = create_user (g , usr )
147- a = create_application (u ) # a = create_application(u, g, app, redirect)
148- t = None
149- if usr is None and app is None :
150- t = create_test_token (u , a )
151- update_grants ()
152- print ("Name:" , a .name )
153- print ("client_id:" , a .client_id )
154- print ("client_secret:" , a .client_secret )
155- print ("access_token:" , t .token if t else "None" )
156- print ("redirect_uri:" , a .redirect_uris )
151+
152+ set_switch ('outreach_email' , False )
153+
154+ the_group = create_group ()
155+ the_user = create_user (the_group )
156+ the_app = create_application (the_user )
157+ create_test_token (the_user , the_app )
158+ update_grants ()
159+
160+ # Restore switch to whatever it was.
161+ set_switch ('outreach_email' , True )
0 commit comments