-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
source for email/name: migrate from AMS to RHSSO
- Loading branch information
Showing
7 changed files
with
120 additions
and
31 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
40 changes: 40 additions & 0 deletions
40
ansible_ai_connect/users/management/commands/migrate_email_name_from_ams.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,40 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright Red Hat | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
|
||
User = get_user_model() | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Initialize the email and names field from the AMS" | ||
|
||
def handle(self, *args, **options): | ||
cpt = 0 | ||
for user in User.objects.all(): | ||
if user.first_name: | ||
continue | ||
ams_data = user.ams() | ||
if not ams_data: | ||
continue | ||
user.given_name = ams_data.get("first_name") | ||
user.family_name = ams_data.get("last_name") | ||
user.email = ams_data.get("email") | ||
user.save() | ||
cpt += 1 | ||
|
||
self.stdout.write(f"{cpt} users migrated.") |
49 changes: 49 additions & 0 deletions
49
ansible_ai_connect/users/management/commands/tests/test_migrate_email_name_from_ams.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,49 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright Red Hat | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from io import StringIO | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.management import call_command | ||
from django.test import override_settings | ||
|
||
from ansible_ai_connect.test_utils import WisdomServiceAPITestCaseBaseOIDC | ||
|
||
User = get_user_model() | ||
|
||
|
||
@override_settings(AUTHZ_BACKEND_TYPE="dummy") | ||
class TestMigrate(WisdomServiceAPITestCaseBaseOIDC): | ||
def call_command(self, *args, **kwargs): | ||
out = StringIO() | ||
call_command( | ||
"migrate_email_name_from_ams", | ||
*args, | ||
stdout=out, | ||
stderr=StringIO(), | ||
**kwargs, | ||
) | ||
return out.getvalue() | ||
|
||
def test_migrate(self): | ||
original = (self.user.email, self.user.given_name, self.user.family_name) | ||
|
||
out = self.call_command() | ||
self.assertIn("1 users migrated", out) | ||
self.user = User.objects.get(id=self.user.id) | ||
self.assertNotEqual( | ||
original, (self.user.email, self.user.given_name, self.user.family_name) | ||
) | ||
self.assertEqual(self.user.given_name, "Robert") |
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
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