Repository: oppia/oppia

Issue:
Remove retrieval `UserAuthDetailsModel` from the `get_user_settings_by_gae_id` 
Remove retrieval `UserAuthDetailsModel` from the `get_user_settings_by_gae_id` after we correctly run `GenerateUserIdentifiersModelOneOffJob` on the production.


Discussion:
Reference: https://github.com/oppia/oppia/pull/11134/commits/9036562cfa58ec767ff74be6c52a8cf1f406bb70#diff-fdb77f0403ddcf664d6de7b98300a1ce1f1cdcd1cae9a87c5b53d1e4347b3703R783

Resolving patch:
diff --git a/core/domain/user_services.py b/core/domain/user_services.py
--- a/core/domain/user_services.py
+++ b/core/domain/user_services.py
@@ -770,12 +770,20 @@ def get_user_settings_by_gae_id(gae_id, strict=False):
     Raises:
         Exception. The value of strict is True and given gae_id does not exist.
     """
+    user_id = None
     user_identifiers_model = (
-        user_models.UserIdentifiersModel.get_by_gae_id(gae_id))
-    user_id = (
-        user_identifiers_model.user_id
-        if user_identifiers_model is not None else None
-    )
+        user_models.UserIdentifiersModel.get(gae_id, strict=False))
+    # If the UserIdentifiersModels are not yet generated use
+    # UserAuthDetailsModel as the backup for retrieving the user_id.
+    # TODO(#11140): Remove this after we run the migration job.
+    if user_identifiers_model is None:
+        user_auth_details_model = (
+            user_models.UserAuthDetailsModel.get_by_auth_id(
+                feconf.AUTH_METHOD_GAE, gae_id))
+        if user_auth_details_model is not None:
+            user_id = user_auth_details_model.id
+    else:
+        user_id = user_identifiers_model.user_id
 
     if user_id is not None:
         user_settings = _get_user_settings_from_model(
diff --git a/core/storage/user/gae_models.py b/core/storage/user/gae_models.py
--- a/core/storage/user/gae_models.py
+++ b/core/storage/user/gae_models.py
@@ -2884,22 +2884,9 @@ def has_reference_to_user_id(cls, user_id):
         """
         return cls.query(cls.user_id == user_id).get(keys_only=True) is not None
 
-    @classmethod
-    def get_by_gae_id(cls, gae_id):
-        """Get an entry by GAE ID.
-
-        Args:
-            gae_id: str. The GAE ID.
-
-        Returns:
-            UserIdentifiersModel. The model with ID field equal to gae_id
-            argument.
-        """
-        return cls.get_by_id(gae_id)
-
     @classmethod
     def get_by_user_id(cls, user_id):
-        """Fetch an entry by user ID.
+        """Fetch a entry by user ID.
 
         Args:
             user_id: str. The user ID.


Test patch:
diff --git a/core/domain/user_services_test.py b/core/domain/user_services_test.py
--- a/core/domain/user_services_test.py
+++ b/core/domain/user_services_test.py
@@ -355,7 +355,7 @@ def test_get_user_id_from_username(self):
         self.assertIsNone(
             user_services.get_user_id_from_username('fakeUsername'))
 
-    def test_get_user_settings_by_gae_id_returns_user_settings(self):
+    def test_get_user_settings_by_gae_id_for_existing_user_is_correct(self):
         gae_id = 'gae_id'
         email = 'user@example.com'
         user_id = 'user_id'
@@ -369,6 +369,10 @@ def test_get_user_settings_by_gae_id_returns_user_settings(self):
             id=gae_id,
             user_id=user_id
         ).put()
+        user_models.UserAuthDetailsModel(
+            id=user_id,
+            gae_id=gae_id
+        ).put()
         user_settings_model = user_models.UserSettingsModel.get_by_id(user_id)
         user_settings = user_services.get_user_settings_by_gae_id(gae_id)
         self.assertEqual(user_settings_model.id, user_settings.user_id)
@@ -378,7 +382,29 @@ def test_get_user_settings_by_gae_id_returns_user_settings(self):
     def test_get_user_settings_by_gae_id_for_nonexistent_gae_id_is_none(self):
         self.assertIsNone(user_services.get_user_settings_by_gae_id('gae_id_x'))
 
