Skip to content

Commit 9f3dea7

Browse files
committed
Fixes.
1 parent 5a9d396 commit 9f3dea7

File tree

8 files changed

+141
-155
lines changed

8 files changed

+141
-155
lines changed

apps/authorization/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Meta:
3131
fields = ('id', 'name', 'logo_uri', 'tos_uri', 'policy_uri', 'contacts')
3232

3333
def get_contacts(self, obj):
34-
print(obj)
34+
# print(obj)
3535
application = Application.objects.get(id=obj.id)
3636
return application.support_email or ""
3737

apps/core/management/commands/create_test_feature_switches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def handle(self, *args, **options):
6262
flag_obj.save()
6363
self._log("User {} added to feature flag: {}".format(u, flag))
6464
except Exception as e:
65-
print(e)
65+
# print(e)
6666
self._log("Exception when adding user {} to feature flag: {}".format(u, flag))
6767
except User.DoesNotExist:
6868
# assuming test users exist before creating flags associated with them

apps/fhir/bluebutton/tests/test_wellknown_endpoints.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ def test_smart_configuration_missing_fields_in_v3(self):
147147
# is commented above for reference.
148148

149149
@skipIf((not settings.RUN_ONLINE_TESTS), "Can't reach external sites.")
150-
# This overrides the switch and sets it to true, always.
151-
# We should only run the test if we have v3 enabled.
152150
@override_switch('v3_endpoints', active=True)
153151
def test_fhir_metadata_extensions_have_v3(self):
154-
response = self.client.get(f'{BASEURL}/v3/fhir/metadata')
152+
the_url = f'{BASEURL}/v3/fhir/metadata'
153+
print(the_url)
154+
response = self.client.get(the_url)
155155
self.assertEqual(response.status_code, 200)
156156
json = response.json()
157157
self.assertIn('v3', json['implementation']['url'])

apps/testclient/management/commands/create_test_user_and_application.py

Lines changed: 93 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -17,188 +17,147 @@
1717
# Imports for quieting things during startup.
1818
from waffle.models import Switch
1919

20+
from uuid import uuid4
2021

21-
def create_group(name="BlueButton"):
2222

23+
def create_group(name="BlueButton"):
2324
g, created = Group.objects.get_or_create(name=name)
2425
if created:
2526
print("%s group created" % (name))
2627
else:
2728
print("%s group pre-existing. Create skipped." % (name))
2829
return g
2930

30-
31-
def get_switch(name):
32-
try:
33-
sw = Switch.objects.get(name=name)
34-
return sw.active
35-
except Exception as e:
36-
print(f"Could not get switch {name}: {e}")
37-
38-
39-
def set_switch(name, b):
40-
# DISABLE SOME WAFFLE SWITCHES
41-
# We don't want email, etc.
42-
sw, _ = Switch.objects.get_or_create(name=name)
43-
sw.active = b
44-
sw.save()
45-
4631
# usr would be a string if it is anything
4732

4833

49-
def create_user(group, usr):
50-
u_name = "rogersf"
34+
def create_user(the_group):
35+
username = "rogersf"
5136
first_name = "Fred"
5237
last_name = "Rogers"
53-
email = "fred@landofmakebelieve.gov"
54-
password = "danielthetiger"
38+
email = "mrrogers@landofmakebelieve.gov"
39+
password = uuid4()
5540
user_type = "BEN"
5641

