Esempio n. 1
0
 def test_change_folder(self):
     """testing the changing folder in directory"""
     obj = server_class([], [], [], {}, {}, {}, [])
     path = os.getcwd()
     list = []
     list.append(path)
     asyncio.run(
         obj.register_account('akhil', 'love', 'user', '121.0.0.5',
                              list[0]))
     try:
         os.chdir('akhil')
     except:
         pass
     asyncio.run(obj.create_folder('data', '121.0.0.5', list[0]))
     asyncio.run(obj.change_folder('data', '121.0.0.5', list[0]))
     asyncio.run(
         obj.write_file('work.txt', 'presentation is needed', '121.0.0.5',
                        path))
     asyncio.run(obj.change_folder('..', '121.0.0.5', list[0]))
     expected_results = [
         'Account created succesfully', 'Folder created successfully',
         'Folder changed successfully',
         'New_file is created and content succesfully written in the file',
         'Folder changed successfully'
     ]
     self.assertListEqual(obj.result, expected_results)
Esempio n. 2
0
 def test_list_files(self):
     """testing the list files in directory"""
     obj = server_class([], [], [], {}, {}, {}, [])
     path = os.getcwd()
     list = []
     list.append(path)
     asyncio.run(
         obj.register_account('andrew', 'hamming', 'user', '121.0.0.7',
                              list[0]))
     try:
         os.chdir('andrew')
     except:
         pass
     asyncio.run(obj.write_file('sample.txt', 'saidattu', '121.0.0.7',
                                path))
     asyncio.run(obj.list_files('121.0.0.7', path))
     asyncio.run(
         obj.register_account('sophia', '&queen', 'user', '121.0.0.8',
                              list[0]))
     try:
         os.chdir('sophia')
     except:
         pass
     asyncio.run(obj.list_files('121.0.0.8', path))
     expected_results = [
         'Account created succesfully',
         'New_file is created and content succesfully written in the file',
         True, 'Account created succesfully',
         'NO Files present in the directory'
     ]
     self.assertListEqual(obj.result, expected_results)
Esempio n. 3
0
 def test_login(self):
     """here we can test the login function"""
     obj = server_class([], [], [], {}, {}, {}, [])
     path = os.getcwd()
     list = []
     list.append(path)
     obj.logined_list['121.0.0.2'] = 0
     obj.logined_list[('vinay', '$vinay123')] = 1
     client = ('vinay', '$vinay123', 'admin', path)
     obj.client_list.append(client)
     asyncio.run(obj.login_account('vinay', '$vinay', '121.0.0.2', list[0]))
     asyncio.run(
         obj.login_account('vinay', '$vinay123', '121.0.0.2', list[0]))
     asyncio.run(
         obj.login_account('vinay', '$vinay123', '121.0.0.2', list[0]))
     expected_results = [
         'logined Access deined', 'login successful', 'Already logined'
     ]
     self.assertListEqual(obj.result, expected_results)
Esempio n. 4
0
 def test_create_folder(self):
     """testing the creating folder in directory"""
     obj = server_class([], [], [], {}, {}, {}, [])
     path = os.getcwd()
     list = []
     list.append(path)
     asyncio.run(
         obj.register_account('kiran', '&kiran', 'user', '121.0.0.3',
                              list[0]))
     try:
         os.chdir('kiran')
     except:
         pass
     asyncio.run(obj.create_folder('work', '121.0.0.3', list[0]))
     asyncio.run(obj.create_folder('work', '121.0.0.3', list[0]))
     expected_results = [
         'Account created succesfully', 'Folder created successfully',
         'Folder_name already created try another name'
     ]
     self.assertListEqual(obj.result, expected_results)
