# Generated by CodiumAI
import json

import pytest
import requests

from NetSuite_Connector.NetSuite import NetSuite

"""
Code Analysis

Main functionalities:
The NetSuite class is a Python wrapper for the NetSuite REST API that provides methods for making HTTP requests to the NetSuite REST API using OAuth 1.0 authentication. It supports GET, PUT, POST, and DELETE HTTP methods and provides error handling for failed requests.

Methods:
- __init__(self, account_id: Any, consumer_keys: dict, token_keys: dict) -> None: Initializes the NetSuite object with the account ID, consumer keys, and token keys.
- get(self, **kwargs) -> NetsuiteObject: Makes a GET request to the NetSuite REST API using the specified URL, headers, and parameters. Returns a NetsuiteObject containing the response data.
- put(self, **kwargs) -> NetsuiteObject: Makes a PUT request to the NetSuite REST API using the specified URL, headers, parameters, and body. Returns a NetsuiteObject containing the response data.
- post(self, **kwargs) -> NetsuiteObject: Makes a POST request to the NetSuite REST API using the specified URL, headers, parameters, and body. Returns a NetsuiteObject containing the response data.
- delete(self, **kwargs) -> NetsuiteObject: Makes a DELETE request to the NetSuite REST API using the specified URL, headers, and parameters. Returns a NetsuiteObject containing the response data.

Fields:
- oauth_version: The OAuth version used for authentication.
- signature_method: The signature method used for authentication.
- account_id: The account ID used for authentication.
- consumer_key: The consumer key used for authentication.
- consumer_secret: The consumer secret used for authentication.
- token_key: The token ID used for authentication.
- token_secret: The token secret used for authentication.
- _request_session: The OAuth1Session object used for making requests to the NetSuite REST API.
"""


