import time
import unittest
from nose.plugins.attrib import attr
from hubspot3.test import helper
from hubspot3.broadcast import Broadcast, BroadcastClient


class BroadcastClientTest(unittest.TestCase):
    """ Unit tests for the HubSpot Broadcast API Python client.

    This file contains some unittest tests for the Broadcast API.

    Questions, comments: http://docs.hubapi.com/wiki/Discussion_Group
    """

    def setUp(self):
        self.client = BroadcastClient(**helper.get_options())
        self.broadcast_guids = None

    def tearDown(self):
        # Cancel any broadcasts created as part of the tests
        if self.broadcast_guids:
            list(map(self.client.cancel_broadcast, self.broadcast_guids))

    @attr("api")
    def test_get_broadcasts(self):
        # Should fetch at least 1 broadcast on the test portal 62515
        broadcasts = self.client.get_broadcasts(limit=1)
        self.assertTrue(len(broadcasts) > 0)

        broadcast = broadcasts[0].to_dict()
        self.assertIsNotNone(broadcast["channelGuid"])
        print("\n\nFetched some broadcasts")

        broadcast_guid = broadcast["broadcastGuid"]
        # Re-fetch the broadcast using different call
        bcast = self.client.get_broadcast(broadcast_guid)
        # Should have expected fields
        self.assertIsNotNone(bcast.broadcast_guid)
        self.assertIsNotNone(bcast.channel_guid)
        self.assertIsNotNone(bcast.status)

    @attr("api")
    def test_get_channels(self):
        # Fetch older channels ensured to exist
        channels = self.client.get_channels(current=True)
        self.assertTrue(len(channels) > 0)

    @attr("api")
    def test_create_broadcast(self):
        content = dict(body="Test hubspot3 unit tests http://www.hubspot.com")
        channels = self.client.get_channels(current=True, publish_only=True)
        if len(channels) == 0:
            self.fail("Failed to find a publishable channel")

        channel = channels[0]

        # Get a trigger in the future
        trigger_at = int(time.time() + 6000) * 1000
        bcast = Broadcast(
            {
                "content": content,
                "triggerAt": trigger_at,
                "channelGuid": channel.channel_guid,
            }
        )

        try:
            resp = self.client.create_broadcast(bcast)
            broadcast = Broadcast(resp)
            self.assertIsNotNone(broadcast.broadcast_guid)
            self.assertEqual(channel.channel_guid, broadcast.channel_guid)
            # Ensure it is canceled
            self.broadcast_guids = []
            self.broadcast_guids.append(broadcast.broadcast_guid)
        except Exception as e:
            self.fail("Should not have raised exception: {}".format(e))


if __name__ == "__main__":
    unittest.main()

[((25, 5, 25, 16), 'nose.plugins.attrib.attr', 'attr', ({(25, 10, 25, 15): '"""api"""'}, {}), "('api')", False, 'from nose.plugins.attrib import attr\n'), ((43, 5, 43, 16), 'nose.plugins.attrib.attr', 'attr', ({(43, 10, 43, 15): '"""api"""'}, {}), "('api')", False, 'from nose.plugins.attrib import attr\n'), ((49, 5, 49, 16), 'nose.plugins.attrib.attr', 'attr', ({(49, 10, 49, 15): '"""api"""'}, {}), "('api')", False, 'from nose.plugins.attrib import attr\n'), ((81, 4, 81, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((60, 16, 66, 9), 'hubspot3.broadcast.Broadcast', 'Broadcast', ({(61, 12, 65, 13): "{'content': content, 'triggerAt': trigger_at, 'channelGuid': channel.\n    channel_guid}"}, {}), "({'content': content, 'triggerAt': trigger_at, 'channelGuid':\n    channel.channel_guid})", False, 'from hubspot3.broadcast import Broadcast, BroadcastClient\n'), ((70, 24, 70, 39), 'hubspot3.broadcast.Broadcast', 'Broadcast', ({(70, 34, 70, 38): 'resp'}, {}), '(resp)', False, 'from hubspot3.broadcast import Broadcast, BroadcastClient\n'), ((17, 40, 17, 60), 'hubspot3.test.helper.get_options', 'helper.get_options', ({}, {}), '()', False, 'from hubspot3.test import helper\n'), ((59, 25, 59, 36), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n')]