def test_data_extraction(self):

        # Register a student and save some form values for that student
        student = self.register()

        entity = StudentFormEntity.load_or_default(student, 'form-0')
        entity.value = transforms.dumps({
            u'form_data': self.FORM_0_DATA})
        entity.put()

        entity = StudentFormEntity.load_or_default(student, u'form-1')
        entity.value = transforms.dumps({
            u'form_data': self.FORM_1_DATA})
        entity.put()

        entity = StudentFormEntity.load_or_default(student, u'form-2')
        entity.value = transforms.dumps({
            u'form_data': self.FORM_2_DATA})
        entity.put()

        # Log in as admin for the data query
        actions.logout()
        actions.login(ADMIN_EMAIL, is_admin=True)

        xsrf_token = crypto.XsrfTokenManager.create_xsrf_token(
            data_sources_utils.DATA_SOURCE_ACCESS_XSRF_ACTION)

        pii_secret = crypto.generate_transform_secret_from_xsrf_token(
            xsrf_token, data_sources_utils.DATA_SOURCE_ACCESS_XSRF_ACTION)

        safe_user_id = crypto.hmac_sha_2_256_transform(
            pii_secret, student.user_id)

        response = self.get(
            'rest/data/questionnaire_responses/items?'
            'data_source_token=%s&page_number=0' % xsrf_token)
        data = transforms.loads(response.body)['data']

        self.assertEqual(3, len(data))

        for index in range(3):
            self.assertIn(safe_user_id, data[index]['user_id'])
            self.assertEqual('form-%s' % index, data[index]['questionnaire_id'])

        self.assertEqual(self.FORM_0_DATA, data[0]['form_data'])
        self.assertEqual(self.FORM_1_DATA, data[1]['form_data'])
        self.assertEqual(self.FORM_2_DATA_OUT, data[2]['form_data'])
    def test_data_extraction(self):

        # Register a student and save some form values for that student
        student = self.register()

        entity = StudentFormEntity.load_or_create(student, 'form-0')
        entity.value = transforms.dumps({u'form_data': self.FORM_0_DATA})
        entity.put()

        entity = StudentFormEntity.load_or_create(student, u'form-1')
        entity.value = transforms.dumps({u'form_data': self.FORM_1_DATA})
        entity.put()

        entity = StudentFormEntity.load_or_create(student, u'form-2')
        entity.value = transforms.dumps({u'form_data': self.FORM_2_DATA})
        entity.put()

        # Log in as admin for the data query
        actions.logout()
        actions.login(ADMIN_EMAIL, is_admin=True)

        xsrf_token = crypto.XsrfTokenManager.create_xsrf_token(
            data_sources_utils.DATA_SOURCE_ACCESS_XSRF_ACTION)

        pii_secret = crypto.generate_transform_secret_from_xsrf_token(
            xsrf_token, data_sources_utils.DATA_SOURCE_ACCESS_XSRF_ACTION)

        safe_user_id = crypto.hmac_sha_2_256_transform(pii_secret,
                                                       student.user_id)

        response = self.get('rest/data/questionnaire_responses/items?'
                            'data_source_token=%s&page_number=0' % xsrf_token)
        data = transforms.loads(response.body)['data']

        self.assertEqual(3, len(data))

        for index in range(3):
            self.assertIn(safe_user_id, data[index]['user_id'])
            self.assertEqual('form-%s' % index,
                             data[index]['questionnaire_id'])

        self.assertEqual(self.FORM_0_DATA, data[0]['form_data'])
        self.assertEqual(self.FORM_1_DATA, data[1]['form_data'])
        self.assertEqual(self.FORM_2_DATA_OUT, data[2]['form_data'])
 def test_form_data_in_datastore(self):
     student = self.register()
     self._put_data_in_datastore(student)
     response = StudentFormEntity.load_or_create(student, UNIQUE_FORM_ID)
     self.assertNotEqual(None, response)
 def _put_data_in_datastore(self, student):
     data = StudentFormEntity.load_or_create(student, UNIQUE_FORM_ID)
     data.value = transforms.dumps(self.PAYLOAD_DICT)
     data.put()
     return data.value
 def test_form_data_in_datastore(self):
     student = self.register()
     self._put_data_in_datastore(student)
     response = StudentFormEntity.load_or_create(student, UNIQUE_FORM_ID)
     self.assertNotEqual(None, response)
 def _put_data_in_datastore(self, student):
     data = StudentFormEntity.load_or_create(student, UNIQUE_FORM_ID)
     data.value = transforms.dumps(self.PAYLOAD_DICT)
     data.put()
     return data.value