88
99
1010class 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