Beispiel #1
0
    def test_register_already_exists(self):

        # Expected result from server.
        expected_result = {"message": "Already exists."}

        # User already created.
        create_test_user(username="******",
                         password="******",
                         email="*****@*****.**")

        # User same data to register.
        data = {
            "username": "******",
            "password": "******",
            "email": "*****@*****.**"
        }

        # Send request to index.
        response = self.client.post("/v1/auth/register", json=data)

        # Log if needed.
        # print(f"Status Code: {response.status_code}\nResult: {response.json}")

        # This raises an AssertionError
        assert 409 == response.status_code

        # This raises an AssertionError
        assert expected_result == response.json
    def test_login(self):

        # User created to login.
        user = create_test_user(username="******",
                                password="******",
                                email="*****@*****.**")

        # User data to register.
        data = {"email": "*****@*****.**", "password": "******"}

        # Send request to index.
        response = self.client.post("/v1/auth/login", json=data)

        # Log if needed.
        # print(f"Status Code: {response.status_code}\nResult: {response.json}")

        # This raises an AssertionError
        assert response.status_code == 200

        # Check access and refresh token in response.
        assert "access_token" in response.json
        assert "refresh_token" in response.json

        # Get access and refresh tokens.
        access_token = response.json.get("access_token")

        # Load access token and see the data.
        tokenized_data = jwt.loads(access_token)

        # This raises an AssertionError
        assert tokenized_data.get("email") == user.email

        # This raises an AssertionError
        assert tokenized_data.get("admin") == 0
Beispiel #3
0
def create_app():

    # Create a flask app.
    app = Flask(__name__)

    # Set debug true for catching the errors.
    app.config['DEBUG'] = True

    # Set database url.
    app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI

    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

    # Generate routes.
    generate_routes(app)

    # Database initialize with app.
    db.init_app(app)

    # Check if there is no database.
    if not os.path.exists(SQLALCHEMY_DATABASE_URI):

        # New db app if no database.
        db.app = app

        # Create all database tables.
        db.create_all()

        # Create default super admin user in database.
        create_super_admin()

        # Create default admin user in database.
        create_admin_user()

        # Create default test user in database.
        create_test_user()

    # Return app.
    return app
    def test_login_none_parameter(self):

        # Expected result from server.
        expected_result = {"message": "Invalid input."}

        # User created to login.
        create_test_user(username="******",
                         password="******",
                         email="*****@*****.**")

        # User data to register. Password NONE parameter.
        data = {"email": "*****@*****.**", "password": None}

        # Send request to index.
        response = self.client.post("/v1/auth/login", json=data)

        # Log if needed.
        # print(f"Status Code: {response.status_code}\nResult: {response.json}")

        # This raises an AssertionError
        assert 422 == response.status_code

        # This raises an AssertionError
        assert expected_result == response.json
        # Create all database tables.
        db.create_all()

    # Return app.
    return app


if __name__ == '__main__':

    # Create app.
    app = create_app()

    # Create database tables.
    db.create_all()

    # Create default super admin user in database.
    create_super_admin()

    # Create default admin user in database.
    create_admin_user()

    # Create default test user in database.
    create_test_user()

    # Generate routes.
    generate_routes(app)

    # Run app.
    app.run(port=5000, debug=True, host='0.0.0.0', use_reloader=True)
        # Create all database tables.
        db.create_all()

    # Return app.
    return app


if __name__ == '__main__':

    # Create app.
    app = create_app()

    # Create database tables.
    db.create_all()

    # Create default super admin user in database.
    create_super_admin()

    # Create default admin user in database.
    create_admin_user()

    # Create default test user in database.
    create_test_user()

    # Generate routes.
    generate_routes(app)

    # Run app.
    app.run(port=5000, debug=True, host='localhost', use_reloader=True)