Commit abf8d0e0 authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Add has_completed_onboarding flag backing the new first-run walkthrough

Nothing previously distinguished a brand-new account from one that's
used Stax for months - /auth/register and /auth/login returned
byte-identical responses. Adds User.has_completed_onboarding
(server_default=true, so every existing/historical row is already
onboarded - only /auth/register explicitly passes False for a
genuinely new account), exposes it on GET /auth/me, and adds an
idempotent POST /auth/onboarding/complete for the frontend's
OnboardingOverlay to call on skip/finish.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019i3NAUkLkHNbsPg5vKfzF9
parent 2f562691
Pipeline #1216 failed with stage
in 25 minutes and 31 seconds
"""add has_completed_onboarding to users
Revision ID: 6c99d29c0175
Revises: a6f66b340a16
Create Date: 2026-10-07 18:58:16.862756
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '6c99d29c0175'
down_revision: Union[str, Sequence[str], None] = 'a6f66b340a16'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# Note: autogenerate also wanted to add a FK constraint on remix_jobs.user_id - a known
# pre-existing drift between the model and the actual schema history in this project, not
# related to this change. Deliberately left out, same as every prior migration that's hit it.
op.add_column('users', sa.Column('has_completed_onboarding', sa.Boolean(), server_default='1', nullable=False))
def downgrade() -> None:
"""Downgrade schema."""
op.drop_column('users', 'has_completed_onboarding')
...@@ -165,7 +165,7 @@ def _issue_tokens(db: Session, user_id: int) -> TokenResponse: ...@@ -165,7 +165,7 @@ def _issue_tokens(db: Session, user_id: int) -> TokenResponse:
async def auth_register(req: RegisterReq, db: Session = Depends(get_db)): async def auth_register(req: RegisterReq, db: Session = Depends(get_db)):
if db.query(User).filter(User.username == req.username).first(): if db.query(User).filter(User.username == req.username).first():
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Username already taken") raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Username already taken")
user = User(username=req.username, password_hash=hash_password(req.password)) user = User(username=req.username, password_hash=hash_password(req.password), has_completed_onboarding=False)
db.add(user) db.add(user)
db.commit() db.commit()
db.refresh(user) db.refresh(user)
...@@ -314,6 +314,7 @@ async def auth_me(db: Session = Depends(get_db), current_user: User = Depends(ge ...@@ -314,6 +314,7 @@ async def auth_me(db: Session = Depends(get_db), current_user: User = Depends(ge
"id": current_user.id, "id": current_user.id,
"username": current_user.username, "username": current_user.username,
"role": current_user.role, "role": current_user.role,
"has_completed_onboarding": current_user.has_completed_onboarding,
"privacy": { "privacy": {
"share_location_enabled": profile.share_location_enabled, "share_location_enabled": profile.share_location_enabled,
"allow_direct_messages": profile.allow_direct_messages, "allow_direct_messages": profile.allow_direct_messages,
...@@ -322,6 +323,17 @@ async def auth_me(db: Session = Depends(get_db), current_user: User = Depends(ge ...@@ -322,6 +323,17 @@ async def auth_me(db: Session = Depends(get_db), current_user: User = Depends(ge
} }
@router.post("/onboarding/complete")
async def complete_onboarding(db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
"""Called once by OnboardingOverlay (stax_webapp) when the user skips or finishes the
welcome walkthrough - idempotent so a retried/duplicate call from a flaky connection never
errors, just no-ops past the first time."""
if not current_user.has_completed_onboarding:
current_user.has_completed_onboarding = True
db.commit()
return {"status": "success", "has_completed_onboarding": True}
@router.patch("/privacy") @router.patch("/privacy")
async def update_privacy( async def update_privacy(
req: PrivacyUpdateReq, db: Session = Depends(get_db), current_user: User = Depends(get_current_user) req: PrivacyUpdateReq, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)
......
...@@ -40,6 +40,12 @@ class User(Base): ...@@ -40,6 +40,12 @@ class User(Base):
# so adding a role later doesn't need a migration to alter the enum type (sqlite's enum # so adding a role later doesn't need a migration to alter the enum type (sqlite's enum
# support is weak anyway). Validated at the API boundary in soundmarks.py, not here. # support is weak anyway). Validated at the API boundary in soundmarks.py, not here.
role = Column(String, default="user", nullable=False) role = Column(String, default="user", nullable=False)
# Drives the one-time welcome overlay (OnboardingOverlay.tsx, stax_webapp). server_default
# of true so every existing/historical row (and any row inserted outside /auth/register,
# including test fixtures) is already-onboarded by default - only /auth/register explicitly
# passes False for a genuinely new account. Declared here (not just in the migration) since
# tests boot their schema via Base.metadata.create_all, which never runs Alembic.
has_completed_onboarding = Column(Boolean, nullable=False, server_default='1')
class RefreshToken(Base): class RefreshToken(Base):
__tablename__ = "refresh_tokens" __tablename__ = "refresh_tokens"
......
...@@ -47,3 +47,53 @@ def test_patch_privacy_empty_body_changes_nothing(client, auth_as): ...@@ -47,3 +47,53 @@ def test_patch_privacy_empty_body_changes_nothing(client, auth_as):
response = client.patch("/auth/privacy", json={}, headers=headers) response = client.patch("/auth/privacy", json={}, headers=headers)
assert response.status_code == 200 assert response.status_code == 200
assert response.json()["privacy"] == {"share_location_enabled": False, "allow_direct_messages": True} assert response.json()["privacy"] == {"share_location_enabled": False, "allow_direct_messages": True}
# --- onboarding ---
def test_a_row_created_outside_register_defaults_to_already_onboarded(client, auth_as):
# make_user (behind auth_as) builds a User row directly, the same way a pre-this-feature
# account or a Telegram-linked signup would - these must not suddenly see onboarding they
# never asked for, so the column's own server_default (not /auth/register) covers them.
# Kept as the first test in this section, before any /auth/register calls below: make_user
# assigns explicit ids from its own counter (see conftest.py's _user_ids) while /auth/register
# relies on SQLite's plain autoincrement (max(id)+1) - running a make_user call after a
# register call in the same session can land both schemes on the same next id and collide.
headers, user = auth_as()
response = client.get("/auth/me", headers=headers)
assert response.json()["user"]["has_completed_onboarding"] is True
def test_register_starts_with_onboarding_incomplete(client):
response = client.post("/auth/register", json={"username": "freshonboarder", "password": "correcthorse"})
token = response.json()["access_token"]
me = client.get("/auth/me", headers={"Authorization": f"Bearer {token}"})
assert me.json()["user"]["has_completed_onboarding"] is False
def test_complete_onboarding_flips_the_flag(client):
register = client.post("/auth/register", json={"username": "onboarder2", "password": "correcthorse"})
headers = {"Authorization": f"Bearer {register.json()['access_token']}"}
response = client.post("/auth/onboarding/complete", headers=headers)
assert response.status_code == 200
assert response.json() == {"status": "success", "has_completed_onboarding": True}
me = client.get("/auth/me", headers=headers)
assert me.json()["user"]["has_completed_onboarding"] is True
def test_complete_onboarding_is_idempotent(client):
register = client.post("/auth/register", json={"username": "onboarder3", "password": "correcthorse"})
headers = {"Authorization": f"Bearer {register.json()['access_token']}"}
first = client.post("/auth/onboarding/complete", headers=headers)
second = client.post("/auth/onboarding/complete", headers=headers)
assert first.status_code == 200 and second.status_code == 200
assert second.json()["has_completed_onboarding"] is True
def test_complete_onboarding_requires_auth(client):
response = client.post("/auth/onboarding/complete")
assert response.status_code == 401
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment