@@ -173,3 +173,54 @@ def test_migrate_DAB_RBAC(self, migrator):
173173 assert Role .objects .filter (
174174 singleton_name = 'system_administrator' , role_field = 'system_administrator'
175175 ).exists (), "expected to find a system_administrator singleton role"
176+
177+
178+ @pytest .mark .django_db
179+ class TestGithubAppBug :
180+ """
181+ Tests that `awx-manage createsuperuser` runs successfully after
182+ the `github_app` CredentialType kind is updated to `github_app_lookup`
183+ via the migration.
184+ """
185+
186+ def test_after_github_app_kind_migration (self , migrator ):
187+ """
188+ Verifies that `createsuperuser` does not raise a KeyError
189+ after the 0202_squashed_deletions migration (which includes
190+ the `update_github_app_kind` logic) is applied.
191+ """
192+ # 1. Apply migrations up to the point *before* the 0204_squashed_deletions migration.
193+ # This simulates the state where the problematic CredentialType might exist.
194+ # We use 0201_create_managed_creds as the direct predecessor.
195+ old_state = migrator .apply_tested_migration (('main' , '0203_remove_team_of_teams' ))
196+
197+ # Get the CredentialType model from the historical state.
198+ CredentialType = old_state .apps .get_model ('main' , 'CredentialType' )
199+
200+ # Create a CredentialType with the old, problematic 'kind' value
201+ CredentialType .objects .create (
202+ name = 'Legacy GitHub App Credential' ,
203+ kind = 'external' , # The old, problematic 'kind' value
204+ namespace = 'github_app' , # The namespace that causes the KeyError in the registry lookup
205+ managed = True ,
206+ created = now (),
207+ modified = now (),
208+ )
209+
210+ # Apply the migration that includes the fix (0204_squashed_deletions).
211+ new_state = migrator .apply_tested_migration (('main' , '0204_squashed_deletions' ))
212+
213+ # Verify that the CredentialType with the old 'kind' no longer exists
214+ # and the 'kind' has been updated to the new value.
215+ CredentialType = new_state .apps .get_model ('main' , 'CredentialType' ) # Get CredentialType model from the new state
216+
217+ # Assertion 1: The CredentialType with the old 'github_app' kind should no longer exist.
218+ assert not CredentialType .objects .filter (
219+ kind = 'github_app'
220+ ).exists (), "CredentialType with old 'github_app' kind should no longer exist after migration."
221+
222+ # Assertion 2: The CredentialType should now exist with the new 'github_app_lookup' kind
223+ # and retain its original name.
224+ assert CredentialType .objects .filter (
225+ namespace = 'github_app_lookup' , name = 'Legacy GitHub App Credential'
226+ ).exists (), "CredentialType should be updated to 'github_app_lookup' and retain its name."
0 commit comments