feat: Add management command to unsubscribe user email (#31705)

This commit is contained in:
Shahbaz Shabbir
2023-03-01 18:23:56 +05:00
committed by GitHub
parent b3b3176d03
commit ff0805a189
2 changed files with 138 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
"""Tests for unsubscribe user's email management command"""
from tempfile import NamedTemporaryFile
from unittest.mock import patch
import pytest
import six
from django.core.management import call_command
from django.core.management.base import CommandError
from django.test import TestCase
COMMAND = 'unsubscribe_user_email'
class UnsubscribeUserEmailTests(TestCase):
"""
Tests unsubscribe_user_email command
"""
def setUp(self):
"""
Set up tests
"""
super().setUp()
self.lines = [
f"test_user{i}@test.com" for i in range(100)
]
self.invalid_csv_path = '/test/test.csv'
@staticmethod
def _write_test_csv(csv, lines):
"""Write a test csv file with the lines provided"""
csv.write(b"email\n")
for line in lines:
csv.write(six.b(line))
csv.seek(0)
return csv
@patch("common.djangoapps.student.management.commands.unsubscribe_user_email.get_braze_client")
def test_unsubscribe_user_email(self, mock_get_braze_client):
""" Test CSV file to unsubscribe user's email"""
with NamedTemporaryFile() as csv:
csv = self._write_test_csv(csv, self.lines)
call_command(
COMMAND,
'--csv_path',
csv.name
)
mock_get_braze_client.assert_called_once()
def test_command_error_for_csv_path(self):
""" Test command error raised if csv_path is not valid"""
with pytest.raises(CommandError):
call_command(
COMMAND,
'--csv_path',
self.invalid_csv_path
)