basejump-supabase_test_helpers

0.0.1Created a month agoBy basejump

Supabase Test Helpers

A collection of functions designed to make testing Supabase projects easier.

Usage

We do not recommend that you activate this extension globally. Instead, activate it as part of your pgTAP tests. That will make sure these functions are not available to your users.

For example, a test could look like:


BEGIN;
    CREATE EXTENSION "basejump-supabase_test_helpers";
    
    select plan(1);
    -- create a table, which will have RLS disabled by default
    CREATE TABLE public.tb1 (id int, data text);
    ALTER TABLE public.tb1 ENABLE ROW LEVEL SECURITY;

    -- test to make sure RLS check works
    select check_test(tests.rls_enabled('public', 'tb1'), true);

    SELECT * FROM finish();
ROLLBACK;

Contributing

Yes, please! Anything you've found helpful for testing Supabase projects is welcome. To contribute, head to the GitHub repo

Test Helpers

The following is auto-generated off of comments in the supabase_test_helpers.sql file. Any changes added to the README directly will be overwritten.

tests.create_supabase_user(identifier text, email text, phone text)

Creates a new user in the auth.users table. You can recall a user's info by using tests.get_supabase_user(identifier text).

Parameters:

  • identifier - A unique identifier for the user. We recommend you keep it memorable like "test_owner" or "test_member"
  • email - (Optional) The email address of the user
  • phone - (Optional) The phone number of the user
  • metadata - (Optional) Additional metadata to be added to the user

Returns:

  • user_id - The UUID of the user in the auth.users table

Example:

  SELECT tests.create_supabase_user('test_owner');
  SELECT tests.create_supabase_user('test_member', 'member@test.com', '555-555-5555');
  SELECT tests.create_supabase_user('test_member', 'member@test.com', '555-555-5555', '{"key": "value"}'::jsonb);

tests.get_supabase_user(identifier text)

Returns the user info for a user created with tests.create_supabase_user.

Parameters:

  • identifier - The unique identifier for the user

Returns:

  • user_id - The UUID of the user in the auth.users table

Example:

  SELECT posts where posts.user_id = tests.get_supabase_user('test_owner') -> 'id';

tests.get_supabase_uid(identifier text)

Returns the user UUID for a user created with tests.create_supabase_user.

Parameters:

  • identifier - The unique identifier for the user

Returns:

  • user_id - The UUID of the user in the auth.users table

Example:

  SELECT posts where posts.user_id = tests.get_supabase_uid('test_owner') -> 'id';

tests.authenticate_as(identifier text)

Authenticates as a user created with tests.create_supabase_user.

Parameters:

  • identifier - The unique identifier for the user

Returns:

  • void

Example:

  SELECT tests.create_supabase_user('test_owner');
  SELECT tests.authenticate_as('test_owner');

tests.clear_authentication()

Clears out the authentication and sets role to anon

Returns:

  • void

Example:

  SELECT tests.create_supabase_user('test_owner');
  SELECT tests.authenticate_as('test_owner');
  SELECT tests.clear_authentication();

tests.rls_enabled(testing_schema text)

pgTAP function to check if RLS is enabled on all tables in a provided schema

Parameters:

  • schema_name text - The name of the schema to check

Example:

  BEGIN;
      select plan(1);
      select tests.rls_enabled('public');
      SELECT * FROM finish();
  ROLLBACK;

tests.rls_enabled(testing_schema text, testing_table text)

pgTAP function to check if RLS is enabled on a specific table

Parameters:

  • schema_name text - The name of the schema to check
  • testing_table text - The name of the table to check

Example:

   BEGIN;
       select plan(1);
       select tests.rls_enabled('public', 'accounts');
       SELECT * FROM finish();
   ROLLBACK;

Install

  1. Install the dbdev package manager
  2. Install the package:
select dbdev.install('basejump-supabase_test_helpers');
create extension "basejump-supabase_test_helpers"
    version '0.0.1';

Downloads

  • 844 all time downloads
  • 822 downloads in the last 30 days
  • 844 downloads in the last 90 days
  • 844 downloads in the last 180 days