コード例 #1
0
    def register(self, username, password, privileges):
        """
        Parameters:
            username : string
                returns the value of the string "username"
            password : string
                returns the value of the string "password"
            privileges : string
                returns the value of the string "privileges"

        Return: None

        Register a new user to the server with a <username>, <password>
            and <previllages>
        previllages have to be either admin or user

        user can be registered only with a unique name
        if a user has been registered with the same name before,
            it will acknowledge the current user with a proper error message.

        when the registration is done, A new personal folder named <username>
        is going to be created on the root directory.
        """
        # The server should be at root to read reg.pickle
        os.chdir(self.absolute_path)

        if privileges not in ("admin", "user"):
            return "Privileges must be either 'user' or 'admin'."

        try:
            with open('reg.pickle', 'rb') as userlist_file:
                userlist = pickle.load(userlist_file)
        # If no user accounts exist yet, create an empty list
        except FileNotFoundError:
            userlist = []

        if privileges == "user":
            new_user = classfile.User(username, password, privileges)
        elif privileges == "admin":
            new_user = classfile.Admin(username, password, privileges)

        if new_user.username not in [User.username for User in userlist]:
            userlist.append(new_user)
            pickle.dump(userlist, open("reg.pickle", "wb"))

            if privileges == "admin":
                group = "Admins"
                os.makedirs(os.path.join(group, username))
            elif privileges == "user":
                group = "Users"
                os.makedirs(os.path.join(group, username))

            result = "User registered sucessfully and folder " + \
                username + " created in " + group
            print(result)
            return result

        # If the entered username clashes with an older one, return error message
        print("Username already exists")
        return "Username already exists"
コード例 #2
0
    def test_repr(self):
        """ Sample test to print the usr name"""

        usr = classfile.User("name", "password", "user")
        expected_result = "name"
        result = str(usr)

        self.assertEqual(result, expected_result)
コード例 #3
0
 def test_change_dir(self):
     """testing to change directory, root and Users directory should already exists
             in under root path if not run the server script """
     usr = classfile.User("name", "password", "user")
     usr.current_path = os.path.join(ROOT_PATH, "root")
     expected_result = "Current working directory " + usr.current_path + "\\Users"
     result = usr.changedir("Users")
     os.chdir(ROOT_PATH)
     self.assertEqual(result, expected_result)
コード例 #4
0
    def test_repr(self):
        usr = classfile.User("name", "password", "user")

        expected_result = "name"
        result = ""

        result = str(usr)

        self.assertEqual(result, expected_result)
コード例 #5
0
    def test_change_prevdir(self):
        """testing to change directory to walk back previous folder, root and Users directory
             should already exists in under root path if not create that folder """

        usr = classfile.User("name", "password", "user")
        usr.current_path = os.path.join(ROOT_PATH, "root", "Users", "name")
        expected_result = "User is not allowed to leave the home folder"
        result = usr.changedir("..")
        os.chdir(ROOT_PATH)
        self.assertEqual(result, expected_result)
コード例 #6
0
    def test_writefile(self):
        usr = classfile.User("name", "password", "user")
        usr.current_path = "C:\\Coding\\Python_New_projects\\Assignment_3"

        expected_result = "File test.txt Created"
        result = ""

        result = usr.writefile("test.txt", "aaaaaaaaaaaa")

        self.assertEqual(result, expected_result)
コード例 #7
0
    def test_emptyreadfile(self):
        """testing to check read function to read the empty file,it should denied request.
           root and Users directory should already exists in under root path
           if not create that folder"""

        usr = classfile.User("name", "password", "user")
        usr.current_path = os.path.join(ROOT_PATH, "root", "Users")
        expected_result = "Request denied"
        result = usr.readfile("invalid.txt")
        os.chdir(ROOT_PATH)
        self.assertEqual(result, expected_result)
コード例 #8
0
    def test_write_notext(self):
        """testing to writefile function if that user passed without text,
           root and Users directory should already exists in under root path
           if not create that folder"""

        usr = classfile.User("name", "password", "user")
        usr.current_path = os.path.join(ROOT_PATH, "root", "Users")
        expected_result = "File test1.txt erased"
        result = usr.write_notext('test1.txt')
        self.assertEqual(result, expected_result)
        os.chdir(ROOT_PATH)
