def test_disable_when_unregistered(self, mock_client): """Test disabling a device when it is unregistered.""" send = mock_client.return_value.send_notification send.side_effect = Unregistered() config = { 'notify': { 'platform': 'apns', 'name': 'test_app', 'topic': 'testapp.appname', 'cert_file': 'test_app.pem' } } hass = get_test_home_assistant() devices_path = hass.config.path('test_app_apns.yaml') with open(devices_path, 'w+') as out: out.write('1234: {name: test device 1}\n') notify.setup(hass, config) self.assertTrue(hass.services.call('notify', 'test_app', {'message': 'Hello'}, blocking=True)) devices = {str(key): value for (key, value) in load_yaml_config_file(devices_path).items()} test_device_1 = devices.get('1234') self.assertIsNotNone(test_device_1) self.assertEqual(True, test_device_1.get('disabled')) os.remove(devices_path)
def test_register_new_device(self): """Test registering a new device with a name.""" config = { 'notify': { 'platform': 'apns', 'name': 'test_app', 'topic': 'testapp.appname', 'cert_file': 'test_app.pem' } } hass = get_test_home_assistant() devices_path = hass.config.path('test_app_apns.yaml') with open(devices_path, 'w+') as out: out.write('5678: {name: test device 2}\n') notify.setup(hass, config) self.assertTrue(hass.services.call('apns', 'test_app', {'push_id': '1234', 'name': 'test device'}, blocking=True)) devices = {str(key): value for (key, value) in load_yaml_config_file(devices_path).items()} test_device_1 = devices.get('1234') test_device_2 = devices.get('5678') self.assertIsNotNone(test_device_1) self.assertIsNotNone(test_device_2) self.assertEqual('test device', test_device_1.get('name')) os.remove(devices_path)
def test_send_when_disabled(self, mock_client): """Test updating an existing device.""" send = mock_client.return_value.send_notification config = { 'notify': { 'platform': 'apns', 'name': 'test_app', 'topic': 'testapp.appname', 'cert_file': 'test_app.pem' } } hass = get_test_home_assistant() devices_path = hass.config.path('test_app_apns.yaml') with open(devices_path, 'w+') as out: out.write('1234: {name: test device 1, disabled: True}\n') notify.setup(hass, config) self.assertTrue(hass.services.call('notify', 'test_app', {'message': 'Hello', 'data': { 'badge': 1, 'sound': 'test.mp3', 'category': 'testing' } }, blocking=True)) self.assertFalse(send.called)
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() self.events = [] self.assertTrue(notify.setup(self.hass, { 'notify': { 'name': 'demo1', 'platform': 'demo' } })) self.assertTrue(notify.setup(self.hass, { 'notify': { 'name': 'demo2', 'platform': 'demo' } })) self.service = group.get_service(self.hass, {'services': [ {'service': 'demo1'}, {'service': 'demo2', 'data': {'target': 'unnamed device', 'data': {'test': 'message'}}}]}) assert self.service is not None def record_event(event): """Record event to send notification.""" self.events.append(event) self.hass.bus.listen("notify", record_event)
def test_bad_config(self): """Test set up the platform with bad/missing config.""" self.assertFalse(notify.setup(self.hass, { 'notify': { 'name': 'test', 'platform': 'bad_platform', } })) self.assertFalse(notify.setup(self.hass, { 'notify': { 'name': 'test', 'platform': 'command_line', } }))
def test_notify_file(self, mock_utcnow): """Test the notify file output.""" mock_utcnow.return_value = dt_util.as_utc(dt_util.now()) with tempfile.TemporaryDirectory() as tempdirname: filename = os.path.join(tempdirname, 'notify.txt') message = 'one, two, testing, testing' self.assertTrue(notify.setup(self.hass, { 'notify': { 'name': 'test', 'platform': 'file', 'filename': filename, 'timestamp': False, } })) title = '{} notifications (Log started: {})\n{}\n'.format( ATTR_TITLE_DEFAULT, dt_util.utcnow().isoformat(), '-' * 80) self.hass.services.call('notify', 'test', {'message': message}, blocking=True) result = open(filename).read() self.assertEqual(result, "{}{}\n".format(title, message))
def test_notify_file(self, mock_utcnow): """Test the notify file output.""" mock_utcnow.return_value = dt_util.as_utc(dt_util.now()) with tempfile.TemporaryDirectory() as tempdirname: filename = os.path.join(tempdirname, 'notify.txt') message = 'one, two, testing, testing' self.assertTrue( notify.setup( self.hass, { 'notify': { 'name': 'test', 'platform': 'file', 'filename': filename, 'timestamp': 0 } })) title = '{} notifications (Log started: {})\n{}\n'.format( ATTR_TITLE_DEFAULT, dt_util.utcnow().isoformat(), '-' * 80) self.hass.services.call('notify', 'test', {'message': message}, blocking=True) result = open(filename).read() self.assertEqual(result, "{}{}\n".format(title, message))
def test_bad_config(self): """Test set up the platform with bad/missing configuration.""" self.assertFalse(notify.setup(self.hass, { 'notify': { 'name': 'test', 'platform': 'bad_platform', } }))
def setUp(self): # pylint: disable=invalid-name self.hass = ha.HomeAssistant() self.assertTrue(notify.setup(self.hass, {"notify": {"platform": "demo"}})) self.events = [] def record_event(event): self.events.append(event) self.hass.bus.listen(demo.EVENT_NOTIFY, record_event)
def test_error_for_none_zero_exit_code(self, mock_error): """Test if an error is logged for non zero exit codes.""" self.assertTrue( notify.setup( self.hass, {"notify": {"name": "test", "platform": "command_line", "command": "echo $(cat); exit 1"}} ) ) self.hass.services.call("notify", "test", {"message": "error"}, blocking=True) self.assertEqual(1, mock_error.call_count)
def test_apns_setup_missing_topic(self): """Test setup with missing topic.""" config = { 'notify': { 'platform': 'apns', 'cert_file': 'test_app.pem', 'name': 'test_app' } } hass = get_test_home_assistant() self.assertFalse(notify.setup(hass, config))
def test_apns_setup_missing_certificate(self): """Test setup with missing name.""" config = { 'notify': { 'platform': 'apns', 'topic': 'testapp.appname', 'name': 'test_app' } } hass = get_test_home_assistant() self.assertFalse(notify.setup(hass, config))
def test_update_existing_device_with_tracking_id(self): """Test updating an existing device that has a tracking id.""" config = { 'notify': { 'platform': 'apns', 'name': 'test_app', 'topic': 'testapp.appname', 'cert_file': 'test_app.pem' } } hass = get_test_home_assistant() devices_path = hass.config.path('test_app_apns.yaml') with open(devices_path, 'w+') as out: out.write('1234: {name: test device 1, ' 'tracking_device_id: tracking123}\n') out.write('5678: {name: test device 2, ' 'tracking_device_id: tracking456}\n') notify.setup(hass, config) self.assertTrue(hass.services.call('apns', 'test_app', {'push_id': '1234', 'name': 'updated device 1'}, blocking=True)) devices = {str(key): value for (key, value) in load_yaml_config_file(devices_path).items()} test_device_1 = devices.get('1234') test_device_2 = devices.get('5678') self.assertIsNotNone(test_device_1) self.assertIsNotNone(test_device_2) self.assertEqual('tracking123', test_device_1.get('tracking_device_id')) self.assertEqual('tracking456', test_device_2.get('tracking_device_id')) os.remove(devices_path)
def setUp(self): # pylint: disable=invalid-name self.hass = get_test_home_assistant() self.assertTrue( notify.setup(self.hass, {'notify': { 'platform': 'demo' }})) self.events = [] def record_event(event): self.events.append(event) self.hass.bus.listen(demo.EVENT_NOTIFY, record_event)
def test_apns_setup_missing_name(self): """Test setup with missing name.""" config = { 'notify': { 'platform': 'apns', 'sandbox': 'True', 'topic': 'testapp.appname', 'cert_file': 'test_app.pem' } } hass = get_test_home_assistant() self.assertFalse(notify.setup(hass, config))
def test_send(self, mock_client): """Test updating an existing device.""" send = mock_client.return_value.send_notification config = { 'notify': { 'platform': 'apns', 'name': 'test_app', 'topic': 'testapp.appname', 'cert_file': 'test_app.pem' } } hass = get_test_home_assistant() devices_path = hass.config.path('test_app_apns.yaml') with open(devices_path, 'w+') as out: out.write('1234: {name: test device 1}\n') notify.setup(hass, config) self.assertTrue(hass.services.call('notify', 'test_app', {'message': 'Hello', 'data': { 'badge': 1, 'sound': 'test.mp3', 'category': 'testing' } }, blocking=True)) self.assertTrue(send.called) self.assertEqual(1, len(send.mock_calls)) target = send.mock_calls[0][1][0] payload = send.mock_calls[0][1][1] self.assertEqual('1234', target) self.assertEqual('Hello', payload.alert) self.assertEqual(1, payload.badge) self.assertEqual('test.mp3', payload.sound) self.assertEqual('testing', payload.category)
def test_apns_setup_full(self): """Test setup with all data.""" config = { 'notify': { 'platform': 'apns', 'name': 'test_app', 'sandbox': 'True', 'topic': 'testapp.appname', 'cert_file': 'test_app.pem' } } self.assertTrue(notify.setup(self.hass, config))
def test_error_for_none_zero_exit_code(self, mock_error): """ Test if an error if logged for non zero exit codes. """ self.assertTrue(notify.setup(self.hass, { 'notify': { 'name': 'test', 'platform': 'command_line', 'command': 'echo $(cat); exit 1' } })) self.hass.services.call('notify', 'test', {'message': 'error'}, blocking=True) self.assertEqual(1, mock_error.call_count)
def setUp(self): # pylint: disable=invalid-name self.hass = get_test_home_assistant() self.assertTrue(notify.setup(self.hass, { 'notify': { 'platform': 'demo' } })) self.events = [] def record_event(event): self.events.append(event) self.hass.bus.listen(demo.EVENT_NOTIFY, record_event)
def test_apns_setup_full(self): """Test setup with all data.""" config = { "notify": { "platform": "apns", "name": "test_app", "sandbox": "True", "topic": "testapp.appname", "cert_file": "test_app.pem", } } self.assertTrue(notify.setup(self.hass, config))
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() self.events = [] self.assertTrue( notify.setup(self.hass, {'notify': { 'name': 'demo1', 'platform': 'demo' }})) self.assertTrue( notify.setup(self.hass, {'notify': { 'name': 'demo2', 'platform': 'demo' }})) self.service = group.get_service( self.hass, { 'services': [{ 'service': 'demo1' }, { 'service': 'demo2', 'data': { 'target': 'unnamed device', 'data': { 'test': 'message' } } }] }) assert self.service is not None def record_event(event): """Record event to send notification.""" self.events.append(event) self.hass.bus.listen("notify", record_event)
def test_error_for_none_zero_exit_code(self, mock_error): """Test if an error is logged for non zero exit codes.""" self.assertTrue(notify.setup(self.hass, { 'notify': { 'name': 'test', 'platform': 'command_line', 'command': 'echo $(cat); exit 1' } })) self.hass.services.call('notify', 'test', {'message': 'error'}, blocking=True) self.assertEqual(1, mock_error.call_count)
def test_apns_setup_full(self): """Test setup with all data.""" config = { 'notify': { 'platform': 'apns', 'name': 'test_app', 'sandbox': 'True', 'topic': 'testapp.appname', 'cert_file': 'test_app.pem' } } hass = get_test_home_assistant() self.assertTrue(notify.setup(hass, config))
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() self.assertTrue( notify.setup(self.hass, {'notify': { 'platform': 'demo' }})) self.events = [] def record_event(event): """Record event to send notification.""" self.events.append(event) self.hass.bus.listen(demo.EVENT_NOTIFY, record_event)
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() self.assertTrue(notify.setup(self.hass, { 'notify': { 'platform': 'demo' } })) self.events = [] def record_event(event): """Record event to send notification.""" self.events.append(event) self.hass.bus.listen(demo.EVENT_NOTIFY, record_event)
def test_command_line_output(self): with tempfile.TemporaryDirectory() as tempdirname: filename = os.path.join(tempdirname, 'message.txt') message = 'one, two, testing, testing' self.assertTrue(notify.setup(self.hass, { 'notify': { 'name': 'test', 'platform': 'command_line', 'command': 'echo $(cat) > {}'.format(filename) } })) self.hass.services.call('notify', 'test', {'message': message}, blocking=True) result = open(filename).read() # the echo command adds a line break self.assertEqual(result, "{}\n".format(message))
def test_command_line_output(self): """Test the command line output.""" with tempfile.TemporaryDirectory() as tempdirname: filename = os.path.join(tempdirname, 'message.txt') message = 'one, two, testing, testing' self.assertTrue(notify.setup(self.hass, { 'notify': { 'name': 'test', 'platform': 'command_line', 'command': 'echo $(cat) > {}'.format(filename) } })) self.hass.services.call('notify', 'test', {'message': message}, blocking=True) result = open(filename).read() # the echo command adds a line break self.assertEqual(result, "{}\n".format(message))
def test_command_line_output(self): """Test the command line output.""" with tempfile.TemporaryDirectory() as tempdirname: filename = os.path.join(tempdirname, "message.txt") message = "one, two, testing, testing" self.assertTrue( notify.setup( self.hass, { "notify": { "name": "test", "platform": "command_line", "command": "echo $(cat) > {}".format(filename), } }, ) ) self.hass.services.call("notify", "test", {"message": message}, blocking=True) result = open(filename).read() # the echo command adds a line break self.assertEqual(result, "{}\n".format(message))
def test_bad_config(self): """Test set up the platform with bad/missing config.""" self.assertFalse(notify.setup(self.hass, {"notify": {"name": "test", "platform": "bad_platform"}})) self.assertFalse(notify.setup(self.hass, {"notify": {"name": "test", "platform": "command_line"}}))