Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions apps/fhir/bluebutton/fixtures/fhir_bluebutton_new_testdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
"fields": {
"user": 1,
"fhir_source": 1,
"fhir_id": "Patient/9342511",
"_fhir_id": "Patient/9342511",
"date_created": "2016-07-01T19:37:00.452Z",
"user_id_hash": ""
"_user_id_hash": "123456"
}
},
{
Expand All @@ -58,9 +58,9 @@
"fields": {
"user": 2,
"fhir_source": 1,
"fhir_id": "Patient/9342511",
"_fhir_id": "Patient/9342511",
"date_created": "2016-08-25T15:55:10.338Z",
"user_id_hash": ""
"_user_id_hash": "123457"
}
},
{
Expand All @@ -69,9 +69,9 @@
"fields": {
"user": 3,
"fhir_source": 1,
"fhir_id": "Patient/9342703",
"_fhir_id": "Patient/9342703",
"date_created": "2016-08-29T03:15:48.436Z",
"user_id_hash": ""
"_user_id_hash": "123458"
}
}
]
3 changes: 2 additions & 1 deletion apps/fhir/bluebutton/fixtures/fhir_bluebutton_test_rt.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"pk": 1,
"fields": {"user": 1,
"fhir_source": 1,
"fhir_id": "4995802",
"_fhir_id": "4995802",
"_user_id_hash": "139e178537ed3bc486e6a7195a47a82a2cd6f46e911660fe9775f6e0dd3f1130",
"date_created": "2016-05-25T05:03:49.342Z"}
},
{
Expand Down
33 changes: 33 additions & 0 deletions apps/fhir/bluebutton/migrations/0003_auto_20191208_0010.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 2.1.11 on 2019-12-08 00:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bluebutton', '0002_auto_20180127_2032'),
]

operations = [
migrations.AlterField(
model_name='crosswalk',
name='fhir_id',
field=models.CharField(db_column='fhir_id', db_index=True, default=None, max_length=80, null=True),
),
migrations.AlterField(
model_name='crosswalk',
name='user_id_hash',
field=models.CharField(db_column='user_id_hash', db_index=True, default=None, max_length=64, unique=True, verbose_name='PBKDF2 of User ID'),
),
migrations.RenameField(
model_name='crosswalk',
old_name='fhir_id',
new_name='_fhir_id',
),
migrations.RenameField(
model_name='crosswalk',
old_name='user_id_hash',
new_name='_user_id_hash',
),
]
48 changes: 38 additions & 10 deletions apps/fhir/bluebutton/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from requests import Response
from django.conf import settings
from django.db import models
from django.core.exceptions import ValidationError
from apps.accounts.models import get_user_id_salt
from apps.fhir.server.models import ResourceRouter
from django.utils.crypto import pbkdf2
Expand Down Expand Up @@ -53,18 +54,23 @@ class Crosswalk(models.Model):
blank=True,
null=True)
# default=settings.FHIR_SERVER_DEFAULT)
fhir_id = models.CharField(max_length=80,
blank=True, default="", db_index=True)
_fhir_id = models.CharField(max_length=80,
null=True,
default=None,
db_column="fhir_id",
db_index=True)
date_created = models.DateTimeField(auto_now_add=True)

user_id_type = models.CharField(max_length=1,
default=settings.USER_ID_TYPE_DEFAULT,
choices=settings.USER_ID_TYPE_CHOICES)
user_id_hash = models.CharField(max_length=64,
blank=True,
default="",
verbose_name="PBKDF2 of User ID",
db_index=True)
_user_id_hash = models.CharField(max_length=64,
verbose_name="PBKDF2 of User ID",
unique=True,
null=False,
default=None,
db_column="user_id_hash",
db_index=True)

objects = models.Manager() # Default manager
real_objects = RealCrosswalkManager() # Real bene manager
Expand All @@ -73,10 +79,32 @@ class Crosswalk(models.Model):
def __str__(self):
return '%s %s' % (self.user.first_name, self.user.last_name)

@property
def fhir_id(self):
return self._fhir_id

@fhir_id.setter
def fhir_id(self, value):
if self._fhir_id:
raise ValidationError("this value cannot be modified.")
self._fhir_id = value

