Example #1
0
    def _create_seft_file(instrument_id, file, encrypt_and_save_to_db=True):
        """
        Creates a seft_file with an encrypted version of the file
        :param file: A file object from which we can read the file contents
        :return: instrument
        """
        log.info("creating instrument seft file")
        file_contents = file.read()
        file_size = len(file_contents)
        seft_file = SEFTModel(instrument_id=instrument_id, file_name=file.filename, length=file_size)
        if encrypt_and_save_to_db:
            cryptographer = Cryptographer()
            encrypted_file = cryptographer.encrypt(file_contents)
            seft_file.data = encrypted_file

        return seft_file
Example #2
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
Example #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