def register(self, user_name, password, privileges): """ Server have to be in root to able to read pickle file """ os.chdir(self.absolute_addr) if privileges not in ("admin", "user"): return "Privileges should be <user> or <admin>." try: with open("reg.pickle", 'rb') as listfile: user_list = pickle.load(listfile) except FileNotFoundError: user_list = [] if privileges == "user": new_user = class_file.User(user_name, password, privileges) elif privileges == "admin": new_user = class_file.Admin(user_name, password, privileges) if new_user.user_name not in [User.user_name for User in user_list]: user_list.append(new_user) pickle.dump(user_list, open("reg.pickle", "wb")) if privileges == "admin": group = "Admins" os.makedirs(os.path.join(group, user_name)) elif privileges == "user": group = "Users" os.makedirs(os.path.join(group, user_name)) # Showing and returning related messages in server. print("Congratulations. New user is registered succesfully") return "Congratulations. New user is registered succesfully" # Check if the username is already exists, return proper message print("Username you enterd is not valid or already exists") return "Username you enterd is not valid or already exists"
def test_write_noninput(self): usr = class_file.User("name", "password", "user") usr.currentPath = os.path.join(MAIN_PATH, "root", "Users") expected_outcome = "Folder text1.txt is removed" outcome = usr.write_nontext('text1.txt') self.assertEqual(outcome, expected_outcome) os.chdir(MAIN_PATH)
def test_change_directory(self): """ Check for changing root and user directory. for doing this function, root and users folder should be already created """ usr = class_file.User("name", "password", "user") usr.currentPath = os.path.join(MAIN_PATH, 'root') expected_outcome = "You are located in "\ + usr.currentPath+"\\Users" outcome = usr.change_directory("Users") os.chdir(MAIN_PATH) self.assertEqual(outcome, expected_outcome)
def test_previous_directory(self): """ Test to change directory to return to previous directory and folder. for doing this function, root and users folder should be already created. """ usr = class_file.User('name', 'password', 'user') usr.currentPath = os.path.join(MAIN_PATH, \ 'root', 'Users', 'name') expected_outcome = "User not permitted to leave the home folder!" outcome = usr.change_directory("..") os.chdir(MAIN_PATH) self.assertEqual(outcome, expected_outcome)
def test_readfile_empty(self): """ check for read function to read empty file for doing this function, root and users folder should be already created """ usr = class_file.User("name", 'password', 'user') usr.currentPath = os.path.join(MAIN_PATH, 'root', "Users") expected_outcome = "* Request not accepted *" outcome = usr.read_file('empty.txt') os.chdir(MAIN_PATH) self.assertEqual(outcome, expected_outcome)
def test_read_noninput(self): """ Check the readfile wether the user passed without any name for the file root and Users directory. """ usr = class_file.User("name", 'password', 'user') usr.currentPath = os.path.join(MAIN_PATH, "root", "Users") usr.file_name = "random_file.extension " expected_outcome = "random_file.extension file has been closed" outcome = usr.read_noninput() self.assertEqual(outcome, expected_outcome) os.chdir(MAIN_PATH)
def test_list_file(self): """ Check to list contents of root and user directory for doing this function, root and users folder should be already created. """ usr = class_file.User("name", "password", "user") usr.currentPath = os.path.join(MAIN_PATH, 'root', 'Users') usr.create_directory("file_test") usr.currentPath = os.path.join(usr.currentPath, "file_test") expected_outcome = '* Folder is empty *' outcome = usr.list_file() self.assertEqual(outcome, expected_outcome) os.chdir(MAIN_PATH)
def test_read_write(self): """ Check for read and write function. for doing this function, root and users folder should be already created """ usr = class_file.User("name", "password", "user") usr.currentPath = os.path.join(MAIN_PATH, "root", "Users") expected_write_c = "File textread.txt is updated." expected_read_c = "File content are: \n\n" + "HelloWorld\n\n" self.assertEqual(usr.write_file('textread.txt',\ ['HelloWorld']), expected_write_c) self.assertEqual((usr.read_file('textread.txt')), expected_read_c) os.remove('textread.txt') os.chdir(MAIN_PATH)
def test_create_directory(self): """ Check tp create root and user directory for doing this function, root and users folder should be already created. """ usr = class_file.User('name', "password", 'user') usr.currentPath = os.path.join(MAIN_PATH, "root", "Users") expected_outcome = ["Folder testing created", \ "Folder testing created"] outcome = [] input_check = ["testing", "testing"] outcome.append(usr.create_directory(input_check[0])) outcome.append(usr.create_directory(input_check[1])) self.assertEqual(outcome, expected_outcome) # delete created test file after test is completed and # change to main directory os.rmdir("testing") os.chdir(MAIN_PATH)
def test_create_write(self): usr = class_file.User("name", "password", "user") usr.currentPath = os.path.join(MAIN_PATH, "root", "Users") expected_outcome_folder = ["Folder TEST_ALL created"] expected_outcome_write = ["File TESTALL.txt is updated."] for expected_folder, expected_write, in zip(\ expected_outcome_folder, expected_outcome_write): self.assertEqual(usr.create_directory("TEST_ALL"), expected_folder) self.assertEqual(usr.write_file("TESTALL.txt", \ '*** This is test ***'), expected_write) # delete created test file after test is completed and # change to main directory os.rmdir('TEST_ALL') os.remove('TESTALL.txt') os.chdir(MAIN_PATH)
def test_write_file(self): """ Check to write text in root and user directory for doing this function, root and users folder should be already created. """ usr = class_file.User("name", "password", "user") usr.currentPath = os.path.join(MAIN_PATH, "root", "Users") expected_outcome = ["File text.txt is updated.", \ "File text.txt is updated."] outcome = [] input_check = ['text.txt', 'text.txt'] outcome.append(usr.write_file(input_check[0], 'testing')) outcome.append(usr.write_file(input_check[1], 'testing')) self.assertEqual(outcome, expected_outcome) # delete created test file after test is completed and # change to main directory os.remove('text.txt') os.chdir(MAIN_PATH)
def test_repr(self): """ Test function to print the username """ usr = class_file.User('name', 'password', 'user') expected_outcome = 'name' outcome = str(usr) self.assertEqual(outcome, expected_outcome)