Spaces:
Running
Running
Upload 11 files
Browse files
main.py
CHANGED
@@ -884,372 +884,469 @@ async def ensure_super_user():
|
|
884 |
"""
|
885 |
logger.info("Ensure super user endpoint accessed")
|
886 |
|
|
|
887 |
try:
|
888 |
-
#
|
889 |
-
|
890 |
-
|
891 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
892 |
|
893 |
-
|
894 |
-
|
895 |
-
|
|
|
|
|
896 |
|
897 |
-
|
898 |
-
|
899 |
-
|
900 |
-
app.db_conn.execute(update_query, (user_id,))
|
901 |
-
app.db_conn.commit()
|
902 |
-
logger.info(f"Updated user {user_id} to have admin privileges")
|
903 |
|
904 |
-
|
905 |
-
|
906 |
-
|
907 |
-
|
908 |
-
"email": super_user_email
|
909 |
-
}
|
910 |
-
else:
|
911 |
-
return {
|
912 |
-
"status": "exists",
|
913 |
-
"message": f"Super user already exists with ID {user_id} and has admin privileges",
|
914 |
-
"user_id": user_id,
|
915 |
-
"email": super_user_email
|
916 |
-
}
|
917 |
|
918 |
-
#
|
919 |
-
|
|
|
|
|
|
|
920 |
|
921 |
-
|
922 |
-
|
923 |
-
|
924 |
-
|
925 |
-
|
926 |
-
|
927 |
-
|
928 |
-
)
|
929 |
|
930 |
-
|
931 |
-
|
932 |
-
|
933 |
|
934 |
-
#
|
935 |
-
|
936 |
-
|
937 |
-
|
938 |
-
logger.info("Inserting super user with transaction-like approach")
|
939 |
-
insert_query = "INSERT INTO users (email, hashed_password, is_admin) VALUES (?, ?, 1)"
|
940 |
-
cursor = app.db_conn.execute(insert_query, (super_user_email, hashed_password))
|
941 |
-
app.db_conn.commit()
|
942 |
-
logger.info("Committed super user insert")
|
943 |
-
except Exception as e:
|
944 |
-
logger.error(f"Error during super user insert: {str(e)}")
|
945 |
-
# If there was an error, check if it's because the user already exists
|
946 |
-
if "UNIQUE constraint failed" in str(e):
|
947 |
-
logger.info("User already exists (UNIQUE constraint)")
|
948 |
-
# Try to get the user ID
|
949 |
-
id_query = "SELECT id FROM users WHERE email = ?"
|
950 |
-
id_result = app.db_conn.execute(id_query, (super_user_email,)).fetchone()
|
951 |
-
if id_result:
|
952 |
-
user_id = id_result[0]
|
953 |
-
logger.info(f"Found existing user with ID: {user_id}")
|
954 |
-
cursor = None # Set cursor to None to indicate we didn't insert
|
955 |
-
else:
|
956 |
-
# If it's another error, try a different approach
|
957 |
-
try:
|
958 |
-
logger.info("Trying alternative insert approach")
|
959 |
-
# Try to get the highest ID to simulate auto-increment
|
960 |
-
max_id_result = app.db_conn.execute("SELECT MAX(id) FROM users").fetchone()
|
961 |
-
logger.info(f"Max ID result: {max_id_result}")
|
962 |
-
|
963 |
-
next_id = 1
|
964 |
-
if max_id_result and max_id_result[0] is not None:
|
965 |
-
next_id = int(max_id_result[0]) + 1
|
966 |
-
|
967 |
-
# Insert with explicit ID
|
968 |
-
insert_query = "INSERT INTO users (id, email, hashed_password, is_admin) VALUES (?, ?, ?, 1)"
|
969 |
-
cursor = app.db_conn.execute(insert_query, (next_id, super_user_email, hashed_password))
|
970 |
-
app.db_conn.commit()
|
971 |
-
logger.info(f"Committed super user insert with explicit ID: {next_id}")
|
972 |
-
except Exception as e2:
|
973 |
-
logger.error(f"Alternative insert approach also failed: {str(e2)}")
|
974 |
-
raise
|
975 |
|
976 |
-
|
977 |
-
|
978 |
-
|
979 |
-
|
980 |
-
|
981 |
-
|
982 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
983 |
|
984 |
-
|
985 |
-
|
986 |
-
|
987 |
-
# Try to get the last inserted ID using a direct query
|
988 |
-
id_query = "SELECT last_insert_rowid()"
|
989 |
-
id_result = app.db_conn.execute(id_query).fetchone()
|
990 |
-
if id_result and id_result[0]:
|
991 |
-
user_id = id_result[0]
|
992 |
-
logger.info(f"Got ID from last_insert_rowid(): {user_id}")
|
993 |
-
except Exception as e2:
|
994 |
-
logger.error(f"Error getting ID from last_insert_rowid(): {str(e2)}")
|
995 |
-
|
996 |
-
# If still no ID, try to get it by email
|
997 |
-
if not user_id or user_id == 0:
|
998 |
-
try:
|
999 |
-
id_query = "SELECT id FROM users WHERE email = ?"
|
1000 |
-
id_result = app.db_conn.execute(id_query, (super_user_email,)).fetchone()
|
1001 |
-
if id_result:
|
1002 |
-
user_id = id_result[0]
|
1003 |
-
logger.info(f"Got ID from query by email: {user_id}")
|
1004 |
-
except Exception as e2:
|
1005 |
-
logger.error(f"Error getting ID from query by email: {str(e2)}")
|
1006 |
|
1007 |
-
|
1008 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1009 |
|
1010 |
-
|
1011 |
-
|
1012 |
-
|
1013 |
|
1014 |
-
|
1015 |
-
|
1016 |
-
|
1017 |
-
"
|
1018 |
-
"user_id": verify_result[0],
|
1019 |
-
"email": super_user_email
|
1020 |
-
}
|
1021 |
-
else:
|
1022 |
-
# Try to list all users to see if our user is there
|
1023 |
-
try:
|
1024 |
-
all_users = app.db_conn.execute("SELECT id, email FROM users").fetchall()
|
1025 |
-
logger.info(f"All users in database: {all_users}")
|
1026 |
-
|
1027 |
-
# Check if our user is in the list
|
1028 |
-
for user in all_users:
|
1029 |
-
if len(user) > 1 and user[1] == super_user_email:
|
1030 |
-
return {
|
1031 |
-
"status": "created_found_in_list",
|
1032 |
-
"message": f"Super user created and found in user list with ID {user[0]}",
|
1033 |
-
"user_id": user[0],
|
1034 |
-
"email": super_user_email
|
1035 |
-
}
|
1036 |
-
except Exception as e:
|
1037 |
-
logger.error(f"Error listing all users: {str(e)}")
|
1038 |
|
1039 |
-
|
1040 |
-
"
|
1041 |
-
"
|
1042 |
-
"email": super_user_email
|
1043 |
-
}
|
1044 |
except Exception as e:
|
1045 |
-
logger.error(f"Error
|
1046 |
-
|
1047 |
-
|
1048 |
-
|
1049 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1050 |
|
1051 |
# Database test endpoint
|
1052 |
@app.get("/test-db", tags=["General"])
|
1053 |
async def test_database():
|
1054 |
"""
|
1055 |
-
Test
|
1056 |
-
This
|
1057 |
-
"""
|
1058 |
-
import time
|
1059 |
|
1060 |
-
|
1061 |
-
|
1062 |
-
|
|
|
1063 |
|
1064 |
results = {
|
1065 |
-
"
|
1066 |
-
"
|
1067 |
-
"operations": []
|
1068 |
}
|
1069 |
|
1070 |
-
|
1071 |
-
results["connection_method"] = app.last_successful_connection_method
|
1072 |
-
|
1073 |
-
if not hasattr(app, "db_conn"):
|
1074 |
-
logger.error(f"[{test_id}] Database connection not available")
|
1075 |
-
results["operations"].append({
|
1076 |
-
"name": "Database connection check",
|
1077 |
-
"success": False,
|
1078 |
-
"error": "Database connection not available"
|
1079 |
-
})
|
1080 |
-
return results
|
1081 |
-
|
1082 |
try:
|
1083 |
-
|
1084 |
-
|
1085 |
-
|
1086 |
-
|
1087 |
-
|
1088 |
-
|
1089 |
-
|
1090 |
-
|
|
|
|
|
|
|
1091 |
})
|
1092 |
-
logger.info(f"[{test_id}] Test 1 result: {result}")
|
1093 |
|
1094 |
-
|
1095 |
-
|
1096 |
-
|
1097 |
-
CREATE TABLE IF NOT EXISTS
|
1098 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
1099 |
-
|
1100 |
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
1101 |
)
|
1102 |
-
"""
|
1103 |
-
app.db_conn.execute(create_temp_table)
|
1104 |
app.db_conn.commit()
|
1105 |
-
results["
|
1106 |
-
"name": "
|
1107 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
1108 |
})
|
1109 |
-
logger.info(f"[{test_id}] Test 2 completed successfully")
|
1110 |
|
1111 |
-
|
1112 |
-
|
1113 |
-
|
1114 |
-
|
1115 |
-
|
1116 |
app.db_conn.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1117 |
|
1118 |
-
|
1119 |
-
|
1120 |
-
|
1121 |
-
|
1122 |
-
|
1123 |
-
|
1124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1125 |
|
1126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1127 |
try:
|
1128 |
-
|
1129 |
-
|
1130 |
-
|
1131 |
-
|
1132 |
-
|
1133 |
-
|
1134 |
-
|
1135 |
-
|
1136 |
-
|
1137 |
-
|
1138 |
-
|
1139 |
-
|
|
|
|
|
|
|
|
|
|
|
1140 |
})
|
1141 |
-
|
1142 |
-
|
1143 |
-
|
1144 |
-
|
1145 |
-
|
1146 |
-
|
1147 |
-
|
1148 |
-
|
1149 |
-
|
1150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1151 |
})
|
1152 |
-
logger.info(f"[{test_id}] Test 4 result: {result}")
|
1153 |
|
1154 |
-
#
|
1155 |
-
logger.info(f"[{test_id}] Test 5: Check users table structure")
|
1156 |
try:
|
1157 |
-
|
1158 |
-
|
1159 |
-
"
|
1160 |
-
|
1161 |
-
|
1162 |
-
|
1163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1164 |
except Exception as e:
|
1165 |
-
|
1166 |
-
|
1167 |
-
"
|
1168 |
-
"success": False,
|
1169 |
"error": str(e)
|
1170 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
1171 |
|
1172 |
-
|
1173 |
-
|
1174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1175 |
all_users = app.db_conn.execute("SELECT id, email FROM users").fetchall()
|
1176 |
-
|
1177 |
-
"name": "List all users",
|
1178 |
-
"success": True,
|
1179 |
-
"count": len(all_users) if all_users else 0,
|
1180 |
-
"users": [{"id": user[0], "email": user[1]} for user in all_users] if all_users else []
|
1181 |
-
})
|
1182 |
-
logger.info(f"[{test_id}] Test 6 result: {all_users}")
|
1183 |
-
except Exception as e:
|
1184 |
-
logger.error(f"[{test_id}] Error listing all users: {str(e)}")
|
1185 |
-
results["operations"].append({
|
1186 |
-
"name": "List all users",
|
1187 |
-
"success": False,
|
1188 |
-
"error": str(e)
|
1189 |
-
})
|
1190 |
|
1191 |
-
|
1192 |
-
|
|
|
|
|
|
|
|
|
1193 |
except Exception as e:
|
1194 |
-
logger.error(f"
|
1195 |
-
|
1196 |
-
"
|
1197 |
-
"
|
1198 |
-
"
|
1199 |
-
}
|
1200 |
-
return results
|
1201 |
|
1202 |
# Test user creation endpoint (direct SQL)
|
1203 |
@app.get("/create-test-user", tags=["General"])
|
1204 |
async def create_test_user():
|
1205 |
"""
|
1206 |
-
Create a test user
|
1207 |
-
This is for testing purposes only.
|
1208 |
|
1209 |
Returns:
|
1210 |
-
dict: Information about the
|
1211 |
"""
|
1212 |
-
|
1213 |
-
|
1214 |
-
|
|
|
|
|
|
|
1215 |
|
1216 |
-
#
|
1217 |
-
|
1218 |
-
|
1219 |
|
1220 |
try:
|
1221 |
-
#
|
1222 |
-
logger.info(f"
|
1223 |
-
|
1224 |
-
|
1225 |
-
|
1226 |
-
capture_output=True,
|
1227 |
-
text=True,
|
1228 |
-
check=True
|
1229 |
)
|
|
|
|
|
1230 |
|
1231 |
-
#
|
1232 |
-
|
1233 |
-
|
1234 |
-
|
1235 |
-
|
1236 |
-
|
1237 |
-
logger.error(f"[{test_id}] Failed to parse direct SQL script output: {result.stdout}")
|
1238 |
return {
|
1239 |
-
"
|
1240 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1241 |
}
|
1242 |
-
except subprocess.CalledProcessError as e:
|
1243 |
-
logger.error(f"[{test_id}] Direct SQL script failed: {e.stderr}")
|
1244 |
-
return {
|
1245 |
-
"success": False,
|
1246 |
-
"error": f"Direct SQL script failed: {e.stderr}"
|
1247 |
-
}
|
1248 |
except Exception as e:
|
1249 |
-
logger.error(f"
|
1250 |
return {
|
1251 |
-
"
|
1252 |
-
"
|
|
|
1253 |
}
|
1254 |
|
1255 |
# Direct HTTP API endpoint for user registration
|
|
|
884 |
"""
|
885 |
logger.info("Ensure super user endpoint accessed")
|
886 |
|
887 |
+
# First, let's test if we can insert and retrieve data from a simple test table
|
888 |
try:
|
889 |
+
# Create a simple test table
|
890 |
+
logger.info("Creating test table")
|
891 |
+
app.db_conn.execute("""
|
892 |
+
CREATE TABLE IF NOT EXISTS super_user_test (
|
893 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
894 |
+
test_value TEXT,
|
895 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
896 |
+
)
|
897 |
+
""")
|
898 |
+
app.db_conn.commit()
|
899 |
|
900 |
+
# Insert a test value
|
901 |
+
test_value = f"test_{int(time.time())}"
|
902 |
+
logger.info(f"Inserting test value: {test_value}")
|
903 |
+
app.db_conn.execute("INSERT INTO super_user_test (test_value) VALUES (?)", (test_value,))
|
904 |
+
app.db_conn.commit()
|
905 |
|
906 |
+
# Try to retrieve the test value
|
907 |
+
logger.info("Retrieving test value")
|
908 |
+
result = app.db_conn.execute("SELECT * FROM super_user_test WHERE test_value = ?", (test_value,)).fetchone()
|
|
|
|
|
|
|
909 |
|
910 |
+
if result:
|
911 |
+
logger.info(f"Test value retrieved successfully: {result}")
|
912 |
+
else:
|
913 |
+
logger.warning("Test value not found after insert")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
914 |
|
915 |
+
# Get all values from the test table
|
916 |
+
all_test_values = app.db_conn.execute("SELECT * FROM super_user_test").fetchall()
|
917 |
+
logger.info(f"All test values: {all_test_values}")
|
918 |
+
except Exception as e:
|
919 |
+
logger.error(f"Error during test table operations: {str(e)}")
|
920 |
|
921 |
+
# Now let's check the users table schema
|
922 |
+
try:
|
923 |
+
logger.info("Checking users table schema")
|
924 |
+
table_info = app.db_conn.execute("PRAGMA table_info(users)").fetchall()
|
925 |
+
logger.info(f"Users table schema: {table_info}")
|
926 |
+
except Exception as e:
|
927 |
+
logger.error(f"Error checking users table schema: {str(e)}")
|
|
|
928 |
|
929 |
+
# Let's try a direct SQL approach for creating the super user
|
930 |
+
try:
|
931 |
+
logger.info("Trying direct SQL approach for super user creation")
|
932 |
|
933 |
+
# Check if super user exists
|
934 |
+
super_user_email = "[email protected]"
|
935 |
+
direct_check = app.db_conn.execute("SELECT COUNT(*) FROM users WHERE email = ?", (super_user_email,)).fetchone()
|
936 |
+
logger.info(f"Direct check result: {direct_check}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
937 |
|
938 |
+
if direct_check and direct_check[0] > 0:
|
939 |
+
logger.info("Super user already exists according to direct check")
|
940 |
+
else:
|
941 |
+
logger.info("Super user does not exist according to direct check, creating now")
|
942 |
+
|
943 |
+
# Create password hashing context
|
944 |
+
pwd_context = CryptContext(
|
945 |
+
schemes=["argon2"],
|
946 |
+
argon2__time_cost=4,
|
947 |
+
argon2__memory_cost=102400,
|
948 |
+
argon2__parallelism=8,
|
949 |
+
argon2__salt_len=16
|
950 |
+
)
|
951 |
|
952 |
+
# Hash the password
|
953 |
+
super_user_password = "TestPassword123!"
|
954 |
+
hashed_password = pwd_context.hash(super_user_password)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
955 |
|
956 |
+
# Try a direct insert with explicit SQL
|
957 |
+
direct_insert = """
|
958 |
+
INSERT INTO users (email, hashed_password, is_admin)
|
959 |
+
VALUES (?, ?, 1)
|
960 |
+
"""
|
961 |
+
app.db_conn.execute(direct_insert, (super_user_email, hashed_password))
|
962 |
+
app.db_conn.commit()
|
963 |
+
logger.info("Direct insert completed")
|
964 |
|
965 |
+
# Check if the insert worked
|
966 |
+
direct_verify = app.db_conn.execute("SELECT * FROM users WHERE email = ?", (super_user_email,)).fetchone()
|
967 |
+
logger.info(f"Direct verify result: {direct_verify}")
|
968 |
|
969 |
+
if direct_verify:
|
970 |
+
logger.info("Super user created successfully with direct SQL approach")
|
971 |
+
else:
|
972 |
+
logger.warning("Super user not found after direct SQL insert")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
973 |
|
974 |
+
# Let's try to list all users
|
975 |
+
all_users = app.db_conn.execute("SELECT * FROM users").fetchall()
|
976 |
+
logger.info(f"All users after direct insert: {all_users}")
|
|
|
|
|
977 |
except Exception as e:
|
978 |
+
logger.error(f"Error during direct SQL approach: {str(e)}")
|
979 |
+
|
980 |
+
# Return diagnostic information
|
981 |
+
return {
|
982 |
+
"status": "diagnostic_complete",
|
983 |
+
"message": "Diagnostic tests completed. Check server logs for details.",
|
984 |
+
"email": "[email protected]",
|
985 |
+
"next_steps": [
|
986 |
+
"Check server logs for detailed diagnostic information",
|
987 |
+
"Verify database permissions",
|
988 |
+
"Try accessing the /test-db endpoint for additional diagnostics"
|
989 |
+
]
|
990 |
+
}
|
991 |
|
992 |
# Database test endpoint
|
993 |
@app.get("/test-db", tags=["General"])
|
994 |
async def test_database():
|
995 |
"""
|
996 |
+
Test database connectivity and operations.
|
997 |
+
This endpoint performs various database operations to diagnose issues.
|
|
|
|
|
998 |
|
999 |
+
Returns:
|
1000 |
+
dict: Results of the database tests.
|
1001 |
+
"""
|
1002 |
+
logger.info("Database test endpoint accessed")
|
1003 |
|
1004 |
results = {
|
1005 |
+
"tests": [],
|
1006 |
+
"overall_status": "unknown"
|
|
|
1007 |
}
|
1008 |
|
1009 |
+
# Test 1: Basic connection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1010 |
try:
|
1011 |
+
test_result = app.db_conn.execute("SELECT 1").fetchone()
|
1012 |
+
results["tests"].append({
|
1013 |
+
"name": "basic_connection",
|
1014 |
+
"status": "success",
|
1015 |
+
"result": str(test_result)
|
1016 |
+
})
|
1017 |
+
except Exception as e:
|
1018 |
+
results["tests"].append({
|
1019 |
+
"name": "basic_connection",
|
1020 |
+
"status": "failure",
|
1021 |
+
"error": str(e)
|
1022 |
})
|
|
|
1023 |
|
1024 |
+
# Test 2: Create test table
|
1025 |
+
try:
|
1026 |
+
app.db_conn.execute("""
|
1027 |
+
CREATE TABLE IF NOT EXISTS db_test_table (
|
1028 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
1029 |
+
test_value TEXT,
|
1030 |
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
1031 |
)
|
1032 |
+
""")
|
|
|
1033 |
app.db_conn.commit()
|
1034 |
+
results["tests"].append({
|
1035 |
+
"name": "create_test_table",
|
1036 |
+
"status": "success"
|
1037 |
+
})
|
1038 |
+
except Exception as e:
|
1039 |
+
results["tests"].append({
|
1040 |
+
"name": "create_test_table",
|
1041 |
+
"status": "failure",
|
1042 |
+
"error": str(e)
|
1043 |
})
|
|
|
1044 |
|
1045 |
+
# Test 3: Insert into test table
|
1046 |
+
test_id = int(time.time())
|
1047 |
+
test_value = f"test_value_{test_id}"
|
1048 |
+
try:
|
1049 |
+
app.db_conn.execute("INSERT INTO db_test_table (test_value) VALUES (?)", (test_value,))
|
1050 |
app.db_conn.commit()
|
1051 |
+
results["tests"].append({
|
1052 |
+
"name": "insert_test_value",
|
1053 |
+
"status": "success",
|
1054 |
+
"value_inserted": test_value
|
1055 |
+
})
|
1056 |
+
except Exception as e:
|
1057 |
+
results["tests"].append({
|
1058 |
+
"name": "insert_test_value",
|
1059 |
+
"status": "failure",
|
1060 |
+
"error": str(e)
|
1061 |
+
})
|
1062 |
|
1063 |
+
# Test 4: Retrieve from test table
|
1064 |
+
try:
|
1065 |
+
result = app.db_conn.execute("SELECT * FROM db_test_table WHERE test_value = ?", (test_value,)).fetchone()
|
1066 |
+
if result:
|
1067 |
+
results["tests"].append({
|
1068 |
+
"name": "retrieve_test_value",
|
1069 |
+
"status": "success",
|
1070 |
+
"result": str(result)
|
1071 |
+
})
|
1072 |
+
else:
|
1073 |
+
results["tests"].append({
|
1074 |
+
"name": "retrieve_test_value",
|
1075 |
+
"status": "failure",
|
1076 |
+
"error": "Value not found after insert"
|
1077 |
+
})
|
1078 |
+
except Exception as e:
|
1079 |
+
results["tests"].append({
|
1080 |
+
"name": "retrieve_test_value",
|
1081 |
+
"status": "failure",
|
1082 |
+
"error": str(e)
|
1083 |
+
})
|
1084 |
|
1085 |
+
# Test 5: List all values in test table
|
1086 |
+
try:
|
1087 |
+
all_values = app.db_conn.execute("SELECT * FROM db_test_table").fetchall()
|
1088 |
+
results["tests"].append({
|
1089 |
+
"name": "list_all_test_values",
|
1090 |
+
"status": "success",
|
1091 |
+
"count": len(all_values),
|
1092 |
+
"values": str(all_values[-5:]) if all_values else "[]" # Show only the last 5 values
|
1093 |
+
})
|
1094 |
+
except Exception as e:
|
1095 |
+
results["tests"].append({
|
1096 |
+
"name": "list_all_test_values",
|
1097 |
+
"status": "failure",
|
1098 |
+
"error": str(e)
|
1099 |
+
})
|
1100 |
+
|
1101 |
+
# Test 6: Check users table
|
1102 |
+
try:
|
1103 |
+
users_count = app.db_conn.execute("SELECT COUNT(*) FROM users").fetchone()
|
1104 |
+
results["tests"].append({
|
1105 |
+
"name": "check_users_table",
|
1106 |
+
"status": "success",
|
1107 |
+
"count": users_count[0] if users_count else 0
|
1108 |
+
})
|
1109 |
+
|
1110 |
+
# If users exist, try to get one
|
1111 |
+
if users_count and users_count[0] > 0:
|
1112 |
try:
|
1113 |
+
user = app.db_conn.execute("SELECT * FROM users LIMIT 1").fetchone()
|
1114 |
+
results["tests"].append({
|
1115 |
+
"name": "retrieve_user",
|
1116 |
+
"status": "success",
|
1117 |
+
"user_id": user[0] if user else None
|
1118 |
+
})
|
1119 |
+
except Exception as e:
|
1120 |
+
results["tests"].append({
|
1121 |
+
"name": "retrieve_user",
|
1122 |
+
"status": "failure",
|
1123 |
+
"error": str(e)
|
1124 |
+
})
|
1125 |
+
except Exception as e:
|
1126 |
+
results["tests"].append({
|
1127 |
+
"name": "check_users_table",
|
1128 |
+
"status": "failure",
|
1129 |
+
"error": str(e)
|
1130 |
})
|
1131 |
+
|
1132 |
+
# Test 7: Try to insert a test user
|
1133 |
+
test_user_email = f"test_user_{test_id}@example.com"
|
1134 |
+
try:
|
1135 |
+
# Create password hashing context
|
1136 |
+
pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
|
1137 |
+
hashed_password = pwd_context.hash("TestPassword123!")
|
1138 |
+
|
1139 |
+
# Insert test user
|
1140 |
+
app.db_conn.execute(
|
1141 |
+
"INSERT INTO users (email, hashed_password, is_admin) VALUES (?, ?, 0)",
|
1142 |
+
(test_user_email, hashed_password)
|
1143 |
+
)
|
1144 |
+
app.db_conn.commit()
|
1145 |
+
results["tests"].append({
|
1146 |
+
"name": "insert_test_user",
|
1147 |
+
"status": "success",
|
1148 |
+
"email": test_user_email
|
1149 |
})
|
|
|
1150 |
|
1151 |
+
# Try to retrieve the test user
|
|
|
1152 |
try:
|
1153 |
+
user = app.db_conn.execute("SELECT * FROM users WHERE email = ?", (test_user_email,)).fetchone()
|
1154 |
+
if user:
|
1155 |
+
results["tests"].append({
|
1156 |
+
"name": "retrieve_test_user",
|
1157 |
+
"status": "success",
|
1158 |
+
"user_id": user[0] if user else None
|
1159 |
+
})
|
1160 |
+
else:
|
1161 |
+
results["tests"].append({
|
1162 |
+
"name": "retrieve_test_user",
|
1163 |
+
"status": "failure",
|
1164 |
+
"error": "Test user not found after insert"
|
1165 |
+
})
|
1166 |
except Exception as e:
|
1167 |
+
results["tests"].append({
|
1168 |
+
"name": "retrieve_test_user",
|
1169 |
+
"status": "failure",
|
|
|
1170 |
"error": str(e)
|
1171 |
})
|
1172 |
+
except Exception as e:
|
1173 |
+
results["tests"].append({
|
1174 |
+
"name": "insert_test_user",
|
1175 |
+
"status": "failure",
|
1176 |
+
"error": str(e)
|
1177 |
+
})
|
1178 |
|
1179 |
+
# Calculate overall status
|
1180 |
+
success_count = sum(1 for test in results["tests"] if test["status"] == "success")
|
1181 |
+
total_count = len(results["tests"])
|
1182 |
+
|
1183 |
+
if success_count == total_count:
|
1184 |
+
results["overall_status"] = "success"
|
1185 |
+
elif success_count > 0:
|
1186 |
+
results["overall_status"] = "partial_success"
|
1187 |
+
else:
|
1188 |
+
results["overall_status"] = "failure"
|
1189 |
+
|
1190 |
+
results["success_rate"] = f"{success_count}/{total_count}"
|
1191 |
+
|
1192 |
+
return results
|
1193 |
+
|
1194 |
+
# Super user creation endpoint (direct SQL)
|
1195 |
+
@app.get("/create-super-user", tags=["General"])
|
1196 |
+
async def create_super_user_endpoint():
|
1197 |
+
"""
|
1198 |
+
Create a super user with email [email protected] directly using SQL.
|
1199 |
+
This endpoint is for testing purposes only.
|
1200 |
+
|
1201 |
+
Returns:
|
1202 |
+
dict: Information about the super user creation.
|
1203 |
+
"""
|
1204 |
+
logger.info("Create super user endpoint accessed")
|
1205 |
+
|
1206 |
+
# Super user details
|
1207 |
+
super_user_email = "[email protected]"
|
1208 |
+
super_user_password = "TestPassword123!"
|
1209 |
+
|
1210 |
+
# Create password hashing context
|
1211 |
+
pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
|
1212 |
+
hashed_password = pwd_context.hash(super_user_password)
|
1213 |
+
|
1214 |
+
try:
|
1215 |
+
# Check if super user already exists
|
1216 |
+
logger.info("Checking if super user already exists")
|
1217 |
+
existing_user = app.db_conn.execute("SELECT id, is_admin FROM users WHERE email = ?", (super_user_email,)).fetchone()
|
1218 |
+
|
1219 |
+
if existing_user:
|
1220 |
+
user_id = existing_user[0]
|
1221 |
+
is_admin = existing_user[1] if len(existing_user) > 1 else 0
|
1222 |
+
|
1223 |
+
logger.info(f"Super user already exists with ID: {user_id}, is_admin: {is_admin}")
|
1224 |
+
|
1225 |
+
# If user exists but is not admin, make them admin
|
1226 |
+
if not is_admin:
|
1227 |
+
logger.info("Updating user to have admin privileges")
|
1228 |
+
app.db_conn.execute("UPDATE users SET is_admin = 1 WHERE id = ?", (user_id,))
|
1229 |
+
app.db_conn.commit()
|
1230 |
+
logger.info("User updated to admin successfully")
|
1231 |
+
|
1232 |
+
return {
|
1233 |
+
"status": "updated",
|
1234 |
+
"message": f"Super user already existed with ID {user_id} and was updated to have admin privileges",
|
1235 |
+
"user_id": user_id,
|
1236 |
+
"email": super_user_email
|
1237 |
+
}
|
1238 |
+
|
1239 |
+
return {
|
1240 |
+
"status": "exists",
|
1241 |
+
"message": f"Super user already exists with ID {user_id} and has admin privileges",
|
1242 |
+
"user_id": user_id,
|
1243 |
+
"email": super_user_email
|
1244 |
+
}
|
1245 |
+
|
1246 |
+
# Insert the super user
|
1247 |
+
logger.info(f"Inserting super user with email: {super_user_email}")
|
1248 |
+
app.db_conn.execute(
|
1249 |
+
"INSERT INTO users (email, hashed_password, is_admin) VALUES (?, ?, 1)",
|
1250 |
+
(super_user_email, hashed_password)
|
1251 |
+
)
|
1252 |
+
app.db_conn.commit()
|
1253 |
+
logger.info("Super user inserted successfully")
|
1254 |
+
|
1255 |
+
# Try to retrieve the super user
|
1256 |
+
logger.info("Retrieving super user")
|
1257 |
+
user = app.db_conn.execute("SELECT * FROM users WHERE email = ?", (super_user_email,)).fetchone()
|
1258 |
+
|
1259 |
+
if user:
|
1260 |
+
logger.info(f"Super user retrieved successfully: {user}")
|
1261 |
+
return {
|
1262 |
+
"status": "success",
|
1263 |
+
"message": "Super user created and retrieved successfully",
|
1264 |
+
"user_id": user[0] if user else None,
|
1265 |
+
"email": super_user_email
|
1266 |
+
}
|
1267 |
+
else:
|
1268 |
+
logger.warning("Super user not found after insert")
|
1269 |
+
|
1270 |
+
# Try to list all users
|
1271 |
all_users = app.db_conn.execute("SELECT id, email FROM users").fetchall()
|
1272 |
+
logger.info(f"All users: {all_users}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1273 |
|
1274 |
+
return {
|
1275 |
+
"status": "partial_success",
|
1276 |
+
"message": "Super user created but could not be retrieved",
|
1277 |
+
"email": super_user_email,
|
1278 |
+
"all_users": str(all_users)
|
1279 |
+
}
|
1280 |
except Exception as e:
|
1281 |
+
logger.error(f"Error creating super user: {str(e)}")
|
1282 |
+
return {
|
1283 |
+
"status": "error",
|
1284 |
+
"message": f"Error creating super user: {str(e)}",
|
1285 |
+
"email": super_user_email
|
1286 |
+
}
|
|
|
1287 |
|
1288 |
# Test user creation endpoint (direct SQL)
|
1289 |
@app.get("/create-test-user", tags=["General"])
|
1290 |
async def create_test_user():
|
1291 |
"""
|
1292 |
+
Create a test user directly using SQL.
|
1293 |
+
This endpoint is for testing purposes only.
|
1294 |
|
1295 |
Returns:
|
1296 |
+
dict: Information about the test user creation.
|
1297 |
"""
|
1298 |
+
logger.info("Create test user endpoint accessed")
|
1299 |
+
|
1300 |
+
# Generate a unique email
|
1301 |
+
test_id = int(time.time())
|
1302 |
+
test_email = f"test_user_{test_id}@example.com"
|
1303 |
+
test_password = "TestPassword123!"
|
1304 |
|
1305 |
+
# Create password hashing context
|
1306 |
+
pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
|
1307 |
+
hashed_password = pwd_context.hash(test_password)
|
1308 |
|
1309 |
try:
|
1310 |
+
# Insert the test user
|
1311 |
+
logger.info(f"Inserting test user with email: {test_email}")
|
1312 |
+
app.db_conn.execute(
|
1313 |
+
"INSERT INTO users (email, hashed_password, is_admin) VALUES (?, ?, 0)",
|
1314 |
+
(test_email, hashed_password)
|
|
|
|
|
|
|
1315 |
)
|
1316 |
+
app.db_conn.commit()
|
1317 |
+
logger.info("Test user inserted successfully")
|
1318 |
|
1319 |
+
# Try to retrieve the test user
|
1320 |
+
logger.info("Retrieving test user")
|
1321 |
+
user = app.db_conn.execute("SELECT * FROM users WHERE email = ?", (test_email,)).fetchone()
|
1322 |
+
|
1323 |
+
if user:
|
1324 |
+
logger.info(f"Test user retrieved successfully: {user}")
|
|
|
1325 |
return {
|
1326 |
+
"status": "success",
|
1327 |
+
"message": "Test user created and retrieved successfully",
|
1328 |
+
"user_id": user[0] if user else None,
|
1329 |
+
"email": test_email
|
1330 |
+
}
|
1331 |
+
else:
|
1332 |
+
logger.warning("Test user not found after insert")
|
1333 |
+
|
1334 |
+
# Try to list all users
|
1335 |
+
all_users = app.db_conn.execute("SELECT id, email FROM users").fetchall()
|
1336 |
+
logger.info(f"All users: {all_users}")
|
1337 |
+
|
1338 |
+
return {
|
1339 |
+
"status": "partial_success",
|
1340 |
+
"message": "Test user created but could not be retrieved",
|
1341 |
+
"email": test_email,
|
1342 |
+
"all_users": str(all_users)
|
1343 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1344 |
except Exception as e:
|
1345 |
+
logger.error(f"Error creating test user: {str(e)}")
|
1346 |
return {
|
1347 |
+
"status": "error",
|
1348 |
+
"message": f"Error creating test user: {str(e)}",
|
1349 |
+
"email": test_email
|
1350 |
}
|
1351 |
|
1352 |
# Direct HTTP API endpoint for user registration
|