コード例 #9
0
    def test_readfile(self):
        usr = classfile.User("name", "password", "user")
        usr.current_path = "C:\\Coding\\Python_New_projects\\Assignment_3"

        expected_result = "Content "
        result = ""

        result = usr.readfile("register.py")
        result = result[:8]

        self.assertEqual(result, expected_result)
コード例 #10
0
    def test_read_noinput(self):
        """testing to readfile function if that user passed without name of the file,
           root and Users directory should already exists in under root path
           if not create that folder"""

        usr = classfile.User("name", "password", "user")
        usr.current_path = os.path.join(ROOT_PATH, "root", "Users")
        usr.filename = "some_file.extension"
        expected_result = "some_file.extension has been closed"
        result = usr.read_noinput()
        self.assertEqual(result, expected_result)
        os.chdir(ROOT_PATH)
コード例 #11
0
    def test_list_function(self):
        """testing to list the content of empty directory, root and Users directory
           should already exists  in under root path if not run the server script
           and placed test_folder in that path"""

        usr = classfile.User("name", "password", "user")
        usr.current_path = os.path.join(ROOT_PATH, "root", "Users")
        usr.create_dir("test_folder")
        usr.current_path = os.path.join(usr.current_path, "test_folder")
        expected_result = "(This folder is empty)"
        result = usr.list_function()
        self.assertEqual(result, expected_result)
        os.chdir(ROOT_PATH)
コード例 #12
0
    def test_writefile(self):
        """ testing to write text in the file, root and Users directory should already exists
                in under root path if not run the server script """

        usr = classfile.User("name", "password", "user")
        usr.current_path = os.path.join(ROOT_PATH, "root", "Users")
        expected_result = ["File test.txt Created", "File test.txt Updated"]
        result = []
        test_input = ['test.txt', 'test.txt']

        result.append(usr.writefile(test_input[0], 'testing'))
        result.append(usr.writefile(test_input[1], 'testing'))

        self.assertEqual(result, expected_result)
        os.remove('test.txt')
        os.chdir(ROOT_PATH)
コード例 #13
0
    def test_read_write(self):
        """testing to check read and write function as mentioned in classfile,
           root and Users directory should already exists in under root path
           if not create that folder"""

        usr = classfile.User("name", "password", "user")
        usr.current_path = os.path.join(ROOT_PATH, "root", "Users")
        expected_result_write = "File testread.txt Created"
        expected_result_read = "Content of line in reading file: \n\n" + "hello" + "\n\n"

        # for exp_write, exp_read in zip(expected_result_write, expected_result_read):
        self.assertEqual(usr.writefile('testread.txt', ['hello']),
                         expected_result_write)
        self.assertEqual((usr.readfile('testread.txt')), expected_result_read)

        os.remove('testread.txt')
        os.chdir(ROOT_PATH)
コード例 #14
0
    def test_create_write(self):
        """testing to check create folder and write function as mentioned in classfile,
           root and Users directory should already exists in under root path
           if not create that folder"""

        usr = classfile.User("name", "password", "user")
        usr.current_path = os.path.join(ROOT_PATH, "root", "Users")
        exp_result_c_folder = ["Directory created test_all"]
        exp_result_write = ["File testall.txt Created"]

        for exp_fold, exp_write, in zip(exp_result_c_folder, exp_result_write):
            self.assertEqual(usr.create_dir('test_all'), exp_fold)
            self.assertEqual(usr.writefile('testall.txt', 'helloworld'),
                             exp_write)

        os.rmdir('test_all')
        os.remove('testall.txt')
        os.chdir(ROOT_PATH)
コード例 #15
0
    def test_create_dir(self):
        """testing to create directory, root and Users directory should already exists
            in under root path if not run the server script"""

        usr = classfile.User("name", "password", "user")
        usr.current_path = os.path.join(ROOT_PATH, "root", "Users")
        expected_result = [
            "Directory created testing", "Directory already exists testing"
        ]
        result = []
        test_input = ['testing', 'testing']

        result.append(usr.create_dir(test_input[0]))
        result.append(usr.create_dir(test_input[1]))

        self.assertEqual(result, expected_result)

        os.rmdir('testing')
        os.chdir(ROOT_PATH)