def test_gcm_key_include(self, mock_wp): """Test if the gcm_key is only included for GCM endpoints.""" opp = MagicMock() data = {"chrome": SUBSCRIPTION_1, "firefox": SUBSCRIPTION_2} m = mock_open(read_data=json.dumps(data)) with patch("openpeerpower.util.json.open", m, create=True): service = html5.get_service(opp, { "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
def test_sending_message(self, mock_wp): """Test sending message.""" opp = MagicMock() data = {"device": SUBSCRIPTION_1} m = mock_open(read_data=json.dumps(data)) with patch("openpeerpower.util.json.open", m, create=True): service = html5.get_service(opp, {"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"
def test_get_service_with_no_json(self): """Test empty json file.""" opp = MagicMock() m = mock_open() with patch("openpeerpower.util.json.open", m, create=True): service = html5.get_service(opp, {}) assert service is not None
def test_fcm_additional_data(self, mock_wp): """Test if the gcm_key is only included for GCM endpoints.""" opp = MagicMock() data = {"chrome": SUBSCRIPTION_5} m = mock_open(read_data=json.dumps(data)) with patch("openpeerpower.util.json.open", m, create=True): service = html5.get_service(opp, 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"
def test_fcm_key_include(self, mock_wp): """Test if the FCM header is included.""" opp = MagicMock() data = {"chrome": SUBSCRIPTION_5} m = mock_open(read_data=json.dumps(data)) with patch("openpeerpower.util.json.open", m, create=True): service = html5.get_service(opp, 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