def test_editable_collection_handler_put_cannot_access(self): """Check that non-editors cannot access editable put handler""" whitelisted_usernames = [self.EDITOR_USERNAME, self.VIEWER_USERNAME] self.set_config_property( config_domain.WHITELISTED_COLLECTION_EDITOR_USERNAMES, whitelisted_usernames) # Assign viewer role to collection. rights_manager.create_new_collection_rights( self.COLLECTION_ID, self.owner_id) rights_manager.assign_role_for_collection( self.admin_id, self.COLLECTION_ID, self.viewer_id, rights_manager.ROLE_VIEWER) rights_manager.publish_collection(self.owner_id, self.COLLECTION_ID) self.login(self.VIEWER_EMAIL) # Call get handler to return the csrf token. response = self.testapp.get( '%s/%s' % (feconf.COLLECTION_URL_PREFIX, self.COLLECTION_ID)) csrf_token = self.get_csrf_token_from_response(response) # Ensure viewers do not have access to the PUT Handler. json_response = self.put_json( '%s/%s' % (feconf.EDITABLE_COLLECTION_DATA_URL_PREFIX, self.COLLECTION_ID), self.json_dict, expect_errors=True, csrf_token=csrf_token, expected_status_int=401) self.assertEqual(json_response['code'], 401) self.logout()
def test_collection_rights_handler(self): collection_id = 'collection_id' collection = collection_domain.Collection.create_default_collection( collection_id, 'A title', 'A Category', 'An Objective') collection_services.save_new_collection(self.owner_id, collection) # Check that collection is published correctly. rights_manager.assign_role_for_collection( self.owner_id, collection_id, self.editor_id, rights_manager.ROLE_EDITOR) rights_manager.publish_collection(self.owner_id, collection_id) # Check that collection cannot be unpublished by non admin. with self.assertRaisesRegexp( Exception, 'This collection cannot be unpublished.'): rights_manager.unpublish_collection(self.owner_id, collection_id) collection_rights = rights_manager.get_collection_rights(collection_id) self.assertEqual(collection_rights.status, rights_manager.ACTIVITY_STATUS_PUBLIC) # Check that collection can be unpublished by admin. rights_manager.unpublish_collection(self.admin_id, collection_id) collection_rights = rights_manager.get_collection_rights(collection_id) self.assertEqual(collection_rights.status, rights_manager.ACTIVITY_STATUS_PRIVATE)
def test_publish_or_publicize_activity_does_not_affect_featured_list(self): self._compare_lists( activity_services.get_featured_activity_references(), []) rights_manager.publish_exploration(self.owner_id, self.EXP_ID_0) self._compare_lists( activity_services.get_featured_activity_references(), []) rights_manager.publicize_exploration(self.moderator_id, self.EXP_ID_0) self._compare_lists( activity_services.get_featured_activity_references(), []) rights_manager.unpublicize_exploration( self.moderator_id, self.EXP_ID_0) self._compare_lists( activity_services.get_featured_activity_references(), []) rights_manager.publish_collection(self.owner_id, self.COL_ID_2) self._compare_lists( activity_services.get_featured_activity_references(), []) rights_manager.publicize_collection(self.moderator_id, self.COL_ID_2) self._compare_lists( activity_services.get_featured_activity_references(), []) rights_manager.unpublicize_collection( self.moderator_id, self.COL_ID_2) self._compare_lists( activity_services.get_featured_activity_references(), [])
def test_deleted_activity_is_removed_from_featured_list(self): rights_manager.publish_exploration(self.owner_id, self.EXP_ID_0) rights_manager.publish_exploration(self.owner_id, self.EXP_ID_1) rights_manager.publish_collection(self.owner_id, self.COL_ID_2) activity_services.update_featured_activity_references([ self._create_exploration_reference(self.EXP_ID_0), self._create_collection_reference(self.COL_ID_2)]) self._compare_lists( activity_services.get_featured_activity_references(), [ self._create_exploration_reference(self.EXP_ID_0), self._create_collection_reference(self.COL_ID_2)]) # Deleting an unfeatured activity does not affect the featured list. exp_services.delete_exploration(self.owner_id, self.EXP_ID_1) self._compare_lists( activity_services.get_featured_activity_references(), [ self._create_exploration_reference(self.EXP_ID_0), self._create_collection_reference(self.COL_ID_2)]) # Deleting a featured activity removes it from the featured list. collection_services.delete_collection(self.owner_id, self.COL_ID_2) self._compare_lists( activity_services.get_featured_activity_references(), [ self._create_exploration_reference(self.EXP_ID_0)]) exp_services.delete_exploration(self.owner_id, self.EXP_ID_0) self._compare_lists( activity_services.get_featured_activity_references(), [])
def test_published_collections_are_visible_to_logged_out_users(self): rights_manager.publish_collection(self.editor_id, self.COLLECTION_ID) response = self.testapp.get( '%s/%s' % (feconf.COLLECTION_URL_PREFIX, self.COLLECTION_ID), expect_errors=True) self.assertEqual(response.status_int, 200)
def test_editable_collection_handler_put_can_access(self): """Check that editors can access put handler""" whitelisted_usernames = [self.EDITOR_USERNAME, self.VIEWER_USERNAME] self.set_config_property( config_domain.WHITELISTED_COLLECTION_EDITOR_USERNAMES, whitelisted_usernames) rights_manager.create_new_collection_rights( self.COLLECTION_ID, self.owner_id) rights_manager.assign_role_for_collection( self.admin_id, self.COLLECTION_ID, self.editor_id, rights_manager.ROLE_EDITOR) rights_manager.publish_collection(self.owner_id, self.COLLECTION_ID) self.login(self.EDITOR_EMAIL) # Call get handler to return the csrf token. response = self.testapp.get( '%s/%s' % (feconf.COLLECTION_URL_PREFIX, self.COLLECTION_ID)) csrf_token = self.get_csrf_token_from_response(response) json_response = self.put_json( '%s/%s' % (feconf.EDITABLE_COLLECTION_DATA_URL_PREFIX, self.COLLECTION_ID), self.json_dict, csrf_token=csrf_token) self.assertEqual(self.COLLECTION_ID, json_response['collection']['id']) self.assertEqual(2, json_response['collection']['version']) self.logout()
def test_published_collections_are_visible_to_logged_in_users(self): rights_manager.publish_collection(self.EDITOR_ID, self.COLLECTION_ID) self.login(self.NEW_USER_EMAIL) response = self.testapp.get( '%s/%s' % (feconf.COLLECTION_URL_PREFIX, self.COLLECTION_ID), expect_errors=True) self.assertEqual(response.status_int, 200)
def publish_collection(self, owner_id, collection_id): """Publish the collection with the given collection_id. Args: owner_id: str. The user_id of the owner of the collection. collection_id: str. ID of the collection to be published. """ committer = user_services.UserActionsInfo(owner_id) rights_manager.publish_collection(committer, collection_id)
def test_updating_with_duplicate_refs_raises_exception(self) -> None: rights_manager.publish_exploration(self.owner, self.EXP_ID_0) # type: ignore[no-untyped-call] rights_manager.publish_collection(self.owner, self.COL_ID_2) # type: ignore[no-untyped-call] self._compare_lists( activity_services.get_featured_activity_references(), []) with self.assertRaisesRegex(Exception, 'should not have duplicates'): # type: ignore[no-untyped-call] activity_services.update_featured_activity_references([ self._create_exploration_reference(self.EXP_ID_0), self._create_exploration_reference(self.EXP_ID_0)])
def test_updating_with_duplicate_refs_raises_exception(self): rights_manager.publish_exploration(self.owner_id, self.EXP_ID_0) rights_manager.publish_collection(self.owner_id, self.COL_ID_2) self._compare_lists( activity_services.get_featured_activity_references(), []) with self.assertRaisesRegexp(Exception, 'should not have duplicates'): activity_services.update_featured_activity_references([ self._create_exploration_reference(self.EXP_ID_0), self._create_exploration_reference(self.EXP_ID_0)])
def test_publish_activity_does_not_affect_featured_list(self): self._compare_lists( activity_services.get_featured_activity_references(), []) rights_manager.publish_exploration(self.owner, self.EXP_ID_0) self._compare_lists( activity_services.get_featured_activity_references(), []) rights_manager.publish_collection(self.owner, self.COL_ID_2) self._compare_lists( activity_services.get_featured_activity_references(), [])
def test_can_publicize_collection(self): self.save_new_default_collection(self.COLLECTION_ID, self.user_id_a) rights_manager.publish_collection(self.user_id_a, self.COLLECTION_ID) self.assertFalse( rights_manager.Actor(self.user_id_a).can_publicize( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) self.assertTrue( rights_manager.Actor(self.user_id_admin).can_publicize( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID))
def test_publish_activity_does_not_affect_featured_list(self) -> None: self._compare_lists( activity_services.get_featured_activity_references(), []) rights_manager.publish_exploration(self.owner, self.EXP_ID_0) # type: ignore[no-untyped-call] self._compare_lists( activity_services.get_featured_activity_references(), []) rights_manager.publish_collection(self.owner, self.COL_ID_2) # type: ignore[no-untyped-call] self._compare_lists( activity_services.get_featured_activity_references(), [])
def test_editable_collection_handler_put_with_invalid_payload_version( self): whitelisted_usernames = [self.EDITOR_USERNAME, self.VIEWER_USERNAME] self.set_collection_editors(whitelisted_usernames) rights_manager.create_new_collection_rights(self.COLLECTION_ID, self.owner_id) rights_manager.assign_role_for_collection(self.moderator, self.COLLECTION_ID, self.editor_id, rights_domain.ROLE_EDITOR) rights_manager.publish_collection(self.owner, self.COLLECTION_ID) self.login(self.EDITOR_EMAIL) # Call get handler to return the csrf token. csrf_token = self.get_new_csrf_token() # Raises error as version is None. sample_change_list = [{ 'cmd': 'edit_collection_property', 'property_name': 'title', 'new_value': 'A new title' }] json_response = self.put_json( '%s/%s' % (feconf.COLLECTION_EDITOR_DATA_URL_PREFIX, self.COLLECTION_ID), { 'version': None, 'change_list': sample_change_list }, csrf_token=csrf_token, expected_status_int=400) self.assertEqual(json_response['error'], 'Invalid POST request: a version must be specified.') # Raises error as version from payload does not match the collection # version. json_response = self.put_json( '%s/%s' % (feconf.COLLECTION_EDITOR_DATA_URL_PREFIX, self.COLLECTION_ID), { 'version': 2, 'change_list': sample_change_list }, csrf_token=csrf_token, expected_status_int=400) self.assertEqual( json_response['error'], 'Trying to update version 1 of collection from version 2, ' 'which is too old. Please reload the page and try again.') self.logout()
def setUp(self): super(LearnerProgressTest, self).setUp() self.signup(self.USER_EMAIL, self.USER_USERNAME) self.user_id = self.get_user_id_from_email(self.USER_EMAIL) self.signup(self.OWNER_EMAIL, self.OWNER_USERNAME) self.owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) # Save and publish explorations. self.save_new_valid_exploration( self.EXP_ID_0, self.owner_id, title='Bridges in England', category='Architecture', language_code='en') self.save_new_valid_exploration( self.EXP_ID_1, self.owner_id, title='Welcome to Gadgets', category='Architecture', language_code='fi') self.save_new_valid_exploration( self.EXP_ID_1_0, self.owner_id, title='Sillat Suomi', category='Architecture', language_code='fi') self.save_new_valid_exploration( self.EXP_ID_1_1, self.owner_id, title='Introduce Interactions in Oppia', category='Welcome', language_code='en') rights_manager.publish_exploration(self.owner_id, self.EXP_ID_0) rights_manager.publish_exploration(self.owner_id, self.EXP_ID_1) rights_manager.publish_exploration(self.owner_id, self.EXP_ID_1_0) rights_manager.publish_exploration(self.owner_id, self.EXP_ID_1_1) # Save a new collection. self.save_new_default_collection( self.COL_ID_0, self.owner_id, title='Welcome', category='Architecture') self.save_new_default_collection( self.COL_ID_1, self.owner_id, title='Bridges in England', category='Architecture') # Add two explorations to the previously saved collection and publish # it. for exp_id in [self.EXP_ID_1_0, self.EXP_ID_1_1]: collection_services.update_collection( self.owner_id, self.COL_ID_1, [{ 'cmd': collection_domain.CMD_ADD_COLLECTION_NODE, 'exploration_id': exp_id }], 'Added new exploration') # Publish the collections. rights_manager.publish_collection(self.owner_id, self.COL_ID_0) rights_manager.publish_collection(self.owner_id, self.COL_ID_1)
def test_update_featured_refs_correctly_promotes_activities(self) -> None: rights_manager.publish_exploration(self.owner, self.EXP_ID_0) # type: ignore[no-untyped-call] rights_manager.publish_collection(self.owner, self.COL_ID_2) # type: ignore[no-untyped-call] self._compare_lists( activity_services.get_featured_activity_references(), []) activity_services.update_featured_activity_references([ self._create_exploration_reference(self.EXP_ID_0), self._create_collection_reference(self.COL_ID_2)]) self._compare_lists( activity_services.get_featured_activity_references(), [ self._create_exploration_reference(self.EXP_ID_0), self._create_collection_reference(self.COL_ID_2)])
def test_update_featured_refs_correctly_promotes_activities(self): rights_manager.publish_exploration(self.owner_id, self.EXP_ID_0) rights_manager.publish_collection(self.owner_id, self.COL_ID_2) self._compare_lists( activity_services.get_featured_activity_references(), []) activity_services.update_featured_activity_references([ self._create_exploration_reference(self.EXP_ID_0), self._create_collection_reference(self.COL_ID_2)]) self._compare_lists( activity_services.get_featured_activity_references(), [ self._create_exploration_reference(self.EXP_ID_0), self._create_collection_reference(self.COL_ID_2)])
def test_requiring_public_activities_to_be_public_succeeds(self): self.save_new_valid_exploration(self.EXP_ID_0, self.owner_id) self.save_new_valid_collection( self.COL_ID_2, self.owner_id, exploration_id=self.EXP_ID_0) rights_manager.publish_exploration(self.owner_id, self.EXP_ID_0) rights_manager.publish_collection(self.owner_id, self.COL_ID_2) # There are no validation errors. summary_services.require_activities_to_be_public([ activity_domain.ActivityReference( feconf.ACTIVITY_TYPE_EXPLORATION, self.EXP_ID_0), activity_domain.ActivityReference( feconf.ACTIVITY_TYPE_COLLECTION, self.COL_ID_2)])
def test_requiring_public_activities_to_be_public_succeeds(self): self.save_new_valid_exploration(self.EXP_ID_0, self.owner_id) self.save_new_valid_collection( self.COL_ID_2, self.owner_id, exploration_id=self.EXP_ID_0) rights_manager.publish_exploration(self.owner, self.EXP_ID_0) rights_manager.publish_collection(self.owner, self.COL_ID_2) # There are no validation errors. summary_services.require_activities_to_be_public([ activity_domain.ActivityReference( constants.ACTIVITY_TYPE_EXPLORATION, self.EXP_ID_0), activity_domain.ActivityReference( constants.ACTIVITY_TYPE_COLLECTION, self.COL_ID_2)])
def publish_collection_and_update_user_profiles(committer_id, col_id): """Publishes the collection with publish_collection() function in rights_manager.py, as well as updates first_contribution_msec. It is the responsibility of the caller to check that the collection is valid prior to publication. """ rights_manager.publish_collection(committer_id, col_id) contribution_time_msec = utils.get_current_time_in_millisecs() collection_summary = get_collection_summary_by_id(col_id) contributor_ids = collection_summary.contributor_ids for contributor in contributor_ids: user_services.update_first_contribution_msec_if_not_set( contributor, contribution_time_msec)
def test_get_displayable_collection_summary_dicts_matching_ids(self): collection_id_1 = self.COLLECTION_ID + '_1' self.save_new_valid_collection(self.COLLECTION_ID, self.owner_id) self.save_new_valid_collection(collection_id_1, self.owner_id) rights_manager.publish_collection(self.owner, self.COLLECTION_ID) rights_manager.publish_collection(self.owner, collection_id_1) collection_id_list = [collection_id_1, self.COLLECTION_ID] collection_summaries = ( summary_services. get_displayable_collection_summary_dicts_matching_ids( collection_id_list)) self.assertEqual(len(collection_summaries), 2) for collection_summary in collection_summaries: self.assertIn(collection_summary['id'], collection_id_list)
def setUp(self): super(UserSubscriptionsModelValidatorTests, self).setUp() self.signup(self.OWNER_EMAIL, self.OWNER_USERNAME) self.signup(USER_EMAIL, USER_NAME) self.owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) self.user_id = self.get_user_id_from_email(USER_EMAIL) self.owner = user_services.UserActionsInfo(self.owner_id) explorations = [exp_domain.Exploration.create_default_exploration( '%s' % i, title='title %d' % i, category='category%d' % i ) for i in xrange(3)] for exp in explorations: exp_services.save_new_exploration(self.owner_id, exp) rights_manager.publish_exploration(self.owner, exp.id) collections = [collection_domain.Collection.create_default_collection( '%s' % i, title='title %d' % i, category='category%d' % i ) for i in xrange(3, 6)] for collection in collections: collection_services.save_new_collection(self.owner_id, collection) rights_manager.publish_collection(self.owner, collection.id) thread_id = feedback_services.create_thread( 'exploration', 'exp_id', None, 'a subject', 'some text') subscription_services.subscribe_to_thread( self.user_id, thread_id) subscription_services.subscribe_to_creator(self.user_id, self.owner_id) for exp in explorations: subscription_services.subscribe_to_exploration( self.user_id, exp.id) for collection in collections: subscription_services.subscribe_to_collection( self.user_id, collection.id) self.process_and_flush_pending_tasks() prod_validation_jobs_one_off.MODEL_TO_VALIDATOR_MAPPING = { user_models.UserSubscriptionsModel: prod_validation_jobs_one_off.UserSubscriptionsModelValidator, }
def test_get_learner_collection_dict_by_id_without_user_id(self): self.save_new_valid_exploration(self.EXP_ID, self.owner_id) self.save_new_valid_collection(self.COLLECTION_ID, self.owner_id, exploration_id=self.EXP_ID) rights_manager.publish_exploration(self.owner, self.EXP_ID) rights_manager.publish_collection(self.owner, self.COLLECTION_ID) mock_user = user_services.get_user_actions_info(None) collection_dict = (summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, mock_user)) self.assertEqual( len(collection_dict['playthrough_dict'] ['completed_exploration_ids']), 0) self.assertEqual( collection_dict['playthrough_dict']['next_exploration_id'], self.EXP_ID)
def test_can_only_delete_unpublished_collections(self): self.save_new_default_collection(self.COLLECTION_ID, self.user_id_a) self.assertTrue( rights_manager.Actor(self.user_id_a).can_delete(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) ) rights_manager.publish_collection(self.user_id_a, self.COLLECTION_ID) self.assertFalse( rights_manager.Actor(self.user_id_a).can_delete(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) ) rights_manager.unpublish_collection(self.user_id_admin, self.COLLECTION_ID) self.assertTrue( rights_manager.Actor(self.user_id_a).can_delete(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) )
def test_can_only_delete_unpublished_collections(self): self.save_new_default_collection(self.COLLECTION_ID, self.user_id_a) self.assertTrue( rights_manager.Actor(self.user_id_a).can_delete( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) rights_manager.publish_collection(self.user_id_a, self.COLLECTION_ID) self.assertFalse( rights_manager.Actor(self.user_id_a).can_delete( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) rights_manager.unpublish_collection( self.user_id_admin, self.COLLECTION_ID) self.assertTrue( rights_manager.Actor(self.user_id_a).can_delete( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID))
def test_can_only_delete_unpublished_collections(self): self.save_new_default_collection(self.COLLECTION_ID, self.user_id_a) collection_rights = rights_manager.get_collection_rights( self.COLLECTION_ID) self.assertTrue(rights_manager.check_can_delete_activity( self.user_a, collection_rights)) rights_manager.publish_collection(self.user_a, self.COLLECTION_ID) collection_rights = rights_manager.get_collection_rights( self.COLLECTION_ID) self.assertFalse(rights_manager.check_can_delete_activity( self.user_a, collection_rights)) rights_manager.unpublish_collection( self.user_admin, self.COLLECTION_ID) collection_rights = rights_manager.get_collection_rights( self.COLLECTION_ID) self.assertTrue(rights_manager.check_can_delete_activity( self.user_a, collection_rights))
def test_get_collection_rights(self): whitelisted_usernames = [self.OWNER_USERNAME] self.set_collection_editors(whitelisted_usernames) self.login(self.OWNER_EMAIL) collection_id = 'collection_id' collection = collection_domain.Collection.create_default_collection( collection_id, 'A title', 'A Category', 'An Objective') collection_services.save_new_collection(self.owner_id, collection) # Check that collection is published correctly. rights_manager.publish_collection(self.owner, collection_id) json_response = self.get_json( '%s/%s' % (feconf.COLLECTION_RIGHTS_PREFIX, self.COLLECTION_ID)) self.assertTrue(json_response['can_edit']) self.assertFalse(json_response['can_unpublish']) self.assertEqual(self.COLLECTION_ID, json_response['collection_id']) self.assertFalse(json_response['is_private']) self.logout()
def test_get_learner_dict_when_referencing_inaccessible_explorations(self): self.save_new_default_collection(self.COLLECTION_ID, self.owner_id) self.save_new_valid_exploration(self.EXP_ID, self.editor_id) collection_services.update_collection( self.owner_id, self.COLLECTION_ID, [{ 'cmd': collection_domain.CMD_ADD_COLLECTION_NODE, 'exploration_id': self.EXP_ID }], 'Added another creator\'s private exploration') # A collection cannot access someone else's private exploration. rights_manager.publish_collection(self.owner, self.COLLECTION_ID) with self.assertRaisesRegex( utils.ValidationError, 'Expected collection to only reference valid explorations, but ' 'found an exploration with ID: exploration_id'): summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner) # After the exploration is published, the dict can now be created. rights_manager.publish_exploration(self.editor, self.EXP_ID) summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner)
def test_get_learner_dict_when_referencing_inaccessible_explorations(self): self.save_new_default_collection(self.COLLECTION_ID, self.owner_id) self.save_new_valid_exploration(self.EXP_ID, self.editor_id) collection_services.update_collection( self.owner_id, self.COLLECTION_ID, [{ 'cmd': collection_domain.CMD_ADD_COLLECTION_NODE, 'exploration_id': self.EXP_ID }], 'Added another creator\'s private exploration') # A collection cannot access someone else's private exploration. rights_manager.publish_collection(self.owner_id, self.COLLECTION_ID) with self.assertRaisesRegexp( utils.ValidationError, 'Expected collection to only reference valid explorations, but ' 'found an exploration with ID: exploration_id'): summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner_id) # After the exploration is published, the dict can now be created. rights_manager.publish_exploration(self.editor_id, self.EXP_ID) summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner_id)
def test_unpublished_activity_is_removed_from_featured_list(self) -> None: rights_manager.publish_exploration( self.owner, self.EXP_ID_0) # type: ignore[no-untyped-call] rights_manager.publish_exploration( self.owner, self.EXP_ID_1) # type: ignore[no-untyped-call] rights_manager.publish_collection( self.owner, self.COL_ID_2) # type: ignore[no-untyped-call] activity_services.update_featured_activity_references([ self._create_exploration_reference(self.EXP_ID_0), self._create_collection_reference(self.COL_ID_2) ]) self._compare_lists( activity_services.get_featured_activity_references(), [ self._create_exploration_reference(self.EXP_ID_0), self._create_collection_reference(self.COL_ID_2) ]) # Unpublishing an unfeatured activity does not affect the featured # list. rights_manager.unpublish_exploration( self.moderator, self.EXP_ID_1) # type: ignore[no-untyped-call] self._compare_lists( activity_services.get_featured_activity_references(), [ self._create_exploration_reference(self.EXP_ID_0), self._create_collection_reference(self.COL_ID_2) ]) # Unpublishing a featured activity removes it from the featured list. rights_manager.unpublish_collection( self.moderator, self.COL_ID_2) # type: ignore[no-untyped-call] self._compare_lists( activity_services.get_featured_activity_references(), [self._create_exploration_reference(self.EXP_ID_0)]) rights_manager.unpublish_exploration( self.moderator, self.EXP_ID_0) # type: ignore[no-untyped-call] self._compare_lists( activity_services.get_featured_activity_references(), [])
def test_publishing_and_unpublishing_collection(self): self.save_new_default_collection(self.COLLECTION_ID, self.user_id_a) self.assertFalse( rights_manager.Actor(self.user_id_b).can_play(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) ) self.assertFalse( rights_manager.Actor(self.user_id_b).can_view(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) ) rights_manager.publish_collection(self.user_id_a, self.COLLECTION_ID) self.assertTrue( rights_manager.Actor(self.user_id_b).can_play(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) ) self.assertTrue( rights_manager.Actor(self.user_id_b).can_view(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) ) self.assertFalse( rights_manager.Actor(self.user_id_a).can_unpublish( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID ) ) rights_manager.unpublish_collection(self.user_id_admin, self.COLLECTION_ID) self.assertTrue( rights_manager.Actor(self.user_id_a).can_play(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) ) self.assertTrue( rights_manager.Actor(self.user_id_a).can_view(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) ) self.assertFalse( rights_manager.Actor(self.user_id_b).can_play(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) ) self.assertFalse( rights_manager.Actor(self.user_id_b).can_view(rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID) )
def test_get_learner_dict_with_allowed_private_exps(self): self.save_new_valid_collection(self.COLLECTION_ID, self.owner_id, exploration_id=self.EXP_ID) self.save_new_valid_exploration(self.EXP_ID_1, self.editor_id) collection_services.update_collection( self.owner_id, self.COLLECTION_ID, [{ 'cmd': collection_domain.CMD_ADD_COLLECTION_NODE, 'exploration_id': self.EXP_ID_1 }], 'Added another creator\'s private exploration') rights_manager.publish_collection(self.owner, self.COLLECTION_ID) collection_dict = summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner, allow_invalid_explorations=True) # The author's private exploration will be contained in the public # collection since invalid explorations are being allowed, but the # private exploration of another author will not. collection_node_dicts = collection_dict['nodes'] self.assertEqual(collection_node_dicts[0]['exploration_summary']['id'], self.EXP_ID) self.assertIsNone(collection_node_dicts[1]['exploration_summary'])
def setUp(self): super(CheckCanUnpublishActivityTest, self).setUp() self.signup(self.OWNER_EMAIL, self.OWNER_USERNAME) self.signup(self.ADMIN_EMAIL, self.ADMIN_USERNAME) self.signup(self.MODERATOR_EMAIL, self.MODERATOR_USERNAME) self.admin_id = self.get_user_id_from_email(self.ADMIN_EMAIL) self.moderator_id = self.get_user_id_from_email(self.MODERATOR_EMAIL) self.set_admins([self.ADMIN_USERNAME]) self.set_moderators([self.MODERATOR_USERNAME]) self.owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) self.admin = user_services.UserActionsInfo(self.admin_id) self.owner = user_services.UserActionsInfo(self.owner_id) self.moderator = user_services.UserActionsInfo(self.moderator_id) self.save_new_valid_exploration(self.published_exp_id, self.owner_id) self.save_new_valid_exploration(self.private_exp_id, self.owner_id) self.save_new_valid_collection(self.published_col_id, self.owner_id, exploration_id=self.published_col_id) self.save_new_valid_collection(self.private_col_id, self.owner_id, exploration_id=self.private_col_id) rights_manager.publish_exploration(self.owner, self.published_exp_id) rights_manager.publish_collection(self.owner, self.published_col_id)
def setUp(self): super(PlayCollectionDecoratorTest, self).setUp() self.signup(self.OWNER_EMAIL, self.OWNER_USERNAME) self.signup(self.ADMIN_EMAIL, self.ADMIN_USERNAME) self.signup(self.user_email, self.username) self.owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) self.set_admins([self.ADMIN_USERNAME]) self.owner = user_services.UserActionsInfo(self.owner_id) self.testapp = webtest.TestApp( webapp2.WSGIApplication( [webapp2.Route('/mock/<collection_id>', self.MockHandler)], debug=feconf.DEBUG, )) self.save_new_valid_exploration(self.published_exp_id, self.owner_id) self.save_new_valid_exploration(self.private_exp_id, self.owner_id) self.save_new_valid_collection(self.published_col_id, self.owner_id, exploration_id=self.published_col_id) self.save_new_valid_collection(self.private_col_id, self.owner_id, exploration_id=self.private_col_id) rights_manager.publish_exploration(self.owner, self.published_exp_id) rights_manager.publish_collection(self.owner, self.published_col_id)
def test_get_learner_dict_with_private_exp_fails_validation(self): self.save_new_valid_collection( self.COLLECTION_ID, self.owner_id, exploration_id=self.EXP_ID) # Since both the collection and exploration are private, the learner # dict can be created. summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner) # A public collection referencing a private exploration is bad, however. rights_manager.publish_collection(self.owner, self.COLLECTION_ID) with self.assertRaisesRegex( utils.ValidationError, 'Cannot reference a private exploration within a public ' 'collection, exploration ID: exploration_id'): summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner) # After the exploration is published, the learner dict can be crated # again. rights_manager.publish_exploration(self.owner, self.EXP_ID) summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner)
def test_get_learner_dict_with_allowed_private_exps(self): self.save_new_valid_collection( self.COLLECTION_ID, self.owner_id, exploration_id=self.EXP_ID) self.save_new_valid_exploration(self.EXP_ID_1, self.editor_id) collection_services.update_collection( self.owner_id, self.COLLECTION_ID, [{ 'cmd': collection_domain.CMD_ADD_COLLECTION_NODE, 'exploration_id': self.EXP_ID_1 }], 'Added another creator\'s private exploration') rights_manager.publish_collection(self.owner_id, self.COLLECTION_ID) collection_dict = summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner_id, allow_invalid_explorations=True) # The author's private exploration will be contained in the public # collection since invalid explorations are being allowed, but the # private exploration of another author will not. collection_node_dicts = collection_dict['nodes'] self.assertEqual( collection_node_dicts[0]['exploration_summary']['id'], self.EXP_ID) self.assertIsNone(collection_node_dicts[1]['exploration_summary'])
def test_get_learner_dict_with_private_exp_fails_validation(self): self.save_new_valid_collection( self.COLLECTION_ID, self.owner_id, exploration_id=self.EXP_ID) # Since both the collection and exploration are private, the learner # dict can be created. summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner_id) # A public collection referencing a private exploration is bad, however. rights_manager.publish_collection(self.owner_id, self.COLLECTION_ID) with self.assertRaisesRegexp( utils.ValidationError, 'Cannot reference a private exploration within a public ' 'collection, exploration ID: exploration_id'): summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner_id) # After the exploration is published, the learner dict can be crated # again. rights_manager.publish_exploration(self.owner_id, self.EXP_ID) summary_services.get_learner_collection_dict_by_id( self.COLLECTION_ID, self.owner_id)
def test_editable_collection_handler_put_can_access(self): """Check that editors can access put handler.""" whitelisted_usernames = [self.EDITOR_USERNAME, self.VIEWER_USERNAME] self.set_collection_editors(whitelisted_usernames) rights_manager.create_new_collection_rights( self.COLLECTION_ID, self.owner_id) rights_manager.assign_role_for_collection( self.admin, self.COLLECTION_ID, self.editor_id, rights_manager.ROLE_EDITOR) rights_manager.publish_collection(self.owner, self.COLLECTION_ID) self.login(self.EDITOR_EMAIL) # Call get handler to return the csrf token. response = self.get_html_response( '%s/%s' % ( feconf.COLLECTION_URL_PREFIX, self.COLLECTION_ID)) csrf_token = self.get_csrf_token_from_response(response) json_response = self.put_json( '%s/%s' % ( feconf.COLLECTION_EDITOR_DATA_URL_PREFIX, self.COLLECTION_ID), self.json_dict, csrf_token=csrf_token) self.assertEqual(self.COLLECTION_ID, json_response['collection']['id']) self.assertEqual(2, json_response['collection']['version']) # Check that title is changed. response = self.get_html_response( '%s/%s' % ( feconf.COLLECTION_EDITOR_URL_PREFIX, self.COLLECTION_ID)) response.mustcontain(self.json_dict['change_list'][0]['new_value']) self.logout()
def test_publishing_and_unpublishing_collection(self): self.save_new_default_collection(self.COLLECTION_ID, self.user_id_a) self.assertFalse( rights_manager.Actor(self.user_id_b).can_play( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) self.assertFalse( rights_manager.Actor(self.user_id_b).can_view( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) rights_manager.publish_collection(self.user_id_a, self.COLLECTION_ID) self.assertTrue( rights_manager.Actor(self.user_id_b).can_play( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) self.assertTrue( rights_manager.Actor(self.user_id_b).can_view( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) self.assertFalse( rights_manager.Actor(self.user_id_a).can_unpublish( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) rights_manager.unpublish_collection( self.user_id_admin, self.COLLECTION_ID) self.assertTrue( rights_manager.Actor(self.user_id_a).can_play( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) self.assertTrue( rights_manager.Actor(self.user_id_a).can_view( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) self.assertFalse( rights_manager.Actor(self.user_id_b).can_play( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID)) self.assertFalse( rights_manager.Actor(self.user_id_b).can_view( rights_manager.ACTIVITY_TYPE_COLLECTION, self.COLLECTION_ID))
def load_demo(collection_id): """Loads a demo collection. The resulting collection will have version 2 (one for its initial creation and one for its subsequent modification.) """ delete_demo(collection_id) if not collection_domain.Collection.is_demo_collection_id(collection_id): raise Exception('Invalid demo collection id %s' % collection_id) demo_filepath = os.path.join( feconf.SAMPLE_COLLECTIONS_DIR, feconf.DEMO_COLLECTIONS[collection_id]) if demo_filepath.endswith('yaml'): yaml_content = utils.get_file_contents(demo_filepath) else: raise Exception('Unrecognized file path: %s' % demo_filepath) collection = save_new_collection_from_yaml( feconf.SYSTEM_COMMITTER_ID, yaml_content, collection_id) rights_manager.publish_collection( feconf.SYSTEM_COMMITTER_ID, collection_id) index_collections_given_ids([collection_id]) # Now, load all of the demo explorations that are part of the collection. for collection_node in collection.nodes: exp_id = collection_node.exploration_id # Only load the demo exploration if it is not yet loaded. if exp_services.get_exploration_by_id(exp_id, strict=False) is None: exp_services.load_demo(exp_id) logging.info('Collection with id %s was loaded.' % collection_id)
def test_get_collection_rights(self): whitelisted_usernames = [self.OWNER_USERNAME] self.set_config_property( config_domain.WHITELISTED_COLLECTION_EDITOR_USERNAMES, whitelisted_usernames) self.login(self.OWNER_EMAIL) collection_id = 'collection_id' collection = collection_domain.Collection.create_default_collection( collection_id, 'A title', 'A Category', 'An Objective') collection_services.save_new_collection(self.owner_id, collection) # Check that collection is published correctly. rights_manager.publish_collection(self.owner_id, collection_id) json_response = self.get_json( '%s/%s' % (feconf.COLLECTION_RIGHTS_PREFIX, self.COLLECTION_ID)) self.assertTrue(json_response['can_edit']) self.assertFalse(json_response['can_unpublish']) self.assertEqual(self.COLLECTION_ID, json_response['collection_id']) self.assertFalse(json_response['is_private']) self.logout()
def test_published_collections_are_visible_to_logged_in_users(self): rights_manager.publish_collection(self.editor, self.COLLECTION_ID) self.login(self.NEW_USER_EMAIL) self.get_html_response( '%s/%s' % (feconf.COLLECTION_URL_PREFIX, self.COLLECTION_ID))