Newer
Older
'n_requested_approved': 0
}]
)
def test_preapproval_create_permission_error(self):
with self.temporarily_switch_to_user(self.student_participation.user):
resp = self.c.get(self.preapproval_url)
self.assertEqual(resp.status_code, 403)
resp = self.post_preapprovel(
"email",
self.preapprove_data_emails,
force_loggin_instructor=False
)
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(
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
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(
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
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
course_attributes_extra = {
"enrollment_approval_required": True,
"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(
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
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
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
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
course_attributes_extra = {"enrollment_approval_required": True}
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