Repository: oppia/oppia

Issue:
Linking another skill when updating a question in the question editor fails
<!--
  - Thanks for taking the time to report a bug in the Oppia project.
  - Before filing a new issue, please do a quick search to check that it hasn't
  - already been filed on the [issue tracker](https://github.com/oppia/oppia/issues)._
  -->

**Describe the bug**
`Link Another Skill` fails to execute when updating a question in the question editor of a topic and gives console errors
**To Reproduce**
Steps to reproduce the behavior:
 1. Go to https://oppiatestserver.appspot.com/ 
 2. Go to Topics and Skills Dashboard
 3. Click an existing topic or skill
 4. Go to the Questions Editor Tab
 4. Choose an existing Question 
 5. Click 'Link Another Skill' button

**Observed behavior**
Clicking the button does nothing and displays console errors

**Expected behavior**
Another skill should be able to get linked without errors
**Screenshots**

![Screenshot from 2021-07-23 01-44-47](https://user-images.githubusercontent.com/43031234/126707277-8686bcff-9052-42a8-9d8d-b3432e22ea63.png)
![Screenshot from 2021-07-23 01-56-04](https://user-images.githubusercontent.com/43031234/126708788-29e4fa51-b333-4e04-9fd8-00a1ea22ce5d.png)


**Desktop (please complete the following information; delete this section if the issue does not arise on desktop):**
 - OS: Linux
 - Browser: chrome, firefox
 - Browser-version: 91.0.4472.164 , 90.0


Console errors can be seen whenever a user clicks on the checkboxes to mention whether the user wants to recieve emails from Oppia
<!--
  - Thanks for taking the time to report a bug in the Oppia project.
  - Before filing a new issue, please do a quick search to check that it hasn't
  - already been filed on the [issue tracker](https://github.com/oppia/oppia/issues)._
  -->

**Describe the bug**
Console errors can be seen whenever a user clicks on the checkboxes to mention whether the user wants to recieve emails from Oppia

**To Reproduce**
Steps to reproduce the behavior:
 1. Go to 'https://oppiatestserver.appspot.com/preferences'
 2. Click on checkboxes in the email section
 3. See the console error.

**Observed behavior**
Console errors can be seen whenever a user clicks on the checkboxes to mention whether the user wants to recieve emails from Oppia

**Expected behavior**
There should be no console errors.

**Screenshots**

Uploading 22.07.2021_23.51.02_REC.mp4…



**Desktop (please complete the following information; delete this section if the issue does not arise on desktop):**
 - OS: [e.g. iOS] Windows
 - Browser [e.g. chrome, safari] chrome


Console errors can be seen whenever a user clicks on the checkboxes to mention whether the user wants to recieve emails from Oppia
<!--
  - Thanks for taking the time to report a bug in the Oppia project.
  - Before filing a new issue, please do a quick search to check that it hasn't
  - already been filed on the [issue tracker](https://github.com/oppia/oppia/issues)._
  -->

**Describe the bug**
Console errors can be seen whenever a user clicks on the checkboxes to mention whether the user wants to recieve emails from Oppia

**To Reproduce**
Steps to reproduce the behavior:
 1. Go to 'https://oppiatestserver.appspot.com/preferences'
 2. Click on checkboxes in the email section
 3. See the console error.

**Observed behavior**
Console errors can be seen whenever a user clicks on the checkboxes to mention whether the user wants to recieve emails from Oppia

**Expected behavior**
There should be no console errors.

**Screenshots**

Uploading 22.07.2021_23.51.02_REC.mp4…



**Desktop (please complete the following information; delete this section if the issue does not arise on desktop):**
 - OS: [e.g. iOS] Windows
 - Browser [e.g. chrome, safari] chrome




Discussion:
@oppia/lace-team Please take a look. Thanks!
@oppia/lace-team please take a look. Thanks!
@aks681 can you take a look at this please (since you've been looking into some mail related tasks recently)? Thanks!

Also, @SAEb-ai, thanks for reporting but I noticed that the screen cast didn't get updated in the description. Could you update it please? Thanks!
@oppia/lace-team please take a look. Thanks!
@aks681 can you take a look at this please (since you've been looking into some mail related tasks recently)? Thanks!

Also, @SAEb-ai, thanks for reporting but I noticed that the screen cast didn't get updated in the description. Could you update it please? Thanks!

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
@@ -1468,7 +1468,9 @@ def update_email_preferences(
     email_preferences_model.subscription_notifications = (
         can_receive_subscription_email)
     email = get_email_from_user_id(user_id)
-    if not bulk_email_db_already_updated:
+    # Mailchimp database should not be updated in servers where sending
+    # emails is not allowed.
+    if not bulk_email_db_already_updated and feconf.CAN_SEND_EMAILS:
         user_creation_successful = (
             bulk_email_services.add_or_update_user_status(
                 email, can_receive_email_updates))


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
@@ -416,7 +416,9 @@ def _mock_logging_function(msg, *args):
             """Mocks logging.info()."""
             observed_log_messages.append(msg % args)
 
-        with self.swap(logging, 'info', _mock_logging_function):
+        logging_swap = self.swap(logging, 'info', _mock_logging_function)
+        send_mail_swap = self.swap(feconf, 'CAN_SEND_EMAILS', True)
+        with logging_swap, send_mail_swap:
             user_services.update_email_preferences(
                 user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE,
                 feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE,
@@ -433,9 +435,11 @@ def _mock_add_or_update_user_status(_email, _can_receive_updates):
             """Mocks bulk_email_services.add_or_update_user_status()."""
             return False
 
-        with self.swap(
+        send_mail_swap = self.swap(feconf, 'CAN_SEND_EMAILS', True)
+        bulk_email_swap = self.swap(
             bulk_email_services, 'add_or_update_user_status',
-            _mock_add_or_update_user_status):
+            _mock_add_or_update_user_status)
+        with send_mail_swap, bulk_email_swap:
             bulk_email_signup_message_should_be_shown = (
                 user_services.update_email_preferences(
                     user_id, True, feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE,
diff --git a/core/domain/wipeout_service_test.py b/core/domain/wipeout_service_test.py
--- a/core/domain/wipeout_service_test.py
+++ b/core/domain/wipeout_service_test.py
@@ -268,9 +268,7 @@ def _mock_logging_function(msg, *args):
             observed_log_messages,
             ['Email ID %s permanently deleted from bulk email provider\'s db. '
              'Cannot access API, since this is a dev environment'
-             % self.USER_1_EMAIL, 'Updated status of email ID %s\'s bulk email '
-             'preference in the service provider\'s db to False. Cannot access '
-             'API, since this is a dev environment.' % self.USER_1_EMAIL])
+             % self.USER_1_EMAIL])
         self.assertFalse(email_preferences.can_receive_email_updates)
         self.assertFalse(email_preferences.can_receive_editor_role_email)
         self.assertFalse(email_preferences.can_receive_feedback_message_email)