From 62f3171c463fc8827f9fa2a363314ab3caec4bb7 Mon Sep 17 00:00:00 2001
From: John Eskew <jeskew@edx.org>
Date: Tue, 16 Oct 2018 16:08:12 -0400
Subject: [PATCH] Add test for bulk email optout mgmt cmd.

---
 .../tests/test_bulk_user_org_email_optout.py  | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 openedx/core/djangoapps/user_api/management/tests/test_bulk_user_org_email_optout.py

diff --git a/openedx/core/djangoapps/user_api/management/tests/test_bulk_user_org_email_optout.py b/openedx/core/djangoapps/user_api/management/tests/test_bulk_user_org_email_optout.py
new file mode 100644
index 00000000000..2511af2fc92
--- /dev/null
+++ b/openedx/core/djangoapps/user_api/management/tests/test_bulk_user_org_email_optout.py
@@ -0,0 +1,49 @@
+"""
+Test the test_bulk_user_org_email_optout management command
+"""
+import os
+import tempfile
+from contextlib import contextmanager
+import mock
+import pytest
+
+from django.core.management import call_command
+
+pytestmark = pytest.mark.django_db
+
+
+CSV_DATA = """1,UniversityX
+2,CollegeX
+3,StateUX
+"""
+
+
+@contextmanager
+def _create_test_csv(csv_data):
+    """
+    Context manager to create and populate a CSV file - and delete it after usage.
+    """
+    __, file_name = tempfile.mkstemp(text=True)
+    with open(file_name, 'w') as file_pointer:
+        file_pointer.write(csv_data.encode('utf-8'))
+    yield file_name
+    os.unlink(file_name)
+
+
+@mock.patch('openedx.core.djangoapps.user_api.management.commands.bulk_user_org_email_optout.log.info')
+def test_successful_dry_run(mock_logger):
+    """
+    Run the command with default states for a successful initial population
+    """
+    with _create_test_csv(CSV_DATA) as tmp_csv_file:
+        args = ['--dry_run', '--optout_csv_path={}'.format(tmp_csv_file)]
+        call_command('bulk_user_org_email_optout', *args)
+        assert mock_logger.call_count == 3
+        mock_logger.assert_any_call("Read %s opt-out rows from CSV file '%s'.", 3, tmp_csv_file)
+        mock_logger.assert_any_call(
+            'Attempting opt-out for rows (%s, %s) through (%s, %s)...', '1', 'UniversityX', '3', 'StateUX'
+        )
+        mock_logger.assert_any_call(
+            'INSERT INTO user_api_userorgtag (`user_id`, `org`, `key`, `value`, `created`, `modified`) \
+VALUES (1,"UniversityX","email-optin","False",NOW(),NOW()),(2,"CollegeX","email-optin","False",NOW(),NOW()),\
+(3,"StateUX","email-optin","False",NOW(),NOW()) ON DUPLICATE KEY UPDATE value="False", modified=NOW();')
-- 
GitLab