def test_set_and_get_user_email_preferences(self): user_id = 'someUser' username = '******' user_email = '*****@*****.**' user_services.get_or_create_user(user_id, user_email) user_services.set_username(user_id, username) # When UserEmailPreferencesModel is yet to be created, # the value returned by get_email_preferences() should be True. email_preferences = user_services.get_email_preferences(user_id) self.assertEquals(email_preferences['can_receive_editor_role_email'], feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE) # The user retrieves their email preferences. This initializes # a UserEmailPreferencesModel instance with the default values. user_services.update_email_preferences( user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE, feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE) email_preferences = user_services.get_email_preferences(user_id) self.assertEquals(email_preferences['can_receive_editor_role_email'], feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE) # The user sets their membership email preference to False. user_services.update_email_preferences( user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE, False) email_preferences = user_services.get_email_preferences(user_id) self.assertEquals(email_preferences['can_receive_editor_role_email'], False)
def test_get_exploration_user_data(self): auth_id = 'test_id' username = '******' user_email = '*****@*****.**' user_id = user_services.create_new_user(auth_id, user_email).user_id user_services.set_username(user_id, username) user_services.update_learner_checkpoint_progress( user_id, self.EXP_1_ID, 'Introduction', 1) expected_user_data_dict = { 'rating': None, 'rated_on': None, 'draft_change_list': None, 'draft_change_list_last_updated': None, 'draft_change_list_exp_version': None, 'draft_change_list_id': 0, 'mute_suggestion_notifications': ( feconf.DEFAULT_SUGGESTION_NOTIFICATIONS_MUTED_PREFERENCE), 'mute_feedback_notifications': ( feconf.DEFAULT_FEEDBACK_NOTIFICATIONS_MUTED_PREFERENCE), 'furthest_reached_checkpoint_exp_version': 1, 'furthest_reached_checkpoint_state_name': 'Introduction', 'most_recently_reached_checkpoint_exp_version': 1, 'most_recently_reached_checkpoint_state_name': 'Introduction' } exp_user_data = exp_fetchers.get_exploration_user_data( user_id, self.EXP_1_ID) self.assertIsNotNone(exp_user_data) self.assertEqual(expected_user_data_dict, exp_user_data.to_dict())
def post(self): """Handles POST requests.""" username = self.payload.get('username') agreed_to_terms = self.payload.get('agreed_to_terms') default_dashboard = self.payload.get('default_dashboard') can_receive_email_updates = self.payload.get( 'can_receive_email_updates') bulk_email_signup_message_should_be_shown = False if can_receive_email_updates is not None: bulk_email_signup_message_should_be_shown = ( user_services.update_email_preferences( self.user_id, can_receive_email_updates, feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE, feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE, feconf.DEFAULT_SUBSCRIPTION_EMAIL_PREFERENCE)) if bulk_email_signup_message_should_be_shown: self.render_json({ 'bulk_email_signup_message_should_be_shown': (bulk_email_signup_message_should_be_shown) }) return has_ever_registered = user_services.has_ever_registered(self.user_id) has_fully_registered_account = ( user_services.has_fully_registered_account(self.user_id)) if has_fully_registered_account: self.render_json({}) return if not isinstance(agreed_to_terms, bool) or not agreed_to_terms: raise self.InvalidInputException( 'In order to edit explorations on this site, you will ' 'need to accept the license terms.') else: user_services.record_agreement_to_terms(self.user_id) if not user_services.get_username(self.user_id): try: user_services.set_username(self.user_id, username) except utils.ValidationError as e: raise self.InvalidInputException(e) # Note that an email is only sent when the user registers for the first # time. if feconf.CAN_SEND_EMAILS and not has_ever_registered: email_manager.send_post_signup_email(self.user_id) user_services.generate_initial_profile_picture(self.user_id) if not has_ever_registered: # Set the default dashboard for new users. user_services.update_user_default_dashboard( self.user_id, default_dashboard) self.render_json({ 'bulk_email_signup_message_should_be_shown': (bulk_email_signup_message_should_be_shown) })
def test_cannot_set_existing_username(self): with self.assertRaisesRegexp( utils.ValidationError, 'Sorry, the username \"%s\" is already taken! Please pick ' 'a different one.' % self.OWNER_USERNAME ): user_services.set_username(self.owner_id, self.OWNER_USERNAME)
def test_set_invalid_usernames(self): user_id = 'someUser' user_services._create_user(user_id, '*****@*****.**') bad_usernames = [ ' bob ', '@', '', 'a' * 100, 'ADMIN', 'admin', 'AdMiN2020'] for username in bad_usernames: with self.assertRaises(utils.ValidationError): user_services.set_username(user_id, username)
def setUp(self): super(SubjectInterestsUnitTests, self).setUp() self.user_id = 'someUser' self.username = '******' self.user_email = '*****@*****.**' user_services.get_or_create_user(self.user_id, self.user_email) user_services.set_username(self.user_id, self.username)
def test_set_and_get_username(self): user_id = 'someUser' username = '******' with self.assertRaisesRegexp(Exception, 'User not found.'): user_services.set_username(user_id, username) user_services.create_new_user(user_id, '*****@*****.**') user_services.set_username(user_id, username) self.assertEqual(username, user_services.get_username(user_id))
def test_set_and_get_username(self): user_id = 'someUser' username = '******' with self.assertRaisesRegexp(Exception, 'User not found.'): user_services.set_username(user_id, username) user_services._create_user(user_id, '*****@*****.**') user_services.set_username(user_id, username) self.assertEquals(username, user_services.get_username(user_id))
def test_get_human_readable_user_ids_with_nonexistent_id_non_strict_passes( self): user_id = user_services.create_new_user('auth_id', '*****@*****.**').user_id user_services.set_username(user_id, 'username') user_services.mark_user_for_deletion(user_id) human_readable_user_ids = user_services.get_human_readable_user_ids( [user_id], strict=False) self.assertEqual(human_readable_user_ids, [user_services.LABEL_FOR_USER_BEING_DELETED])
def test_record_user_started_state_translation_tutorial(self): # Testing of the user translation tutorial firsttime state storage. user_id = 'someUser' username = '******' user_services.create_new_user(user_id, '*****@*****.**') user_services.set_username(user_id, username) user_services.record_user_started_state_translation_tutorial(user_id) user_settings = user_services.get_user_settings(user_id) self.assertIsInstance( user_settings.last_started_state_translation_tutorial, datetime.datetime) self.assertTrue( user_settings.last_started_state_translation_tutorial is not None)
def test_update_user_role(self): user_id = 'test_id' user_name = 'testname' user_email = '*****@*****.**' user_services.create_new_user(user_id, user_email) user_services.set_username(user_id, user_name) self.assertEqual(user_services.get_user_role_from_id(user_id), feconf.ROLE_EXPLORATION_EDITOR) user_services.update_user_role(user_id, feconf.ROLE_COLLECTION_EDITOR) self.assertEqual(user_services.get_user_role_from_id(user_id), feconf.ROLE_COLLECTION_EDITOR)
def put(self): old_username = self.normalized_payload.get('old_username') new_username = self.normalized_payload.get('new_username') user_id = user_services.get_user_id_from_username(old_username) if user_id is None: raise self.InvalidInputException('Invalid username: %s' % old_username) if user_services.is_username_taken(new_username): raise self.InvalidInputException('Username already taken.') user_services.set_username(user_id, new_username) user_services.log_username_change(self.user_id, old_username, new_username) self.render_json({})
def test_update_user_creator_dashboard_display(self): user_id = 'test_id' username = '******' user_email = '*****@*****.**' user_services.create_new_user(user_id, user_email) user_services.set_username(user_id, username) user_setting = user_services.get_user_settings(user_id) self.assertEqual( user_setting.creator_dashboard_display_pref, constants.ALLOWED_CREATOR_DASHBOARD_DISPLAY_PREFS['CARD']) user_services.update_user_creator_dashboard_display( user_id, constants.ALLOWED_CREATOR_DASHBOARD_DISPLAY_PREFS['LIST']) user_setting = user_services.get_user_settings(user_id) self.assertEqual( user_setting.creator_dashboard_display_pref, constants.ALLOWED_CREATOR_DASHBOARD_DISPLAY_PREFS['LIST'])
def test_set_and_get_user_email_preferences(self): user_id = 'someUser' username = '******' user_email = '*****@*****.**' user_services.create_new_user(user_id, user_email) user_services.set_username(user_id, username) # When UserEmailPreferencesModel is yet to be created, # the value returned by get_email_preferences() should be True. email_preferences = user_services.get_email_preferences(user_id) self.assertEqual(email_preferences.can_receive_editor_role_email, feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE) email_preferences = user_services.get_email_preferences(user_id) self.assertEqual(email_preferences.can_receive_feedback_message_email, feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE) # The user retrieves their email preferences. This initializes # a UserEmailPreferencesModel instance with the default values. user_services.update_email_preferences( user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE, feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE, feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE, feconf.DEFAULT_SUBSCRIPTION_EMAIL_PREFERENCE) email_preferences = user_services.get_email_preferences(user_id) self.assertEqual(email_preferences.can_receive_editor_role_email, feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE) self.assertEqual(email_preferences.can_receive_feedback_message_email, feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_PREFERENCE) # The user sets their membership email preference to False. user_services.update_email_preferences( user_id, feconf.DEFAULT_EMAIL_UPDATES_PREFERENCE, False, False, False) email_preferences = user_services.get_email_preferences(user_id) self.assertFalse(email_preferences.can_receive_editor_role_email) self.assertFalse(email_preferences.can_receive_feedback_message_email) self.assertFalse(email_preferences.can_receive_subscription_email)
def test_get_email_from_username(self): user_id = 'someUser' username = '******' user_email = '*****@*****.**' user_services._create_user(user_id, user_email) user_services.set_username(user_id, username) self.assertEquals(user_services.get_username(user_id), username) # Handle usernames that exist. self.assertEquals(user_services.get_email_from_username(username), user_email) # Handle usernames in the same equivalence class correctly. self.assertEquals(user_services.get_email_from_username('USERNAME'), user_email) # Return None for usernames which don't exist. self.assertIsNone( user_services.get_email_from_username('fakeUsername'))
def test_get_usernames(self): user_ids = ['test1', feconf.SYSTEM_COMMITTER_ID, 'test2'] usernames = ['name1', feconf.SYSTEM_COMMITTER_ID, 'name2'] user_emails = [ '*****@*****.**', feconf.SYSTEM_EMAIL_ADDRESS, '*****@*****.**' ] for uid, email, name in zip(user_ids, user_emails, usernames): if uid != feconf.SYSTEM_COMMITTER_ID: user_services.create_new_user(uid, email) user_services.set_username(uid, name) # Handle usernames that exists. self.assertEqual(usernames, user_services.get_usernames(user_ids)) # Return empty list when no user id passed. self.assertEqual([], user_services.get_usernames([])) # Return None for usernames that don't exists. self.assertEqual([None, 'name1'], user_services.get_usernames(['fakeUser', 'test1']))
def post(self): """Handles POST requests.""" username = self.payload.get('username') agreed_to_terms = self.payload.get('agreed_to_terms') can_receive_email_updates = self.payload.get( 'can_receive_email_updates') has_ever_registered = user_services.has_ever_registered(self.user_id) has_fully_registered = user_services.has_fully_registered(self.user_id) if has_fully_registered: self.render_json({}) return if not isinstance(agreed_to_terms, bool) or not agreed_to_terms: raise self.InvalidInputException( 'In order to edit explorations on this site, you will ' 'need to accept the license terms.') else: user_services.record_agreement_to_terms(self.user_id) if not user_services.get_username(self.user_id): try: user_services.set_username(self.user_id, username) except utils.ValidationError as e: raise self.InvalidInputException(e) if can_receive_email_updates is not None: user_services.update_email_preferences( self.user_id, can_receive_email_updates, feconf.DEFAULT_EDITOR_ROLE_EMAIL_PREFERENCE) # Note that an email is only sent when the user registers for the first # time. if feconf.CAN_SEND_EMAILS_TO_USERS and not has_ever_registered: email_manager.send_post_signup_email(self.user_id) user_services.generate_initial_profile_picture(self.user_id) self.render_json({})
def put(self): old_username = self.payload.get('old_username', None) new_username = self.payload.get('new_username', None) if old_username is None: raise self.InvalidInputException( 'Invalid request: The old username must be specified.') if new_username is None: raise self.InvalidInputException( 'Invalid request: A new username must be specified.') if not isinstance(old_username, python_utils.UNICODE): raise self.InvalidInputException( 'Expected old username to be a unicode string, received %s' % old_username) if not isinstance(new_username, python_utils.UNICODE): raise self.InvalidInputException( 'Expected new username to be a unicode string, received %s' % new_username) user_id = user_services.get_user_id_from_username(old_username) if user_id is None: raise self.InvalidInputException('Invalid username: %s' % old_username) if len(new_username) > constants.MAX_USERNAME_LENGTH: raise self.InvalidInputException( 'Expected new username to be less than %s characters, ' 'received %s' % (constants.MAX_USERNAME_LENGTH, new_username)) if user_services.is_username_taken(new_username): raise self.InvalidInputException('Username already taken.') user_services.set_username(user_id, new_username) user_services.log_username_change(self.user_id, old_username, new_username) self.render_json({})
def post(self): """Handles POST requests.""" username = self.payload.get('username') agreed_to_terms = self.payload.get('agreed_to_terms') if not isinstance(agreed_to_terms, bool) or not agreed_to_terms: raise self.InvalidInputException( 'In order to edit explorations on this site, you will ' 'need to accept the license terms.') else: user_services.record_agreement_to_terms(self.user_id) if user_services.get_username(self.user_id): # A username has already been set for this user. self.render_json({}) return try: user_services.set_username(self.user_id, username) except utils.ValidationError as e: raise self.InvalidInputException(e) self.render_json({})
def test_get_user_ids_by_role(self): user_ids = ['test1', 'test2', 'test3', 'test4'] usernames = ['name1', 'name2', 'name3', 'name4'] user_emails = [ '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**' ] for uid, email, name in zip(user_ids, user_emails, usernames): user_services.create_new_user(uid, email) user_services.set_username(uid, name) user_services.update_user_role(user_ids[0], feconf.ROLE_ID_MODERATOR) user_services.update_user_role(user_ids[1], feconf.ROLE_ID_MODERATOR) user_services.update_user_role(user_ids[2], feconf.ROLE_ID_BANNED_USER) user_services.update_user_role(user_ids[3], feconf.ROLE_ID_BANNED_USER) self.assertEqual( set(user_services.get_user_ids_by_role(feconf.ROLE_ID_MODERATOR)), set(['test1', 'test2'])) self.assertEqual( set(user_services.get_user_ids_by_role( feconf.ROLE_ID_BANNED_USER)), set(['test3', 'test4']))
def test_is_username_taken_different_case(self): user_id = 'someUser' username = '******' user_services._create_user(user_id, '*****@*****.**') user_services.set_username(user_id, username) self.assertTrue(user_services.is_username_taken('CaMeLcAsE'))
def test_is_username_taken_true(self): user_id = 'someUser' username = '******' user_services._create_user(user_id, '*****@*****.**') user_services.set_username(user_id, username) self.assertTrue(user_services.is_username_taken(username))
def test_is_username_taken_true(self): user_id = 'someUser' username = '******' user_services.get_or_create_user(user_id, '*****@*****.**') user_services.set_username(user_id, username) self.assertTrue(user_services.is_username_taken(username))
def test_set_and_get_user_email_preferences_for_exploration(self): user_id = 'someUser' exploration_id = 'someExploration' username = '******' user_email = '*****@*****.**' user_services.get_or_create_user(user_id, user_email) user_services.set_username(user_id, username) # When ExplorationUserDataModel is yet to be created, the value # of mute_feedback_notifications and mute_suggestion_notifications # should match the default values. exploration_user_model = ( user_services.user_models.ExplorationUserDataModel.get( user_id, exploration_id)) self.assertIsNone(exploration_user_model) email_preferences = user_services.get_email_preferences_for_exploration( user_id, exploration_id) self.assertEquals( email_preferences.mute_feedback_notifications, feconf.DEFAULT_FEEDBACK_NOTIFICATIONS_MUTED_PREFERENCE) self.assertEquals( email_preferences.mute_suggestion_notifications, feconf.DEFAULT_SUGGESTION_NOTIFICATIONS_MUTED_PREFERENCE) # This initializes a ExplorationUserDataModel instance with # the default mute values. user_services.set_email_preferences_for_exploration( user_id, exploration_id, feconf.DEFAULT_FEEDBACK_NOTIFICATIONS_MUTED_PREFERENCE, feconf.DEFAULT_SUGGESTION_NOTIFICATIONS_MUTED_PREFERENCE) email_preferences = user_services.get_email_preferences_for_exploration( user_id, exploration_id) self.assertEquals( email_preferences.mute_feedback_notifications, feconf.DEFAULT_FEEDBACK_NOTIFICATIONS_MUTED_PREFERENCE) self.assertEquals( email_preferences.mute_suggestion_notifications, feconf.DEFAULT_SUGGESTION_NOTIFICATIONS_MUTED_PREFERENCE) # This sets only mute_suggestion_notifications property to True. # mute_feedback_notifications should remain same as before. user_services.set_email_preferences_for_exploration( user_id, exploration_id, mute_suggestion_notifications=True) email_preferences = user_services.get_email_preferences_for_exploration( user_id, exploration_id) self.assertEquals( email_preferences.mute_feedback_notifications, feconf.DEFAULT_FEEDBACK_NOTIFICATIONS_MUTED_PREFERENCE) self.assertTrue(email_preferences.mute_suggestion_notifications) # This sets only mute_feedback_notifications property to True. # mute_suggestion_notifications should remain same as before. user_services.set_email_preferences_for_exploration( user_id, exploration_id, mute_feedback_notifications=True) email_preferences = user_services.get_email_preferences_for_exploration( user_id, exploration_id) self.assertTrue(email_preferences.mute_feedback_notifications) self.assertTrue(email_preferences.mute_suggestion_notifications)