Ejemplo n.º 1
0
    def test_sending_message(self, mock_wp):
        """Test sending message."""
        hass = MagicMock()

        data = {"device": SUBSCRIPTION_1}

        m = mock_open(read_data=json.dumps(data))
        with patch("homeassistant.util.json.open", m, create=True):
            service = html5.get_service(hass, {"gcm_sender_id": "100"})

        assert service is not None

        service.send_message("Hello",
                             target=["device", "non_existing"],
                             data={"icon": "beer.png"})

        assert len(mock_wp.mock_calls) == 3

        # WebPusher constructor
        assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1["subscription"]
        # Third mock_call checks the status_code of the response.
        assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"

        # Call to send
        payload = json.loads(mock_wp.mock_calls[1][1][0])

        assert payload["body"] == "Hello"
        assert payload["icon"] == "beer.png"
Ejemplo n.º 2
0
    def test_gcm_key_include(self, mock_wp):
        """Test if the gcm_key is only included for GCM endpoints."""
        hass = MagicMock()

        data = {"chrome": SUBSCRIPTION_1, "firefox": SUBSCRIPTION_2}

        m = mock_open(read_data=json.dumps(data))
        with patch("homeassistant.util.json.open", m, create=True):
            service = html5.get_service(hass, {
                "gcm_sender_id": "100",
                "gcm_api_key": "Y6i0JdZ0mj9LOaSI"
            })

        assert service is not None

        service.send_message("Hello", target=["chrome", "firefox"])

        assert len(mock_wp.mock_calls) == 6

        # WebPusher constructor
        assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1["subscription"]
        assert mock_wp.mock_calls[3][1][0] == SUBSCRIPTION_2["subscription"]

        # Third mock_call checks the status_code of the response.
        assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
        assert mock_wp.mock_calls[5][0] == "().send().status_code.__eq__"

        # Get the keys passed to the WebPusher's send method
        assert mock_wp.mock_calls[1][2]["gcm_key"] is not None
        assert mock_wp.mock_calls[4][2]["gcm_key"] is None
Ejemplo n.º 3
0
    def test_dismissing_message(self, mock_wp):
        """Test dismissing message."""
        hass = MagicMock()

        data = {'device': SUBSCRIPTION_1}

        m = mock_open(read_data=json.dumps(data))
        with patch('homeassistant.util.json.open', m, create=True):
            service = html5.get_service(hass, {'gcm_sender_id': '100'})

        assert service is not None

        service.dismiss(target=['device', 'non_existing'],
                        data={'tag': 'test'})

        assert len(mock_wp.mock_calls) == 3

        # WebPusher constructor
        assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1['subscription']
        # Third mock_call checks the status_code of the response.
        assert mock_wp.mock_calls[2][0] == '().send().status_code.__eq__'

        # Call to send
        payload = json.loads(mock_wp.mock_calls[1][1][0])

        assert payload['dismiss'] is True
        assert payload['tag'] == 'test'
Ejemplo n.º 4
0
    def test_gcm_key_include(self, mock_wp):
        """Test if the gcm_key is only included for GCM endpoints."""
        hass = MagicMock()

        data = {'chrome': SUBSCRIPTION_1, 'firefox': SUBSCRIPTION_2}

        m = mock_open(read_data=json.dumps(data))
        with patch('homeassistant.util.json.open', m, create=True):
            service = html5.get_service(hass, {
                'gcm_sender_id': '100',
                'gcm_api_key': 'Y6i0JdZ0mj9LOaSI'
            })

        assert service is not None

        service.send_message('Hello', target=['chrome', 'firefox'])

        assert len(mock_wp.mock_calls) == 6

        # WebPusher constructor
        assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1['subscription']
        assert mock_wp.mock_calls[3][1][0] == SUBSCRIPTION_2['subscription']

        # Third mock_call checks the status_code of the response.
        assert mock_wp.mock_calls[2][0] == '().send().status_code.__eq__'
        assert mock_wp.mock_calls[5][0] == '().send().status_code.__eq__'

        # Get the keys passed to the WebPusher's send method
        assert mock_wp.mock_calls[1][2]['gcm_key'] is not None
        assert mock_wp.mock_calls[4][2]['gcm_key'] is None
