|
| 1 | +# Supabase Testing Guide |
| 2 | + |
| 3 | +Complete this checklist to verify your Phase 1 backend setup is working correctly. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- ✅ Migration has been run successfully |
| 8 | +- ✅ Email/password authentication is enabled |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## 1. Test Schema Creation |
| 13 | + |
| 14 | +**Location:** `Table Editor` |
| 15 | + |
| 16 | +Verify all tables exist: |
| 17 | + |
| 18 | +- [ ] `profiles` table exists |
| 19 | +- [ ] `sleep_sessions` table exists |
| 20 | +- [ ] `user_settings` table exists |
| 21 | + |
| 22 | +Check table structure for `sleep_sessions`: |
| 23 | + |
| 24 | +- [ ] Has columns: `id`, `user_id`, `start_time`, `end_time`, `source`, `updated_at` |
| 25 | +- [ ] `source` has default value of `'manual'` |
| 26 | +- [ ] Constraint exists: `valid_end_time` (end_time >= start_time) |
| 27 | +- [ ] Constraint exists: `valid_source` (source in manual/widget/shortcut) |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 2. Test User Creation & Auto-Triggers |
| 32 | + |
| 33 | +**Location:** `Authentication` → `Users` |
| 34 | + |
| 35 | +### Create a test user: |
| 36 | + |
| 37 | +1. Click "Add user" → "Create new user" |
| 38 | +2. Email: `test@example.com` |
| 39 | +3. Password: `testpassword123` |
| 40 | +4. Click "Create user" |
| 41 | + |
| 42 | +### Verify auto-creation trigger worked: |
| 43 | + |
| 44 | +**Check profiles table:** |
| 45 | + |
| 46 | +- [ ] Go to `Table Editor` → `profiles` |
| 47 | +- [ ] New row exists with the user's UUID |
| 48 | +- [ ] `created_at` and `updated_at` are populated |
| 49 | + |
| 50 | +**Check user_settings table:** |
| 51 | + |
| 52 | +- [ ] Go to `Table Editor` → `user_settings` |
| 53 | +- [ ] New row exists with the user's UUID |
| 54 | +- [ ] `target_hours` = 8.0 (default) |
| 55 | +- [ ] `timezone` = 'UTC' (default) |
| 56 | +- [ ] `created_at` and `updated_at` are populated |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## 3. Test CRUD Operations (as authenticated user) |
| 61 | + |
| 62 | +**Location:** `SQL Editor` |
| 63 | + |
| 64 | +### Setup: Get your test user's ID |
| 65 | + |
| 66 | +```sql |
| 67 | +SELECT id, email FROM auth.users WHERE email = 'test@example.com'; |
| 68 | +``` |
| 69 | + |
| 70 | +Copy the UUID - you'll need it below. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +### Test INSERT (Create) |
| 75 | + |
| 76 | +**Insert a sleep session:** |
| 77 | + |
| 78 | +```sql |
| 79 | +INSERT INTO public.sleep_sessions (user_id, start_time, end_time, source) |
| 80 | +VALUES ( |
| 81 | + 'YOUR_USER_ID_HERE', |
| 82 | + '2024-11-11 22:00:00+00', |
| 83 | + '2024-11-12 06:00:00+00', |
| 84 | + 'manual' |
| 85 | +); |
| 86 | +``` |
| 87 | + |
| 88 | +**Verify:** |
| 89 | + |
| 90 | +- [ ] Query succeeds |
| 91 | +- [ ] `id` was auto-generated (UUID) |
| 92 | +- [ ] `updated_at` was auto-set to now() |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +### Test SELECT (Read) |
| 97 | + |
| 98 | +**Fetch the session:** |
| 99 | + |
| 100 | +```sql |
| 101 | +SELECT * FROM public.sleep_sessions |
| 102 | +WHERE user_id = 'YOUR_USER_ID_HERE' |
| 103 | +ORDER BY start_time DESC; |
| 104 | +``` |
| 105 | + |
| 106 | +**Verify:** |
| 107 | + |
| 108 | +- [ ] Returns the session you just created |
| 109 | +- [ ] All fields are correct |
| 110 | +- [ ] Timestamps are in UTC |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +### Test UPDATE |
| 115 | + |
| 116 | +**Update the session:** |
| 117 | + |
| 118 | +```sql |
| 119 | +UPDATE public.sleep_sessions |
| 120 | +SET end_time = '2024-11-12 07:00:00+00' |
| 121 | +WHERE user_id = 'YOUR_USER_ID_HERE'; |
| 122 | +``` |
| 123 | + |
| 124 | +**Verify the auto-update trigger:** |
| 125 | + |
| 126 | +```sql |
| 127 | +SELECT id, end_time, updated_at |
| 128 | +FROM public.sleep_sessions |
| 129 | +WHERE user_id = 'YOUR_USER_ID_HERE'; |
| 130 | +``` |
| 131 | + |
| 132 | +- [ ] `end_time` changed to 07:00 |
| 133 | +- [ ] `updated_at` automatically updated to current timestamp (newer than created_at) |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +### Test DELETE |
| 138 | + |
| 139 | +**Delete the session:** |
| 140 | + |
| 141 | +```sql |
| 142 | +DELETE FROM public.sleep_sessions |
| 143 | +WHERE user_id = 'YOUR_USER_ID_HERE'; |
| 144 | +``` |
| 145 | + |
| 146 | +**Verify:** |
| 147 | + |
| 148 | +```sql |
| 149 | +SELECT COUNT(*) FROM public.sleep_sessions |
| 150 | +WHERE user_id = 'YOUR_USER_ID_HERE'; |
| 151 | +``` |
| 152 | + |
| 153 | +- [ ] Returns 0 (session was deleted) |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## 4. Test Constraints |
| 158 | + |
| 159 | +### Test: end_time must be >= start_time |
| 160 | + |
| 161 | +**Try to insert invalid session:** |
| 162 | + |
| 163 | +```sql |
| 164 | +INSERT INTO public.sleep_sessions (user_id, start_time, end_time) |
| 165 | +VALUES ( |
| 166 | + 'YOUR_USER_ID_HERE', |
| 167 | + '2024-11-12 08:00:00+00', |
| 168 | + '2024-11-12 06:00:00+00' -- Earlier than start_time! |
| 169 | +); |
| 170 | +``` |
| 171 | + |
| 172 | +- [ ] Query FAILS with constraint violation error |
| 173 | +- [ ] Error mentions `valid_end_time` constraint |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +### Test: source must be valid |
| 178 | + |
| 179 | +**Try to insert invalid source:** |
| 180 | + |
| 181 | +```sql |
| 182 | +INSERT INTO public.sleep_sessions (user_id, start_time, end_time, source) |
| 183 | +VALUES ( |
| 184 | + 'YOUR_USER_ID_HERE', |
| 185 | + '2024-11-11 22:00:00+00', |
| 186 | + '2024-11-12 06:00:00+00', |
| 187 | + 'invalid_source' -- Not in allowed values! |
| 188 | +); |
| 189 | +``` |
| 190 | + |
| 191 | +- [ ] Query FAILS with constraint violation error |
| 192 | +- [ ] Error mentions `valid_source` constraint |
| 193 | + |
| 194 | +--- |
| 195 | + |
| 196 | +### Test: target_hours must be valid |
| 197 | + |
| 198 | +**Try to set invalid target_hours:** |
| 199 | + |
| 200 | +```sql |
| 201 | +UPDATE public.user_settings |
| 202 | +SET target_hours = 25.0 -- More than 24 hours! |
| 203 | +WHERE user_id = 'YOUR_USER_ID_HERE'; |
| 204 | +``` |
| 205 | + |
| 206 | +- [ ] Query FAILS with constraint violation error |
| 207 | +- [ ] Error mentions `valid_target_hours` constraint |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +## 5. Test Row Level Security (RLS) |
| 212 | + |
| 213 | +### Create a second test user: |
| 214 | + |
| 215 | +1. Go to `Authentication` → `Users` |
| 216 | +2. Create another user: `test2@example.com` / `testpassword123` |
| 217 | +3. Get the new user's ID |
| 218 | + |
| 219 | +### Test RLS isolation: |
| 220 | + |
| 221 | +**Insert session for user 1:** |
| 222 | + |
| 223 | +```sql |
| 224 | +INSERT INTO public.sleep_sessions (user_id, start_time, end_time) |
| 225 | +VALUES ( |
| 226 | + 'USER_1_ID_HERE', |
| 227 | + '2024-11-11 22:00:00+00', |
| 228 | + '2024-11-12 06:00:00+00' |
| 229 | +); |
| 230 | +``` |
| 231 | + |
| 232 | +**Insert session for user 2:** |
| 233 | + |
| 234 | +```sql |
| 235 | +INSERT INTO public.sleep_sessions (user_id, start_time, end_time) |
| 236 | +VALUES ( |
| 237 | + 'USER_2_ID_HERE', |
| 238 | + '2024-11-11 23:00:00+00', |
| 239 | + '2024-11-12 07:00:00+00' |
| 240 | +); |
| 241 | +``` |
| 242 | + |
| 243 | +**Verify each user only sees their own data:** |
| 244 | + |
| 245 | +```sql |
| 246 | +-- This should return 1 session |
| 247 | +SELECT COUNT(*) FROM public.sleep_sessions WHERE user_id = 'USER_1_ID_HERE'; |
| 248 | + |
| 249 | +-- This should return 1 session |
| 250 | +SELECT COUNT(*) FROM public.sleep_sessions WHERE user_id = 'USER_2_ID_HERE'; |
| 251 | +``` |
| 252 | + |
| 253 | +- [ ] User 1 has 1 session |
| 254 | +- [ ] User 2 has 1 session |
| 255 | +- [ ] Total sessions in table = 2 |
| 256 | + |
| 257 | +**Note:** In SQL Editor, you're running as the service role (bypasses RLS). In your actual apps, RLS will automatically filter based on `auth.uid()`. |
| 258 | + |
| 259 | +--- |
| 260 | + |
| 261 | +## 6. Test Cascade Deletes |
| 262 | + |
| 263 | +**Delete user 1:** |
| 264 | + |
| 265 | +```sql |
| 266 | +DELETE FROM auth.users WHERE id = 'USER_1_ID_HERE'; |
| 267 | +``` |
| 268 | + |
| 269 | +**Verify cascade worked:** |
| 270 | + |
| 271 | +```sql |
| 272 | +-- Should return 0 (deleted) |
| 273 | +SELECT COUNT(*) FROM public.profiles WHERE id = 'USER_1_ID_HERE'; |
| 274 | + |
| 275 | +-- Should return 0 (deleted) |
| 276 | +SELECT COUNT(*) FROM public.sleep_sessions WHERE user_id = 'USER_1_ID_HERE'; |
| 277 | + |
| 278 | +-- Should return 0 (deleted) |
| 279 | +SELECT COUNT(*) FROM public.user_settings WHERE user_id = 'USER_1_ID_HERE'; |
| 280 | +``` |
| 281 | + |
| 282 | +- [ ] Profile was deleted |
| 283 | +- [ ] All sleep sessions were deleted |
| 284 | +- [ ] User settings were deleted |
| 285 | +- [ ] User 2's data is still intact |
| 286 | + |
| 287 | +--- |
| 288 | + |
| 289 | +## 7. Test User Settings CRUD |
| 290 | + |
| 291 | +**Update settings for user 2:** |
| 292 | + |
| 293 | +```sql |
| 294 | +UPDATE public.user_settings |
| 295 | +SET |
| 296 | + target_hours = 7.5, |
| 297 | + timezone = 'America/New_York' |
| 298 | +WHERE user_id = 'USER_2_ID_HERE'; |
| 299 | +``` |
| 300 | + |
| 301 | +**Verify:** |
| 302 | + |
| 303 | +```sql |
| 304 | +SELECT * FROM public.user_settings WHERE user_id = 'USER_2_ID_HERE'; |
| 305 | +``` |
| 306 | + |
| 307 | +- [ ] `target_hours` = 7.5 |
| 308 | +- [ ] `timezone` = 'America/New_York' |
| 309 | +- [ ] `updated_at` was automatically updated |
| 310 | + |
| 311 | +--- |
| 312 | + |
| 313 | +## 8. Test Multiple Sleep Sessions |
| 314 | + |
| 315 | +**Insert multiple sessions for user 2:** |
| 316 | + |
| 317 | +```sql |
| 318 | +INSERT INTO public.sleep_sessions (user_id, start_time, end_time, source) VALUES |
| 319 | + ('USER_2_ID_HERE', '2024-11-10 22:00:00+00', '2024-11-11 06:00:00+00', 'manual'), |
| 320 | + ('USER_2_ID_HERE', '2024-11-11 23:00:00+00', '2024-11-12 07:00:00+00', 'widget'), |
| 321 | + ('USER_2_ID_HERE', '2024-11-12 22:30:00+00', '2024-11-13 06:30:00+00', 'shortcut'); |
| 322 | +``` |
| 323 | + |
| 324 | +**Verify ordering and filtering:** |
| 325 | + |
| 326 | +```sql |
| 327 | +SELECT start_time, end_time, source |
| 328 | +FROM public.sleep_sessions |
| 329 | +WHERE user_id = 'USER_2_ID_HERE' |
| 330 | +ORDER BY start_time DESC; |
| 331 | +``` |
| 332 | + |
| 333 | +- [ ] Returns 3 sessions |
| 334 | +- [ ] Ordered by most recent first |
| 335 | +- [ ] Different sources are preserved |
| 336 | + |
| 337 | +--- |
| 338 | + |
| 339 | +## 9. Test Indexes (Performance Check) |
| 340 | + |
| 341 | +**Check that indexes exist:** |
| 342 | + |
| 343 | +```sql |
| 344 | +SELECT indexname, indexdef |
| 345 | +FROM pg_indexes |
| 346 | +WHERE tablename = 'sleep_sessions'; |
| 347 | +``` |
| 348 | + |
| 349 | +**Verify these indexes exist:** |
| 350 | + |
| 351 | +- [ ] `sleep_sessions_user_id_idx` on `user_id` |
| 352 | +- [ ] `sleep_sessions_start_time_idx` on `start_time DESC` |
| 353 | +- [ ] `sleep_sessions_user_start_idx` on `(user_id, start_time DESC)` |
| 354 | + |
| 355 | +--- |
| 356 | + |
| 357 | +## ✅ Phase 1 Complete! |
| 358 | + |
| 359 | +If all tests pass, your backend is ready for Phase 2 (iOS development). |
| 360 | + |
| 361 | +### Summary: |
| 362 | + |
| 363 | +- ✅ Schema created successfully |
| 364 | +- ✅ Auto-triggers work (profile + settings creation) |
| 365 | +- ✅ CRUD operations work |
| 366 | +- ✅ Constraints prevent invalid data |
| 367 | +- ✅ RLS policies isolate user data |
| 368 | +- ✅ Cascade deletes work |
| 369 | +- ✅ Indexes exist for performance |
| 370 | +- ✅ Email/password auth enabled |
| 371 | + |
| 372 | +### Save your credentials: |
| 373 | + |
| 374 | +Don't forget to save these to your `.env` file: |
| 375 | + |
| 376 | +- `SUPABASE_URL` (from Project Settings → API) |
| 377 | +- `SUPABASE_ANON_KEY` (from Project Settings → API) |
0 commit comments