コード例 #1
0
ファイル: tests.py プロジェクト: onnela-lab/beiwe-backend
    def test_get_all_studies_by_name(self):
        study_names = ['My studies', 'MY STUDY', 'my_study', 'your study']
        encryption_key = 'aabbccddeeffgghhiijjkkllmmnnoopp'
        for name in study_names:
            good_study = Study.create_with_object_id(name=name, encryption_key=encryption_key)

        self.assertIn(good_study, Study.get_all_studies_by_name())
        self.assertEqual(list(Study.get_all_studies_by_name().values_list('name', flat=True)), study_names)

        bad_study = Study.create_with_object_id(name='name', encryption_key=encryption_key, deleted=True)
        self.assertNotIn(bad_study, Study.get_all_studies_by_name())
コード例 #2
0
ファイル: tests.py プロジェクト: onnela-lab/beiwe-backend
 def test_study_create_with_object_id(self):
     self.assertEqual(Study.objects.count(), 0)
     self.assertEqual(DeviceSettings.objects.count(), 0)
     study_name = 'my study'
     encryption_key = 'aabbccddeeffgghhiijjkkllmmnnoopp'
     Study.create_with_object_id(name=study_name, encryption_key=encryption_key)
     new_study = Study.objects.get()
     new_ds = DeviceSettings.objects.get()
     self.assertEqual(Study.objects.count(), 1)
     self.assertEqual(DeviceSettings.objects.count(), 1)
     self.assertEqual(new_study.name, study_name)
     self.assertEqual(new_study.encryption_key, encryption_key)
     self.assertEqual(len(new_study.object_id), 24)
     self.assertEqual(new_study.device_settings, new_ds)
     self.assertFalse(new_study.deleted)
コード例 #3
0
    def setup(self, researcher=True, apikey=True, study=True):
        """
        This function is used to initialize that database for each test.

        This was written in this fashion because of a known issue with the HybridTest class that does not consistently
        use database changes that persist between tests
        """
        if apikey and not researcher:
            raise Exception("invalid setup criteria")
        if researcher:
            self.researcher = Researcher.create_with_password(
                username=TEST_USERNAME, password=TEST_PASSWORD)
        if apikey:
            self.api_key = ApiKey.generate(self.researcher,
                                           has_tableau_api_permissions=True)
            self.api_key_public = self.api_key.access_key_id
            self.api_key_private = self.api_key.access_key_secret_plaintext
        if study:
            self.study = Study.create_with_object_id(
                device_settings=DeviceSettings(),
                encryption_key=TEST_STUDY_ENCRYPTION_KEY,
                name=TEST_STUDY_NAME,
            )
            if researcher:
                self.study_relation = StudyRelation(
                    study=self.study,
                    researcher=self.researcher,
                    relationship="researcher",
                ).save()
コード例 #4
0
def create_study():
    # ONLY THE SITE ADMIN CAN CREATE NEW STUDIES.
    if not get_session_researcher().site_admin:
        return abort(403)

    if request.method == 'GET':
        studies = [
            study.as_native_python()
            for study in Study.get_all_studies_by_name()
        ]
        return render_template(
            'create_study.html',
            studies=json.dumps(studies),
            allowed_studies=get_researcher_allowed_studies(),
            is_admin=researcher_is_an_admin())

    name = request.form.get('name', '')
    encryption_key = request.form.get('encryption_key', '')
    is_test = request.form.get(
        'is_test') == 'true'  # 'true' -> True, 'false' -> False

    assert len(name) <= 2**16, "safety check on new study name failed"

    try:
        study = Study.create_with_object_id(name=name,
                                            encryption_key=encryption_key,
                                            is_test=is_test)
        copy_existing_study_if_asked_to(study)
        flash('Successfully created study {}.'.format(name), 'success')
        return redirect('/device_settings/{:d}'.format(study.pk))
    except ValidationError as ve:
        for field, message in ve.message_dict.iteritems():
            flash('{}: {}'.format(field, message[0]), 'danger')
        return redirect('/create_study')