Esempio n. 5
0
 def test_register(self):
     """here we can test the register function"""
     obj = server_class([], [], [], {}, {}, {}, [])
     path = os.getcwd()
     list = []
     list.append(path)
     asyncio.run(
         obj.register_account('vinay', '$vinay123', 'admin', '121.0.0.1',
                              list[0]))
     asyncio.run(
         obj.register_account('vinay', '$vinay123', 'admin', '121.0.0.1',
                              list[0]))
     asyncio.run(
         obj.register_account('ramesh', 'ramesh88', 'usser', '121.0.0.1',
                              list[0]))
     expected_results = [
         'Account created succesfully', 'Account already created',
         'privilages must be either admin or user'
     ]
     self.assertListEqual(obj.result, expected_results)
Esempio n. 6
0
 def test_write_file(self):
     """testing the write files in directory"""
     obj = server_class([], [], [], {}, {}, {}, [])
     path = os.getcwd()
     list = []
     list.append(path)
     asyncio.run(
         obj.register_account('ramesh', 'coder', 'user', '121.0.0.4',
                              list[0]))
     try:
         os.chdir('ramesh')
     except:
         pass
     asyncio.run(obj.write_file('sample.txt', 'saidattu', '121.0.0.4',
                                path))
     asyncio.run(
         obj.write_file('sample.txt', 'is a good', '121.0.0.4', path))
     expected_results = [
         'Account created succesfully',
         'New_file is created and content succesfully written in the file',
         'content succesfully written in the file'
     ]
     self.assertListEqual(obj.result, expected_results)
Esempio n. 7
0
 def test_read_file(self):
     """testing the reading files in directory"""
     obj = server_class([], [], [], {}, {}, {}, [])
     path = os.getcwd()
     list = []
     list.append(path)
     asyncio.run(
         obj.register_account('koushik', 'ilovecomputer', 'user',
                              '121.0.0.9', list[0]))
     try:
         os.chdir('koushik')
     except:
         pass
     asyncio.run(
         obj.write_file('sample.txt', 'hello world', '121.0.0.9', path))
     asyncio.run(obj.read_file('sample.txt', '121.0.0.9', path))
     asyncio.run(obj.read_file('hello.txt', '121.0.0.9', path))
     expected_results = [
         'Account created succesfully',
         'New_file is created and content succesfully written in the file',
         'File reading is done', 'File does not exits in the directory'
     ]
     self.assertListEqual(obj.result, expected_results)
