Esempio n. 1
0
    def test_download_exercise_csv(self):
        # Given a patched exercise
        instrument = InstrumentModel()
        seft_file = SEFTModel(instrument_id=instrument.instrument_id,
                              file_name="file_name",
                              data="test_data",
                              length=999)
        instrument.seft_file = seft_file
        exercise = ExerciseModel(
            exercise_id="cb0711c3-0ac8-41d3-ae0e-567e5ea1ef87")
        business = BusinessModel(ru_ref="test_ru_ref")
        instrument.exercises.append(exercise)
        instrument.businesses.append(business)

        with patch(
                "application.controllers.collection_instrument.query_exercise_by_id",
                return_value=exercise):
            # When a call is made to the download_csv end point
            response = self.client.get(
                "/collection-instrument-api/1.0.2/download_csv/cb0711c3-0ac8-41d3-ae0e-567e5ea1ef87",
                headers=self.get_auth_headers(),
            )

            # Then the response contains the correct data
            self.assertStatus(response, 200)
            self.assertIn('"Count","File Name","Length","Time Stamp"',
                          response.data.decode())
            self.assertIn('"1","file_name","999"', response.data.decode())
 def add_instrument_data(session=None):
     instrument = InstrumentModel(ci_type="SEFT")
     seft_file = SEFTModel(instrument_id=instrument.instrument_id, file_name="test_file")
     exercise = ExerciseModel(exercise_id="db0711c3-0ac8-41d3-ae0e-567e5ea1ef87")
     business = BusinessModel(ru_ref="test_ru_ref")
     instrument.seft_file = seft_file
     instrument.exercises.append(exercise)
     instrument.businesses.append(business)
     survey = SurveyModel(survey_id="cb0711c3-0ac8-41d3-ae0e-567e5ea1ef87")
     instrument.survey = survey
     session.add(instrument)
     return instrument.instrument_id
Esempio n. 3
0
    def upload_to_bucket(self, exercise_id, file, ru_ref=None, classifiers=None, session=None):
        """
        Encrypt and upload a collection instrument to the bucket and db

        :param exercise_id: An exercise id (UUID)
        :param ru_ref: The name of the file we're receiving
        :param classifiers: Classifiers associated with the instrument
        :param file: A file object from which we can read the file contents
        :param session: database session
        :return: a collection instrument instance
        """

        log.info("Upload exercise", exercise_id=exercise_id)

        validate_uuid(exercise_id)
        self.validate_non_duplicate_instrument(file, exercise_id, session)
        instrument = InstrumentModel(ci_type="SEFT")

        seft_file = self._create_seft_file(instrument.instrument_id, file, encrypt_and_save_to_db=False)
        instrument.seft_file = seft_file

        exercise = self._find_or_create_exercise(exercise_id, session)
        instrument.exercises.append(exercise)

        survey = self._find_or_create_survey_from_exercise_id(exercise_id, session)
        instrument.survey = survey

        if ru_ref:
            business = self._find_or_create_business(ru_ref, session)
            self.validate_one_instrument_for_ru_specific_upload(exercise, business, session)
            instrument.businesses.append(business)

        if classifiers:
            instrument.classifiers = loads(classifiers)

        try:
            survey_ref = get_survey_ref(instrument.survey.survey_id)
            file.filename = survey_ref + "/" + exercise_id + "/" + file.filename
            seft_ci_bucket = GoogleCloudSEFTCIBucket(current_app.config)
            seft_ci_bucket.upload_file_to_bucket(file=file)
            instrument.seft_file.gcs = True
        except Exception as e:
            log.exception("An error occurred when trying to put SEFT CI in bucket")
            raise e

        session.add(instrument)
        return instrument
Esempio n. 4
0
 def add_instrument_data(session=None):
     instrument = InstrumentModel(classifiers={
         "form_type": "001",
         "geography": "EN"
     },
                                  ci_type="SEFT")
     crypto = Cryptographer()
     data = BytesIO(b"test data")
     data = crypto.encrypt(data.read())
     seft_file = SEFTModel(instrument_id=instrument.instrument_id,
                           file_name="test_file",
                           length="999",
                           data=data)
     instrument.seft_file = seft_file
     exercise = ExerciseModel(exercise_id=linked_exercise_id)
     business = BusinessModel(ru_ref="test_ru_ref")
     instrument.exercises.append(exercise)
     instrument.businesses.append(business)
     survey = SurveyModel(survey_id="cb0711c3-0ac8-41d3-ae0e-567e5ea1ef87")
     instrument.survey = survey
     session.add(instrument)
     return instrument.instrument_id
    def upload_instrument(self, exercise_id, file, ru_ref=None, classifiers=None, session=None):
        """
        Encrypt and upload a collection instrument to the db

        :param exercise_id: An exercise id (UUID)
        :param ru_ref: The name of the file we're receiving
        :param classifiers: Classifiers associated with the instrument
        :param file: A file object from which we can read the file contents
        :param session: database session
        :return: a collection instrument instance
        """

        log.info('Upload exercise', exercise_id=exercise_id)

        validate_uuid(exercise_id)
        instrument = InstrumentModel(ci_type='SEFT')

        seft_file = self._create_seft_file(instrument.instrument_id, file)
        instrument.seft_file = seft_file

        exercise = self._find_or_create_exercise(exercise_id, session)
        instrument.exercises.append(exercise)

        survey = self._find_or_create_survey_from_exercise_id(exercise_id, session)
        instrument.survey = survey

        if ru_ref:
            business = self._find_or_create_business(ru_ref, session)
            self.validate_one_instrument_for_ru_specific_upload(exercise, business, session)
            instrument.businesses.append(business)

        if classifiers:
            instrument.classifiers = loads(classifiers)

        session.add(instrument)
        return instrument