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
16-
17- # Imports for quieting things during startup.
18- from waffle .models import Switch
19-
20- from uuid import uuid4
2115
2216
2317def create_group (name = "BlueButton" ):
18+
2419 g , created = Group .objects .get_or_create (name = name )
2520 if created :
2621 print ("%s group created" % (name ))
@@ -29,29 +24,41 @@ def create_group(name="BlueButton"):
2924 return g
3025
3126
32- def create_user (the_group ):
33- username = "rogersf "
27+ def create_user (group , usr ):
28+ u_name = "fred "
3429 first_name = "Fred"
35- last_name = "Rogers "
36- 37- password = uuid4 ()
30+ last_name = "Flinstone "
31+ 32+ password = "foobarfoobarfoobar"
3833 user_type = "BEN"
3934
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 ,
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 ,
5562 user_type = user_type ,
5663 create_applications = True ,
5764 password_reset_question_1 = '1' ,
@@ -60,17 +67,17 @@ def create_user(the_group):
6067 password_reset_answer_2 = 'Frank' ,
6168 password_reset_question_3 = '3' ,
6269 password_reset_answer_3 = 'Bentley' )
63- user_obj .groups .add (the_group )
6470
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
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
7481
7582
7683def create_application (user ):
@@ -103,59 +110,47 @@ def create_application(user):
103110 return the_app
104111
105112
106- def create_test_token (the_user , the_app ):
113+ def create_test_token (user , application ):
107114
108- # Set expiration one day from now.
109115 now = timezone .now ()
110116 expires = now + timedelta (days = 1 )
117+ token_value = "sample-token-string"
111118
112- scopes = the_app .scope .all ()
119+ scopes = application .scope .all ()
113120 scope = []
114121 for s in scopes :
115122 scope .append (s .slug )
116123
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" ,
124+ AccessToken .objects .filter (token = token_value ).delete ()
125+ t = AccessToken .objects .create (user = user , application = application ,
126+ token = token_value ,
129127 expires = expires ,
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 ()
128+ scope = ' ' .join (scope ))
129+ return t
145130
146131
147132class Command (BaseCommand ):
148133 help = 'Create a test user and application for the test client'
149134
150- def handle (self , * args , ** options ):
151-
152- set_switch ('outreach_email' , False )
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." )
153139
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 )
140+ 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 )
0 commit comments