debate_LMS / tests /test_password_update.py
raymondEDS
user management
a1146b3
import streamlit as st
from supabase import create_client, Client
import os
def init_supabase():
"""Initialize Supabase client"""
try:
url = st.secrets.get("SUPABASE_URL")
key = st.secrets.get("SUPABASE_KEY")
except:
url = os.getenv("SUPABASE_URL")
key = os.getenv("SUPABASE_KEY")
if not url or not key:
st.error("Supabase credentials not found.")
return None
return create_client(url, key)
def test_password_update():
"""Test password update functionality"""
st.title("πŸ” Password Update Test")
supabase = init_supabase()
if not supabase:
st.error("Failed to initialize Supabase client")
return
try:
# Test 1: Get all users with detailed info
st.subheader("πŸ“Š Current Users (Detailed)")
response = supabase.table('users').select('*').execute()
if response.data:
st.success(f"Found {len(response.data)} users:")
for user in response.data:
st.write(f"**{user.get('username')}**")
st.write(f" - ID: `{user.get('id')}`")
st.write(f" - Password: `{user.get('password')}`")
st.write(f" - Email: `{user.get('email')}`")
st.write(f" - Role: `{user.get('role')}`")
st.write("---")
else:
st.warning("No users found")
return
# Test 2: Try to update password for instructor
st.subheader("πŸ”§ Testing Password Update")
# Find instructor user
instructor_user = None
for user in response.data:
if user.get('username') == 'instructor':
instructor_user = user
break
if not instructor_user:
st.error("Instructor user not found")
return
st.write(f"**Testing update for:** {instructor_user.get('username')}")
st.write(f"**User ID:** `{instructor_user.get('id')}`")
st.write(f"**Current password:** `{instructor_user.get('password')}`")
# Try to update password
new_password = "test123"
st.write(f"**Attempting to update to:** `{new_password}`")
# Method 1: Update by ID
st.write("**Method 1: Update by ID**")
try:
update_response = supabase.table('users').update({'password': new_password}).eq('id', instructor_user.get('id')).execute()
st.write(f"**Response:** {update_response}")
st.write(f"**Data:** {update_response.data}")
st.write(f"**Count:** {update_response.count}")
if update_response.data:
st.success("βœ… Password update successful!")
else:
st.error("❌ No rows were updated")
except Exception as e:
st.error(f"❌ Password update failed: {str(e)}")
# Method 2: Update by username
st.write("**Method 2: Update by username**")
try:
update_response2 = supabase.table('users').update({'password': new_password}).eq('username', 'instructor').execute()
st.write(f"**Response:** {update_response2}")
st.write(f"**Data:** {update_response2.data}")
st.write(f"**Count:** {update_response2.count}")
if update_response2.data:
st.success("βœ… Password update by username successful!")
else:
st.error("❌ No rows were updated by username")
except Exception as e:
st.error(f"❌ Password update by username failed: {str(e)}")
# Method 3: Try without RLS
st.write("**Method 3: Check if RLS is the issue**")
try:
# Try to disable RLS temporarily (this might not work with anon key)
st.write("Attempting to check RLS status...")
# Try a simple update to see if it's an RLS issue
test_update = supabase.table('users').update({'full_name': 'Test Update'}).eq('username', 'instructor').execute()
st.write(f"**Test update response:** {test_update}")
except Exception as e:
st.error(f"❌ Test update failed: {str(e)}")
# Verify current state
st.subheader("πŸ” Verification")
try:
verify_response = supabase.table('users').select('*').eq('username', 'instructor').execute()
if verify_response.data:
current_user = verify_response.data[0]
st.write(f"**Current password in database:** `{current_user.get('password')}`")
if current_user.get('password') == new_password:
st.success("βœ… Password was actually updated!")
else:
st.warning("⚠️ Password was not updated")
else:
st.error("❌ Could not verify user")
except Exception as e:
st.error(f"❌ Verification failed: {str(e)}")
except Exception as e:
st.error(f"❌ Test failed: {str(e)}")
if __name__ == "__main__":
test_password_update()