Exemplo n.º 1
0
    def test_validate_email_false(self, mock_u):
        """
            test: email does already exist in database.
        """
        from xmppchat.CustomValidatonError import CustomValidationError

        mock_u.query.filter_by.return_value.first.return_value = User(
            "testuser", "*****@*****.**", "hallo123", "topic-id1")
        with pytest.raises(CustomValidationError):
            Validator.validate_email("*****@*****.**")
Exemplo n.º 2
0
def register():
    """
        registers a user on platform ejabberd and kafka
        and inserts the user into the user database
    """
    if current_user.is_authenticated:
        return get_url_chatpage(current_user.user_id)

    if request.method == "POST":
        req_content = request.get_json()
        if not req_content:
            return make_response(jsonify({'feedback': 'invalid post data.', 'category': 'danger'}), 404)

        res, exit_code = {'feedback': 'registration successfull.', 'category': 'success'}, 200
        topic_id = str(uuid.uuid4())
        try:
            Validator.validate_username(req_content["username"])
            Validator.validate_email(req_content["eMail"])
            user_reg_obj = UserManagement(config['ejabberd_ip'], config['ejabberd_ssh_user'], priv_key=config['ejabberd_ssh_private_key'], sudo_passwd=config['ejabberd_ssh_sudo_password'])
            return_code = user_reg_obj.create_user_remotely(req_content["username"], req_content["password"], config["ejabberd_domain"])
            if return_code != 0:
                raise CustomValidationError("Error. User was not created. Please try again.")

            kafka_admin_client = KafkaAdminClient(bootstrap_servers=f'{config["apache_kafka_ip"]}:{config["apache_kafka_port"]}')
            topic_list = [NewTopic(name=topic_id, num_partitions=1, replication_factor=1)]
            kafka_admin_client.create_topics(new_topics=topic_list, validate_only=False)
            
            user = User(req_content["username"], req_content["eMail"], req_content["password"], topic_id)
            db.session.add(user)
            db.session.commit()
        except KeyError as e:
            loggger.error(str(e))
            res, exit_code = {'feedback': 'invalid credentials.', 'category': 'danger'}, 401
        except CustomValidationError as cve:
            logger.error(str(cve))
            res, exit_code = {'feedback': str(cve), 'category': 'danger'}, 401
        except Exception as e:
            db.session.rollback()
            return_code = user_reg_obj.delete_user_remotely(req_content["username"], config["ejabberd_domain"])
            if return_code != 0:
                res, exit_code = {'feedback': 'User could not be created. Another error occurred while handling an internal error.', 'category': 'danger'}, 500
            else:
                res, exit_code = {'feedback': 'Users could not be created due to an internal error.', 'category': 'danger'}, 500
            logger.error(e)

        return make_response(jsonify(res), exit_code)
    else:
        return render_template('register.html', navs=navs, currentNav="Go Chat!")
Exemplo n.º 3
0
 def test_validate_email_true(self, mock_u):
     """
         test: email does not already exist in database.
     """
     mock_u.query.filter_by.return_value.first.return_value = None
     self.assertIsNone(Validator.validate_email("*****@*****.**"))