-    def test_get_user_settings_by_gae_id_strict_returns_user_settings(self):
+    def test_get_user_settings_by_gae_id_with_missing_user_identifiers_works(
+            self):
+        gae_id = 'gae_id'
+        email = 'user@example.com'
+        user_id = 'user_id'
+        username = 'username'
+        user_models.UserSettingsModel(
+            id=user_id,
+            email=email,
+            username=username,
+        ).put()
+        user_models.UserAuthDetailsModel(
+            id=user_id,
+            gae_id=gae_id
+        ).put()
+        user_settings_model = user_models.UserSettingsModel.get_by_id(user_id)
+        user_settings = user_services.get_user_settings_by_gae_id(gae_id)
+        self.assertEqual(user_settings_model.id, user_settings.user_id)
+        self.assertEqual(user_settings_model.email, user_settings.email)
+        self.assertEqual(user_settings_model.username, user_settings.username)
+
+    def test_get_user_settings_by_gae_id_with_missing_user_auth_details_works(
+            self):
         gae_id = 'gae_id'
         email = 'user@example.com'
         user_id = 'user_id'
@@ -392,18 +418,39 @@ def test_get_user_settings_by_gae_id_strict_returns_user_settings(self):
             id=gae_id,
             user_id=user_id
         ).put()
-        user_settings_model = user_models.UserSettingsModel.get_by_id(
-            user_id)
-        user_settings = (
-            user_services.get_user_settings_by_gae_id(gae_id, strict=True))
+        user_settings_model = user_models.UserSettingsModel.get_by_id(user_id)
+        user_settings = user_services.get_user_settings_by_gae_id(gae_id)
+        self.assertEqual(user_settings_model.id, user_settings.user_id)
+        self.assertEqual(user_settings_model.email, user_settings.email)
+        self.assertEqual(user_settings_model.username, user_settings.username)
+
+    def test_get_auth_details_by_gae_id_for_nonexistent_gae_id_is_none(self):
+        self.assertIsNone(user_services.get_user_settings_by_gae_id('gae_id_x'))
+
+    def test_get_user_settings_by_gae_id_strict_existing_user_is_correct(self):
+        non_existent_user_id = 'id_x'
+        gae_id = 'gae_id'
+        email = 'user@example.com'
+        user_id = 'user_id'
+        username = 'username'
+        user_models.UserSettingsModel(
+            id=user_id,
+            email=email,
+            username=username,
+        ).put()
+        user_models.UserAuthDetailsModel(
+            id=user_id,
+            gae_id=gae_id
+        ).put()
+        user_settings_model = user_models.UserSettingsModel.get_by_id(user_id)
+        user_settings = user_services.get_user_settings_by_gae_id(gae_id)
         self.assertEqual(user_settings_model.id, user_settings.user_id)
         self.assertEqual(user_settings_model.email, user_settings.email)
         self.assertEqual(user_settings_model.username, user_settings.username)
 
-    def test_get_user_settings_by_gae_id_strict_for_nonexistent_gae_id_is_none(
-            self):
         with self.assertRaisesRegexp(Exception, 'User not found.'):
-            user_services.get_user_settings_by_gae_id('gae_id_x', strict=True)
+            user_services.get_user_settings_by_gae_id(
+                non_existent_user_id, strict=True)
 
     def test_fetch_gravatar_success(self):
         user_email = 'user@example.com'
diff --git a/core/storage/user/gae_models_test.py b/core/storage/user/gae_models_test.py
--- a/core/storage/user/gae_models_test.py
+++ b/core/storage/user/gae_models_test.py
@@ -2606,12 +2606,6 @@ def test_has_reference_to_non_existing_user_id_is_false(self):
                 self.NONEXISTENT_USER_ID)
         )
 
-    def test_get_by_gae_id_for_correct_gae_id(self):
-        self.assertEqual(
-            user_models.UserIdentifiersModel.get_by_id(self.USER_GAE_ID),
-            user_models.UserIdentifiersModel.get_by_gae_id(self.USER_GAE_ID)
-        )
-
     def test_get_by_user_id_for_correct_user_id(self):
         self.assertEqual(
             user_models.UserIdentifiersModel.get_by_id(self.USER_GAE_ID),