generated from kyegomez/Python-Package-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kye
committed
Dec 8, 2023
1 parent
a0c4d4f
commit da0e07b
Showing
5 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import secrets | ||
import string | ||
|
||
|
||
def generate_api_key(prefix="sk-", length: int = 50): | ||
"""Generate a random api key | ||
Args: | ||
prefix (str, optional): _description_. Defaults to "sk-". | ||
length (int, optional): _description_. Defaults to 50. | ||
Raises: | ||
ValueError: _description_ | ||
RuntimeError: _description_ | ||
Returns: | ||
_type_: _description_ | ||
Example: | ||
>>> from swarms_cloud.api_key_generator import generate_api_key | ||
>>> generate_api_key() | ||
>>> sk-9a7b8c5d6e7f8g9h0i1j2k3l4m5n6o7p8q9r0s1t2u3v4w5x6y7z8A9B0C1D2E3F4G5H6I7J8K9L0M1N2O3P4Q5R6S7T8U9V0W1X2Y3Z4 | ||
""" | ||
if length <= len(prefix): | ||
raise ValueError("Length must be greater than prefix length") | ||
|
||
try: | ||
# Generate a random string of letters for the api | ||
characters = string.ascii_letters + string.digits | ||
secure_key = "".join(secrets.choice(characters) for _ in range(length)) | ||
return prefix + secure_key | ||
except Exception as error: | ||
# Handle unexpeccted errors | ||
raise RuntimeError(f"Error generating api key: {error}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import pytest | ||
from swarms_cloud.api_key_generator import generate_api_key | ||
|
||
|
||
# Basic tests | ||
def test_generate_api_key_default(): | ||
api_key = generate_api_key() | ||
assert isinstance(api_key, str) | ||
assert api_key.startswith("sk-") | ||
assert len(api_key) == 52 # Prefix (3) + 50 random characters | ||
|
||
|
||
def test_generate_api_key_custom_length(): | ||
api_key = generate_api_key(length=10) | ||
assert len(api_key) == 13 # Prefix (3) + 10 random characters | ||
|
||
|
||
def test_generate_api_key_custom_prefix(): | ||
api_key = generate_api_key(prefix="custom-") | ||
assert api_key.startswith("custom-") | ||
assert len(api_key) == 57 # Custom prefix (7) + 50 random characters | ||
|
||
|
||
# Exception tests | ||
def test_generate_api_key_short_length(): | ||
with pytest.raises(ValueError): | ||
generate_api_key(length=2) | ||
|
||
|
||
def test_generate_api_key_unexpected_error(): | ||
with pytest.raises(RuntimeError): | ||
generate_api_key(length=1000000) | ||
|
||
|
||
# Parameterized tests | ||
@pytest.mark.parametrize( | ||
"prefix, length", | ||
[ | ||
("pre-", 15), | ||
("test-", 25), | ||
("longprefix-", 100), | ||
], | ||
) | ||
def test_generate_api_key_parameterized(prefix, length): | ||
api_key = generate_api_key(prefix=prefix, length=length) | ||
assert api_key.startswith(prefix) | ||
assert len(api_key) == len(prefix) + length | ||
|
||
|
||
# Test performance (optional) | ||
def test_generate_api_key_performance(benchmark): | ||
benchmark(generate_api_key) | ||
|
||
|
||
# Additional tests | ||
def test_generate_api_key_invalid_prefix_type(): | ||
with pytest.raises(ValueError): | ||
generate_api_key(prefix=123) | ||
|
||
|
||
def test_generate_api_key_negative_length(): | ||
with pytest.raises(ValueError): | ||
generate_api_key(length=-10) | ||
|
||
|
||
def test_generate_api_key_empty_prefix(): | ||
api_key = generate_api_key(prefix="") | ||
assert api_key.startswith("sk-") | ||
assert len(api_key) == 53 # Prefix (3) + 50 random characters | ||
|
||
|
||
def test_generate_api_key_special_characters_prefix(): | ||
api_key = generate_api_key(prefix="@#$") | ||
assert api_key.startswith("@#$") | ||
assert len(api_key) == 53 # Special prefix (3) + 50 random characters | ||
|
||
|
||
def test_generate_api_key_prefix_length(): | ||
api_key = generate_api_key(prefix="x" * 100) | ||
assert len(api_key) == 150 # Custom prefix (100) + 50 random characters | ||
|
||
|
||
def test_generate_api_key_zero_length(): | ||
with pytest.raises(ValueError): | ||
generate_api_key(length=0) | ||
|
||
|
||
def test_generate_api_key_zero_length_custom_prefix(): | ||
with pytest.raises(ValueError): | ||
generate_api_key(prefix="custom-", length=0) | ||
|
||
|
||
def test_generate_api_key_negative_length_custom_prefix(): | ||
with pytest.raises(ValueError): | ||
generate_api_key(prefix="custom-", length=-10) | ||
|
||
|
||
def test_generate_api_key_long_prefix(): | ||
api_key = generate_api_key(prefix="verylongprefix-") | ||
assert api_key.startswith("verylongprefix-") | ||
assert len(api_key) == 63 # Custom prefix (16) + 50 random characters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters