def test_delete(self): question_id = question_services.add_question( self.new_user_id, self.question) question_services.add_question_id_to_skill( question_id, self.collection_id, self.skill_id, self.new_user_id) self.login(self.NEW_USER_EMAIL) response = self.testapp.get('/preferences') csrf_token = self.get_csrf_token_from_response(response) response = self.testapp.delete( '%s/%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id, question_id), csrf_token, expect_errors=False) self.assertEqual(response.status_int, 200) response = self.testapp.delete( '%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id), csrf_token, expect_errors=True) self.assertEqual(response.status_int, 404) response = self.testapp.delete( '%s/' % feconf.QUESTION_DATA_URL, csrf_token, expect_errors=True) self.assertEqual(response.status_int, 404) self.logout() self.login(self.random_email) response = self.testapp.get('/preferences') csrf_token = self.get_csrf_token_from_response(response) response = self.testapp.delete( '%s/%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id, question_id), csrf_token, expect_errors=True) self.assertEqual(response.status_int, 401)
def setUp(self): super(QuestionsBatchHandlerTest, self).setUp() self.collection_id = 'coll_0' self.exp_id = 'exp_1' self.owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) self.viewer_id = self.get_user_id_from_email(self.VIEWER_EMAIL) # Create a new collection and exploration. self.save_new_valid_collection(self.collection_id, self.owner_id, exploration_id=self.exp_id) # Add a skill. collection_services.update_collection( self.owner_id, self.collection_id, [{ 'cmd': collection_domain.CMD_ADD_COLLECTION_SKILL, 'name': 'test' }], 'Add a new skill') collection = collection_services.get_collection_by_id( self.collection_id) self.skill_id = collection.get_skill_id_from_skill_name('test') collection_node = collection.get_node(self.exp_id) collection_node.update_acquired_skill_ids([self.skill_id]) # Update the acquired skill IDs for the exploration. collection_services.update_collection( self.owner_id, self.collection_id, [{ 'cmd': collection_domain.CMD_EDIT_COLLECTION_NODE_PROPERTY, 'property_name': (collection_domain.COLLECTION_NODE_PROPERTY_ACQUIRED_SKILL_IDS ), # pylint: disable=line-too-long 'exploration_id': self.exp_id, 'new_value': [self.skill_id] }], 'Update skill') question = question_domain.Question( 'dummy', 'A Question', exp_domain.State.create_default_state('ABC').to_dict(), 1, self.collection_id, 'en') question_id = question_services.add_question(self.owner_id, question) self.question = question_services.get_question_by_id(question_id) question_services.add_question_id_to_skill(self.question.question_id, self.question.collection_id, self.skill_id, self.owner_id) self.signup(self.NEW_USER_EMAIL, self.NEW_USER_USERNAME) self.new_user_id = self.get_user_id_from_email(self.NEW_USER_EMAIL) collection_services.record_played_exploration_in_collection_context( self.new_user_id, self.collection_id, self.exp_id) self.payload = {}
def test_put(self): question_id = question_services.add_question( self.new_user_id, self.question) question_services.add_question_id_to_skill( question_id, self.collection_id, self.skill_id, self.new_user_id) collection_services.record_played_exploration_in_collection_context( self.new_user_id, self.collection_id, self.exp_id) payload = {} change_list = [{'cmd': 'update_question_property', 'property_name': 'title', 'new_value': 'ABC', 'old_value': 'A Question'}] payload['change_list'] = json.dumps(change_list) payload['commit_message'] = 'update title' self.login(self.NEW_USER_EMAIL) response = self.testapp.get('/preferences') csrf_token = self.get_csrf_token_from_response(response) response_json = self.put_json( '%s/%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id, question_id), payload, csrf_token, expect_errors=False) self.assertIn('question_id', response_json.keys()) del payload['change_list'] self.put_json( '%s/%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id, question_id), payload, csrf_token, expect_errors=True, expected_status_int=404) del payload['commit_message'] payload['change_list'] = json.dumps(change_list) self.put_json( '%s/%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id, question_id), payload, csrf_token, expect_errors=True, expected_status_int=404) payload['commit_message'] = 'update title' self.put_json( '%s/%s' % (feconf.QUESTION_DATA_URL, self.collection_id), payload, csrf_token, expect_errors=True, expected_status_int=404) self.logout() self.login(self.random_email) response = self.testapp.get('/preferences') csrf_token = self.get_csrf_token_from_response(response) self.put_json( '%s/%s/%s' % ( feconf.QUESTION_DATA_URL, self.collection_id, question_id), payload, csrf_token, expect_errors=True, expected_status_int=401)
def test_get_question_batch(self): coll_id_0 = '0_collection_id' exp_id_0 = '0_exploration_id' self.owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) # Create a new collection and exploration. self.save_new_valid_collection(coll_id_0, self.owner_id, exploration_id=exp_id_0) # Add a skill. collection_services.update_collection( self.owner_id, coll_id_0, [{ 'cmd': collection_domain.CMD_ADD_COLLECTION_SKILL, 'name': 'skill0' }], 'Add a new skill') collection = collection_services.get_collection_by_id(coll_id_0) skill_id = collection.get_skill_id_from_skill_name('skill0') collection_node = collection.get_node(exp_id_0) collection_node.update_acquired_skill_ids([skill_id]) # Update the acquired skill IDs for the exploration. collection_services.update_collection( self.owner_id, coll_id_0, [{ 'cmd': collection_domain.CMD_EDIT_COLLECTION_NODE_PROPERTY, 'property_name': (collection_domain.COLLECTION_NODE_PROPERTY_ACQUIRED_SKILL_IDS ), # pylint: disable=line-too-long 'exploration_id': exp_id_0, 'new_value': [skill_id] }], 'Update skill') question = question_domain.Question( 'dummy', 'A Question', exp_domain.State.create_default_state('ABC').to_dict(), 1, coll_id_0, 'en') question_id = question_services.add_question(self.owner_id, question) question = question_services.get_question_by_id(question_id) question_services.add_question_id_to_skill(question.question_id, coll_id_0, skill_id, self.owner_id) collection_services.record_played_exploration_in_collection_context( self.owner_id, coll_id_0, exp_id_0) question_batch = question_services.get_questions_batch( coll_id_0, [skill_id], self.owner_id, 1) self.assertEqual(question_batch[0].title, question.title)
def post(self): """Handles POST requests.""" if not self.payload.get('question'): raise self.PageNotFoundException if not self.payload.get('skill_id'): raise self.PageNotFoundException question = question_domain.Question.from_dict( self.payload.get('question')) skill_id = self.payload.get('skill_id') question_id = question_services.add_question(self.user_id, question) question_services.add_question_id_to_skill(question_id, question.collection_id, skill_id, self.user_id) return self.render_json({'question_id': question_id})
def test_remove_question_id_from_skill(self): """Tests to verify remove_question_id_from_skill method.""" collection_id = 'col1' exp_id = '0_exploration_id' owner_id = self.get_user_id_from_email(self.OWNER_EMAIL) # Create a new collection and exploration. self.save_new_valid_collection(collection_id, owner_id, exploration_id=exp_id) # Add a skill. collection_services.update_collection( owner_id, collection_id, [{ 'cmd': collection_domain.CMD_ADD_COLLECTION_SKILL, 'name': 'skill0' }], 'Add a new skill') state = exp_domain.State.create_default_state('ABC') question_data = state.to_dict() question_dict = { 'question_id': 'col1.random', 'title': 'abc', 'question_data': question_data, 'question_data_schema_version': 1, 'collection_id': 'col1', 'language_code': 'en' } collection = collection_services.get_collection_by_id(collection_id) skill_id = collection.get_skill_id_from_skill_name('skill0') question = question_domain.Question.from_dict(question_dict) question_services.add_question_id_to_skill(question.question_id, collection_id, skill_id, owner_id) collection = collection_services.get_collection_by_id(collection_id) self.assertIn(question.question_id, collection.skills[skill_id].question_ids) skill_id = collection.get_skill_id_from_skill_name('skill0') question_services.remove_question_id_from_skill( question.question_id, collection_id, skill_id, owner_id) collection = collection_services.get_collection_by_id(collection_id) self.assertEqual(len(collection.skills[skill_id].question_ids), 0)
def test_batch_get(self): """Tests get method of questions batch handler.""" question_id = question_services.add_question( self.owner_id, self.question) self.question = question_services.get_question_by_id(question_id) question_services.add_question_id_to_skill( self.question.question_id, self.question.collection_id, self.skill_id, self.owner_id) collection_services.record_played_exploration_in_collection_context( self.new_user_id, self.collection_id, self.exp_id) payload = {} payload['collection_id'] = self.collection_id payload['stringified_skill_ids'] = json.dumps( [self.skill_id, 'test']) self.login(self.NEW_USER_EMAIL) response_json = self.get_json( '%s/batch' % feconf.QUESTION_DATA_URL, payload, expect_errors=False) self.assertIn(self.question.to_dict(), response_json['questions_dict']) self.assertEqual(len(response_json['questions_dict']), 1) response = self.testapp.get( '%s/batch' % feconf.QUESTION_DATA_URL, expect_errors=True) self.assertEqual(response.status_int, 404) del payload['stringified_skill_ids'] response = self.testapp.get( '%s/batch' % feconf.QUESTION_DATA_URL, payload, expect_errors=True) self.assertEqual(response.status_int, 404) self.logout() self.login(self.random_email) response_json = self.testapp.get( '%s/batch' % feconf.QUESTION_DATA_URL, payload, expect_errors=True) self.assertEqual(response.status_int, 404)
def test_manager_get(self): """Tests get method of question manager handler.""" question_id = question_services.add_question( self.owner_id, self.question) self.question = question_services.get_question_by_id(question_id) question_services.add_question_id_to_skill( self.question.question_id, self.question.collection_id, self.skill_id, self.owner_id) self.login(self.NEW_USER_EMAIL) payload = {} payload['collection_id'] = self.collection_id response_json = self.get_json( '%s' % feconf.QUESTION_MANAGER_URL, payload, expect_errors=False) expected_question_summary = question_domain.QuestionSummary( self.question.question_id, self.question.title, ['test']) self.assertIn( expected_question_summary.to_dict(), response_json['question_summary_dicts']) response = self.testapp.get( '%s/batch' % feconf.QUESTION_MANAGER_URL, expect_errors=True) self.assertEqual(response.status_int, 404) del payload['collection_id'] response = self.testapp.get( '%s/batch' % feconf.QUESTION_MANAGER_URL, payload, expect_errors=True) self.assertEqual(response.status_int, 404) self.logout() self.login(self.random_email) payload['collection_id'] = self.collection_id response = self.testapp.get( '%s' % feconf.QUESTION_MANAGER_URL, payload, expect_errors=True) self.assertEqual(response.status_int, 401)