Skip to content

Commit 00f5e74

Browse files
author
Shivam Tiwari
committed
BB2-4303: Revert changes to fix selenium tests
1 parent 1e0bdf7 commit 00f5e74

File tree

3 files changed

+85
-94
lines changed

3 files changed

+85
-94
lines changed

apps/testclient/management/commands/create_test_user_and_application.py

Lines changed: 84 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@
1212
from datetime import timedelta, datetime
1313
from django.conf import settings
1414
from 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

2317
def 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,42 @@ def create_group(name="BlueButton"):
2924
return g
3025

3126

32-
def create_user(the_group):
33-
username = "rogersf"
34-
first_name = "Fred"
35-
last_name = "Rogers"
36-
37-
password = uuid4()
27+
def create_user(group, usr):
28+
u_name = "fred"
29+
first_name = "Fred"
30+
last_name = "Flinstone"
31+
32+
password = "foobarfoobarfoobar"
3833
user_type = "BEN"
39-
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,
34+
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+
43+
if User.objects.filter(username=u_name).exists():
44+
User.objects.filter(username=u_name).delete()
45+
46+
u = None
47+
48+
if usr is not None:
49+
u = User.objects.create_user(username=u_name,
50+
first_name=first_name,
51+
last_name=last_name,
52+
email=email)
53+
u.set_unusable_password()
54+
else:
55+
# create a sample user 'fred' for dev local that has a usable password
56+
u = User.objects.create_user(username=u_name,
57+
first_name=first_name,
58+
last_name=last_name,
59+
email=email,
60+
password=password,)
61+
62+
UserProfile.objects.create(user=u,
5563
user_type=user_type,
5664
create_applications=True,
5765
password_reset_question_1='1',
@@ -60,35 +68,33 @@ def create_user(the_group):
6068
password_reset_answer_2='Frank',
6169
password_reset_question_3='3',
6270
password_reset_answer_3='Bentley')
63-
user_obj.groups.add(the_group)
6471

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
72+
u.groups.add(group)
7473

74+
if usr is None:
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+
return u
7579

76-
def create_application(user):
77-
app_name = "TestApp"
78-
if Application.objects.filter(name=app_name).exists():
79-
return Application.objects.get(name=app_name)
80-
81-
# If the app doesn't exist, create the test app.
8280

81+
def create_application(user, group, app, redirect):
82+
app_name = "TestApp" if app is None else app
8383
Application.objects.filter(name=app_name).delete()
8484
redirect_uri = "{}{}".format(settings.HOSTNAME_URL, settings.TESTCLIENT_REDIRECT_URI)
8585

86-
the_app = Application.objects.create(name=app_name,
87-
redirect_uris=redirect_uri,
88-
user=user,
89-
data_access_type="THIRTEEN_MONTH",
90-
client_type="confidential",
91-
authorization_grant_type="authorization-code",)
86+
if redirect:
87+
redirect_uri = redirect
88+
89+
if not(redirect_uri.startswith("http://") or redirect_uri.startswith("https://")):
90+
redirect_uri = "https://" + redirect_uri
91+
92+
a = Application.objects.create(name=app_name,
93+
redirect_uris=redirect_uri,
94+
user=user,
95+
data_access_type="THIRTEEN_MONTH",
96+
client_type="confidential",
97+
authorization_grant_type="authorization-code")
9298

9399
titles = ["My Medicare and supplemental coverage information.",
94100
"My Medicare claim information.",
@@ -98,64 +104,49 @@ def create_application(user):
98104

99105
for t in titles:
100106
c = ProtectedCapability.objects.get(title=t)
101-
the_app.scope.add(c)
102-
103-
return the_app
107+
a.scope.add(c)
108+
return a
104109

105110

106-
def create_test_token(the_user, the_app):
111+
def create_test_token(user, application):
107112

108-
# Set expiration one day from now.
109113
now = timezone.now()
110114
expires = now + timedelta(days=1)
111115

112-
scopes = the_app.scope.all()
116+
scopes = application.scope.all()
113117
scope = []
114118
for s in scopes:
115119
scope.append(s.slug)
116120

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.
121+
t = AccessToken.objects.create(user=user, application=application,
128122
token="sample-token-string",
129123
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()
124+
scope=' '.join(scope))
125+
return t
145126

146127

147128
class Command(BaseCommand):
148129
help = 'Create a test user and application for the test client'
149130

150-
def handle(self, *args, **options):
151-
152-
set_switch('outreach_email', False)
131+
def add_arguments(self, parser):
132+
parser.add_argument("-u", "--user", help="Name of the user to be created (unique).")
133+
parser.add_argument("-a", "--app", help="Name of the application to be created (unique).")
134+
parser.add_argument("-r", "--redirect", help="Redirect url of the application.")
153135

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)
136+
def handle(self, *args, **options):
137+
usr = options["user"]
138+
app = options["app"]
139+
redirect = options["redirect"]
140+
141+
g = create_group()
142+
u = create_user(g, usr)
143+
a = create_application(u, g, app, redirect)
144+
t = None
145+
if usr is None and app is None:
146+
t = create_test_token(u, a)
147+
update_grants()
148+
print("Name:", a.name)
149+
print("client_id:", a.client_id)
150+
print("client_secret:", a.client_secret)
151+
print("access_token:", t.token if t else "None")
152+
print("redirect_uri:", a.redirect_uris)

docker-compose.selenium.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ services:
2424
- "5900:5900"
2525

2626
msls:
27-
build: ./dev-local
27+
build: ./msls-local
2828
command: bash -c "python -m debugpy --listen 0.0.0.0:7890 app.py"
2929
ports:
3030
- "8080:8080"
File renamed without changes.

0 commit comments

Comments
 (0)