Ejemplo n.º 5
0
    def test_dismissing_message(self, mock_wp):
        """Test dismissing message."""
        hass = MagicMock()

        data = {
            'device': SUBSCRIPTION_1
        }

        m = mock_open(read_data=json.dumps(data))
        with patch(
            'homeassistant.util.json.open',
            m, create=True
        ):
            service = html5.get_service(hass, {'gcm_sender_id': '100'})

        assert service is not None

        service.dismiss(target=['device', 'non_existing'],
                        data={'tag': 'test'})

        assert len(mock_wp.mock_calls) == 3

        # WebPusher constructor
        assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1['subscription']
        # Third mock_call checks the status_code of the response.
        assert mock_wp.mock_calls[2][0] == '().send().status_code.__eq__'

        # Call to send
        payload = json.loads(mock_wp.mock_calls[1][1][0])

        assert payload['dismiss'] is True
        assert payload['tag'] == 'test'
Ejemplo n.º 6
0
    def test_gcm_key_include(self, mock_wp):
        """Test if the gcm_key is only included for GCM endpoints."""
        hass = MagicMock()

        data = {
            'chrome': SUBSCRIPTION_1,
            'firefox': SUBSCRIPTION_2
        }

        m = mock_open(read_data=json.dumps(data))
        with patch('homeassistant.util.json.open', m, create=True):
            service = html5.get_service(hass, {
                'gcm_sender_id': '100',
                'gcm_api_key': 'Y6i0JdZ0mj9LOaSI'
            })

        assert service is not None

        service.send_message('Hello', target=['chrome', 'firefox'])

        assert len(mock_wp.mock_calls) == 6

        # WebPusher constructor
        assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_1['subscription']
        assert mock_wp.mock_calls[3][1][0] == SUBSCRIPTION_2['subscription']

        # Third mock_call checks the status_code of the response.
        assert mock_wp.mock_calls[2][0] == '().send().status_code.__eq__'
        assert mock_wp.mock_calls[5][0] == '().send().status_code.__eq__'

        # Get the keys passed to the WebPusher's send method
        assert mock_wp.mock_calls[1][2]['gcm_key'] is not None
        assert mock_wp.mock_calls[4][2]['gcm_key'] is None
Ejemplo n.º 7
0
    def test_get_service_with_no_json(self):
        """Test empty json file."""
        hass = MagicMock()

        m = mock_open()
        with patch("homeassistant.util.json.open", m, create=True):
            service = html5.get_service(hass, {})

        assert service is not None
Ejemplo n.º 8
0
    def test_get_service_with_no_json(self):
        """Test empty json file."""
        hass = MagicMock()

        m = mock_open()
        with patch(
            'homeassistant.util.json.open',
            m, create=True
        ):
            service = html5.get_service(hass, {})

        assert service is not None
Ejemplo n.º 9
0
    def test_fcm_additional_data(self, mock_wp):
        """Test if the gcm_key is only included for GCM endpoints."""
        hass = MagicMock()

        data = {"chrome": SUBSCRIPTION_5}

        m = mock_open(read_data=json.dumps(data))
        with patch("homeassistant.util.json.open", m, create=True):
            service = html5.get_service(hass, VAPID_CONF)

        assert service is not None

        service.send_message("Hello", data={"mykey": "myvalue"})

        assert len(mock_wp.mock_calls) == 3
        # WebPusher constructor
        assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_5["subscription"]

        # Third mock_call checks the status_code of the response.
        assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"

        # Get the keys passed to the WebPusher's send method
        assert mock_wp.mock_calls[1][2]["headers"]["priority"] == "normal"
Ejemplo n.º 10
0
    def test_fcm_key_include(self, mock_wp):
        """Test if the FCM header is included."""
        hass = MagicMock()

        data = {"chrome": SUBSCRIPTION_5}

        m = mock_open(read_data=json.dumps(data))
        with patch("homeassistant.util.json.open", m, create=True):
            service = html5.get_service(hass, VAPID_CONF)

        assert service is not None

        service.send_message("Hello", target=["chrome"])

        assert len(mock_wp.mock_calls) == 3
        # WebPusher constructor
        assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_5["subscription"]

        # Third mock_call checks the status_code of the response.
        assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"

        # Get the keys passed to the WebPusher's send method
        assert mock_wp.mock_calls[1][2]["headers"]["Authorization"] is not None