Commit 9907cb52 by cahrens Committed by Andy Armstrong

Tests for profile image eventing.

parent 4797a74e
...@@ -223,12 +223,6 @@ class LearnerProfilePage(FieldsMixin, PageObject): ...@@ -223,12 +223,6 @@ class LearnerProfilePage(FieldsMixin, PageObject):
self.wait_for_ajax() self.wait_for_ajax()
def upload_correct_image_file(self, filename):
"""
Selects the correct file and clicks the upload button.
"""
self._upload_file(filename)
@property @property
def image_upload_success(self): def image_upload_success(self):
""" """
......
...@@ -27,6 +27,8 @@ class LearnerProfileTestMixin(EventsTestMixin): ...@@ -27,6 +27,8 @@ class LearnerProfileTestMixin(EventsTestMixin):
PUBLIC_PROFILE_EDITABLE_FIELDS = ['country', 'language_proficiencies', 'bio'] PUBLIC_PROFILE_EDITABLE_FIELDS = ['country', 'language_proficiencies', 'bio']
USER_SETTINGS_CHANGED_EVENT_NAME = u"edx.user.settings.changed"
def log_in_as_unique_user(self): def log_in_as_unique_user(self):
""" """
Create a unique user and return the account's username and id. Create a unique user and return the account's username and id.
...@@ -95,6 +97,16 @@ class LearnerProfileTestMixin(EventsTestMixin): ...@@ -95,6 +97,16 @@ class LearnerProfileTestMixin(EventsTestMixin):
}] }]
) )
def assert_event_emitted_num_times(self, profile_user_id, setting, num_times):
"""
Verify a particular user settings change event was emitted a certain
number of times.
"""
# pylint disable=no-member
super(LearnerProfileTestMixin, self).assert_event_emitted_num_times(
self.USER_SETTINGS_CHANGED_EVENT_NAME, self.start_time, profile_user_id, num_times, setting=setting
)
class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest): class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest):
""" """
...@@ -399,6 +411,8 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest): ...@@ -399,6 +411,8 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest):
profile_page.visit() profile_page.visit()
self.assertTrue(profile_page.image_upload_success) self.assertTrue(profile_page.image_upload_success)
self.assert_event_emitted_num_times(user_id, 'profile_image_uploaded_at', 1)
def test_user_can_see_error_for_exceeding_max_file_size_limit(self): def test_user_can_see_error_for_exceeding_max_file_size_limit(self):
""" """
Scenario: Upload profile image does not work for > 1MB image file. Scenario: Upload profile image does not work for > 1MB image file.
...@@ -421,6 +435,8 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest): ...@@ -421,6 +435,8 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest):
profile_page.visit() profile_page.visit()
self.assertTrue(profile_page.profile_has_default_image) self.assertTrue(profile_page.profile_has_default_image)
self.assert_event_emitted_num_times(user_id, 'profile_image_uploaded_at', 0)
def test_user_can_see_error_for_file_size_below_the_min_limit(self): def test_user_can_see_error_for_file_size_below_the_min_limit(self):
""" """
Scenario: Upload profile image does not work for < 100 Bytes image file. Scenario: Upload profile image does not work for < 100 Bytes image file.
...@@ -443,6 +459,8 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest): ...@@ -443,6 +459,8 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest):
profile_page.visit() profile_page.visit()
self.assertTrue(profile_page.profile_has_default_image) self.assertTrue(profile_page.profile_has_default_image)
self.assert_event_emitted_num_times(user_id, 'profile_image_uploaded_at', 0)
def test_user_can_see_error_for_wrong_file_type(self): def test_user_can_see_error_for_wrong_file_type(self):
""" """
Scenario: Upload profile image does not work for wrong file types. Scenario: Upload profile image does not work for wrong file types.
...@@ -465,6 +483,8 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest): ...@@ -465,6 +483,8 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest):
profile_page.visit() profile_page.visit()
self.assertTrue(profile_page.profile_has_default_image) self.assertTrue(profile_page.profile_has_default_image)
self.assert_event_emitted_num_times(user_id, 'profile_image_uploaded_at', 0)
def test_user_can_remove_profile_image(self): def test_user_can_remove_profile_image(self):
""" """
Scenario: Remove profile image works correctly. Scenario: Remove profile image works correctly.
...@@ -489,6 +509,8 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest): ...@@ -489,6 +509,8 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest):
profile_page.visit() profile_page.visit()
self.assertTrue(profile_page.profile_has_default_image) self.assertTrue(profile_page.profile_has_default_image)
self.assert_event_emitted_num_times(user_id, 'profile_image_uploaded_at', 1)
def test_user_cannot_remove_default_image(self): def test_user_cannot_remove_default_image(self):
""" """
Scenario: Remove profile image does not works for default images. Scenario: Remove profile image does not works for default images.
...@@ -505,6 +527,21 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest): ...@@ -505,6 +527,21 @@ class OwnLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest):
self.assert_default_image_has_public_access(profile_page) self.assert_default_image_has_public_access(profile_page)
self.assertFalse(profile_page.remove_link_present) self.assertFalse(profile_page.remove_link_present)
def test_eventing_after_multiple_uploads(self):
"""
Scenario: An event is fired when a user with a profile image uploads another image
Given that I am on my profile page with public access
And I upload a new image via file uploader
When I upload another image via the file uploader
Then two upload events have been emitted
"""
username, user_id = self.log_in_as_unique_user()
profile_page = self.visit_profile_page(username, privacy=self.PRIVACY_PUBLIC)
profile_page.upload_file(filename='image.jpg')
profile_page.upload_file(filename='image.jpg')
self.assert_event_emitted_num_times(user_id, 'profile_image_uploaded_at', 2)
class DifferentUserLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest): class DifferentUserLearnerProfilePageTest(LearnerProfileTestMixin, WebAppTest):
""" """
......
...@@ -27,6 +27,7 @@ from .helpers import make_image_file ...@@ -27,6 +27,7 @@ from .helpers import make_image_file
TEST_PASSWORD = "test" TEST_PASSWORD = "test"
TEST_UPLOAD_DT = datetime.datetime(2002, 1, 9, 15, 43, 01, tzinfo=UTC) TEST_UPLOAD_DT = datetime.datetime(2002, 1, 9, 15, 43, 01, tzinfo=UTC)
TEST_UPLOAD_DT2 = datetime.datetime(2003, 1, 9, 15, 43, 01, tzinfo=UTC)
class ProfileImageEndpointTestCase(UserSettingsEventTestMixin, APITestCase): class ProfileImageEndpointTestCase(UserSettingsEventTestMixin, APITestCase):
...@@ -110,13 +111,13 @@ class ProfileImageUploadTestCase(ProfileImageEndpointTestCase): ...@@ -110,13 +111,13 @@ class ProfileImageUploadTestCase(ProfileImageEndpointTestCase):
""" """
_view_name = "profile_image_upload" _view_name = "profile_image_upload"
def check_upload_event_emitted(self): def check_upload_event_emitted(self, old=None, new=TEST_UPLOAD_DT):
""" """
Make sure we emit a UserProfile event corresponding to the Make sure we emit a UserProfile event corresponding to the
profile_image_uploaded_at field changing. profile_image_uploaded_at field changing.
""" """
self.assert_user_setting_event_emitted( self.assert_user_setting_event_emitted(
setting='profile_image_uploaded_at', old=None, new=TEST_UPLOAD_DT setting='profile_image_uploaded_at', old=old, new=new
) )
def test_unsupported_methods(self, mock_log): def test_unsupported_methods(self, mock_log):
...@@ -140,7 +141,7 @@ class ProfileImageUploadTestCase(ProfileImageEndpointTestCase): ...@@ -140,7 +141,7 @@ class ProfileImageUploadTestCase(ProfileImageEndpointTestCase):
self.assertFalse(mock_log.info.called) self.assertFalse(mock_log.info.called)
self.assert_no_events_were_emitted() self.assert_no_events_were_emitted()
@patch('openedx.core.djangoapps.profile_images.views._make_upload_dt', return_value=TEST_UPLOAD_DT) @patch('openedx.core.djangoapps.profile_images.views._make_upload_dt', side_effect=[TEST_UPLOAD_DT, TEST_UPLOAD_DT2])
def test_upload_self(self, mock_make_image_version, mock_log): # pylint: disable=unused-argument def test_upload_self(self, mock_make_image_version, mock_log): # pylint: disable=unused-argument
""" """
Test that an authenticated user can POST to their own upload endpoint. Test that an authenticated user can POST to their own upload endpoint.
...@@ -156,6 +157,13 @@ class ProfileImageUploadTestCase(ProfileImageEndpointTestCase): ...@@ -156,6 +157,13 @@ class ProfileImageUploadTestCase(ProfileImageEndpointTestCase):
) )
self.check_upload_event_emitted() self.check_upload_event_emitted()
# Try another upload and make sure that a second event is emitted.
with make_image_file() as image_file:
response = self.client.post(self.url, {'file': image_file}, format='multipart')
self.check_response(response, 204)
self.check_upload_event_emitted(old=TEST_UPLOAD_DT, new=TEST_UPLOAD_DT2)
def test_upload_other(self, mock_log): def test_upload_other(self, mock_log):
""" """
Test that an authenticated user cannot POST to another user's upload endpoint. Test that an authenticated user cannot POST to another user's upload endpoint.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment