Use bytestrings to create SimpleUploadedFiles. BOM-616

This commit is contained in:
Ned Batchelder
2019-09-18 17:38:55 -04:00
committed by Feanil Patel
parent f259a911c7
commit 835a84f33c
4 changed files with 36 additions and 38 deletions

View File

@@ -50,7 +50,7 @@ class Oauth2ProviderConfigAdminTest(testutil.TestCase):
provider1 = self.configure_dummy_provider(
enabled=True,
icon_class='',
icon_image=SimpleUploadedFile('icon.svg', '<svg><rect width="50" height="100"/></svg>'),
icon_image=SimpleUploadedFile('icon.svg', b'<svg><rect width="50" height="100"/></svg>'),
)
# Get the provider instance with active flag

View File

@@ -180,17 +180,17 @@ class CertificateTemplateAssetTest(TestCase):
"""
CertificateTemplateAsset(description='test description', asset=SimpleUploadedFile(
'picture1.jpg',
'these are the file contents!')).save()
b'these are the file contents!')).save()
certificate_template_asset = CertificateTemplateAsset.objects.get(id=1)
self.assertEqual(certificate_template_asset.asset, 'certificate_template_assets/1/picture1.jpg')
# Now save asset with same file again, New file will be uploaded after deleting the old one with the same name.
certificate_template_asset.asset = SimpleUploadedFile('picture1.jpg', 'file contents')
certificate_template_asset.asset = SimpleUploadedFile('picture1.jpg', b'file contents')
certificate_template_asset.save()
self.assertEqual(certificate_template_asset.asset, 'certificate_template_assets/1/picture1.jpg')
# Now replace the asset with another file
certificate_template_asset.asset = SimpleUploadedFile('picture2.jpg', 'file contents')
certificate_template_asset.asset = SimpleUploadedFile('picture2.jpg', b'file contents')
certificate_template_asset.save()
certificate_template_asset = CertificateTemplateAsset.objects.get(id=1)

View File

@@ -675,7 +675,7 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
"""
Happy path test to create a single new user
"""
csv_content = "test_student@example.com,test_student_1,tester1,USA"
csv_content = b"test_student@example.com,test_student_1,tester1,USA"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.url, {'students_list': uploaded_file})
self.assertEqual(response.status_code, 200)
@@ -718,8 +718,8 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
If the email address and username already exists
and the user is enrolled in the course, do nothing (including no email gets sent out)
"""
csv_content = "test_student@example.com,test_student_1,tester1,USA\n" \
"test_student@example.com,test_student_1,tester2,US"
csv_content = b"test_student@example.com,test_student_1,tester1,USA\n" \
b"test_student@example.com,test_student_1,tester2,US"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.url, {'students_list': uploaded_file})
self.assertEqual(response.status_code, 200)
@@ -771,7 +771,7 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
"""
Try uploading a CSV file which does not have the exact four columns of data
"""
csv_content = "test_student@example.com,test_student_1\n"
csv_content = b"test_student@example.com,test_student_1\n"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.url, {'students_list': uploaded_file})
self.assertEqual(response.status_code, 200)
@@ -788,8 +788,7 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
"""
Test failure case of a poorly formatted email field
"""
csv_content = "test_student.example.com,test_student_1,tester1,USA"
csv_content = b"test_student.example.com,test_student_1,tester1,USA"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.url, {'students_list': uploaded_file})
data = json.loads(response.content.decode('utf-8'))
@@ -808,8 +807,7 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
If the email address and username already exists
and the user is not enrolled in the course, enrolled him/her and iterate to next one.
"""
csv_content = "nonenrolled@test.com,NotEnrolledStudent,tester1,USA"
csv_content = b"nonenrolled@test.com,NotEnrolledStudent,tester1,USA"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.url, {'students_list': uploaded_file})
self.assertEqual(response.status_code, 200)
@@ -827,8 +825,8 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
If the email address already exists, but the username is different,
assume it is the correct user and just register the user in the course.
"""
csv_content = "test_student@example.com,test_student_1,tester1,USA\n" \
"test_student@example.com,test_student_2,tester2,US"
csv_content = b"test_student@example.com,test_student_1,tester1,USA\n" \
b"test_student@example.com,test_student_2,tester2,US"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.url, {'students_list': uploaded_file})
@@ -862,7 +860,7 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
user.is_active = False
user.save()
csv_content = "{email},{username},tester,USA".format(email=conflicting_email, username='new_test_student')
csv_content = b"{email},{username},tester,USA".format(email=conflicting_email, username='new_test_student')
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.url, {'students_list': uploaded_file})
@@ -880,8 +878,8 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
If the username already exists (but not the email),
assume it is a different user and fail to create the new account.
"""
csv_content = "test_student1@example.com,test_student_1,tester1,USA\n" \
"test_student2@example.com,test_student_1,tester2,US"
csv_content = b"test_student1@example.com,test_student_1,tester1,USA\n" \
b"test_student2@example.com,test_student_1,tester2,US"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
@@ -895,8 +893,8 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
"""
Test when the user does not attach a file
"""
csv_content = "test_student1@example.com,test_student_1,tester1,USA\n" \
"test_student2@example.com,test_student_1,tester2,US"
csv_content = b"test_student1@example.com,test_student_1,tester1,USA\n" \
b"test_student2@example.com,test_student_1,tester2,US"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
@@ -913,8 +911,8 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
"""
Test that exceptions are handled well
"""
csv_content = "test_student1@example.com,test_student_1,tester1,USA\n" \
"test_student2@example.com,test_student_1,tester2,US"
csv_content = b"test_student1@example.com,test_student_1,tester1,USA\n" \
b"test_student2@example.com,test_student_1,tester2,US"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
with patch('lms.djangoapps.instructor.views.api.create_manual_course_enrollment') as mock:
@@ -947,10 +945,10 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
user.is_active = False
user.save()
csv_content = "test_student1@example.com,test_student_1,tester1,USA\n" \
"test_student3@example.com,test_student_1,tester3,CA\n" \
"test_student4@example.com,test_student_4,tester4,USA\n" \
"test_student2@example.com,test_student_2,tester2,USA"
csv_content = b"test_student1@example.com,test_student_1,tester1,USA\n" \
b"test_student3@example.com,test_student_1,tester3,CA\n" \
b"test_student4@example.com,test_student_4,tester4,USA\n" \
b"test_student2@example.com,test_student_2,tester2,USA"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.url, {'students_list': uploaded_file})
@@ -985,7 +983,7 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
@patch.dict(settings.FEATURES, {'ALLOW_AUTOMATED_SIGNUPS': False})
def test_allow_automated_signups_flag_not_set(self):
csv_content = "test_student1@example.com,test_student_1,tester1,USA"
csv_content = b"test_student1@example.com,test_student_1,tester1,USA"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.url, {'students_list': uploaded_file})
self.assertEquals(response.status_code, 403)
@@ -1001,7 +999,7 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
# Login Audit Course instructor
self.client.login(username=self.audit_course_instructor.username, password='test')
csv_content = "test_student_wl@example.com,test_student_wl,Test Student,USA"
csv_content = b"test_student_wl@example.com,test_student_wl,Test Student,USA"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.audit_course_url, {'students_list': uploaded_file})
@@ -1032,7 +1030,7 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
# Login Audit Course instructor
self.client.login(username=self.white_label_course_instructor.username, password='test')
csv_content = "test_student_wl@example.com,test_student_wl,Test Student,USA"
csv_content = b"test_student_wl@example.com,test_student_wl,Test Student,USA"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.white_label_course_url, {'students_list': uploaded_file})
@@ -1058,7 +1056,7 @@ class TestInstructorAPIBulkAccountCreationAndEnrollment(SharedModuleStoreTestCas
# Login white label course instructor
self.client.login(username=self.white_label_course_instructor.username, password='test')
csv_content = "test_student_wl@example.com,test_student_wl,Test Student,USA"
csv_content = b"test_student_wl@example.com,test_student_wl,Test Student,USA"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)
response = self.client.post(self.white_label_course_url, {'students_list': uploaded_file})

View File

@@ -928,8 +928,8 @@ class TestCertificatesInstructorApiBulkWhiteListExceptions(SharedModuleStoreTest
"""
Happy path test to create a single new white listed record
"""
csv_content = "test_student1@example.com,dummy_notes\n" \
"test_student2@example.com,dummy_notes"
csv_content = b"test_student1@example.com,dummy_notes\n" \
b"test_student2@example.com,dummy_notes"
data = self.upload_file(csv_content=csv_content)
self.assertEquals(len(data['general_errors']), 0)
self.assertEquals(len(data['row_errors']['data_format_error']), 0)
@@ -943,8 +943,8 @@ class TestCertificatesInstructorApiBulkWhiteListExceptions(SharedModuleStoreTest
"""
Try uploading a CSV file with invalid data formats and verify the errors.
"""
csv_content = "test_student1@example.com,test,1,USA\n" \
"test_student2@example.com,test,1"
csv_content = b"test_student1@example.com,test,1,USA\n" \
b"test_student2@example.com,test,1"
data = self.upload_file(csv_content=csv_content)
self.assertEquals(len(data['row_errors']['data_format_error']), 2)
@@ -979,7 +979,7 @@ class TestCertificatesInstructorApiBulkWhiteListExceptions(SharedModuleStoreTest
"""
Test failure case of a poorly formatted email field
"""
csv_content = "test_student.example.com,dummy_notes"
csv_content = b"test_student.example.com,dummy_notes"
data = self.upload_file(csv_content=csv_content)
self.assertEquals(len(data['row_errors']['user_not_exist']), 1)
@@ -990,7 +990,7 @@ class TestCertificatesInstructorApiBulkWhiteListExceptions(SharedModuleStoreTest
"""
If the user is not enrolled in the course then there should be a user_not_enrolled error.
"""
csv_content = "nonenrolled@test.com,dummy_notes"
csv_content = b"nonenrolled@test.com,dummy_notes"
data = self.upload_file(csv_content=csv_content)
self.assertEquals(len(data['row_errors']['user_not_enrolled']), 1)
@@ -1007,7 +1007,7 @@ class TestCertificatesInstructorApiBulkWhiteListExceptions(SharedModuleStoreTest
whitelist=True,
notes=''
)
csv_content = "test_student1@example.com,dummy_notes"
csv_content = b"test_student1@example.com,dummy_notes"
data = self.upload_file(csv_content=csv_content)
self.assertEquals(len(data['row_errors']['user_already_white_listed']), 1)
self.assertEquals(len(data['general_errors']), 0)
@@ -1018,8 +1018,8 @@ class TestCertificatesInstructorApiBulkWhiteListExceptions(SharedModuleStoreTest
"""
Test when the user does not attach a file
"""
csv_content = "test_student1@example.com,dummy_notes\n" \
"test_student2@example.com,dummy_notes"
csv_content = b"test_student1@example.com,dummy_notes\n" \
b"test_student2@example.com,dummy_notes"
uploaded_file = SimpleUploadedFile("temp.csv", csv_content)