Skip to content

Commit

Permalink
Refactor secret key and database configuration to use environment var…
Browse files Browse the repository at this point in the history
…iables; enhance user creation tests.
  • Loading branch information
witbrock committed Oct 6, 2024
1 parent 7649bfb commit 3dcf9e4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
Binary file modified src/web_interface/__pycache__/user_data.cpython-312.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion src/web_interface/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# I want to be able to easily call python backend code from the server.
# Make sure the start code for the local server has an option to run in foreground or background.
app = Flask(__name__)
app.secret_key = 'your_secret_key' # Replace with a secure secret key in production
app.secret_key = os.getenv("VON_APP_SECRET_KEY") # Replace with a secure secret key in production

# Configure Flask-Login
login_manager = LoginManager()
Expand Down
15 changes: 8 additions & 7 deletions src/web_interface/user_data.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import os
from bson.objectid import ObjectId
from werkzeug.security import generate_password_hash, check_password_hash

from vonlib import database_driver as vondb
# fyi in mongosh > show databases ; > use xxx ; > show collections ; Kill collection yyy > db.yyy.drop() ; kill db xxx > db.dropDatabase()

config = {} #not entirely clear that this config
config["CONFIG_DB"] = os.getenv('VON_CONFIG_DB')
config["USER_COLLECTION"] = os.getenv('VON_USER_COLLECTION')

config = {}
config["USER_DB"] = "vonUsers"
config["USER_COLLECTION"] = "users"

vonuserdb = config["USER_DB"]
vonusercollection = config["USER_COLLECTION"]

class VonUser:
vomongo = vondb.DatabaseDriver()
userdb=None
usercollection=None

@classmethod
def set_userDB(cls, userdb_name=vonuserdb, usercollection_name=vonusercollection):
def set_userDB(cls, userdb_name=config["CONFIG_DB"], usercollection_name=config["USER_COLLECTION"]):
"""
Sets the user database collection for the class.
Expand Down Expand Up @@ -130,7 +131,7 @@ def get_email(self):
# }


VonUser.set_userDB(vonuserdb, vonusercollection)
VonUser.set_userDB(config["CONFIG_DB"], config["USER_COLLECTION"])



Expand Down
Binary file not shown.
26 changes: 20 additions & 6 deletions tests/test_web_interface/user_data_test.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import pytest
from web_interface.user_data import VonUser
import os

test_user={'username':'testuser', 'email':'[email protected]', 'password':'testpassword'}

def test_set_userDB():
VonUser.set_userDB('testDB', 'testCollection')
assert VonUser.userdb.name == 'testDB'
assert VonUser.usercollection.name == 'testCollection'

def test_create_user():
VonUser.set_userDB('testDB', 'testCollection')
user = VonUser.create_user(test_user['username'], test_user['email'], test_user['password'])

def test_init_user():
user = VonUser('zhan')
assert user.get_username() == 'zhan'
assert user.email == '[email protected]'
assert user.id is not None
VonUser.set_userDB('testDB', 'testCollection')
user = VonUser.create_user(test_user['username'], test_user['email'], test_user['password'])

user = VonUser(test_user['username'])
assert user.get_username() == test_user['username']
assert user.email == test_user['email']
assert user.password == test_user['password']
assert user.id is not None

def test_database_structure():
dbname=os.getenv('VON_CONFIG_DB')
collname=os.getenv('VON_USER_COLLECTION')

VonUser.set_userDB() # use default values
db=VonUser.get_userDB()
assert db.name == 'vonUsers'
assert db.name == dbname
coll=VonUser.get_userCollection()
assert coll.name == 'users'
assert coll.name == collname

# def test_update_email(mock_user_collection):
# VonUser.usercollection = mock_user_collection
Expand Down

0 comments on commit 3dcf9e4

Please sign in to comment.