Newer
Older
self.assertEqual(
self.get_preapproval_count(), 0)
self.assertEqual(resp.status_code, 403)
def test_preapproval_email_type_approve_pendings(self):
enroll_request_users = [self.non_participation_user1]
for u in enroll_request_users:
with self.temporarily_switch_to_user(u):
self.c.post(self.enroll_request_url, follow=True)
self.flush_mailbox()
expected_participation_count = (
self.get_participation_count_by_status(participation_status.active) + 1)
resp = self.post_preapprovel(
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
self.preapprove_data_emails)
self.assertEqual(
self.get_participation_count_by_status(
participation_status.active), expected_participation_count)
self.assertResponseMessagesEqual(
resp,
[MESSAGE_BATCH_PREAPPROVED_RESULT_PATTERN
% {
'n_created': len(self.preapprove_data_emails),
'n_exist': 0,
'n_requested_approved': len(enroll_request_users)
}]
)
self.assertEqual(
len([m.to for m in mail.outbox]), len(enroll_request_users))
def test_preapproval_inst_id_type_approve_pending_require_id_verified(self):
assert self.course.preapproval_require_verified_inst_id is True
enroll_request_users = [
self.non_participation_user1, self.non_participation_user2]
for u in enroll_request_users:
with self.temporarily_switch_to_user(u):
self.c.post(self.enroll_request_url, follow=True)
self.flush_mailbox()
n_expected_newly_enrolled_users = (
len([u for u in enroll_request_users if u.institutional_id_verified]))
expected_participation_count = (
self.get_participation_count_by_status(participation_status.active)
+ n_expected_newly_enrolled_users
)
resp = self.post_preapprovel(
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
self.preapprove_data_institutional_ids)
self.assertEqual(
self.get_participation_count_by_status(
participation_status.active), expected_participation_count)
self.assertResponseMessagesEqual(
resp,
[MESSAGE_BATCH_PREAPPROVED_RESULT_PATTERN
% {
'n_created': len(self.preapprove_data_institutional_ids),
'n_exist': 0,
'n_requested_approved': n_expected_newly_enrolled_users
}]
)
self.assertEqual(
len([m.to for m in mail.outbox]), n_expected_newly_enrolled_users)
class EnrollmentPreapprovalInstIdNotRequireVerifiedTest(
EnrollmentPreapprovalTestMixin, TestCase):
# We'll have to mock course at two place if use mock, so I separate this
# test out of EnrollmentPreapprovalTest
courses_attributes_extra_list = [{
"preapproval_require_verified_inst_id": False}]
def test_preapproval_inst_id_type_approve_pending_not_require_id_verified(self):
assert self.course.preapproval_require_verified_inst_id is False
enroll_request_users = [
self.non_participation_user1, self.non_participation_user2]
for u in enroll_request_users:
with self.temporarily_switch_to_user(u):
self.c.post(self.enroll_request_url, follow=True)
self.flush_mailbox()
n_expected_newly_enrolled_users = len(enroll_request_users)
expected_participation_count = (
self.get_participation_count_by_status(participation_status.active)
+ n_expected_newly_enrolled_users
)
resp = self.post_preapprovel(
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
self.preapprove_data_institutional_ids)
self.assertEqual(
self.get_participation_count_by_status(
participation_status.active), expected_participation_count)
self.assertResponseMessagesEqual(
resp,
[MESSAGE_BATCH_PREAPPROVED_RESULT_PATTERN
% {
'n_created': len(self.preapprove_data_institutional_ids),
'n_exist': 0,
'n_requested_approved': n_expected_newly_enrolled_users
}]
)
self.assertEqual(
len([m.to for m in mail.outbox]), n_expected_newly_enrolled_users)
class EnrollmentEmailConnectionsTestMixin(LocmemBackendTestsMixin):
# Ensure request/decision mail will be sent with/without EmailConnection
# settings. https://github.com/inducer/relate/pull/366
courses_attributes_extra_list = [{"enrollment_approval_required": True}]
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
email_connections = {
"enroll": {
'host': 'smtp.gmail.com',
'username': 'blah@blah.com',
'password': 'password',
'port': 587,
'use_tls': True,
},
}
email_connections_none = {}
enrollment_email_from = "enroll@example.com"
robot_email_from = "robot@example.com"
class EnrollmentRequestEmailConnectionsTest(
EnrollmentEmailConnectionsTestMixin, EnrollmentTestBaseMixin, TestCase):
def test_email_with_email_connections1(self):
# with EMAIL_CONNECTIONS and ENROLLMENT_EMAIL_FROM configured
with self.settings(
EMAIL_CONNECTIONS=self.email_connections,
ROBOT_EMAIL_FROM=self.robot_email_from,
ENROLLMENT_EMAIL_FROM=self.enrollment_email_from):
expected_from_email = settings.ENROLLMENT_EMAIL_FROM
with self.temporarily_switch_to_user(self.non_participation_user1):
self.c.post(self.enroll_request_url, follow=True)
msg = mail.outbox[0]
self.assertEqual(msg.from_email, expected_from_email)
def test_email_with_email_connections3(self):
# with neither EMAIL_CONNECTIONS nor ENROLLMENT_EMAIL_FROM configured
with self.settings(
EMAIL_CONNECTIONS=self.email_connections,
ROBOT_EMAIL_FROM=self.robot_email_from):
if hasattr(settings, ENROLLMENT_EMAIL_FROM):
del settings.ENROLLMENT_EMAIL_FROM
expected_from_email = settings.ROBOT_EMAIL_FROM
with self.temporarily_switch_to_user(self.non_participation_user1):
self.c.post(self.enroll_request_url, follow=True)
msg = mail.outbox[0]
self.assertEqual(msg.from_email, expected_from_email)
class EnrollmentDecisionEmailConnectionsTest(
EnrollmentEmailConnectionsTestMixin, EnrollmentDecisionTestMixin, TestCase):
# {{{ with EMAIL_CONNECTIONS and ENROLLMENT_EMAIL_FROM configured
def test_email_with_email_connections1(self):
with self.settings(
RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=False,
EMAIL_CONNECTIONS=self.email_connections,
ROBOT_EMAIL_FROM=self.robot_email_from,
ENROLLMENT_EMAIL_FROM=self.enrollment_email_from):
expected_from_email = settings.ENROLLMENT_EMAIL_FROM
with self.temporarily_switch_to_user(
self.instructor_participation.user):
self.c.post(self.my_participation_edit_url, self.approve_post_data)
msg = mail.outbox[0]
self.assertEqual(msg.from_email, expected_from_email)
def test_email_with_email_connections2(self):
with self.settings(
RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=True,
EMAIL_CONNECTIONS=self.email_connections,
ROBOT_EMAIL_FROM=self.robot_email_from,
ENROLLMENT_EMAIL_FROM=self.enrollment_email_from):
expected_from_email = self.course.from_email
with self.temporarily_switch_to_user(
self.instructor_participation.user):
self.c.post(self.my_participation_edit_url, self.approve_post_data)
msg = mail.outbox[0]
self.assertEqual(msg.from_email, expected_from_email)
# }}}
# {{{ with neither EMAIL_CONNECTIONS nor ENROLLMENT_EMAIL_FROM configured
def test_email_with_email_connections3(self):
with self.settings(
RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=False,
ROBOT_EMAIL_FROM=self.robot_email_from):
if hasattr(settings, EMAIL_CONNECTIONS):
del settings.EMAIL_CONNECTIONS
if hasattr(settings, ENROLLMENT_EMAIL_FROM):
del settings.ENROLLMENT_EMAIL_FROM
expected_from_email = settings.ROBOT_EMAIL_FROM
with self.temporarily_switch_to_user(
self.instructor_participation.user):
self.c.post(self.my_participation_edit_url, self.approve_post_data)
msg = mail.outbox[0]
self.assertEqual(msg.from_email, expected_from_email)
def test_email_with_email_connections4(self):
with self.settings(
RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=True,
ROBOT_EMAIL_FROM=self.robot_email_from):
if hasattr(settings, EMAIL_CONNECTIONS):
del settings.EMAIL_CONNECTIONS
if hasattr(settings, ENROLLMENT_EMAIL_FROM):
del settings.ENROLLMENT_EMAIL_FROM
expected_from_email = self.course.from_email
with self.temporarily_switch_to_user(
self.instructor_participation.user):
self.c.post(self.my_participation_edit_url, self.approve_post_data)
msg = mail.outbox[0]
self.assertEqual(msg.from_email, expected_from_email)
# }}}
# vim: foldmethod=marker