Ejemplo n.º 1
0
    def create_user_file(self, **kwargs):
        if 'original_name' not in kwargs:
            kwargs['original_name'] = self.fake.file_name()

        if 'original_extension' not in kwargs:
            kwargs['original_extension'] = pathlib.Path(
                kwargs['original_name']).suffix[1:].lower()

        if 'unique' not in kwargs:
            kwargs['unique'] = random_string(32)

        if 'size' not in kwargs:
            kwargs['size'] = 100

        if 'mime_type' not in kwargs:
            kwargs['mime_type'] = (mimetypes.guess_type(
                kwargs['original_name'])[0] or '')

        if 'uploaded_by' not in kwargs:
            kwargs['uploaded_by'] = self.create_user()

        if 'sha256_hash' not in kwargs:
            kwargs['sha256_hash'] = random_string(64)

        return UserFile.objects.create(**kwargs)
Ejemplo n.º 2
0
    def create_user_file(self, **kwargs):
        if "original_name" not in kwargs:
            kwargs["original_name"] = self.fake.file_name()

        if "original_extension" not in kwargs:
            kwargs["original_extension"] = (pathlib.Path(
                kwargs["original_name"]).suffix[1:].lower())

        if "unique" not in kwargs:
            kwargs["unique"] = random_string(32)

        if "size" not in kwargs:
            kwargs["size"] = 100

        if "mime_type" not in kwargs:
            kwargs["mime_type"] = mimetypes.guess_type(
                kwargs["original_name"])[0] or ""

        if "uploaded_by" not in kwargs:
            kwargs["uploaded_by"] = self.create_user()

        if "sha256_hash" not in kwargs:
            kwargs["sha256_hash"] = random_string(64)

        return UserFile.objects.create(**kwargs)
Ejemplo n.º 3
0
    def generate_unique_key(self, length=32, max_tries=1000):
        """
        Generates a unique token key.

        :param length: Indicates the amount of characters that the token must contain.
        :type length: int
        :param max_tries: The maximum amount of tries to check if a token with the key
            already exists.
        :type max_tries: int
        :raises MaximumUniqueTokenTriesError: When the maximum amount of tries has
            been exceeded. A new generated token is tried every time the token
            already exists.
        :return: A unique token
        :rtype: str
        """

        i = 0

        while True:
            if i > max_tries:
                raise MaximumUniqueTokenTriesError(
                    f'Tried {max_tries} tokens, but none of them are unique.'
                )

            i += 1
            token = random_string(length)

            if not Token.objects.filter(key=token).exists():
                return token
Ejemplo n.º 4
0
    def create_token(self, **kwargs):
        if "key" not in kwargs:
            kwargs["key"] = random_string(32)

        if "name" not in kwargs:
            kwargs["name"] = self.fake.name()

        if "user" not in kwargs:
            kwargs["user"] = self.create_user()

        if "group" not in kwargs:
            kwargs["group"] = self.create_group(user=kwargs["user"])

        return Token.objects.create(**kwargs)
Ejemplo n.º 5
0
    def create_token(self, **kwargs):
        if 'key' not in kwargs:
            kwargs['key'] = random_string(32)

        if 'name' not in kwargs:
            kwargs['name'] = self.fake.name()

        if 'user' not in kwargs:
            kwargs['user'] = self.create_user()

        if 'group' not in kwargs:
            kwargs['group'] = self.create_group(user=kwargs['user'])

        return Token.objects.create(**kwargs)
Ejemplo n.º 6
0
    def generate_unique(self,
                        sha256_hash,
                        extension,
                        length=32,
                        max_tries=1000):
        """
        Generates a unique non existing string for a new user file.

        :param sha256_hash: The hash of the file name. Needed because they are
            required to be unique together.
        :type sha256_hash: str
        :param extension: The extension of the file name. Needed because they are
            required to be unique together.
        :type extension: str
        :param length: Indicates the amount of characters that the unique must contain.
        :type length: int
        :param max_tries: The maximum amount of tries to check if a unique already
            exists.
        :type max_tries: int
        :raises MaximumUniqueTriesError: When the maximum amount of tries has
            been exceeded.
        :return: The generated unique string
        :rtype: str
        """

        i = 0

        while True:
            if i > max_tries:
                raise MaximumUniqueTriesError(
                    f"Tried {max_tries} tokens, but none of them are unique.")

            i += 1
            unique = random_string(length)

            if not UserFile.objects.filter(sha256_hash=sha256_hash,
                                           original_extension=extension,
                                           unique=unique).exists():
                return unique
Ejemplo n.º 7
0
def test_random_string():
    assert len(random_string(32)) == 32
    assert random_string(32) != random_string(32)