Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
resp,
[MESSAGE_BATCH_PREAPPROVED_RESULT_PATTERN
% {
'n_created': len(self.preapprove_data_emails),
'n_exist': 0,
'n_requested_approved': 0
}]
)
# repost same data
resp = self.post_preapprovel(
"email",
self.preapprove_data_emails)
self.assertEqual(
self.get_preapproval_count(), len(self.preapprove_data_emails))
self.assertResponseMessagesEqual(
resp,
[MESSAGE_BATCH_PREAPPROVED_RESULT_PATTERN
% {
'n_created': 0,
'n_exist': len(self.preapprove_data_emails),
'n_requested_approved': 0
}]
)
def test_preapproval_create_institutional_id_type(self):
resp = self.post_preapprovel(
self.preapprove_data_institutional_ids)
self.assertEqual(
self.get_preapproval_count(),
len(self.preapprove_data_institutional_ids))
self.assertResponseMessagesEqual(
resp,
[MESSAGE_BATCH_PREAPPROVED_RESULT_PATTERN
% {
'n_created': len(self.preapprove_data_institutional_ids),
'n_exist': 0,
'n_requested_approved': 0
}]
)
# repost same data
resp = self.post_preapprovel(
self.preapprove_data_institutional_ids)
self.assertEqual(
self.get_preapproval_count(),
len(self.preapprove_data_institutional_ids))
self.assertResponseMessagesEqual(
resp,
[MESSAGE_BATCH_PREAPPROVED_RESULT_PATTERN
% {
'n_created': 0,
'n_exist': len(self.preapprove_data_institutional_ids),
'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):
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(p_status.active) + 1)
self.preapprove_data_emails)
self.assertEqual(
self.get_participation_count_by_status(
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 = [
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 = (
+ n_expected_newly_enrolled_users
)
resp = self.post_preapprovel(
self.preapprove_data_institutional_ids)
self.assertEqual(
self.get_participation_count_by_status(
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):
courses_attributes_extra_list = [{
"preapproval_require_verified_inst_id": False}]
def test_preapproval_inst_id_type_approve_pending_not_require_id_verified(self):
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 = (
+ n_expected_newly_enrolled_users
)
resp = self.post_preapprovel(
self.preapprove_data_institutional_ids)
self.assertEqual(
self.get_participation_count_by_status(
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}]
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
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_ptcp_active_user1):
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_ptcp_active_user1):
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)
# }}}
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
class ParticipationQueryFormTest(unittest.TestCase):
def setUp(self):
self.course = factories.CourseFactory()
def test_form_valid(self):
data = {
"queries": "id:1234",
"op": "apply_tag",
"tag": "hello"}
form = enrollment.ParticipationQueryForm(data=data)
self.assertTrue(form.is_valid())
def test_form_valid_no_tag(self):
data = {
"queries": "id:1234",
"op": "drop"}
form = enrollment.ParticipationQueryForm(data=data)
self.assertTrue(form.is_valid())
def test_form_tag_invalid(self):
data = {
"queries": "id:1234",
"op": "apply_tag",
"tag": "~hello~"}
form = enrollment.ParticipationQueryForm(data=data)
self.assertIn("Name contains invalid characters.", form.errors["tag"])