57-
if usr is not None:
58-
u_name = usr
59-
first_name = "{}{}".format(usr, "First")
60-
last_name = "{}{}".format(usr, "Last")
61-
email = "{}.{}@{}".format(first_name, last_name, email)
62-
user_type = "DEV"
63-
64-
# This violates constraints on other tables.
65-
usr_q = User.objects.filter(username=u_name)
66-
if usr_q.exists():
67-
# Delete any ADAGs for this user, or we will run into a
68-
# constraint issue at startup.
69-
count = ArchivedDataAccessGrant.objects.filter(beneficiary=usr_q.first()).delete()
70-
print(f"Deleted {count} ADAGs for {u_name}")
71-
count = DataAccessGrant.objects.filter(beneficiary=usr_q.first()).delete()
72-
print(f"Deleted {count} ADAGs for {u_name}")
73-
74-
User.objects.filter(username=u_name).delete()
75-
76-
u = None
77-
78-
if usr is not None:
79-
try:
80-
u, _ = User.objects.get_or_create(username=u_name,
81-
first_name=first_name,
82-
last_name=last_name,
83-
email=email,
84-
signals_to_disable=["post_save"])
85-
u.set_unusable_password()
86-
except Exception as e:
87-
print(f"Did not create user: {e}")
88-
else:
89-
# create a sample user 'fred' for dev local that has a usable password
90-
try:
91-
# get_or_create returns a tuple (v, bool)
92-
u, _ = User.objects.get_or_create(username=u_name,
93-
first_name=first_name,
94-
last_name=last_name,
95-
email=email,
96-
password=password,)
97-
98-
UserProfile.objects.create(user=u,
99-
user_type=user_type,
100-
create_applications=True,
101-
password_reset_question_1='1',
102-
password_reset_answer_1='blue',
103-
password_reset_question_2='2',
104-
password_reset_answer_2='Frank',
105-
password_reset_question_3='3',
106-
password_reset_answer_3='Bentley')
107-
except Exception as e:
108-
print(f"Did not create user and profile: {e}")
109-
110-
if u is None:
111-
print(f"Error creating user; exiting.")
112-
else:
113-
u.groups.add(group)
114-
42+
# We will do this over-and-over.
43+
# If we don't already exist, then create the user.
44+
if User.objects.filter(username=username).exists():
45+
print(f"👟 {username} already exists. Skipping test user creation.")
46+
return User.objects.get(username=username)
47+
48+
# If the user didn't exist, it is our first time through.
49+
# Create the user.
50+
user_obj = User.objects.create(username=username,
51+
first_name=first_name,
52+
last_name=last_name,
53+
email=email,
54+
password=password,)
55+
user_obj.set_unusable_password()
56+
UserProfile.objects.create(user=user_obj,
57+
user_type=user_type,
58+
create_applications=True,
59+
password_reset_question_1='1',
60+
password_reset_answer_1='blue',
61+
password_reset_question_2='2',
62+
password_reset_answer_2='Frank',
63+
password_reset_question_3='3',
64+
password_reset_answer_3='Bentley')
65+
user_obj.groups.add(the_group)
66+
67+
# CROSSWALK
68+
# Removing any existing crosswalks for this artificial user.
69+
# Why? Just in case.
11570
user_id_hash = "ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536"
11671
Crosswalk.objects.filter(_user_id_hash=user_id_hash).delete()
117-
c, _ = Crosswalk.objects.get_or_create(user=u,
118-
fhir_id_v2=settings.DEFAULT_SAMPLE_FHIR_ID_V2,
119-
_user_id_hash=user_id_hash)
120-
return u
72+
Crosswalk.objects.get_or_create(user=user_obj,
73+
fhir_id_v2=settings.DEFAULT_SAMPLE_FHIR_ID_V2,
74+
_user_id_hash=user_id_hash)
75+
return user_obj
76+
12177

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

123-
def create_application(user, group, app, redirect):
124-
app_name = "TestApp" if app is None else app
12585
Application.objects.filter(name=app_name).delete()
12686
redirect_uri = "{}{}".format(settings.HOSTNAME_URL, settings.TESTCLIENT_REDIRECT_URI)
12787

128-
if redirect:
129-
redirect_uri = redirect
88+
the_app = Application.objects.create(name=app_name,
89+
redirect_uris=redirect_uri,
90+
user=user,
91+
data_access_type="THIRTEEN_MONTH",
92+
client_type="confidential",
93+
authorization_grant_type="authorization-code",)
13094

131-
if not (redirect_uri.startswith("http://") or redirect_uri.startswith("https://")):
132-
redirect_uri = "https://" + redirect_uri
95+
titles = ["My Medicare and supplemental coverage information.",
96+
"My Medicare claim information.",
97+
"My general patient and demographic information.",
98+
"Profile information including name and email."
99+
]
133100

134-
try:
135-
a = Application.objects.create(name=app_name,
136-
redirect_uris=redirect_uri,
137-
user=user,
138-
data_access_type="THIRTEEN_MONTH",
139-
client_type="confidential",
140-
authorization_grant_type="authorization-code",)
141-
142-
titles = ["My Medicare and supplemental coverage information.",
143-
"My Medicare claim information.",
144-
"My general patient and demographic information.",
145-
"Profile information including name and email."
146-
]
147-
148-
for t in titles:
149-
c = ProtectedCapability.objects.get(title=t)
150-
a.scope.add(c)
151-
return a
152-
except Exception as e:
153-
print(f"Skipped creation of {app_name}: {e}")
101+
for t in titles:
102+
c = ProtectedCapability.objects.get(title=t)
103+
the_app.scope.add(c)
154104

105+
return the_app
155106

156-
def create_test_token(user, application):
157107

108+
def create_test_token(the_user, the_app):
109+
110+
# Set expiration one day from now.
158111
now = timezone.now()
159112
expires = now + timedelta(days=1)
160113

161-
scopes = application.scope.all()
114+
scopes = the_app.scope.all()
162115
scope = []
163116
for s in scopes:
164117
scope.append(s.slug)
165118

