from unittest.mock import patch

from pmg.sharpspring import Sharpspring
from tests import PMGTestCase
from tests.fixtures import dbfixture, UserData, OrganisationData


class MockResponse:
    def __init__(self, json_data, status_code):
        self.json_data = json_data
        self.status_code = status_code

    def raise_for_status(self):
        pass

    def json(self):
        return self.json_data


def mocked_requests_post_success(*args, **kwargs):
    return MockResponse(
        {"result": {"creates": [{"success": True,}]}, "error": False}, 200
    )


class TestSharpspring(PMGTestCase):
    def setUp(self):
        super(TestSharpspring, self).setUp()

        self.fx = dbfixture.data(UserData, OrganisationData)
        self.fx.setup()

    def tearDown(self):
        self.fx.teardown()
        super(TestSharpspring, self).tearDown()

    @patch("pmg.sharpspring.requests.post", side_effect=mocked_requests_post_success)
    def test_make_sharpspring_request(self, post_mock):
        sharpspring = Sharpspring()
        details = {
            "emailAddress": "test@example.com",
            "companyName": "Test Company",
        }
        result = sharpspring.call("createLeads", {"objects": [details]})
        post_mock.assert_called()
        self.assertTrue(result["result"]["creates"][0])

    @patch.object(
        Sharpspring,
        "call",
        return_value={"result": {"creates": [{"success": True,}]}, "error": False},
    )
    def test_subscribe_to_list_success(self, sharpspring_call_mock):
        user = self.fx.UserData.admin

        sharpspring = Sharpspring()
        sharpspring.subscribeToList(user, "1234")
        sharpspring_call_mock.assert_any_call(
            "createLeads",
            {
                "objects": [
                    {"emailAddress": user.email, "companyName": user.organisation.name}
                ]
            },
        )
        sharpspring_call_mock.assert_any_call(
            "addListMemberEmailAddress", {"emailAddress": user.email, "listID": "1234"}
        )

    @patch.object(
        Sharpspring,
        "call",
        return_value={
            "result": {"creates": [{"success": False, "error": {"code": 500}}]}
        },
    )
    def test_subscribe_to_list_fail(self, sharpspring_call_mock):
        user = self.fx.UserData.admin

        sharpspring = Sharpspring()
        self.assertRaises(ValueError, sharpspring.subscribeToList, user, "1234")

@@ -408,22 +408,19 @@
 a%0A%0A%0Adef 
-mocked
+get
 _request
@@ -560,16 +560,613 @@
     )%0A%0A%0A
+def get_sharpspring_call_success(*args, **kwargs):%0A    return %7B%22result%22: %7B%22creates%22: %5B%7B%22success%22: True,%7D%5D%7D, %22error%22: False%7D%0A%0A%0Adef get_sharpspring_call_fail_create_lead(*args, **kwargs):%0A    return %7B%22result%22: %7B%22creates%22: %5B%7B%22success%22: False, %22error%22: %7B%22code%22: 500%7D%7D%5D%7D%7D%0A%0A%0Adef get_sharpspring_call_fail_add_list_member(method, params):%0A    if method == %22createLeads%22:%0A        return %7B%22result%22: %7B%22creates%22: %5B%7B%22success%22: True,%7D%5D%7D, %22error%22: False%7D%0A    else:%0A        return %7B%0A            %22result%22: %7B%22creates%22: %5B%7B%22success%22: False, %22error%22: %7B%22code%22: 500%7D%7D%5D%7D,%0A            %22error%22: %7B%22code%22: 500%7D,%0A        %7D%0A%0A%0A
 class Te
@@ -1506,14 +1506,11 @@
 ect=
-mocked
+get
 _req
@@ -1949,107 +1949,60 @@
 ing,
-%0A        %22call%22,%0A        return_value=%7B%22result%22: %7B%22creates%22: %5B%7B%22success%22: True,%7D%5D%7D, %22error%22: False%7D
+ %22call%22, return_value=get_sharpspring_call_success()
 ,%0A  
@@ -2636,137 +2636,401 @@
 ing,
-%0A        %22call%22
+ %22call%22, return_value=get_sharpspring_call_fail_create_lead()
 ,%0A    
+)%0A
     
-return_value=%7B%0A            %22result%22: %7B%22creates%22: %5B%7B%22success%22: Fal
+def test_subscribe_to_list_fail(self, sharpspring_call_mock):%0A        user = self.fx.UserData.admin%0A%0A        sharpspring = Sharpspring()%0A        self.assertRaises(ValueError, sharpspring.subscribeToList, u
 se
+r
 , %22
-error%22: %7B%22code%22: 500%7D%7D%5D%7D%0A        %7D
+1234%22)%0A%0A    @patch.object(%0A        Sharpspring, %22call%22, side_effect=get_sharpspring_call_fail_add_list_member
 ,%0A  

Test sharpspring addListMemberEmailAddress fail