def undo(self): rev_list = self.get_rev_list() res = "" if len(rev_list) > 0: doc = self.db.get(self.doc['_id'], rev=rev_list[0]) self.doc['device_name'] = doc['device_name'] self.doc['device_type'] = doc['device_type'] self.doc['name'] = doc['name'] self.doc['action'] = doc['action'] self.doc['changed_by'] = 'user' res = self.db.save_doc(self.doc, force_update=True) else: self.doc['_deleted'] = True self.remove_from_hostapd_blacklist(self.doc['mac_address']) res = self.db.save_doc(self.doc, force_update=True) doc_arr = [{ 'doc_id': self.doc['_id'], 'doc_rev': res['rev'], 'doc_collection': self.doc['collection'], 'action': 'delete' }] add_history.add_history_item("Device removed", "%s has been removed" % self.doc['device_name'], doc_arr, undoable=False) return res['rev']
def test_process_undo_notification_edit_doc(self): nd = { "collection": "notifications", "status": "done", "name": "Rob", "service": "twitter", "user": "******" } res = self.db.save_doc(nd) notification_registration_client.registration(nd) nd['user'] = '******' res2 = self.db.save_doc(nd, force_update=True) title = "Edit Notification" desc = "Edited twitter username for Rob now identified by robjspencer" doc_arr = [{ 'doc_id': res2['id'], 'doc_rev': res2['rev'], 'doc_collection': 'notifications', 'action': 'edit' }] event_res = add_history_item(title, desc, doc_arr, True) event = self.db.get(event_res['id']) result = perform_undo.perform_undo(event) updated = self.db.get(nd['_id'], rev=result) self.assertEqual(updated['user'], 'rjspencer1989') nd['hidden'] = True self.db.save_doc(nd, force_update=True) event['_deleted'] = True self.db.save_doc(event, force_update=True)
def test_process_undo_notification_delete_doc(self): nd = { "collection": "notifications", "status": "done", "name": "Rob", "service": "twitter", "user": "******" } self.db.save_doc(nd) notification_registration_client.registration(nd) nd = self.db.get(nd['_id']) nd['hidden'] = True res = self.db.save_doc(nd) title = "Deleted Notification" desc = "Removed rjspencer1989 as twitter username for Rob" doc_arr = [{ 'doc_id': res['id'], 'doc_rev': res['rev'], 'doc_collection': 'notifications', 'action': 'delete' }] event_res = add_history_item(title, desc, doc_arr, undoable=True) event = self.db.get(event_res['id']) result = perform_undo.perform_undo(event) updated = self.db.get(nd['_id'], rev=result) self.assertTrue('hidden' not in updated) self.assertTrue('suid' in updated) event['_deleted'] = True self.db.save_doc(event, force_update=True)
def check_device_status(self): remote_stream = ChangesStream(self.selected_db, heartbeat=True, since=self.last_seq, filter='homework-remote/devices_pox') for change in remote_stream: self.last_seq = change['seq'] the_id = change['id'] the_rev = change['changes'][0]['rev'] doc = self.selected_db.open_doc(the_id, rev=the_rev) prompt = True if (doc['state'] == 'pending') else False doc_arr = [{'doc_id': the_id, 'doc_rev': the_rev, 'doc_collection': 'devices', 'action': 'edit'}] strings = self.get_history_strings(doc['device_name'], doc['action']) timestamp = doc['event_timestamp'] if 'event_timestamp' in doc else None add_history_item(strings['title'], strings['desc'], docs=doc_arr, undoable=True, prompt=prompt, ts=timestamp) device = {'mac': EthAddr(doc['mac_address']), 'action': doc['action']} devices = [device] self.raiseEvent(DeviceStateChange(devices)) core.callDelayed(1, self.check_device_status)
def test_process_undo_device_doc(self): doc = { "_id": "11:aa:33:bb:cc:ff", "action": "", "collection": "devices", "device_name": "", "host_name": "test-device", "ip_address": "10.2.0.61", "lease_action": "add", "mac_address": "11:aa:33:bb:cc:ff", "name": "", "state": "pending", "device_type": "", "notification_service": "", "timestamp": time.time(), "connection_event": "connect", "changed_by": "system" } self.db.save_doc(doc) # initial from DHCP doc['action'] = 'deny' doc['device_name'] = 'test-device' doc['name'] = 'Rob' doc['device_type'] = 'laptop' doc['notification_service'] = 'phone' doc['changed_by'] = 'user' self.db.save_doc(doc) # initial user decision doc['action'] = '' doc['state'] = 'deny' doc['changed_by'] = 'system' self.db.save_doc(doc) # POX applying change doc['action'] = 'permit' doc['changed_by'] = 'user' res = self.db.save_doc(doc) # user changes their mind title = "Permit Device" desc = "Permitted Test Device" doc_arr = [{ 'doc_id': res['id'], 'doc_rev': res['rev'], 'doc_collection': 'devices', 'action': 'edit' }] event_res = add_history_item(title, desc, doc_arr, True) event = self.db.get(event_res['id']) result = perform_undo.perform_undo(event) updated = self.db.open_doc(doc['_id'], rev=result) self.assertEqual('deny', updated['action']) event['_deleted'] = True doc['_deleted'] = True self.db.save_doc(doc, force_update=True) self.db.save_doc(event, force_update=True)
def test_get_reverted_events(self): self.notification_doc['hidden'] = True res5 = self.db.save_doc(self.notification_doc, force_update=True) doc_arr = [{ 'doc_id': res5['id'], 'doc_rev': res5['rev'], 'doc_collection': 'notifications', 'action': 'delete' }] self.hist5 = add_history.add_history_item(self.title, self.description, doc_arr, undoable=True) self.test_doc_ids.append(self.hist5['id']) result = self.undo_revert.get_reverted_events()
def test_process_undo_wifi_edit_doc(self): nd = { "collection": "wifi", "status": "pending", "ssid": "test", "channel": 1, "mode": "g", "encryption_type": "wpa", "password_type": "txt", "password": "******", } cons = edit_wifi keys = [ 'interface', 'bridge', 'driver', 'ssid', 'hw_mode', 'channel', 'ieee80211n', 'macaddr_acl', 'auth_algs', 'ignore_broadcast_ssid', 'eapol_key_index_workaround', 'eap_server', 'own_ip_addr', 'wpa', 'wpa_passphrase', 'wpa_key_mgmt', 'wpa_pairwise', 'rsn_pairwise' ] values = [ 'wlan0', 'br0', 'nl80211', 'test_old', 'g', '1', '1', '0', '1', '0', '0', '0', '127.0.0.1', '3', 'whatever', 'WPA-PSK', 'TKIP', 'CCMP' ] cons.get_config = MagicMock(return_value=(keys, values)) res = self.db.save_doc(nd) nd['status'] = 'done' self.db.save_doc(nd, force_update=True) nd['ssid'] = 'robjspencer' nd['status'] = 'pending' res3 = self.db.save_doc(nd, force_update=True) title = "edit wifi" desc = "Edited wifi config" doc_arr = [{ 'doc_id': nd['_id'], 'doc_rev': res3['rev'], 'doc_collection': 'wifi', 'action': 'edit' }] event_res = add_history_item(title, desc, doc_arr, True) event = self.db.get(event_res['id']) result = perform_undo.perform_undo(event) updated = self.db.get(nd['_id'], rev=result) self.assertEqual(updated['ssid'], 'test') nd['_deleted'] = True self.db.save_doc(nd, force_update=True) event['_deleted'] = True self.db.save_doc(event, force_update=True)
def test_history(self): doc_arr = [{ 'doc_id': "aabbc", 'doc_rev': "2-33aabbcc", 'doc_collection': 'wifi', 'action': 'edit' }] result = add_history.add_history_item("Change WiFi", "Wifi Updated", doc_arr, True) self.assertIsNotNone(result) db = couchdb_config_parser.get_db() doc = db.get(result['id']) self.assertIsNotNone(doc) self.assertEqual(doc['collection'], 'events') self.assertIn('timestamp', doc) self.assertEqual("Change WiFi", doc['title']) self.assertEqual("Wifi Updated", doc['description']) self.assertEqual("aabbc", doc['docs'][0]['doc_id']) self.assertEqual("2-33aabbcc", doc['docs'][0]['doc_rev']) self.assertTrue(doc['undoable']) self.assertFalse(doc['perform_undo']) doc['_deleted'] = True db.save_doc(doc)
def setUp(self): self.test_doc_ids = [] self.title = "test rollback" self.description = "test rollback description" self.wifi_doc = { "collection": "wifi", "status": "done", "ssid": "testing", "mode": "g", "encryption_type": "wpa", "password_type": "txt", "password": "******", "channel": 1, "with_bss": False } self.notification_doc = { "collection": "notifications", "name": "Rob", "service": "phone", "user": "******", "status": "done" } self.revert_doc = { "collection": "request_revert", "timestamp": datetime.datetime(2015, 1, 20, hour=10, minute=25).isoformat(), "status": "pending" } res_rev_doc = self.db.save_doc(self.revert_doc) self.test_doc_ids.append(res_rev_doc['id']) self.revert_doc = self.db.get(res_rev_doc['id']) res1 = self.db.save_doc(self.wifi_doc) self.test_doc_ids.append(res1['id']) dt = datetime.datetime(2015, 1, 5, hour=10, minute=5) doc_arr = [{ 'doc_id': res1['id'], 'doc_rev': res1['rev'], 'doc_collection': 'wifi', 'action': 'edit' }] self.hist1 = add_history.add_history_item(self.title, self.description, doc_arr, True, ts=dt.isoformat()) self.test_doc_ids.append(self.hist1['id']) self.wifi_doc['ssid'] = 'testing2' res2 = self.db.save_doc(self.wifi_doc) dt = datetime.datetime(2015, 2, 5, hour=10, minute=5) doc_arr = [{ 'doc_id': res2['id'], 'doc_rev': res2['rev'], 'doc_collection': 'wifi', 'action': 'edit' }] self.hist2 = add_history.add_history_item(self.title, self.description, doc_arr, True, ts=dt.isoformat()) self.test_doc_ids.append(self.hist2['id']) self.wifi_doc['ssid'] = 'testing3' res3 = self.db.save_doc(self.wifi_doc) dt = datetime.datetime(2015, 2, 23, hour=15, minute=0) doc_arr = [{ 'doc_id': res3['id'], 'doc_rev': res3['rev'], 'doc_collection': 'wifi', 'action': 'edit' }] self.hist3 = add_history.add_history_item(self.title, self.description, doc_arr, True, ts=dt.isoformat()) self.test_doc_ids.append(self.hist3['id']) res4 = self.db.save_doc(self.notification_doc) self.test_doc_ids.append(res4['id']) self.notification_doc = self.db.get(res4['id']) dt = datetime.datetime(2015, 2, 12, hour=14, minute=34) doc_arr = [{ 'doc_id': res4['id'], 'doc_rev': res4['rev'], 'doc_collection': 'notifications', 'action': 'add' }] self.notification_doc = self.db.get(res4['id']) self.hist4 = add_history.add_history_item(self.title, self.description, doc_arr, True, ts=dt.isoformat()) self.test_doc_ids.append(self.hist4['id']) self.rb = perform_rollback.Rollback(self.db, self.revert_doc) rd = self.db.get(self.revert_doc['_id'], revs_info=True) dt = datetime.datetime(2015, 7, 23, hour=15, minute=0) doc_arr = [{ 'doc_id': rd['_id'], 'doc_rev': rd['_rev'], 'doc_collection': 'request_revert', 'action': 'edit' }] self.rd_hist = add_history.add_history_item("unrev", "unrev", doc_arr, ts=dt.isoformat()) self.test_doc_ids.append(self.rd_hist['id']) self.undo_revert = request_revert.Request_revert( rd, self.db.get(self.rd_hist['id']))