def test_get_product_list(): tarallo_session = Tarallo(t_url, t_token) pl = tarallo_session.get_product_list("Samsung", "S667ABC1024") assert isinstance(pl, list) assert len(pl) == 2 assert type(pl[0]) == Product assert type(pl[1]) == Product
def test_add_item_cloned(): tarallo_session = Tarallo(t_url, t_token) cpu = tarallo_session.get_item("C100TEST") assert cpu is not None cpu_to_upload = ItemToUpload(cpu) assert cpu.code == cpu_to_upload.code assert cpu.location[-1] == cpu_to_upload.parent assert len(cpu.features) == len(cpu_to_upload.features) # It comes from a computer, path will still contain that, I don't care: I want it on the Table. # Send this location to the server, not the path. cpu_to_upload.set_parent("Table") assert cpu.location[-1] != cpu_to_upload.parent # Let the server generate another code (since there's no way to delete items permanently we # can't test manually assigned codes... or rather we can, but just once) cpu_to_upload.set_code(None) assert cpu.code != cpu_to_upload.code # Add it: should succeed assert tarallo_session.add_item(cpu_to_upload) # Let's get the entire item again and check... # The generated code was added to the original dict cpu_cloned = tarallo_session.get_item(cpu_to_upload.code) assert cpu_cloned.code != cpu.code assert cpu_cloned.location[-1] == cpu_to_upload.parent assert len(cpu_cloned.features) == len(cpu_to_upload.features)
def test_add_item_cloned(): tarallo_session = Tarallo(t_url, t_token) cpu = tarallo_session.get_item("C1") assert cpu is not None # it comes from a computer, path will still contain that, I don't care: I want it on the Table. # Send this location to the server, not the path. cpu.parent = "Table" # Let the server generate another code (since there's no way to delete items permanently we # can't test manually assigned codes... or rather we can, but just once) cpu.code = None # Add it: should succeed assert tarallo_session.add_item(cpu) # This stuff should be updated assert cpu.code is not None assert not cpu.code == "C1" # Just set path to none after inserting an item. The server doesn't return the full path so you have no way to # assign the correct value to this variable. # This assert checks just that: assert cpu.path is None # Let's get the entire item again and check... cpu = tarallo_session.get_item(cpu.code) assert cpu.path[-1:] == ['Table'] assert cpu.location == 'Table' # This may seem redundant, but these are different feature types... assert cpu.features["brand"] == "AMD" assert cpu.features["type"] == "cpu" assert cpu.features["cpu-socket"] == "am3" assert cpu.features["frequency-hertz"] == 3000000000
def test_get_product(): tarallo_session = Tarallo(t_url, t_token) p = tarallo_session.get_product("testBrand", "testModel", "testVariant") assert type(p) == Product assert p.brand == "testBrand" assert p.model == "testModel" assert p.variant == "testVariant" assert isinstance(p.features, dict)
def test_add_item(): tarallo_session = Tarallo(t_url, t_token) ram = ItemToUpload() ram.set_code("!N\\/@L!D") ram.set_parent("Table") ram.features = {'type': 'ram'} tarallo_session.add_item(ram)
def test_add_item(): tarallo_session = Tarallo(t_url, t_token) ram = Item() ram.code = "!N\\/@L!D" ram.features["type"] = "ram" ram.location = "Table" tarallo_session.add_item(ram)
def test_get_product2(): tarallo_session = Tarallo(t_url, t_token) p = tarallo_session.get_product("Samsung", "S667ABC1024", "v1") assert type(p) == Product assert p.brand == "Samsung" assert p.model == "S667ABC1024" assert p.variant == "v1" assert isinstance(p.features, dict) assert len(p.features) > 0
def test_get_item(): tarallo_session = Tarallo(t_url, t_token) item = tarallo_session.get_item('schifomacchina') assert item is not None assert type(item) == Item assert item.code == 'SCHIFOMACCHINA' assert isinstance(item.features, dict) assert isinstance(item.product, Product) assert item.product.features["type"] == "case" assert item.location == ["Polito", "Chernobyl", "Table"]
def test_get_item_history(): tarallo_session = Tarallo(t_url, t_token) history = tarallo_session.get_history('schifomacchina') assert history is not None assert len(history) > 0 for entry in history: assert isinstance(entry, AuditEntry) assert entry.user is not None assert entry.time is not None assert entry.time > 0
def test_update_one_feature(): tarallo_session = Tarallo(t_url, t_token) work = tarallo_session.get_item('R777').features['working'] new_feat = 'yes' # If operation succeeds, return True assert tarallo_session.update_item_features('R777', {'working': new_feat}) freq_updated = tarallo_session.get_item('R777').features['working'] assert freq_updated == new_feat
def test_codes_by_feature(): tarallo_session = Tarallo(t_url, t_token) codes = tarallo_session.get_codes_by_feature("model", "S667ABC256") assert isinstance(codes, Iterable) # noinspection PyTypeChecker assert len(codes) > 0 # These are all motherboards for code in codes: assert isinstance(code, str) assert code.startswith('R') assert code[1:].isdigit()
def test_get_item(): tarallo_session = Tarallo(t_url, t_token) item = tarallo_session.get_item('1') assert item is not None assert type(item) == Item assert item.code == '1' assert isinstance(item.features, dict) assert item.features["type"] == "case" assert item.path[-2:-1][0] == "Polito" assert item.path[-1:][0] == "Magazzino" assert item.location == "Magazzino"
def test_add_item(): tarallo_session = Tarallo(t_url, t_token) data = { 'code': "!N\\/@L!D", 'features': { 'type': 'ram' }, 'parent': 'Table' } ram = ItemToUpload(data) tarallo_session.add_item(ram)
def test_add_remove_products(): tarallo_session = Tarallo(t_url, t_token) data = { "brand": "testBrand", "model": "testModel", "variant": "testVariant", "features": {"psu-volt": 19} } deleted = tarallo_session.delete_product(data.get("brand"), data.get("model"), data.get("variant")) assert deleted or deleted is None p = ProductToUpload(data) assert tarallo_session.add_product(p)
def test_update_one_feature(): tarallo_session = Tarallo(t_url, t_token) freq = tarallo_session.get_item('R777').features['frequency-hertz'] if freq % 2 == 0: new_freq = freq + 1 else: new_freq = freq - 1 # If operation succeeds, return True assert tarallo_session.update_features('R777', {'frequency-hertz': new_freq}) freq_updated = tarallo_session.get_item('R777').features['frequency-hertz'] assert freq_updated == new_freq
def setup_class(cls): # Dummy disk used throughout all the tests cls.dummy_disk = { 'brand': 'PYTHON_TEST', 'capacity-byte': 1, 'hdd-form-factor': '2.5-7mm', 'model': 'TEST', 'sata-ports-n': 1, 'smart-data': SMART.working.value, 'sn': 'USB123456', 'type': 'ssd', } # Load url and token from env load_dotenv() tarallo_url = os.getenv("TARALLO_URL") tarallo_token = os.getenv("TARALLO_TOKEN") try: cls.tarallo_instance = Tarallo(tarallo_url, tarallo_token) status = cls.tarallo_instance.status() except: cls.connected = False else: if status == 200: cls.connected = True else: cls.connected = False finally: if cls.connected is False: print("Couldn't connect to tarallo server") else: cls.tarallo_interface = TaralloInterface(cls.tarallo_instance)
def test_add_products(): # delete product # variant timestamp o number casuale tarallo_session = Tarallo(t_url, t_token) data = { "brand": "testBrand", "model": "testModel", "variant": "testVariant", "features": { "psu-volt": 19 } } tarallo_session.delete_product(data.get("brand"), data.get("model"), data.get("variant")) p = ProductToUpload(data) assert tarallo_session.add_product(p) #raises ValidationError ??
def test_delete_product_feature(): tarallo_session = Tarallo(t_url, t_token) p = tarallo_session.get_product('AMD', 'Opteron 3300', 'AJEJE') assert p.features['type'] == 'cpu' # Add feature if missing if 'color' not in p.features and p.features['color'] == 'red': new_features = {"color": "grey"} tarallo_session.update_product_features('AMD', 'Opteron 3300', 'AJEJE', new_features) p = tarallo_session.get_product('AMD', 'Opteron 3300', 'AJEJE') assert "color" in p.features new_features = {"color": None} tarallo_session.update_product_features('AMD', 'Opteron 3300', 'AJEJE', new_features) p = tarallo_session.get_product('AMD', 'Opteron 3300', 'AJEJE') assert p.features["type"] == 'cpu' assert "color" not in p.features
def test_add_item(): tarallo_session = Tarallo(t_url, t_token) ram = ItemToUpload() ram.features["type"] = "ram" ram.features["color"] = "red" ram.features["capacity-byte"] = 1024 * 1024 * 512 # 512 MiB ram.parent = "Table" assert tarallo_session.add_item(ram) # set the code to the one received from the server assert ram.code is not None assert isinstance(ram.code, str) # Let's get it again and check... ram = tarallo_session.get_item(ram.code) assert ram.location == 'Table' assert ram.features["type"] == "ram" assert ram.features["color"] == "red" assert ram.features["capacity-byte"] == 1024 * 1024 * 512
def test_add_item(): tarallo_session = Tarallo(t_url, t_token) ram = Item() ram.features["type"] = "ram" ram.features["color"] = "red" ram.features["capacity-byte"] = 1024 * 1024 * 512 # 512 MiB ram.location = "LabFis4" assert tarallo_session.add_item(ram) is True # set the code to the one received from the server assert ram.code is not None assert isinstance(ram.code, str) # Let's get it again and check... ram = tarallo_session.get_item(ram.code) assert ram.path[-1:] == ['LabFis4'] assert ram.location == 'LabFis4' assert ram.features["type"] == "ram" assert ram.features["color"] == "red" assert ram.features["capacity-byte"] == 1024 * 1024 * 512
def test_add_item_cloned(): tarallo_session = Tarallo(t_url, t_token) cpu = tarallo_session.get_item("C1") assert cpu is not None # it comes from a computer, path will still contain that, I don't care: I want it on the Table. # Send this location to the server, not the path. # Let the server generate another code (since there's no way to delete items permanently we # can't test manually assigned codes... or rather we can, but just once) # Add it: should succeed # the following lines convert a Item -> ItemToUpload # and test some basic stuff data = cpu.serializable() del data["code"] data["parent"] = data.get("location")[-1] del data["location"] assert data["parent"] == 'Table' cpu_toUpload = ItemToUpload(data) assert tarallo_session.add_item(cpu_toUpload) # Let's get the entire item again and check... cpu = tarallo_session.get_item(cpu.code)
def test_update_p_feature(): tarallo_session = Tarallo(t_url, t_token) p = tarallo_session.get_product('AMD', 'Opteron 3300', 'AJEJE') new_features = p.features new_features['type'] = 'ram' tarallo_session.update_product_features('AMD', 'Opteron 3300', 'AJEJE', new_features) p = tarallo_session.get_product('AMD', 'Opteron 3300', 'AJEJE') assert p.features["type"] == 'ram'
def test_travaso(): tarallo_session = Tarallo(t_url, t_token) test_item = tarallo_session.travaso("schifomacchina", "RamBox") assert test_item item_r69 = tarallo_session.get_item("R69") assert item_r69 is not None assert type(item_r69) == Item assert item_r69.code == 'R69' assert isinstance(item_r69.features, dict) assert item_r69.location == 'RamBox' item_r188 = tarallo_session.get_item("R188") assert item_r188 is not None assert type(item_r188) == Item assert item_r188.code == 'R188' assert isinstance(item_r188.features, dict) assert item_r188.location == 'RamBox' tarallo_session.move("R69", "schifomacchina") tarallo_session.move("R188", "schifomacchina")
def test_delete_one_feature(): tarallo_session = Tarallo(t_url, t_token) # Insert a frequency assert tarallo_session.update_item_features('R70', {'frequency-hertz': 800000000}) # Remove it assert tarallo_session.update_item_features('R70', {'frequency-hertz': None}) # Check that it's gone assert 'frequency-hertz' not in tarallo_session.get_item('R70').features # Add it again assert tarallo_session.update_item_features('R70', {'frequency-hertz': 800000000})
def test_update_product_feature(): tarallo_session = Tarallo(t_url, t_token) p = tarallo_session.get_product('AMD', 'Opteron 3300', 'AJEJE') assert p.features['type'] == 'cpu' if 'color' in p.features and p.features['color'] == 'red': new_features = {"color": "grey"} else: new_features = {"color": "red"} tarallo_session.update_product_features('AMD', 'Opteron 3300', 'AJEJE', new_features) p = tarallo_session.get_product('AMD', 'Opteron 3300', 'AJEJE') assert p.features["type"] == 'cpu' assert p.features["color"] == new_features["color"]
def test_add_duplicate_products(): tarallo_session = Tarallo(t_url, t_token) data = { "brand": "testBrand", "model": "testModel", "variant": "testVariant", "features": {"psu-volt": 12} } p = ProductToUpload(data) # Remove and add deleted = tarallo_session.delete_product(data.get("brand"), data.get("model"), data.get("variant")) assert deleted or deleted is None assert tarallo_session.add_product(p) # This one fails tarallo_session.add_product(p)
def test_travaso(): tarallo_session = Tarallo(t_url, t_token) test_item = tarallo_session.travaso("1", "LabFis4") assert test_item is True item_a2 = tarallo_session.get_item("A2") assert item_a2 is not None assert type(item_a2) == Item assert item_a2.code == 'A2' assert isinstance(item_a2.features, dict) assert item_a2.location == 'LabFis4' item_b1 = tarallo_session.get_item("B1") assert item_b1 is not None assert type(item_b1) == Item assert item_b1.code == 'B1' assert isinstance(item_b1.features, dict) assert item_b1.location == 'LabFis4' tarallo_session.move("A2", "1") tarallo_session.move("B1", "1")
def main(): """main function of the bot""" print("Entered main") oc = owncloud.Client(OC_URL) oc.login(OC_USER, OC_PWD) bot = BotHandler(TOKEN_BOT) tarallo = Tarallo(TARALLO, TARALLO_TOKEN) logs = WeeelabLogs(oc, LOG_PATH, LOG_BASE, USER_BOT_PATH) tolab = ToLab(oc, TOLAB_PATH) if os.path.isfile("weeedong.wav"): wave_obj = simpleaudio.WaveObject.from_wave_file("weeedong.wav") else: wave_obj = simpleaudio.WaveObject.from_wave_file("weeedong_default.wav") users = Users(LDAP_ADMIN_GROUPS, LDAP_TREE_PEOPLE, LDAP_TREE_INVITES) people = People(LDAP_ADMIN_GROUPS, LDAP_TREE_PEOPLE) conn = LdapConnection(LDAP_SERVER, LDAP_USER, LDAP_PASS) wol = WOL_MACHINES handler = CommandHandler(bot, tarallo, logs, tolab, users, people, conn, wol) while True: # call the function to check if there are new messages last_update = bot.get_last_update() if last_update == -1: # When no messages are received... # print("last_update = -1") continue # per Telegram docs, either message or callback_query are None # noinspection PyBroadException try: if "channel_post" in last_update: # Leave scam channels where people add our bot randomly chat_id = last_update['channel_post']['chat']['id'] print(bot.leave_chat(chat_id).text) elif 'message' in last_update: # Handle private messages command = last_update['message']['text'].split() message_type = last_update['message']['chat']['type'] # print(last_update['message']) # Extremely advanced debug techniques # Don't respond to messages in group chats if message_type != "private": continue authorized = handler.read_user_from_message(last_update) if not authorized: continue if command[0] == "/start" or command[0] == "/start@weeelab_bot": handler.start() elif command[0] == "/inlab" or command[0] == "/inlab@weeelab_bot": handler.inlab() elif command[0] == "/history" or command[0] == "/history@weeelab_bot": if len(command) < 2: handler.item_command_error('history') elif len(command) < 3: handler.history(command[1]) else: handler.history(command[1], command[2]) elif command[0] == "/item" or command[0] == "/item@weeelab_bot": if len(command) < 2: handler.item_command_error('item') else: handler.item_info(command[1]) elif command[0] == "/log" or command[0] == "/log@weeelab_bot": if len(command) > 1: handler.log(command[1]) else: handler.log() elif command[0] == "/tolab" or command[0] == "/tolab@weeelab_bot": if len(command) == 2: handler.tolab(command[1]) elif len(command) >= 3: handler.tolab(command[1], command[2]) else: handler.tolab_help() elif command[0] == "/ring": handler.ring(wave_obj) elif command[0] == "/stat" or command[0] == "/stat@weeelab_bot": if len(command) > 1: handler.stat(command[1]) else: handler.stat() elif command[0] == "/top" or command[0] == "/top@weeelab_bot": if len(command) > 1: handler.top(command[1]) else: handler.top() elif command[0] == "/deletecache" or command[0] == "/deletecache@weeelab_bot": handler.delete_cache() elif command[0] == "/help" or command[0] == "/help@weeelab_bot": handler.help() elif command[0] == "/lofi" or command[0] == "/lofi@weeelab_bot": handler.lofi() elif command[0] == "/wol" or command[0] == "/wol@weeelab_bot": handler.wol() elif command[0] == "/logout" or command[0] == "/logout@weeelab_bot": if len(command) > 1: # handler.logout(command[1:]) logout = Thread(target=handler.logout, args=(command[1:],)) logout.start() else: handler.logout_help() else: handler.unknown() elif 'callback_query' in last_update: authorized = handler.read_user_from_callback(last_update) if not authorized: continue # Handle button callbacks query = last_update['callback_query']['data'] message_id = last_update['callback_query']['message']['message_id'] if query.startswith('wol_'): handler.wol_callback(query, message_id) elif query.startswith('lofi_'): handler.lofi_callback(query, message_id) else: handler.unknown() else: print('Unsupported "last_update" type') print(last_update) except: # catch any exception if raised print("ERROR!") print(last_update) print(traceback.format_exc())
def test_move_item_not_existing(): tarallo_session = Tarallo(t_url, t_token) assert tarallo_session.move("INVALID", 'schifomaccchina')
def test_move_item(): tarallo_session = Tarallo(t_url, t_token) assert tarallo_session.move("R111", 'schifomacchina')