Skip to content

Commit 591040d

Browse files
committed
Re-split the env configs into different modules
- Configurations for different environments all existed in the user_service.config.config module. Python will execute this file from top to bottom, regardless of the configs (and environment) in use. - This brought along "cross contamination" between environments. For example, when in the test environment, TestConfig is used and the value for SQLALCHEMY_DATABASE_URI is a hardcoded string that points to sqlite3 memory database, so there is no need to set the SQLALCHEMY_DATABASE_URI environment variable, right? - Wrong. DevConfig which previously existed in the same module as TestConfig was fetching its value for SQLALCHEMY_DATABASE_URI from an environment variable, but because this has not been set for the test environment the tests would fail with a KeyError (from os.environ['SQLALCHEMY_DATABASE_URI']). - The tests were failing because of an environment variable that is not needed. - Splitting the configurations into different modules should fix this issue.
1 parent bcb2d63 commit 591040d

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

.github/workflows/continuous-integration.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
env:
1616
SERVER_NAME: 'localhost:5000'
17-
SECRET_KEY: ${{ secrets.SECRET_KEY }}
17+
JWT_SECRET_KEY: ${{ secrets.JWT_SECRET_KEY }}
1818
REDIS_HOST: 'redis-host-has-been-mocked'
1919
REDIS_PORT: 1234
2020
steps:

user_service/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def create_app(test_config=False):
1717
app = Flask(__name__)
1818
if test_config:
19-
app.config.from_object('user_service.config.config.TestConfig')
19+
app.config.from_object('user_service.config.test.TestConfig')
2020
else:
2121
app.config.from_object(os.environ['USER_SERVICE_CONFIG_MODULE'])
2222

user_service/config/base.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
3+
4+
class CommonConfig(object):
5+
SQLALCHEMY_TRACK_MODIFICATIONS = False
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import os
21
import datetime
2+
import os
33

4-
5-
class CommonConfig(object):
6-
SQLALCHEMY_TRACK_MODIFICATIONS = False
4+
from user_service.config.base import CommonConfig
75

86

97
class DevConfig(CommonConfig):
@@ -12,10 +10,3 @@ class DevConfig(CommonConfig):
1210
JWT_ACCESS_TOKEN_EXPIRES = datetime.timedelta(
1311
minutes=int(os.environ['JWT_ACCESS_TOKEN_EXPIRES_MINUTES'])
1412
)
15-
16-
17-
class TestConfig(CommonConfig):
18-
TESING = True
19-
JWT_SECRET_KEY = 'super secret'
20-
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
21-
SERVER_NAME = os.environ['SERVER_NAME']

user_service/config/test.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import os
2+
3+
from user_service.config.base import CommonConfig
4+
5+
class TestConfig(CommonConfig):
6+
TESING = True
7+
JWT_SECRET_KEY = os.environ['JWT_SECRET_KEY']
8+
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
9+
SERVER_NAME = os.environ['SERVER_NAME']

0 commit comments

Comments
 (0)