|
| 1 | +-- Test file for invite_organization_member function |
| 2 | +BEGIN; |
| 3 | + |
| 4 | +-- Load the pgtap extension |
| 5 | +SELECT plan(7); |
| 6 | + |
| 7 | +-- Create test users and organization |
| 8 | +SELECT tests.create_supabase_user('inviter@example.com', 'inviter_user'); |
| 9 | +SELECT tests.create_supabase_user('existing@example.com', 'existing_user'); |
| 10 | +SELECT tests.create_supabase_user('new@example.com', 'new_user'); |
| 11 | +SELECT tests.create_supabase_user('reinvite@example.com', 'reinvite_user'); |
| 12 | + |
| 13 | +-- Create test organization |
| 14 | +INSERT INTO organizations (id, name) |
| 15 | +VALUES ('11111111-1111-1111-1111-111111111111', 'Test Organization') |
| 16 | +ON CONFLICT DO NOTHING; |
| 17 | + |
| 18 | +-- Store user IDs in temporary variables |
| 19 | +DO $$ |
| 20 | +DECLARE |
| 21 | + v_inviter_id uuid; |
| 22 | + v_existing_id uuid; |
| 23 | + v_new_id uuid; |
| 24 | + v_reinvite_id uuid; |
| 25 | +BEGIN |
| 26 | + -- Get user IDs |
| 27 | + SELECT tests.get_supabase_uid('inviter@example.com') INTO v_inviter_id; |
| 28 | + SELECT tests.get_supabase_uid('existing@example.com') INTO v_existing_id; |
| 29 | + SELECT tests.get_supabase_uid('new@example.com') INTO v_new_id; |
| 30 | + SELECT tests.get_supabase_uid('reinvite@example.com') INTO v_reinvite_id; |
| 31 | + |
| 32 | + -- Add inviter and existing user to the organization |
| 33 | + INSERT INTO organization_members (user_id, organization_id) |
| 34 | + VALUES |
| 35 | + (v_inviter_id, '11111111-1111-1111-1111-111111111111'), |
| 36 | + (v_existing_id, '11111111-1111-1111-1111-111111111111') |
| 37 | + ON CONFLICT DO NOTHING; |
| 38 | + |
| 39 | + -- Create an existing invitation for reinvite user |
| 40 | + INSERT INTO invitations (id, email, invite_by_user_id, organization_id, invited_at) |
| 41 | + VALUES ( |
| 42 | + '22222222-2222-2222-2222-222222222222', |
| 43 | + 'reinvite@example.com', |
| 44 | + v_inviter_id, |
| 45 | + '11111111-1111-1111-1111-111111111111', |
| 46 | + NOW() - INTERVAL '1 day' |
| 47 | + ) |
| 48 | + ON CONFLICT DO NOTHING; |
| 49 | +END $$; |
| 50 | + |
| 51 | +-- Set up authentication context for tests |
| 52 | +DO $$ |
| 53 | +DECLARE |
| 54 | + v_inviter_id uuid; |
| 55 | +BEGIN |
| 56 | + -- Get inviter user ID |
| 57 | + SELECT tests.get_supabase_uid('inviter@example.com') INTO v_inviter_id; |
| 58 | + |
| 59 | + -- Set up auth context to simulate the inviter user |
| 60 | + EXECUTE format('SET LOCAL ROLE authenticated; SET LOCAL "request.jwt.claims" = ''{"sub": "%s"}''', v_inviter_id); |
| 61 | +END $$; |
| 62 | + |
| 63 | +-- Test 1: Successfully invite a new member |
| 64 | +SELECT is( |
| 65 | + (SELECT invite_organization_member('new@example.com', '11111111-1111-1111-1111-111111111111')), |
| 66 | + '{"success": true, "error": null}'::jsonb, |
| 67 | + 'Should successfully invite a new member' |
| 68 | +); |
| 69 | + |
| 70 | +-- Test 2: Verify the invitation was created |
| 71 | +SELECT is( |
| 72 | + (SELECT COUNT(*) FROM invitations WHERE email = 'new@example.com' AND organization_id = '11111111-1111-1111-1111-111111111111'), |
| 73 | + 1::bigint, |
| 74 | + 'Should create a new invitation record' |
| 75 | +); |
| 76 | + |
| 77 | +-- Test 3: Attempt to invite a user who is already a member |
| 78 | +SELECT is( |
| 79 | + (SELECT invite_organization_member('existing@example.com', '11111111-1111-1111-1111-111111111111')), |
| 80 | + '{"success": false, "error": "this user is already a member of the organization"}'::jsonb, |
| 81 | + 'Should fail when inviting an existing member' |
| 82 | +); |
| 83 | + |
| 84 | +-- Test 4: Re-invite a user who already has a pending invitation |
| 85 | +SELECT is( |
| 86 | + (SELECT invite_organization_member('reinvite@example.com', '11111111-1111-1111-1111-111111111111')), |
| 87 | + '{"success": true, "error": null}'::jsonb, |
| 88 | + 'Should successfully re-invite a user' |
| 89 | +); |
| 90 | + |
| 91 | +-- Test 5: Verify the invitation was updated, not duplicated |
| 92 | +SELECT is( |
| 93 | + (SELECT COUNT(*) FROM invitations WHERE email = 'reinvite@example.com' AND organization_id = '11111111-1111-1111-1111-111111111111'), |
| 94 | + 1::bigint, |
| 95 | + 'Should not create duplicate invitation' |
| 96 | +); |
| 97 | + |
| 98 | +-- Test 6: Verify the invited_at timestamp was updated |
| 99 | +SELECT ok( |
| 100 | + (SELECT invited_at > (NOW() - INTERVAL '10 seconds') |
| 101 | + FROM invitations |
| 102 | + WHERE email = 'reinvite@example.com' AND organization_id = '11111111-1111-1111-1111-111111111111'), |
| 103 | + 'Should update the invited_at timestamp' |
| 104 | +); |
| 105 | + |
| 106 | +-- Test 7: Case insensitivity test |
| 107 | +SELECT is( |
| 108 | + (SELECT invite_organization_member('NEW@example.com', '11111111-1111-1111-1111-111111111111')), |
| 109 | + '{"success": true, "error": null}'::jsonb, |
| 110 | + 'Should be case insensitive when checking existing invitations' |
| 111 | +); |
| 112 | + |
| 113 | +-- Reset authentication context |
| 114 | +RESET ROLE; |
| 115 | + |
| 116 | +-- Finish the tests and print a diagnostic count |
| 117 | +SELECT * FROM finish(); |
| 118 | + |
| 119 | +ROLLBACK; |
0 commit comments