def get(self): #user is uid user = get_current_user(self) if not user: user = self.get_argument( "user", None) # mock user since node client has no cookie if user: try: uid = int(user) except ValueError: err = json.dumps({"err": 4, "what": "uid invalid"}) self.write(err) return #TODO user_name = "TODO FIXME" # get the real user name from DB file_name = self.get_argument("file_name", None) file_hash = self.get_argument("file_hash", None) tags = self.get_argument("tags", None) try: main_type = int(self.get_argument("main_type", 0)) sub_type = int(self.get_argument("sub_type", 0)) res_grade = float(self.get_argument("res_grade", 1)) file_size = int(self.get_argument("file_size", None)) except ValueError: err = json.dumps({ "err": 3, "what": "main_type sub_type or res_grade invalid" }) self.write(err) return comment = self.get_argument("comment", None) try: is_public = int(self.get_argument("is_public", 1)) #default is public except ValueError: err = json.dumps({"err": 5, "what": "is_public invalid"}) self.write(err) return if file_name and file_hash and file_size and tags and \ res_grade and comment: tags_list = tags.split(" ") if (len(tags_list) > 0): ResourcesCache.user_upload_resource( uid, user_name, file_hash, file_name, file_size, is_public, tags_list, main_type, sub_type, res_grade, comment) ok = json.dumps({"err": 0}) self.write(ok) else: err = json.dumps({ "err": 2, "what": "tags or blocks hash invalid" }) self.write(err) else: err = json.dumps({"err": 1, "what": "argument missing error"}) self.write(err)
def test_add_owner(): # user 2 uploaded the resource ResourcesCache.user_upload_resource(2,"user2","file_hash2","test2.txt",1023,1,"test",0,1,3,"user2 uploaded file") ResourcesCache.add_owner('file_hash2',2) #the same user assert ResourcesCache.get_resource_owners("file_hash2")==[2] ResourcesCache.add_owner('file_hash2',20) #the same user assert ResourcesCache.get_resource_owners("file_hash2")==[2,20] ResourcesCache.increase_download_num("file_hash2") assert ResourcesCache.get_resources_list()["file_hash2"]["download_num"]==0 ResourcesCache.increase_download_num("file_hash2") assert ResourcesCache.get_resources_list()["file_hash2"]["download_num"]==1 #if __name__ == "__main__": # test_add_owner()
def test_download(): UserIPCache.update_my_ip(1,"1.1.1.1") UserIPCache.update_my_ip(2,"2.2.2.2") UserIPCache.update_my_ip(3,"2.2.2.2") UserIPCache.update_my_ip(4,"2.2.2.2") #2,3,4 are the same IP UserIPCache.update_my_ip(5,"2:2:2::2") UserIPCache.update_my_ip(6,"2:2:2::3") UserIPCache.update_my_ip(7,"5:2:2::3") UserIPCache.update_my_ip(8,"5:4:2::3") HttpServerInfoCache.update_ipv4_address(1,'192.168.1.101') HttpServerInfoCache.update_ipv4_address(2,'192.168.1.103') HttpServerInfoCache.update_ipv4_address(3,'192.168.1.104') HttpServerInfoCache.update_ipv4_address(4,'192.168.1.105') HttpServerInfoCache.update_ipv6_address(5,'2:2:2::2') HttpServerInfoCache.update_ipv6_address(6,'2:2:2::3') HttpServerInfoCache.update_ipv6_address(7,"5:2:2::3") #7,8 are the same IP HttpServerInfoCache.update_ipv6_address(8,"5:4:2::3") # user 2,3,4 uploaded the same file ResourcesCache.user_upload_resource(2,"user2","file_hash2","test2.txt",1023,1,"test",0,1,3,"user2 uploaded file") ResourcesCache.user_upload_resource(3,"user3","file_hash2","test2.txt",1023,1,"test",0,1,3,"user3 uploaded file") ResourcesCache.user_upload_resource(4,"user4","file_hash2","test2.txt",1023,1,"test",0,1,3,"user4 uploaded file") # user 3,4 uploaded the same file ResourcesCache.user_upload_resource(3,"user3","file_hash3","test4.txt",1023,1,"test",0,1,3,"user3 uploaded file") ResourcesCache.user_upload_resource(4,"user4","file_hash3","test4.txt",1023,1,"test",0,1,3,"user4 uploaded file") # user 5,6,7 uploaded the same file ResourcesCache.user_upload_resource(5,"user5","file_hash5","test5.txt",10123,1,"test",0,1,3,"user5 uploaded file") ResourcesCache.user_upload_resource(6,"user6","file_hash5","test5.txt",10123,1,"test",0,1,3,"user6 uploaded file") ResourcesCache.user_upload_resource(7,"user7","file_hash5","test5.txt",10123,1,"test",0,1,3,"user7 uploaded file") assert DownloadMedium.get_online_file_owner(1,"file_hash2") == [] assert DownloadMedium.get_online_file_owner(1,"file_hash5") == [] assert DownloadMedium.get_online_file_owner(1,"file_hash_not_exist") == [] assert DownloadMedium.get_online_file_owner(8,"file_hash_not_exist") == [] assert DownloadMedium.get_online_file_owner(8,"file_hash2") == [] assert DownloadMedium.get_online_file_owner(2,"file_hash3") == [{"uid": 3, "host": '192.168.1.104', "port":8884},{"uid": 4, "host": '192.168.1.105', "port":8884}] assert DownloadMedium.get_online_file_owner(8,"file_hash5") == [{"uid": 5, "host": '2:2:2::2', "port":8886},{"uid": 6, "host": '2:2:2::3', "port":8886},{"uid": 7, "host": "5:2:2::3", "port":8886}] #if __name__ == "__main__": # test_download()