-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
112 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 4.2.16 on 2024-12-18 16:05 | ||
|
||
from django.db import migrations, models | ||
|
||
from awx.main.migrations._create_system_jobs import delete_clear_tokens_sjt | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('main', '0199_alter_oauth2application_unique_together_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(delete_clear_tokens_sjt, migrations.RunPython.noop), | ||
migrations.AlterField( | ||
model_name='systemjob', | ||
name='job_type', | ||
field=models.CharField( | ||
blank=True, | ||
choices=[ | ||
('cleanup_jobs', 'Remove jobs older than a certain number of days'), | ||
('cleanup_activitystream', 'Remove activity stream entries older than a certain number of days'), | ||
('cleanup_sessions', 'Removes expired browser sessions from the database'), | ||
], | ||
default='', | ||
max_length=32, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name='systemjobtemplate', | ||
name='job_type', | ||
field=models.CharField( | ||
blank=True, | ||
choices=[ | ||
('cleanup_jobs', 'Remove jobs older than a certain number of days'), | ||
('cleanup_activitystream', 'Remove activity stream entries older than a certain number of days'), | ||
('cleanup_sessions', 'Removes expired browser sessions from the database'), | ||
], | ||
default='', | ||
max_length=32, | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
{ | ||
"INSIGHTS_USER": "fooo", | ||
"INSIGHTS_PASSWORD": "fooo" | ||
"INSIGHTS_PASSWORD": "fooo", | ||
"INSIGHTS_CLIENT_ID": "fooo", | ||
"INSIGHTS_CLIENT_SECRET": "fooo" | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 58 additions & 0 deletions
58
awx/main/tests/functional/migrations/test_token_sjt_removal.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import pytest | ||
from django.apps import apps | ||
from django.utils.timezone import now | ||
|
||
from awx.main.migrations._create_system_jobs import delete_clear_tokens_sjt | ||
|
||
SJT_NAME = 'Cleanup Expired OAuth 2 Tokens' | ||
|
||
|
||
def create_cleartokens_jt(apps, schema_editor): | ||
# Deleted data migration | ||
SystemJobTemplate = apps.get_model('main', 'SystemJobTemplate') | ||
Schedule = apps.get_model('main', 'Schedule') | ||
ContentType = apps.get_model('contenttypes', 'ContentType') | ||
sjt_ct = ContentType.objects.get_for_model(SystemJobTemplate) | ||
now_dt = now() | ||
schedule_time = now_dt.strftime('%Y%m%dT%H%M%SZ') | ||
|
||
sjt, created = SystemJobTemplate.objects.get_or_create( | ||
job_type='cleanup_tokens', | ||
defaults=dict( | ||
name=SJT_NAME, | ||
description='Cleanup expired OAuth 2 access and refresh tokens', | ||
polymorphic_ctype=sjt_ct, | ||
created=now_dt, | ||
modified=now_dt, | ||
), | ||
) | ||
if created: | ||
sched = Schedule( | ||
name=SJT_NAME, | ||
rrule='DTSTART:%s RRULE:FREQ=WEEKLY;INTERVAL=1' % schedule_time, | ||
description='Removes expired OAuth 2 access and refresh tokens', | ||
enabled=True, | ||
created=now_dt, | ||
modified=now_dt, | ||
extra_data={}, | ||
) | ||
sched.unified_job_template = sjt | ||
sched.save() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_clear_token_sjt(): | ||
SystemJobTemplate = apps.get_model('main', 'SystemJobTemplate') | ||
Schedule = apps.get_model('main', 'Schedule') | ||
create_cleartokens_jt(apps, None) | ||
qs = SystemJobTemplate.objects.filter(name=SJT_NAME) | ||
assert qs.count() == 1 | ||
sjt = qs.first() | ||
assert Schedule.objects.filter(unified_job_template=sjt).count() == 1 | ||
assert Schedule.objects.filter(unified_job_template__systemjobtemplate__name=SJT_NAME).count() == 1 | ||
|
||
# Now run the migration logic to remove | ||
delete_clear_tokens_sjt(apps, None) | ||
assert SystemJobTemplate.objects.filter(name=SJT_NAME).count() == 0 | ||
# Making sure that the schedule is cleaned up is the main point of this test | ||
assert Schedule.objects.filter(unified_job_template__systemjobtemplate__name=SJT_NAME).count() == 0 |