@property
def user_id_hash(self):
return self._user_id_hash

@user_id_hash.setter
def user_id_hash(self, value):
if self.pk:
raise ValidationError("this value cannot be modified.")
if self._user_id_hash:
raise ValidationError("this value cannot be modified.")
self._user_id_hash = value

def set_hicn(self, hicn):
self.user_id_hash = binascii.hexlify(pbkdf2(hicn,
get_user_id_salt(),
settings.USER_ID_ITERATIONS)).decode("ascii")
if self.pk:
raise ValidationError("this value cannot be modified.")
self.user_id_hash = hash_hicn(hicn)

def get_fhir_patient_url(self):
# Return the fhir server url and {Resource_name}/{id}
Expand Down
2 changes: 1 addition & 1 deletion apps/fhir/bluebutton/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def has_permission(self, request, view):
logger.info('Crosswalk for %s does not exist' % request.user)
return False

if crosswalk.fhir_id == "":
if crosswalk.fhir_id is None:
authenticate_crosswalk(crosswalk)

request.crosswalk = crosswalk
Expand Down
57 changes: 55 additions & 2 deletions apps/fhir/bluebutton/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.db.utils import IntegrityError
from django.core.exceptions import ValidationError
from apps.test import BaseApiTest

from ..models import Crosswalk
Expand All @@ -19,7 +21,8 @@ def test_get_full_url_good(self):

cw = Crosswalk.objects.create(user=user,
fhir_source=fs,
fhir_id="123456")
fhir_id="-20000000000001",
user_id_hash="139e178537ed3bc486e6a7195a47a82a2cd6f46e911660fe9775f6e0dd3f1130")

fhir = Crosswalk.objects.get(user=user.pk)

Expand All @@ -43,11 +46,61 @@ def test_get_full_url_bad(self):

Crosswalk.objects.create(user=user,
fhir_source=fs,
fhir_id="123456")
fhir_id="-20000000000001",
user_id_hash="139e178537ed3bc486e6a7195a47a82a2cd6f46e911660fe9775f6e0dd3f1130")

fhir = Crosswalk.objects.get(user=user.pk)

url_info = fhir.get_fhir_patient_url()

invalid_match = "http://localhost:8000/fhir/" + "Practitioner/123456"
self.assertNotEqual(url_info, invalid_match)

def test_require_user_id_hash(self):
user = self._create_user('john', 'password',
first_name='John',
last_name='Smith',
email='john@smith.net')
with self.assertRaises(IntegrityError):
Crosswalk.objects.create(user=user)

def test_immutable_fhir_id(self):
user = self._create_user('john', 'password',
first_name='John',
last_name='Smith',
email='john@smith.net')

# created a default user
fs = ResourceRouter.objects.create(name="Main Server",
fhir_url="http://localhost:8000/fhir/",
shard_by="Patient",
server_search_expiry=1800)

Crosswalk.objects.create(user=user,
fhir_source=fs,
fhir_id="-20000000000001",
user_id_hash="139e178537ed3bc486e6a7195a47a82a2cd6f46e911660fe9775f6e0dd3f1130")
cw = Crosswalk.objects.get(_fhir_id="-20000000000001")
with self.assertRaises(ValidationError):
cw.fhir_id = "-20000000000002"

def test_immuatble_user_id_hash(self):
user = self._create_user('john', 'password',
first_name='John',
last_name='Smith',
email='john@smith.net')

# created a default user
fs = ResourceRouter.objects.create(name="Main Server",
fhir_url="http://localhost:8000/fhir/",
shard_by="Patient",
server_search_expiry=1800)

cw = Crosswalk.objects.create(user=user,
fhir_source=fs,
fhir_id="-20000000000001",
user_id_hash="139e178537ed3bc486e6a7195a47a82a2cd6f46e911660fe9775f6e0dd3f1130")
cw = Crosswalk.objects.get(_fhir_id="-20000000000001")
self.assertEqual(cw.user_id_hash, "139e178537ed3bc486e6a7195a47a82a2cd6f46e911660fe9775f6e0dd3f1130")
with self.assertRaises(ValidationError):
cw.user_id_hash = "239e178537ed3bc486e6a7195a47a82a2cd6f46e911660fe9775f6e0dd3f1130"
3 changes: 3 additions & 0 deletions apps/fhir/bluebutton/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import uuid
from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase, RequestFactory
Expand Down Expand Up @@ -405,6 +406,7 @@ def setUp(self):
xwalk = Crosswalk()
xwalk.user = self.user
xwalk.fhir_id = "Patient/12345"
xwalk.set_hicn(uuid.uuid4())
xwalk.save()

def test_crosswalk_fhir_id(self):
Expand All @@ -422,6 +424,7 @@ def test_crosswalk_fhir_id(self):
x = Crosswalk()
x.user = u
x.fhir_id = "Patient/23456"
x.set_hicn(uuid.uuid4())
x.save()

result = crosswalk_patient_id(u)
Expand Down
2 changes: 1 addition & 1 deletion apps/fhir/bluebutton/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def generate_info_headers(request):
crosswalk = get_crosswalk(user)
if crosswalk:
# we need to send the HicnHash or the fhir_id
if len(crosswalk.fhir_id) > 0:
if crosswalk.fhir_id is not None:
result['BlueButton-BeneficiaryId'] = 'patientId:' + str(crosswalk.fhir_id)
else:
result['BlueButton-BeneficiaryId'] = 'hicnHash:' + str(crosswalk.user_id_hash)
Expand Down
5 changes: 3 additions & 2 deletions apps/mymedicare_cb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ def create_beneficiary_record(username=None,
assert username is not None
assert username != ""
assert user_id_hash is not None
assert len(user_id_hash) == 64, "incorrect user id hash format"

if User.objects.filter(username=username).exists():
raise ValidationError("user already exists", username)

if Crosswalk.objects.filter(user_id_hash=user_id_hash).exists():
if Crosswalk.objects.filter(_user_id_hash=user_id_hash).exists():
raise ValidationError("user_id_hash already exists", user_id_hash)

if fhir_id and Crosswalk.objects.filter(fhir_id=fhir_id).exists():
if fhir_id and Crosswalk.objects.filter(_fhir_id=fhir_id).exists():
raise ValidationError("fhir_id already exists", fhir_id)

with transaction.atomic():
Expand Down
2 changes: 1 addition & 1 deletion apps/mymedicare_cb/tests/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def sls_user_info_mock(url, request):
return {
'status_code': 200,
'content': {
'sub': '0123456789abcdefghijklmnopqrstuvwxyz',
'sub': '00112233-4455-6677-8899-aabbccddeeff',
'given_name': '',
'family_name': '',
'email': 'bob@bobserver.bob',
Expand Down
5 changes: 3 additions & 2 deletions apps/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def create_token(self, first_name, last_name):
last_name=last_name,
email="%s@%s.net" % (first_name, last_name))
Crosswalk.objects.get_or_create(user=user,
fhir_id=settings.DEFAULT_SAMPLE_FHIR_ID,
_fhir_id=settings.DEFAULT_SAMPLE_FHIR_ID,
_user_id_hash=user.username,
fhir_source=get_resourcerouter())

# create a oauth2 application and add capabilities
Expand All @@ -116,7 +117,7 @@ def create_token_no_fhir(self, first_name, last_name):
last_name=last_name,
email="%s@%s.net" % (first_name, last_name))
Crosswalk.objects.get_or_create(user=user,
user_id_hash="139e178537ed3bc486e6a7195a47a82a2cd6f46e911660fe9775f6e0dd3f1130",
_user_id_hash="139e178537ed3bc486e6a7195a47a82a2cd6f46e911660fe9775f6e0dd3f1130",
fhir_source=get_resourcerouter())

# create a oauth2 application and add capabilities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def create_user(group):

u.groups.add(group)
c, g_o_c = Crosswalk.objects.get_or_create(user=u,
fhir_id=settings.DEFAULT_SAMPLE_FHIR_ID,
_fhir_id=settings.DEFAULT_SAMPLE_FHIR_ID,
_user_id_hash="139e178537ed3bc486e6a7195a47a82a2cd6f46e911660fe9775f6e0dd3f1130",
fhir_source=get_resourcerouter())
return u

Expand Down