async def new_echo_handle(reader, writer):
    server = server_class(reader, writer, client_list, logined_list,\
        temporary_clients, read_file_data, result)
    """attributes
    reader: which the client is used to read a data from server
    writer: client is used to write the data from the server
    client_list: which is used to print the current clients in the server
    logined_list: which is used to print the current login list from the server
    temporary_clients: it will display the current clients in the server
    read_file: which is used to read the file from the server
    result: which is used to print the result"""
    addr = writer.get_extra_info('peername')
    logined_list[addr] = 0
    try:
        root = os.getcwd()
    except OSError:
        pass
    message = f"{addr} is connected !!!!"
    print(message)
    while True:
        try:
            data = await reader.read(100)
            nw_data = data.decode()
            print(nw_data)
            queue_data.put(data)
            while queue_data.empty() == 0:
                queue_data.get()
                message = data.decode().split()
                if message[0] == 'login':
                    # here we can login from cilent to server
                    if len(message) != 3:
                        try:
                            new_msg = 'Error occured in syntax of login data'\
                            'It must login user_name password'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            print('client connection disconnected')
                    else:
                        addr = writer.get_extra_info('peername')
                        await server.login_account(message[1], message[2],
                                                   addr, root)
                elif message[0] == 'register':
                    """we have to register from client to server"""
                    if len(message) != 4:
                        try:
                            new_msg = 'Error occured in syntax of register data'\
                            'It must login user_name password privilages'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    else:
                        addr = writer.get_extra_info('peername')
                        await server.register_account(message[1], message[2],
                                                      message[3], addr, root)
                elif message[0] == 'quit':
                    #Quit
                    try:
                        print('client disconnected')
                        break
                    except Exception:
                        pass
                elif message[0] == 'create_folder':
                    #we can Create a folder from client to server
                    if len(message) != 2:
                        try:
                            new_msg = 'Error occured in the format of create_folder'\
                            'It must be create_folder and Folder_name'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    addr = writer.get_extra_info('peername')
                    if logined_list[addr] == 0:
                        try:
                            new_msg = 'Login to account or register an account is required primary'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    else:
                        await server.create_folder(message[1], addr, root)
                elif message[0] == 'read_file':
                    #we can read the file from client to server"""
                    if len(message) != 2:
                        try:
                            new_msg = 'Error occured in the format of read_file'\
                            'It must be read_file and file_name'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    addr = writer.get_extra_info('peername')
                    if logined_list[addr] == 0:
                        try:
                            new_msg = 'Login to account or register an account is required primary'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    else:
                        await server.read_file(message[1], addr, root)
                elif message[0] == 'write_file':
                    # we can Write the file from client to server
                    if len(message) <= 1:
                        try:
                            new_msg = 'Error occured in the format of write_file'\
                            'It must be write_file and file_name and data'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    addr = writer.get_extra_info('peername')
                    if logined_list[addr] == 0:
                        try:
                            new_msg = 'Login to account or register an account is required primary'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    else:
                        if (len(message)) == 2:
                            string = ''
                            await server.write_file(message[1], string, addr,
                                                    root)
                        elif (len(message)) > 2:
                            string = ''
                            for i in range(2, len(message)):
                                string = string + message[i] + ' '
                            await server.write_file(message[1], string, addr,
                                                    root)
                        else:
                            try:
                                new_msg = 'Error occured in the format of write_file'\
                                'It must be write_file and file_name and data'
                                new_msg = new_msg.encode()
                                writer.write(new_msg + '\n'.encode())
                            except Exception:
                                pass
                elif message[0] == 'change_folder':
                    #we can replace the existing folder with new folder
                    if len(message) != 2:
                        try:
                            new_msg = 'Error occured in the format of change_folder'\
                            'It must be change_folder and name_to_be_changed'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    addr = writer.get_extra_info('peername')
                    if logined_list[addr] == 0:
                        try:
                            new_msg = 'Login to account or register an account is required primary'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    else:
                        path = os.getcwd()
                        await server.change_folder(message[1], addr, path)
                elif message[0] == 'list':
                    # List is used to show existing folders from the directory
                    if len(message) != 1:
                        try:
                            new_msg = 'Error occured in the format of list of files'\
                            'it should be just list'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    addr = writer.get_extra_info('peername')
                    if logined_list[addr] == 0:
                        try:
                            new_msg = 'Login to account or register an account is required primary'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    else:
                        await server.list_files(addr, root)
                elif message[0] == 'delete':
                    #Deleting the files
                    if len(message) != 3:
                        try:
                            new_msg = 'Error occured in the format of delete'\
                            'It must be delete and user_name and admin_password'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    addr = writer.get_extra_info('peername')
                    if logined_list[addr] == 0:
                        try:
                            new_msg = 'Login to account or register an account is required primary'
                            new_msg = new_msg.encode()
                            writer.write(new_msg + '\n'.encode())
                        except Exception:
                            pass
                    else:
                        addr = writer.get_extra_info('peername')
                        await server.delete_user(message[1], message[2], addr)
                else:
                    try:
                        new_msg = 'Invalid command is entered and try again'
                        new_msg = new_msg.encode()
                        writer.write(new_msg + '\n'.encode())
                    except Exception:
                        print('Connection disconnected')
        except:
            addr = writer.get_extra_info('peername')
            # for i in range(len(client_list)):
            #     if client_list[i][3] == addr:
            #         user_name = client_list[i][0]
            #         pass_word = client_list[i][1]
            #         logined_list[(user_name,pass_word)] = 0
            logined_list[addr] = 0
            print('connection closed', addr)
            break
    writer.close()