コード例 #5
0
def create_study():
    if request.method == 'GET':
        studies = [
            study.as_native_python()
            for study in Study.get_all_studies_by_name()
        ]
        return render_template('create_study.html',
                               studies=json.dumps(studies),
                               allowed_studies=get_admins_allowed_studies(),
                               system_admin=admin_is_system_admin())

    name = request.form.get('name', '')
    encryption_key = request.form.get('encryption_key', '')

    is_test = request.form.get(
        'is_test') == 'true'  # 'true' -> True, 'false' -> False
    try:
        study = Study.create_with_object_id(name=name,
                                            encryption_key=encryption_key,
                                            is_test=is_test)
        copy_existing_study_if_asked_to(study)
        flash('Successfully created study {}.'.format(name), 'success')
        return redirect('/device_settings/{:d}'.format(study.pk))
    except ValidationError as ve:
        for field, message in ve.message_dict.iteritems():
            flash('{}: {}'.format(field, message[0]), 'danger')
        return redirect('/create_study')
コード例 #6
0
ファイル: tests.py プロジェクト: onnela-lab/beiwe-backend
    def test_study_validation(self):
        study_name = 'my study'
        good_encryption_key = 'aabbccddeeffgghhiijjkkllmmnnoopp'
        short_encryption_key = 'aabbccddeeffgghhiijjkkllmm'
        long_encryption_key = 'aabbccddeeffgghhiijjkkllmmnnooppqqrrsstt'
        with self.assertRaises(ValidationError):
            Study.create_with_object_id(name=study_name, encryption_key=short_encryption_key)
        with self.assertRaises(ValidationError):
            Study.create_with_object_id(name=study_name, encryption_key=long_encryption_key)

        bad_object_id = 'I am too long to be an ObjectID'
        with self.assertRaises(ValidationError):
            Study.objects.create(name=study_name, encryption_key=good_encryption_key, object_id=bad_object_id)

        Study.create_with_object_id(name=study_name, encryption_key=good_encryption_key)
        with self.assertRaises(ValidationError):
            Study.create_with_object_id(name=study_name, encryption_key=good_encryption_key)
コード例 #7
0
    def test_all_routes(self):
        """
        Tests urls
        """
        app2 = subdomain("frontend")
        app2.register_blueprint(admin_pages.admin_pages)

        long_encryption_key = 'aabbccddefggiijjkklmnoppqqrrsstt'

        researcher = Researcher.create_with_password('test_user',
                                                     'test_password')
        researcher.admin = True
        researcher.reset_access_credentials()
        researcher.save()

        study = Study.create_with_object_id(name='test_study',
                                            encryption_key=long_encryption_key)
        researcher.studies.add(study)

        self.selenium.get("localhost:54321")
        self.selenium.find_element_by_name('username').send_keys('test_user')
        self.selenium.find_element_by_name('password').send_keys(
            'test_password')
        self.selenium.find_element_by_name('submit').click()

        for rule in app2.url_map.iter_rules():
            str_rule = str(rule)
            self.assertIn(str_rule, ADMIN_PAGES)

            if ADMIN_PAGES[str_rule]['method'] == 'get':
                self.selenium.get("localhost:54321" + str_rule)
            elif ADMIN_PAGES[str_rule]['method'] == 'post':
                continue
            elif ADMIN_PAGES[str_rule]['method'] == 'get_param':
                str_rule_formatted = re.sub(r"<\w+:\w+>", str(study.id),
                                            str_rule)
                self.selenium.get("localhost:54321" + str_rule_formatted)
            else:
                continue

            response = self.determine_errors()
            self.assertEqual(response, '200')
コード例 #8
0
def create_study():
    # Only a SITE admin can create new studies.
    if not get_session_researcher().site_admin:
        return abort(403)

    if request.method == 'GET':
        studies = [
            study.as_unpacked_native_python()
            for study in Study.get_all_studies_by_name()
        ]
        return render_template('create_study.html', studies=studies)

    name = request.form.get('name', '')
    encryption_key = request.form.get('encryption_key', '')
    is_test = request.form.get(
        'is_test', "").lower() == 'true'  # 'true' -> True, 'false' -> False
    duplicate_existing_study = request.form.get('copy_existing_study',
                                                None) == 'true'

    if not (len(name) <= 2**16) or escape(name) != name:
        raise Exception("safety check on new study name failed")

    try:
        new_study = Study.create_with_object_id(name=name,
                                                encryption_key=encryption_key,
                                                is_test=is_test)
        if duplicate_existing_study:
            old_study = Study.objects.get(
                pk=request.form.get('existing_study_id', None))
            copy_existing_study(new_study, old_study)
        flash(f'Successfully created study {name}.', 'success')
        return redirect('/device_settings/{:d}'.format(new_study.pk))

    except ValidationError as ve:
        # display message describing failure based on the validation error (hacky, but works.)
        for field, message in ve.message_dict.items():
            flash(f'{field}: {message[0]}', 'danger')
        return redirect('/create_study')