def test_empty_mail():
    """Tests that an empty email is accepted"""

    validators = {'email': valid.validate_email}
    data = {'email': ""}
    res = valid.validate(data, validators)
    assert res == data
Esempio n. 2
0
    def post(self):
        """
        Analyses the results from a data dissemination quiz and gives the result.
        The result can be either that stereotypes were found, or that they weren't found.
        """
        validators = {
            "data": valid.validate_dissemination_answers,
        }

        data = valid.validate(valid.read_form_data(request), validators)
        if not data:
            return ANSWERS[400], 400

        data = valid.read_form_data(request)

        question3, block_3_answers = self.get_block_information(2, data)
        question5, block_5_answers = self.get_block_information(4, data)

        t_statistic, p_value = ttest(block_3_answers, block_5_answers, equal_var=False)

        response = DISSEMINATION_NO_ASSOCIATION

        if p_value <= 0.1:
            if t_statistic < 0:
                response = DISSEMINATION_RESULT_MALE
            else:
                response = DISSEMINATION_RESULT_FEMALE

        if 'email' in data and valid.validate_email(data['email']):
            self.send_email(res=response, email=data['email'])

        return response, 200
def test_wrong_mail():
    """Tests that an invalid email is rejected"""

    validators = {'email': valid.validate_email}
    data = {'email': "bademail@."}
    res = valid.validate(data, validators)
    assert res is None
Esempio n. 4
0
    def post(self):
        """Route for application login.
            Sends back a JSON with an access and a refresh token"""

        validators = {
            'username': valid.validate_string,
            'password': valid.validate_string
        }
        data = valid.validate(valid.read_form_data(request), validators)
        if not data:
            return ANSWERS[400], 400

        username = data['username']
        password = data['password']

        user = User.query.filter_by(username=username).first()
        if not (user and bcrypt.check_password_hash(user.password, password)):
            return ANSWERS[403], 403

        # Use create_access_token() and create_refresh_token()
        # to create our access and refresh tokens
        ret = {
            'access_token': create_access_token(identity=username, fresh=True),
            'refresh_token': create_refresh_token(identity=username)
        }
        return ret, 200
def test_mismatch():
    """Test validate incoming data containing a different type of value than expected"""

    validators = {
        'year': valid.validate_int,
    }
    data = {'year': '2020'}
    res = valid.validate(data, validators)
    assert res is None
def test_keynotinvalidators():
    """Test validate incoming data containing more keys than expected"""

    validators = {
        'year': valid.validate_int,
    }
    data = {'year': 2020, 'month': 'May'}
    res = valid.validate(data, validators)
    assert 'month' not in res
def test_validate_missingkey():
    """Test validate incoming data containing fewer keys than expected"""

    validators = {
        'username': valid.validate_string,
        'password': valid.validate_string
    }
    data = {'username': '******'}

    res = valid.validate(data, validators)
    assert res is None
Esempio n. 8
0
    def post(self):
        """
        On a post request on the /form endpoint we add the consent form to the database
        :return: If the request is valid, a 200 status code, otherwise a 400 code
        """

        validators = {
            'parent': valid.validate_person_data,
            'children': valid.validate_children_data,
            'signature': valid.validate_signature,
            'email': valid.validate_email
        }

        data = valid.validate(valid.read_form_data(request), validators)
        if not data:
            return ANSWERS[400], 400

        parent = data['parent']
        signature = data['signature']

        if not current_app.config['TESTING']:
            upload_result = upload(signature, folder="signatures")
            signature = upload_result["secure_url"]

        cons = Consent.create_consent(parent_first_name=parent['firstName'],
                                      parent_last_name=parent['lastName'],
                                      signature=signature,
                                      email=data['email'])

        for child in data['children']:
            participant = Participant(first_name=child['firstName'],
                                      last_name=child['lastName'],
                                      consent_id=cons.id)
            add_to_db(participant)
            obj = {
                "firstName": participant.first_name,
                "lastName": participant.last_name,
                "id": participant.id
            }
            red.rpush("queue", str(obj))

        socketio.emit("free-laptops")

        return ANSWERS[200], 200
def test_validators():
    """Test validate accept, boolean, float, email, list types"""

    validators = {
        'accept': valid.validate_accept,
        'boolean': valid.validate_boolean,
        'float': valid.validate_float,
        'email': valid.validate_email,
        'list': valid.validate_list
    }
    anytype = any
    data = {
        'accept': anytype,
        'boolean': True,
        'float': 1.12,
        'email': "*****@*****.**",
        'list': [1, 2, 3, 4]
    }
    res = valid.validate(data, validators)
    assert res == data
Esempio n. 10
0
    def post(self):
        """Route to get a fresh access token by entering credentials again
            Returns a json with a fresh access token"""

        validators = {
            'username': valid.validate_string,
            'password': valid.validate_string
        }

        data = valid.validate(valid.read_form_data(request), validators)
        if not data:
            return ANSWERS[400], 400

        username = data['username']
        password = data['password']

        user = User.query.filter_by(username=username).first()
        if not (user and bcrypt.check_password_hash(user.password, password)):
            return ANSWERS[403], 403

        new_token = create_access_token(identity=username, fresh=True)
        ret = {'access_token': new_token}
        return ret, 200
Esempio n. 11
0
    def post(self):
        """
        On a post request on the /answers endpoint we add the quiz answers
        :return: If the request is valid, a 201 CREATED status code, otherwise a 400 code
        """

        validators = {
            "data": valid.validate_answers,
            "id": valid.validate_int,
            "version": valid.validate_string
        }

        data = valid.validate(valid.read_form_data(request), validators)
        if not data:
            return ANSWERS[400], 400

        participant = Participant.query.filter_by(id=data['id']).first()
        participant.quiz_version = Version[data["version"]]

        # Iterate through every answer and insert demographics ones
        # into Participant, and the rest into ParticipantAnswer
        for answer in data['data']:
            q_type = Question.query.filter_by(
                id=answer["question_id"]).first().q_type
            i_type = Question.query.filter_by(
                id=answer["question_id"]).first().information

            if q_type == QuestionType.mc_single_answer \
                    and i_type == ParticipantInformationType.age:

                age_string = QuestionChoice.query.filter_by(
                    choice_num=answer['answers'],
                    question_id=answer["question_id"]).first().text
                if age_string != "Anders":
                    participant.age = int(age_string)

            elif q_type == QuestionType.mc_single_answer \
                    and i_type == ParticipantInformationType.gender:

                gender = QuestionChoice.query.filter_by(
                    choice_num=answer['answers'],
                    question_id=answer["question_id"]).first().text
                if gender != "Zeg ik liever niet":
                    participant.gender = gender

            elif q_type == QuestionType.mc_multiple_answer \
                    and i_type == ParticipantInformationType.ethnicity:

                ethinicities = []
                for choice_num in answer['answers']:
                    eth = QuestionChoice.query.filter_by(
                        choice_num=choice_num,
                        question_id=answer["question_id"]).first().text
                    ethinicities.append(eth)
                participant.ethnicity = ethinicities

            elif i_type == ParticipantInformationType.researcher_notes:
                participant.researcher_notes = answer['open_answer']

            else:
                ParticipantAnswer.create_participant_answer(
                    p_id=answer["participant_id"],
                    q_id=answer["question_id"],
                    img_link=answer["img_id"]
                    if "img_id" in answer else None,  # for likert
                    answers=answer["answers"] if "answers" in answer else None,
                    open_answer=answer["open_answer"]
                    if "open_answer" in answer else None,
                    r_time=answer["response_time"]
                    if 'response_time' in answer else None,
                    before_video=answer["before_video"])

        commit_db_session()
        return ANSWERS[201], 201
def test_validate_none():
    """Test None type for both validators and data"""

    validators = None
    data = None
    assert valid.validate(data, validators) is None