class TestNetSuite:
    # Tests that a GET request is successful.
    def test_successful_get_request(self, requests_mock):
        # Arrange
        url = "https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=xxxx&deploy=xxxx"
        headers = {"Content-Type": "application/json"}
        params = {"param1": "value1"}
        response_data = {"foo": "bar"}
        requests_mock.get(url, json=response_data, status_code=200)

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )
        result = ns.get(url=url, headers=headers, params=params)

        # Assert
        assert result.url == url
        assert result.request_headers == headers
        assert result.response == json.dumps(response_data)
        assert result.code == 200

    # Tests that a PUT request is successful.
    def test_successful_put_request(self, requests_mock):
        # Arrange
        url = "https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=xxxx&deploy=xxxx"
        headers = {"Content-Type": "application/json"}
        params = {"param1": "value1"}
        body = {"foo": "bar"}
        response_data = {"success": True}
        requests_mock.put(url, json=response_data, status_code=200)

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )
        result = ns.put(url=url, headers=headers, params=params, body=body)

        # Assert
        assert result.url == url
        assert result.request_headers == headers
        assert result.response == json.dumps(response_data)
        assert result.code == 200

    # Tests that an invalid URL raises an exception.
    def test_invalid_url(self):
        # Arrange
        url = "invalid_url"

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )

        # Assert
        with pytest.raises(Exception):
            ns.get(url=url)

    # Tests that an invalid HTTP method raises an exception.
    def test_invalid_http_method(self):
        # Arrange
        http_method = "INVALID_METHOD"

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )

        # Assert
        with pytest.raises(Exception):
            ns._make_request(
                http_method=http_method, url="", headers={}, params={}, body={}
            )

    # Tests that failed requests are handled properly.
    def test_failed_request_error_handling(self, requests_mock):
        # Arrange
        url = "https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=xxxx&deploy=xxxx"
        headers = {"Content-Type": "application/json"}
        params = {"param1": "value1"}
        body = {"foo": "bar"}
        requests_mock.get(url, exc=requests.exceptions.RequestException)

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )
        result = ns.get(url=url, headers=headers, params=params, body=body)

        # Assert
        assert result.url == url
        assert result.request_headers == headers
        assert result.response is not None
        assert result.code == 500

    # Tests that a POST request is successful.
    def test_successful_post_request(self, requests_mock):
        # Arrange
        url = "https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=xxxx&deploy=xxxx"
        headers = {"Content-Type": "application/json"}
        params = {"param1": "value1"}
        body = {"foo": "bar"}
        response_data = {"success": True}
        requests_mock.post(url, json=response_data, status_code=200)

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )
        result = ns.post(url=url, headers=headers, params=params, body=body)

        # Assert
        assert result.url == url
        assert result.request_headers == headers
        assert result.response == json.dumps(response_data)
        assert result.code == 200

    # Tests that a DELETE request is successful.
    def test_successful_delete_request(self, requests_mock):
        # Arrange
        url = "https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=xxxx&deploy=xxxx"
        headers = {"Content-Type": "application/json"}
        params = {"param1": "value1"}
        response_data = {"success": True}
        requests_mock.delete(url, json=response_data, status_code=200)

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )
        result = ns.delete(url=url, headers=headers, params=params)

        # Assert
        assert result.url == url
        assert result.request_headers == headers
        assert result.response == json.dumps(response_data)
        assert result.code == 200

    # Tests that an empty headers dictionary does not raise an exception.
    def test_empty_headers(self, requests_mock):
        # Arrange
        url = "https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=xxxx&deploy=xxxx"
        headers = {}
        params = {"param1": "value1"}
        response_data = {"success": True}
        requests_mock.get(url, json=response_data, status_code=200)

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )
        result = ns.get(url=url, headers=headers, params=params)

        # Assert
        assert result.url == url
        assert result.request_headers == headers
        assert result.response == json.dumps(response_data)
        assert result.code == 200

    # Tests that an empty parameters dictionary does not raise an exception.
    def test_empty_parameters(self, requests_mock):
        # Arrange
        url = "https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=xxxx&deploy=xxxx"
        headers = {"Content-Type": "application/json"}
        params = {}
        response_data = {"success": True}
        requests_mock.get(url, json=response_data, status_code=200)

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )
        result = ns.get(url=url, headers=headers, params=params)

        # Assert
        assert result.url == url
        assert result.request_headers == headers
        assert result.response == json.dumps(response_data)
        assert result.code == 200

    # Tests that an empty body dictionary does not raise an exception.
    def test_empty_body(self, requests_mock):
        # Arrange
        url = "https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=xxxx&deploy=xxxx"
        headers = {"Content-Type": "application/json"}
        params = {"param1": "value1"}
        body = {}
        response_data = {"success": True}
        requests_mock.get(url, json=response_data, status_code=200)

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )
        result = ns.get(url=url, headers=headers, params=params, body=body)

        # Assert
        assert result.url == url
        assert result.request_headers == headers
        assert result.response == json.dumps(response_data)
        assert result.code == 200

    # Tests that OAuth1 authentication is successful.
    def test_oauth1_authentication(self, requests_mock):
        # Arrange
        url = "https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=xxxx&deploy=xxxx"
        headers = {"Content-Type": "application/json"}
        params = {"param1": "value1"}
        response_data = {"success": True}
        requests_mock.get(url, json=response_data, status_code=200)

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys=dict(consumer_key="2345678", consumer_secret="3456yhg"),
            token_keys=dict(token_key="wfdbfdsdfg", token_secret="efguhfjoidejhfije"),
        )
        result = ns.get(url=url, headers=headers, params=params)

        # Assert
        assert result.url == url
        assert result.request_headers == headers
        assert result.response == json.dumps(response_data)
        assert result.code == 200

    # Tests that rate limiting is handled properly.
    def test_rate_limiting_handling(self, requests_mock):
        # Arrange
        url = "https://xxxx.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=xxxx&deploy=xxxx"
        headers = {"Content-Type": "application/json"}
        params = {"param1": "value1"}
        response_data = {"success": True}
        requests_mock.get(url, json=response_data, status_code=429)

        # Act
        ns = NetSuite(
            account_id=123456,
            consumer_keys={"consumer_key": "", "consumer_secret": ""},
            token_keys={"token_key": "", "token_secret": ""},
        )
        result = ns.get(url=url, headers=headers, params=params)

        # Assert
        assert result.url == url
        assert result.request_headers == headers
        assert result.response == json.dumps(response_data)
        assert result.code == 429
