Exemple #1
0
    def test_thread_count_by_survey_my_conversations_on(self):
        """checks that the returned thread count is the same for every internal user
        even if they are not part of the conversations"""
        request_args = MessageArgs(page=0,
                                   limit=100,
                                   business_id=None,
                                   cc=None,
                                   label=None,
                                   desc=None,
                                   ce=None,
                                   surveys=[self.BRES_SURVEY],
                                   is_closed=False,
                                   my_conversations=True,
                                   new_respondent_conversations=False,
                                   all_conversation_types=False,
                                   unread_conversations=False)

        for _ in range(5):
            self.create_thread(no_of_messages=2)

        with self.app.app_context():
            with current_app.test_request_context():
                thread_count_internal = Retriever.thread_count_by_survey(
                    request_args,
                    User(self.default_internal_actor, role='internal'))
                thread_count_second_internal = Retriever.thread_count_by_survey(
                    request_args,
                    User(self.second_internal_actor, role='internal'))
                self.assertEqual(thread_count_internal, 5)
                self.assertEqual(thread_count_second_internal, 0)
def check_jwt(token):
    try:
        decoded_jwt_token = decode(token)
        logger.info("Decoded JWT",
                    user_uuid=decoded_jwt_token.get(constants.USER_IDENTIFIER))

        if not decoded_jwt_token.get(constants.USER_IDENTIFIER):
            raise BadRequest(
                description="Missing user_uuid claim,"
                "user_uuid is required to access this Microservice Resource")
        if not decoded_jwt_token.get('role'):
            raise BadRequest(
                description=
                "Missing role claim, role is required to access this Microservice Resource"
            )

        g.user = User(decoded_jwt_token.get(constants.USER_IDENTIFIER),
                      decoded_jwt_token.get('role'))

        return {'status': "ok"}

    except InvalidTokenError:
        logger.error(
            'Failed decode the JWT. Is the JWT Algorithm and Secret setup correctly?'
        )
        return Response(
            response="Invalid token to access this Microservice Resource",
            status=400,
            mimetype="text/html")
    except BadRequest as e:
        logger.error(str(e))
        raise
 def test_valid_message_passes_validation(self):
     """marshaling a valid message"""
     self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
     schema = MessageSchema()
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         result = schema.load(self.json_message)
     self.assertTrue(result.errors == {})
 def test_missing_thread_field_does_not_cause_error(self):
     """marshalling message with no thread_id field"""
     self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
     self.json_message.pop('thread_id', "")
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         errors = schema.load(self.json_message)[1]
     self.assertTrue(errors == {})
 def test_subject_with_only_spaces_fails_validation(self):
     """marshalling message with no subject field """
     self.json_message['subject'] = '  '
     self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         errors = schema.load(self.json_message)[1]
     self.assertTrue(errors == {'subject': ['Please enter a subject']})
 def test_thread_field_too_long_causes_error(self):
     """marshalling message with thread_id field too long"""
     self.json_message['thread_id'] = "x" * (MAX_THREAD_LEN + 1)
     expected_error = f"Thread field length must not be greater than {MAX_THREAD_LEN}"
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         sut = schema.load(self.json_message)
     self.assertTrue(expected_error in sut.errors['thread_id'])
    def setUp(self):
        """setup test environment"""

        self.app = create_app()

        self.app.testing = True
        self.engine = create_engine(self.app.config['SQLALCHEMY_DATABASE_URI'])

        with self.app.app_context():
            database.db.init_app(current_app)
            database.db.drop_all()
            database.db.create_all()
            self.db = database.db

        self.user_internal = User('ce12b958-2a5f-44f4-a6da-861e59070a31',
                                  'internal')
        self.user_respondent = User('0a7ad740-10d5-4ecb-b7ca-3c0384afb882',
                                    'respondent')
    def test_msg_to_string(self):
        """marshalling message where msg_to field is string"""
        self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
        with self.app.app_context():
            g.user = User(self.json_message['msg_from'], 'respondent')
            schema = MessageSchema()
            errors = schema.load(self.json_message)[1]

        self.assertTrue(errors == {})
 def test_missing_survey_causes_error(self):
     """marshalling message with no survey field"""
     self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
     self.json_message['survey'] = ''
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         errors = schema.load(self.json_message)[1]
     self.assertTrue(errors == {'survey': ['Please enter a survey']})
 def test_missing_subject_fails_validation(self):
     """marshalling message with no subject field """
     self.json_message.pop('subject', 'MyMessage')
     self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         errors = schema.load(self.json_message)[1]
     self.assertTrue(
         errors == {'subject': ['Missing data for required field.']})
 def test_missing_msg_id_causes_a_string_the_same_length_as_uuid_to_be_used(
         self):
     """Missing msg_id causes a uuid is used"""
     self.json_message['msg_id'] = ''
     self.json_message['msg_to'] = ['01b51fcc-ed43-4cdb-ad1c-450f9986859b']
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         sut = schema.load(self.json_message)
     self.assertEqual(len(sut.data.msg_id), 36)
    def test_msg_to_validation_invalid_respondent(self):
        """marshalling message where msg_to field is a invalid user"""
        self.json_message['msg_to'] = ["NotAValidUser"]
        self.json_message['msg_from'] = "01b51fcc-ed43-4cdb-ad1c-450f9986859b"
        with self.app.app_context():
            g.user = User("01b51fcc-ed43-4cdb-ad1c-450f9986859b", 'internal')
            schema = MessageSchema()
            errors = schema.load(self.json_message)[1]

        self.assertTrue(
            errors == {'msg_to': ['NotAValidUser is not a valid respondent.']})
    def test_body_too_big_fails_validation(self):
        """marshalling message with body field too long """
        self.json_message['body'] = "x" * (MAX_BODY_LEN + 1)
        expected_error = f"Body field length must not be greater than {MAX_BODY_LEN}"

        with self.app.app_context():
            g.user = User(self.json_message['msg_from'], 'respondent')
            schema = MessageSchema()
            sut = schema.load(self.json_message)

        self.assertTrue(expected_error in sut.errors['body'])
    def test_same_to_from_causes_error(self):
        """marshalling message with same to and from field"""
        self.json_message['msg_from'] = "01b51fcc-ed43-4cdb-ad1c-450f9986859b"
        self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
        with self.app.app_context():
            g.user = User(self.json_message['msg_from'], 'respondent')
            schema = MessageSchema()
            errors = schema.load(self.json_message)[1]

        self.assertTrue(
            errors ==
            {'_schema': ['msg_to and msg_from fields can not be the same.']})
