Skip to content

Commit 0e4cce4

Browse files
author
Max Azatian
committed
JWT_SECRET_KEY fix 2: naming
1 parent 29c8a20 commit 0e4cce4

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

backend/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PROJECT_NAME=integr8scode
2-
JWT_SECRET_KEY=your_secret_key_here
2+
SECRET_KEY=your_secret_key_here
33
ALGORITHM=HS256
44
ACCESS_TOKEN_EXPIRE_MINUTES=30
55
MONGODB_URL=mongodb://mongo:27017/integr8scode

backend/app/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Settings(BaseSettings):
1010
PROJECT_NAME: str = "integr8scode"
1111
API_V1_STR: str = "/api/v1"
12-
SECRET_KEY: str = Field(default=None, env="JWT_SECRET_KEY")
12+
SECRET_KEY: str = Field(default=None)
1313
ALGORITHM: str = "HS256"
1414
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
1515
MONGODB_URL: str = "mongodb://mongo:27017/integr8scode"
@@ -47,11 +47,11 @@ class Settings(BaseSettings):
4747
@classmethod
4848
def validate_secret_key(cls, v: Optional[str]) -> str:
4949
if not v:
50-
raise ValueError("JWT_SECRET_KEY environment variable must be set")
50+
raise ValueError("SECRET_KEY environment variable must be set")
5151
if len(v) < 32:
52-
raise ValueError("JWT_SECRET_KEY must be at least 32 characters long")
52+
raise ValueError("SECRET_KEY must be at least 32 characters long")
5353
if v == "your_secret_key_here" or v == "default_secret_key":
54-
raise ValueError("JWT_SECRET_KEY must not use default placeholder values")
54+
raise ValueError("SECRET_KEY must not use default placeholder values")
5555
return v
5656

5757
class Config:

backend/tests/unit/test_config.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,55 @@
88

99

1010
class TestSettingsValidation:
11-
def test_secret_key_missing(self):
11+
def test_secret_key_missing(self) -> None:
1212
with mock.patch.dict(os.environ, {}, clear=True):
1313
with pytest.raises(ValidationError) as exc_info:
1414
Settings()
1515

1616
errors = exc_info.value.errors()
1717
assert len(errors) == 1
1818
assert errors[0]["loc"] == ("SECRET_KEY",)
19-
assert "JWT_SECRET_KEY environment variable must be set" in errors[0]["msg"]
19+
assert "SECRET_KEY environment variable must be set" in errors[0]["msg"]
2020

21-
def test_secret_key_too_short(self):
22-
with mock.patch.dict(os.environ, {"JWT_SECRET_KEY": "short_key"}, clear=True):
21+
def test_secret_key_too_short(self) -> None:
22+
with mock.patch.dict(os.environ, {"SECRET_KEY": "short_key"}, clear=True):
2323
with pytest.raises(ValidationError) as exc_info:
2424
Settings()
2525

2626
errors = exc_info.value.errors()
2727
assert len(errors) == 1
2828
assert errors[0]["loc"] == ("SECRET_KEY",)
29-
assert "JWT_SECRET_KEY must be at least 32 characters long" in errors[0]["msg"]
29+
assert "SECRET_KEY must be at least 32 characters long" in errors[0]["msg"]
3030

31-
def test_secret_key_default_placeholder(self):
31+
def test_secret_key_default_placeholder(self) -> None:
3232
test_cases = ["your_secret_key_here", "default_secret_key"]
3333

3434
for placeholder in test_cases:
35-
with mock.patch.dict(os.environ, {"JWT_SECRET_KEY": placeholder}, clear=True):
35+
with mock.patch.dict(os.environ, {"SECRET_KEY": placeholder}, clear=True):
3636
with pytest.raises(ValidationError) as exc_info:
3737
Settings()
3838

3939
errors = exc_info.value.errors()
4040
assert len(errors) == 1
4141
assert errors[0]["loc"] == ("SECRET_KEY",)
42-
assert "JWT_SECRET_KEY must not use default placeholder values" in errors[0]["msg"]
42+
assert "SECRET_KEY must not use default placeholder values" in errors[0]["msg"]
4343

44-
def test_secret_key_valid(self):
44+
def test_secret_key_valid(self) -> None:
4545
valid_key = "a" * 32 # 32 character key
46-
with mock.patch.dict(os.environ, {"JWT_SECRET_KEY": valid_key}, clear=True):
46+
with mock.patch.dict(os.environ, {"SECRET_KEY": valid_key}, clear=True):
4747
settings = Settings()
4848
assert settings.SECRET_KEY == valid_key
4949

50-
def test_secret_key_longer_than_minimum(self):
50+
def test_secret_key_longer_than_minimum(self) -> None:
5151
long_key = "a" * 64 # 64 character key
52-
with mock.patch.dict(os.environ, {"JWT_SECRET_KEY": long_key}, clear=True):
52+
with mock.patch.dict(os.environ, {"SECRET_KEY": long_key}, clear=True):
5353
settings = Settings()
54-
assert settings.SECRET_KEY == long_key
54+
assert settings.SECRET_KEY == long_key
55+
56+
def test_env_file_loading(self) -> None:
57+
"""Test that .env file is loaded if present."""
58+
# This test verifies that the env_file configuration works
59+
# In a real scenario, the .env file would contain SECRET_KEY
60+
with mock.patch.dict(os.environ, {"SECRET_KEY": "a" * 32}, clear=True):
61+
settings = Settings()
62+
assert settings.Config.env_file == ".env"

0 commit comments

Comments
 (0)