Example #1
0
def test_users(mocked_mysql_var):
    with app.test_request_context("/users"):

        # Establish MySQL connection -- mocked
        conn = MagicMock()
        mocked_mysql_var.connect.return_value = conn

        # Get a MySQL cursor -- mocked
        cursor = MagicMock()
        conn.cursor.return_value = cursor
        cursor.execute.return_value = MagicMock()
        cursor.fetchall.return_value = users_dict
        cursor.close.return_value = MagicMock()
        conn.close.return_value = MagicMock()

        # Call users procedure
        resp = users()
        expected_json = users_dict

        # Check workflow
        mocked_mysql_var.connect.assert_called_once_with()
        conn.cursor.assert_called_once_with(pymysql.cursors.DictCursor)
        cursor.execute.assert_called_once_with("SELECT * FROM USERS")
        assert resp.status_code == 200
        assert resp.json == expected_json
        cursor.close.assert_called_once_with()
        conn.close.assert_called_once_with()
Example #2
0
def test_auth_user_invalid_password(mocked_mysql_var):
    with app.test_request_context("/user/<id>/<passwd>"):

        # Establish MySQL connection -- mocked
        conn = MagicMock()
        mocked_mysql_var.connect.return_value = conn

        # Get a MySQL cursor -- mocked
        cursor = MagicMock()
        conn.cursor.return_value = cursor
        cursor.execute.return_value = MagicMock()
        cursor.fetchone.return_value = users_dict[0]
        cursor.close.return_value = MagicMock()
        conn.close.return_value = MagicMock()

        # Call users procedure
        id = "TestUsername"
        passwd = "InvalidPasswd"
        resp = auth_user(id, passwd)
        expected_json = {"authenticated": False}

        # Check workflow
        mocked_mysql_var.connect.assert_called_once_with()
        conn.cursor.assert_called_once_with(pymysql.cursors.DictCursor)
        cursor.execute.assert_called_once_with(
            "SELECT * FROM USERS WHERE Username=%s", id)
        assert resp.status_code == 200
        assert resp.json == expected_json
        cursor.close.assert_called_once_with()
        conn.close.assert_called_once_with()
Example #3
0
def test_update_user(mocked_mysql_var, mocked_hash):
    with app.test_request_context(
            "/update/TestUsername",
            json={
                "last_name": "flask_test_last_name",
                "first_name": "flask_test_first_name",
                "username": "******",
                "email": "flask_test_email",
                "pwd": "flask_test_password",
            },
            method="POST",
    ):

        # Same Hash Gen
        mocked_hash.return_value = "pbkdf2:sha256:150000$uQP6jkPa$a70e2bab07128ebf878ba0e0c6ea03a6f397794b36c794bac2087c944d03b40b"

        # Establish MySQL connection -- mocked
        conn = MagicMock()
        mocked_mysql_var.connect.return_value = conn

        # Get a MySQL cursor -- mocked
        cursor = MagicMock()
        conn.cursor.return_value = cursor
        cursor.execute.return_value = MagicMock()
        conn.commit.return_value = MagicMock()
        cursor.close.return_value = MagicMock()
        conn.close.return_value = MagicMock()

        # Call add_user procedure
        id = "TestUsername"
        resp = update_user(id)

        # Check workflow
        mocked_hash.assert_called_once_with("flask_test_password")
        mocked_mysql_var.connect.assert_called_once_with()
        conn.cursor.assert_called_once_with()
        cursor.execute.assert_called_once_with(
            "UPDATE USERS SET LastName=%s, FirstName=%s, Email=%s, hashedpassword=%s WHERE Username=%s",
            (
                "flask_test_last_name",
                "flask_test_first_name",
                "flask_test_email",
                "pbkdf2:sha256:150000$uQP6jkPa$a70e2bab07128ebf878ba0e0c6ea03a6f397794b36c794bac2087c944d03b40b",
                "TestUsername",
            ),
        )
        conn.commit.assert_called_once_with()
        assert resp.status_code == 200
        assert resp.json == "User updated successfully!"
        cursor.close.assert_called_once_with()
        conn.close.assert_called_once_with()
Example #4
0
def test_delete(mocked_mysql_var, ):
    with app.test_request_context(
            "/update/TestUsername",
            json={
                "last_name": "flask_test_last_name",
                "first_name": "flask_test_first_name",
                "username": "******",
                "email": "flask_test_email",
                "pwd": "flask_test_password",
            },
            method="POST",
    ):

        # Establish MySQL connection -- mocked
        conn = MagicMock()
        mocked_mysql_var.connect.return_value = conn

        # Get a MySQL cursor -- mocked
        cursor = MagicMock()
        conn.cursor.return_value = cursor
        cursor.execute.return_value = MagicMock()
        conn.commit.return_value = MagicMock()
        cursor.close.return_value = MagicMock()
        conn.close.return_value = MagicMock()

        # Call add_user procedure
        id = "TestUsername"
        resp = delete_user(id)

        # Check workflow
        mocked_mysql_var.connect.assert_called_once_with()
        conn.cursor.assert_called_once_with()
        cursor.execute.assert_called_once_with(
            "DELETE FROM USERS WHERE Username=%s", (id, ))
        conn.commit.assert_called_once_with()
        assert resp.status_code == 200
        assert resp.json == "User deleted successfully!"
        cursor.close.assert_called_once_with()
        conn.close.assert_called_once_with()