166-
t = AccessToken.objects.create(user=user, application=application,
119+
# We have to have a tokent with token="sample-token-string", because we
120+
# rely on it existing for unit tests. Which are actually integration tests.
121+
if AccessToken.objects.filter(token="sample-token-string").exists():
122+
t = AccessToken.objects.get(token="sample-token-string")
123+
t.expires = expires
124+
t.save()
125+
else:
126+
AccessToken.objects.create(user=the_user,
127+
application=the_app,
128+
# This needs to be "sample-token-string", because
129+
# we have tests that rely on it.
167130
token="sample-token-string",
168131
expires=expires,
169132
scope=' '.join(scope),)
170133

171-
return t
134+
135+
def get_switch(name):
136+
try:
137+
sw = Switch.objects.get(name=name)
138+
return sw.active
139+
except Exception as e:
140+
print(f"Could not get switch {name}: {e}")
141+
142+
143+
def set_switch(name, b):
144+
sw, _ = Switch.objects.get_or_create(name=name)
145+
sw.active = b
146+
sw.save()
172147

173148

174149
class Command(BaseCommand):
175150
help = 'Create a test user and application for the test client'
176151

177-
def add_arguments(self, parser):
178-
parser.add_argument("-u", "--user", help="Name of the user to be created (unique).")
179-
parser.add_argument("-a", "--app", help="Name of the application to be created (unique).")
180-
parser.add_argument("-r", "--redirect", help="Redirect url of the application.")
181-
182152
def handle(self, *args, **options):
183-
usr = options.get("user", None)
184-
app = options.get("app", None)
185-
redirect = options["redirect"]
186153

187154
set_switch('outreach_email', False)
188155

189-
g = create_group()
190-
u = create_user(g, usr)
191-
print(f"Created user {u}")
192-
a = create_application(u, g, app, redirect)
193-
t = None
194-
if usr is None and app is None:
195-
t = create_test_token(u, a)
196-
update_grants()
197-
print("Name:", a.name)
198-
print("client_id:", a.client_id)
199-
print("client_secret:", a.client_secret)
200-
print("access_token:", t.token if t else "None")
201-
print("redirect_uri:", a.redirect_uris)
156+
the_group = create_group()
157+
the_user = create_user(the_group)
158+
the_app = create_application(the_user)
159+
create_test_token(the_user, the_app)
160+
update_grants()
202161

203162
# Restore switch to whatever it was.
204163
set_switch('outreach_email', True)

apps/testclient/tests.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from apps.testclient.views import FhirDataParams, _build_pagination_uri
1212
from django.http import HttpRequest
1313

14+
import os
15+
1416

1517
class TestclientHelpers(TestCase):
1618
def test_ormap(self):
@@ -307,15 +309,15 @@ def test_offset_math(self):
307309
response = self.client.get(uri)
308310
response_data = response.json()
309311
self.assertEqual(response.status_code, 200)
310-
# self.assertEqual(response_data["total"], 32)
311-
# 20251022 MCJ
312-
# For some reason, this no longer passes when asserted equal to 7.
313-
# I do not know what data we test against, if it is consistent, etc.
314-
# I have updated the test to `5`, and it passes. If the data is potentially variable/not in
315-
# our control, then these unit tests will always be suspect (including offsets and pagination values).
316-
# This seems to have been the case 7mo ago with the "total" test, above.
317-
# self.assertEqual(len(response_data["entry"]), 7)
318-
self.assertEqual(len(response_data["entry"]), 5)
312+
313+
# Different environments have different data in them.
314+
# If we are testing against sandbox, we expect fewer responses.
315+
316+
if os.getenv("LOCAL_TESTING_TARGET", None) in ["impl"]:
317+
self.assertEqual(len(response_data["entry"]), 12)
318+
else:
319+
self.assertEqual(len(response_data["entry"]), 5)
320+
319321
previous_links = [
320322
data["url"]
321323
for data in response_data["link"]
@@ -327,11 +329,20 @@ def test_offset_math(self):
327329
first_links = [
328330
data["url"] for data in response_data["link"] if data["relation"] == "first"
329331
]
330-
self.assertEqual(len(previous_links), 1)
331-
self.assertEqual(len(next_links), 0)
332-
self.assertEqual(len(first_links), 1)
333-
self.assertIn("startIndex=13", previous_links[0])
334-
self.assertIn("startIndex=0", first_links[0])
332+
333+
if os.getenv("LOCAL_TESTING_TARGET", None) in ["impl"]:
334+
self.assertEqual(len(previous_links), 1)
335+
self.assertEqual(len(next_links), 1)
336+
self.assertEqual(len(first_links), 1)
337+
self.assertIn("startIndex=13", previous_links[0])
338+
self.assertIn("startIndex=0", first_links[0])
339+
else:
340+
self.assertEqual(len(previous_links), 1)
341+
self.assertEqual(len(next_links), 0)
342+
self.assertEqual(len(first_links), 1)
343+
self.assertIn("startIndex=13", previous_links[0])
344+
self.assertIn("startIndex=0", first_links[0])
345+
335346
self.assertContains(response, "ExplanationOfBenefit")
336347

337348
def _test_get_eob_negative(self, version=Versions.NOT_AN_API_VERSION):

dev-local/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ These tools assume you are a developer working on the project, and have access t
1010

1111
## TL;DR
1212

13+
```
14+
make build-local
15+
```
16+
17+
And then
18+
19+
```
20+
make run-local bfd=test auth=live daemon=1
21+
```
22+
23+
or maybe
24+
25+
```
26+
make run-local bfd=sbx auth=live
27+
```
1328

1429

1530
## pre-requisites

0 commit comments

Comments
 (0)