Exemple #15
0
    def setUp(self):
        """setup test environment"""
        self.app = create_app()
        self.app.testing = True
        self.engine = create_engine(self.app.config['SQLALCHEMY_DATABASE_URI'])
        self.MESSAGE_LIST_ENDPOINT = "http://localhost:5050/messages"
        self.MESSAGE_BY_ID_ENDPOINT = "http://localhost:5050/message/"
        with self.app.app_context():
            database.db.init_app(current_app)
            database.db.drop_all()
            database.db.create_all()
            self.db = database.db

        self.user_internal = User(
            RetrieverTestCaseHelper.default_internal_actor, 'internal')
        self.second_user_internal = User(
            RetrieverTestCaseHelper.second_internal_actor, 'internal')
        self.user_respondent = User(
            RetrieverTestCaseHelper.default_external_actor, 'respondent')
        self.second_user_respondent = User(
            RetrieverTestCaseHelper.second_external_actor, 'respondent')
        party.use_mock_service()
    def test_missing_body_fails_validation(self):
        """marshalling message with no body field """
        message = {
            'msg_to': ['01b51fcc-ed43-4cdb-ad1c-450f9986859b'],
            'msg_from': 'torrance',
            'body': '',
            'survey': 'RSI',
            'subject': 'MyMessage',
            'business_id': '7fc0e8ab-189c-4794-b8f4-9f05a1db185b'
        }

        with self.app.app_context():
            g.user = User(message['msg_from'], 'respondent')
            schema = MessageSchema()
            errors = schema.load(message)[1]
        self.assertTrue(errors == {'body': ['Please enter a message']})
    def test_setting_read_date_field_causes_error(self):
        """marshalling message with no thread_id field"""
        message = {
            'msg_to': ['torrance'],
            'msg_from': 'someone',
            'body': 'hello',
            'subject': 'subject',
            'read_date': self.now
        }

        with self.app.app_context():
            g.user = User(message['msg_from'], 'respondent')
            schema = MessageSchema()
            errors = schema.load(message)[1]

        self.assertTrue(errors == {'_schema': ['read_date can not be set']})
Exemple #18
0
 def test_logging_message_endpoint(self):
     """logging message endpoint"""
     out = StringIO()
     sys.stdout = out
     message = {
         'msg_to': [constants.NON_SPECIFIC_INTERNAL_USER],
         'msg_from': self.msg_from,
         'subject': 'hello',
         'body': 'hello world',
         'thread_id': '',
         'business_id': self.business_id,
         'survey': self.survey_id
     }
     with self.app.app_context():
         g.user = User('Gemma', 'respondent')
         schema = MessageSchema()
         schema.load(message)
     output = out.getvalue().strip()
     self.assertIsNotNone(output)