From f8e8212b2a7ad060c201b4a9a313fb72f90b2ae4 Mon Sep 17 00:00:00 2001 From: Shivam Tiwari Date: Mon, 1 Dec 2025 11:14:22 -0500 Subject: [PATCH 1/5] BB2-4303: Revert changes to fix selenium tests --- .../create_test_user_and_application.py | 177 +++++++++--------- docker-compose.selenium.yml | 2 +- 2 files changed, 85 insertions(+), 94 deletions(-) diff --git a/apps/testclient/management/commands/create_test_user_and_application.py b/apps/testclient/management/commands/create_test_user_and_application.py index 650fdf190..fb351e520 100644 --- a/apps/testclient/management/commands/create_test_user_and_application.py +++ b/apps/testclient/management/commands/create_test_user_and_application.py @@ -12,15 +12,10 @@ from datetime import timedelta, datetime from django.conf import settings from apps.authorization.models import update_grants -from apps.authorization.models import ArchivedDataAccessGrant, DataAccessGrant - -# Imports for quieting things during startup. -from waffle.models import Switch - -from uuid import uuid4 def create_group(name="BlueButton"): + g, created = Group.objects.get_or_create(name=name) if created: print("%s group created" % (name)) @@ -29,29 +24,42 @@ def create_group(name="BlueButton"): return g -def create_user(the_group): - username = "rogersf" - first_name = "Fred" - last_name = "Rogers" - email = "mrrogers@landofmakebelieve.gov" - password = uuid4() +def create_user(group, usr): + u_name = "fred" + first_name = "Fred" + last_name = "Flinstone" + email = "fred@example.com" + password = "foobarfoobarfoobar" user_type = "BEN" - - # We will do this over-and-over. - # If we don't already exist, then create the user. - if User.objects.filter(username=username).exists(): - print(f"👟 {username} already exists. Skipping test user creation.") - return User.objects.get(username=username) - - # If the user didn't exist, it is our first time through. - # Create the user. - user_obj = User.objects.create(username=username, - first_name=first_name, - last_name=last_name, - email=email, - password=password,) - user_obj.set_unusable_password() - UserProfile.objects.create(user=user_obj, + + if usr is not None: + u_name = usr + first_name = "{}{}".format(usr, "First") + last_name = "{}{}".format(usr, "Last") + email = "{}.{}@example.com".format(first_name, last_name) + user_type = "DEV" + + + if User.objects.filter(username=u_name).exists(): + User.objects.filter(username=u_name).delete() + + u = None + + if usr is not None: + u = User.objects.create_user(username=u_name, + first_name=first_name, + last_name=last_name, + email=email) + u.set_unusable_password() + else: + # create a sample user 'fred' for dev local that has a usable password + u = User.objects.create_user(username=u_name, + first_name=first_name, + last_name=last_name, + email=email, + password=password,) + + UserProfile.objects.create(user=u, user_type=user_type, create_applications=True, password_reset_question_1='1', @@ -60,35 +68,33 @@ def create_user(the_group): password_reset_answer_2='Frank', password_reset_question_3='3', password_reset_answer_3='Bentley') - user_obj.groups.add(the_group) - # CROSSWALK - # Removing any existing crosswalks for this artificial user. - # Why? Just in case. - user_id_hash = "ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536" - Crosswalk.objects.filter(_user_id_hash=user_id_hash).delete() - Crosswalk.objects.get_or_create(user=user_obj, - fhir_id_v2=settings.DEFAULT_SAMPLE_FHIR_ID_V2, - _user_id_hash=user_id_hash) - return user_obj + u.groups.add(group) + if usr is None: + c, g_o_c = Crosswalk.objects.get_or_create(user=u, + fhir_id_v2=settings.DEFAULT_SAMPLE_FHIR_ID_V2, + _user_id_hash="ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536") + return u -def create_application(user): - app_name = "TestApp" - if Application.objects.filter(name=app_name).exists(): - return Application.objects.get(name=app_name) - - # If the app doesn't exist, create the test app. +def create_application(user, group, app, redirect): + app_name = "TestApp" if app is None else app Application.objects.filter(name=app_name).delete() redirect_uri = "{}{}".format(settings.HOSTNAME_URL, settings.TESTCLIENT_REDIRECT_URI) - the_app = Application.objects.create(name=app_name, - redirect_uris=redirect_uri, - user=user, - data_access_type="THIRTEEN_MONTH", - client_type="confidential", - authorization_grant_type="authorization-code",) + if redirect: + redirect_uri = redirect + + if not(redirect_uri.startswith("http://") or redirect_uri.startswith("https://")): + redirect_uri = "https://" + redirect_uri + + a = Application.objects.create(name=app_name, + redirect_uris=redirect_uri, + user=user, + data_access_type="THIRTEEN_MONTH", + client_type="confidential", + authorization_grant_type="authorization-code") titles = ["My Medicare and supplemental coverage information.", "My Medicare claim information.", @@ -98,64 +104,49 @@ def create_application(user): for t in titles: c = ProtectedCapability.objects.get(title=t) - the_app.scope.add(c) - - return the_app + a.scope.add(c) + return a -def create_test_token(the_user, the_app): +def create_test_token(user, application): - # Set expiration one day from now. now = timezone.now() expires = now + timedelta(days=1) - scopes = the_app.scope.all() + scopes = application.scope.all() scope = [] for s in scopes: scope.append(s.slug) - # We have to have a tokent with token="sample-token-string", because we - # rely on it existing for unit tests. Which are actually integration tests. - if AccessToken.objects.filter(token="sample-token-string").exists(): - t = AccessToken.objects.get(token="sample-token-string") - t.expires = expires - t.save() - else: - AccessToken.objects.create(user=the_user, - application=the_app, - # This needs to be "sample-token-string", because - # we have tests that rely on it. + t = AccessToken.objects.create(user=user, application=application, token="sample-token-string", expires=expires, - scope=' '.join(scope),) - - -def get_switch(name): - try: - sw = Switch.objects.get(name=name) - return sw.active - except Exception as e: - print(f"Could not get switch {name}: {e}") - - -def set_switch(name, b): - sw, _ = Switch.objects.get_or_create(name=name) - sw.active = b - sw.save() + scope=' '.join(scope)) + return t class Command(BaseCommand): help = 'Create a test user and application for the test client' - def handle(self, *args, **options): - - set_switch('outreach_email', False) + def add_arguments(self, parser): + parser.add_argument("-u", "--user", help="Name of the user to be created (unique).") + parser.add_argument("-a", "--app", help="Name of the application to be created (unique).") + parser.add_argument("-r", "--redirect", help="Redirect url of the application.") - the_group = create_group() - the_user = create_user(the_group) - the_app = create_application(the_user) - create_test_token(the_user, the_app) - update_grants() - - # Restore switch to whatever it was. - set_switch('outreach_email', True) + def handle(self, *args, **options): + usr = options["user"] + app = options["app"] + redirect = options["redirect"] + + g = create_group() + u = create_user(g, usr) + a = create_application(u, g, app, redirect) + t = None + if usr is None and app is None: + t = create_test_token(u, a) + update_grants() + print("Name:", a.name) + print("client_id:", a.client_id) + print("client_secret:", a.client_secret) + print("access_token:", t.token if t else "None") + print("redirect_uri:", a.redirect_uris) \ No newline at end of file diff --git a/docker-compose.selenium.yml b/docker-compose.selenium.yml index f05b58072..55ab55289 100755 --- a/docker-compose.selenium.yml +++ b/docker-compose.selenium.yml @@ -24,7 +24,7 @@ services: - "5900:5900" msls: - build: ./dev-local + build: ./msls-local command: bash -c "python -m debugpy --listen 0.0.0.0:7890 app.py" ports: - "8080:8080" From 47dffd523ef10bcf7ba7c984d2631f952e4c1fc4 Mon Sep 17 00:00:00 2001 From: Matt Jadud Date: Mon, 1 Dec 2025 11:26:26 -0500 Subject: [PATCH 2/5] Fixing naming One more change needed for the MSLS/Selenium fixes. --- msls-local/{Dockerfile.msls => Dockerfile} | 0 msls-local/Makefile | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename msls-local/{Dockerfile.msls => Dockerfile} (100%) diff --git a/msls-local/Dockerfile.msls b/msls-local/Dockerfile similarity index 100% rename from msls-local/Dockerfile.msls rename to msls-local/Dockerfile diff --git a/msls-local/Makefile b/msls-local/Makefile index a1318dea6..737b164c5 100644 --- a/msls-local/Makefile +++ b/msls-local/Makefile @@ -4,4 +4,4 @@ build-local: docker build \ --platform "linux/amd64" \ -t msls-local:latest \ - -f Dockerfile.msls . \ No newline at end of file + -f Dockerfile . \ No newline at end of file From 00f5e747db8eaea480b0ccfded6e4db652d714fe Mon Sep 17 00:00:00 2001 From: Shivam Tiwari Date: Mon, 1 Dec 2025 11:14:22 -0500 Subject: [PATCH 3/5] BB2-4303: Revert changes to fix selenium tests --- .../create_test_user_and_application.py | 177 +++++++++--------- docker-compose.selenium.yml | 2 +- msls-local/{Dockerfile.msls => Dockerfile} | 0 3 files changed, 85 insertions(+), 94 deletions(-) rename msls-local/{Dockerfile.msls => Dockerfile} (100%) diff --git a/apps/testclient/management/commands/create_test_user_and_application.py b/apps/testclient/management/commands/create_test_user_and_application.py index 650fdf190..fb351e520 100644 --- a/apps/testclient/management/commands/create_test_user_and_application.py +++ b/apps/testclient/management/commands/create_test_user_and_application.py @@ -12,15 +12,10 @@ from datetime import timedelta, datetime from django.conf import settings from apps.authorization.models import update_grants -from apps.authorization.models import ArchivedDataAccessGrant, DataAccessGrant - -# Imports for quieting things during startup. -from waffle.models import Switch - -from uuid import uuid4 def create_group(name="BlueButton"): + g, created = Group.objects.get_or_create(name=name) if created: print("%s group created" % (name)) @@ -29,29 +24,42 @@ def create_group(name="BlueButton"): return g -def create_user(the_group): - username = "rogersf" - first_name = "Fred" - last_name = "Rogers" - email = "mrrogers@landofmakebelieve.gov" - password = uuid4() +def create_user(group, usr): + u_name = "fred" + first_name = "Fred" + last_name = "Flinstone" + email = "fred@example.com" + password = "foobarfoobarfoobar" user_type = "BEN" - - # We will do this over-and-over. - # If we don't already exist, then create the user. - if User.objects.filter(username=username).exists(): - print(f"👟 {username} already exists. Skipping test user creation.") - return User.objects.get(username=username) - - # If the user didn't exist, it is our first time through. - # Create the user. - user_obj = User.objects.create(username=username, - first_name=first_name, - last_name=last_name, - email=email, - password=password,) - user_obj.set_unusable_password() - UserProfile.objects.create(user=user_obj, + + if usr is not None: + u_name = usr + first_name = "{}{}".format(usr, "First") + last_name = "{}{}".format(usr, "Last") + email = "{}.{}@example.com".format(first_name, last_name) + user_type = "DEV" + + + if User.objects.filter(username=u_name).exists(): + User.objects.filter(username=u_name).delete() + + u = None + + if usr is not None: + u = User.objects.create_user(username=u_name, + first_name=first_name, + last_name=last_name, + email=email) + u.set_unusable_password() + else: + # create a sample user 'fred' for dev local that has a usable password + u = User.objects.create_user(username=u_name, + first_name=first_name, + last_name=last_name, + email=email, + password=password,) + + UserProfile.objects.create(user=u, user_type=user_type, create_applications=True, password_reset_question_1='1', @@ -60,35 +68,33 @@ def create_user(the_group): password_reset_answer_2='Frank', password_reset_question_3='3', password_reset_answer_3='Bentley') - user_obj.groups.add(the_group) - # CROSSWALK - # Removing any existing crosswalks for this artificial user. - # Why? Just in case. - user_id_hash = "ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536" - Crosswalk.objects.filter(_user_id_hash=user_id_hash).delete() - Crosswalk.objects.get_or_create(user=user_obj, - fhir_id_v2=settings.DEFAULT_SAMPLE_FHIR_ID_V2, - _user_id_hash=user_id_hash) - return user_obj + u.groups.add(group) + if usr is None: + c, g_o_c = Crosswalk.objects.get_or_create(user=u, + fhir_id_v2=settings.DEFAULT_SAMPLE_FHIR_ID_V2, + _user_id_hash="ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536") + return u -def create_application(user): - app_name = "TestApp" - if Application.objects.filter(name=app_name).exists(): - return Application.objects.get(name=app_name) - - # If the app doesn't exist, create the test app. +def create_application(user, group, app, redirect): + app_name = "TestApp" if app is None else app Application.objects.filter(name=app_name).delete() redirect_uri = "{}{}".format(settings.HOSTNAME_URL, settings.TESTCLIENT_REDIRECT_URI) - the_app = Application.objects.create(name=app_name, - redirect_uris=redirect_uri, - user=user, - data_access_type="THIRTEEN_MONTH", - client_type="confidential", - authorization_grant_type="authorization-code",) + if redirect: + redirect_uri = redirect + + if not(redirect_uri.startswith("http://") or redirect_uri.startswith("https://")): + redirect_uri = "https://" + redirect_uri + + a = Application.objects.create(name=app_name, + redirect_uris=redirect_uri, + user=user, + data_access_type="THIRTEEN_MONTH", + client_type="confidential", + authorization_grant_type="authorization-code") titles = ["My Medicare and supplemental coverage information.", "My Medicare claim information.", @@ -98,64 +104,49 @@ def create_application(user): for t in titles: c = ProtectedCapability.objects.get(title=t) - the_app.scope.add(c) - - return the_app + a.scope.add(c) + return a -def create_test_token(the_user, the_app): +def create_test_token(user, application): - # Set expiration one day from now. now = timezone.now() expires = now + timedelta(days=1) - scopes = the_app.scope.all() + scopes = application.scope.all() scope = [] for s in scopes: scope.append(s.slug) - # We have to have a tokent with token="sample-token-string", because we - # rely on it existing for unit tests. Which are actually integration tests. - if AccessToken.objects.filter(token="sample-token-string").exists(): - t = AccessToken.objects.get(token="sample-token-string") - t.expires = expires - t.save() - else: - AccessToken.objects.create(user=the_user, - application=the_app, - # This needs to be "sample-token-string", because - # we have tests that rely on it. + t = AccessToken.objects.create(user=user, application=application, token="sample-token-string", expires=expires, - scope=' '.join(scope),) - - -def get_switch(name): - try: - sw = Switch.objects.get(name=name) - return sw.active - except Exception as e: - print(f"Could not get switch {name}: {e}") - - -def set_switch(name, b): - sw, _ = Switch.objects.get_or_create(name=name) - sw.active = b - sw.save() + scope=' '.join(scope)) + return t class Command(BaseCommand): help = 'Create a test user and application for the test client' - def handle(self, *args, **options): - - set_switch('outreach_email', False) + def add_arguments(self, parser): + parser.add_argument("-u", "--user", help="Name of the user to be created (unique).") + parser.add_argument("-a", "--app", help="Name of the application to be created (unique).") + parser.add_argument("-r", "--redirect", help="Redirect url of the application.") - the_group = create_group() - the_user = create_user(the_group) - the_app = create_application(the_user) - create_test_token(the_user, the_app) - update_grants() - - # Restore switch to whatever it was. - set_switch('outreach_email', True) + def handle(self, *args, **options): + usr = options["user"] + app = options["app"] + redirect = options["redirect"] + + g = create_group() + u = create_user(g, usr) + a = create_application(u, g, app, redirect) + t = None + if usr is None and app is None: + t = create_test_token(u, a) + update_grants() + print("Name:", a.name) + print("client_id:", a.client_id) + print("client_secret:", a.client_secret) + print("access_token:", t.token if t else "None") + print("redirect_uri:", a.redirect_uris) \ No newline at end of file diff --git a/docker-compose.selenium.yml b/docker-compose.selenium.yml index f05b58072..55ab55289 100755 --- a/docker-compose.selenium.yml +++ b/docker-compose.selenium.yml @@ -24,7 +24,7 @@ services: - "5900:5900" msls: - build: ./dev-local + build: ./msls-local command: bash -c "python -m debugpy --listen 0.0.0.0:7890 app.py" ports: - "8080:8080" diff --git a/msls-local/Dockerfile.msls b/msls-local/Dockerfile similarity index 100% rename from msls-local/Dockerfile.msls rename to msls-local/Dockerfile From cd000e775f661b0fc954bdfb846917a8532ed3a1 Mon Sep 17 00:00:00 2001 From: Matt Jadud Date: Mon, 1 Dec 2025 12:17:27 -0500 Subject: [PATCH 4/5] Updates to make new local stack run --- .../create_test_user_and_application.py | 106 ++++++++++++------ dev-local/utility-functions.bash | 17 +-- 2 files changed, 71 insertions(+), 52 deletions(-) diff --git a/apps/testclient/management/commands/create_test_user_and_application.py b/apps/testclient/management/commands/create_test_user_and_application.py index fb351e520..ad8b7faf7 100644 --- a/apps/testclient/management/commands/create_test_user_and_application.py +++ b/apps/testclient/management/commands/create_test_user_and_application.py @@ -26,20 +26,19 @@ def create_group(name="BlueButton"): def create_user(group, usr): u_name = "fred" - first_name = "Fred" + first_name = "Fred" last_name = "Flinstone" email = "fred@example.com" password = "foobarfoobarfoobar" user_type = "BEN" - + if usr is not None: u_name = usr - first_name = "{}{}".format(usr, "First") + first_name = "{}{}".format(usr, "First") last_name = "{}{}".format(usr, "Last") email = "{}.{}@example.com".format(first_name, last_name) user_type = "DEV" - if User.objects.filter(username=u_name).exists(): User.objects.filter(username=u_name).delete() @@ -47,17 +46,17 @@ def create_user(group, usr): if usr is not None: u = User.objects.create_user(username=u_name, - first_name=first_name, - last_name=last_name, - email=email) + first_name=first_name, + last_name=last_name, + email=email) u.set_unusable_password() else: # create a sample user 'fred' for dev local that has a usable password u = User.objects.create_user(username=u_name, - first_name=first_name, - last_name=last_name, - email=email, - password=password,) + first_name=first_name, + last_name=last_name, + email=email, + password=password,) UserProfile.objects.create(user=u, user_type=user_type, @@ -72,29 +71,60 @@ def create_user(group, usr): u.groups.add(group) if usr is None: - c, g_o_c = Crosswalk.objects.get_or_create(user=u, - fhir_id_v2=settings.DEFAULT_SAMPLE_FHIR_ID_V2, - _user_id_hash="ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536") + if not Crosswalk.objects.filter(_user_id_hash="ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536").exists(): + c, g_o_c = Crosswalk.objects.get_or_create(user=u, + fhir_id_v2=settings.DEFAULT_SAMPLE_FHIR_ID_V2, + _user_id_hash="ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536") + else: + print("Skipping crosswalk creation; already exists.") return u -def create_application(user, group, app, redirect): - app_name = "TestApp" if app is None else app - Application.objects.filter(name=app_name).delete() - redirect_uri = "{}{}".format(settings.HOSTNAME_URL, settings.TESTCLIENT_REDIRECT_URI) +# def create_application(user, group, app, redirect): +# app_name = "TestApp" if app is None else app +# Application.objects.filter(name=app_name).delete() +# redirect_uri = "{}{}".format(settings.HOSTNAME_URL, settings.TESTCLIENT_REDIRECT_URI) + +# if redirect: +# redirect_uri = redirect + +# if not (redirect_uri.startswith("http://") or redirect_uri.startswith("https://")): +# redirect_uri = "https://" + redirect_uri + +# a = Application.objects.create(name=app_name, +# redirect_uris=redirect_uri, +# user=user, +# data_access_type="THIRTEEN_MONTH", +# client_type="confidential", +# authorization_grant_type="authorization-code") - if redirect: - redirect_uri = redirect +# titles = ["My Medicare and supplemental coverage information.", +# "My Medicare claim information.", +# "My general patient and demographic information.", +# "Profile information including name and email." +# ] - if not(redirect_uri.startswith("http://") or redirect_uri.startswith("https://")): - redirect_uri = "https://" + redirect_uri +# for t in titles: +# c = ProtectedCapability.objects.get(title=t) +# a.scope.add(c) +# return a - a = Application.objects.create(name=app_name, - redirect_uris=redirect_uri, - user=user, - data_access_type="THIRTEEN_MONTH", - client_type="confidential", - authorization_grant_type="authorization-code") +def create_application(user): + app_name = "TestApp" + if Application.objects.filter(name=app_name).exists(): + return Application.objects.get(name=app_name) + + # If the app doesn't exist, create the test app. + + Application.objects.filter(name=app_name).delete() + redirect_uri = "{}{}".format(settings.HOSTNAME_URL, settings.TESTCLIENT_REDIRECT_URI) + + the_app = Application.objects.create(name=app_name, + redirect_uris=redirect_uri, + user=user, + data_access_type="THIRTEEN_MONTH", + client_type="confidential", + authorization_grant_type="authorization-code",) titles = ["My Medicare and supplemental coverage information.", "My Medicare claim information.", @@ -104,8 +134,9 @@ def create_application(user, group, app, redirect): for t in titles: c = ProtectedCapability.objects.get(title=t) - a.scope.add(c) - return a + the_app.scope.add(c) + + return the_app def create_test_token(user, application): @@ -118,10 +149,13 @@ def create_test_token(user, application): for s in scopes: scope.append(s.slug) - t = AccessToken.objects.create(user=user, application=application, - token="sample-token-string", - expires=expires, - scope=' '.join(scope)) + if AccessToken.objects.filter(token="sample-token-string").exists(): + t = AccessToken.objects.get(token="sample-token-string") + else: + t = AccessToken.objects.create(user=user, application=application, + token="sample-token-string", + expires=expires, + scope=' '.join(scope)) return t @@ -140,7 +174,7 @@ def handle(self, *args, **options): g = create_group() u = create_user(g, usr) - a = create_application(u, g, app, redirect) + a = create_application(u) # a = create_application(u, g, app, redirect) t = None if usr is None and app is None: t = create_test_token(u, a) @@ -149,4 +183,4 @@ def handle(self, *args, **options): print("client_id:", a.client_id) print("client_secret:", a.client_secret) print("access_token:", t.token if t else "None") - print("redirect_uri:", a.redirect_uris) \ No newline at end of file + print("redirect_uri:", a.redirect_uris) diff --git a/dev-local/utility-functions.bash b/dev-local/utility-functions.bash index d0193a324..99f955092 100755 --- a/dev-local/utility-functions.bash +++ b/dev-local/utility-functions.bash @@ -24,22 +24,7 @@ check_valid_env () { echo "Exiting." return -2 fi - - - if [[ "${bfd}" == "local" && "${auth}" == "live" ]]; then - echo "⚠️ ${bfd}/${auth} may work for SLSX testing, but not for BFD calls." - fi - - if [[ "${bfd}" == "test" && "${auth}" == "mock" ]]; then - echo "⛔ ${bfd}/${auth} is not a valid combination. Exiting." - return -3 - fi - - if [[ "${bfd}" == "sbx" && "${auth}" == "mock" ]]; then - echo "⛔ ${bfd}/${auth} is not a valid combination. Exiting." - return -4 - fi - + echo "✅ check_valid_env" } From d3b700735de407d6c9f691ddad1465a58b24c922 Mon Sep 17 00:00:00 2001 From: Shivam Tiwari Date: Tue, 2 Dec 2025 11:13:35 -0500 Subject: [PATCH 5/5] Fixed test --- .../commands/create_test_user_and_application.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/apps/testclient/management/commands/create_test_user_and_application.py b/apps/testclient/management/commands/create_test_user_and_application.py index 321920f1b..64fcaf297 100644 --- a/apps/testclient/management/commands/create_test_user_and_application.py +++ b/apps/testclient/management/commands/create_test_user_and_application.py @@ -114,19 +114,18 @@ def create_test_token(user, application): now = timezone.now() expires = now + timedelta(days=1) + token_value = "sample-token-string" scopes = application.scope.all() scope = [] for s in scopes: scope.append(s.slug) - if AccessToken.objects.filter(token="sample-token-string").exists(): - t = AccessToken.objects.get(token="sample-token-string") - - t = AccessToken.objects.create(user=user, application=application, - token="sample-token-string", - expires=expires, - scope=' '.join(scope)) + AccessToken.objects.filter(token=token_value).delete() + t = AccessToken.objects.create(user=user, application=application, + token=token_value, + expires=expires, + scope=' '.join(scope)) return t