raminder@supabase_test_helpers

v0.0.1Created a year agoBy raminder

Supabase Test Helpers

A collection of functions designed to make testing Supabase projects easier. Created as part of our open source SaaS starter for Supabase.

Quick Start (recommended)

If you're using Supabase:

  1. Install dbdev following the instructions here: github.com/supabase/dbdev
  2. Install the test helpers as an extension:
select dbdev.install('basejump-supabase_test_helpers');

I don't recommend activating the extension in production directly, instead you can activate it as part of your test suite. For example:

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;

For a basic example, check out the example blog tests.

Manual Installation (not recommended)

Copy the contents of the most recent version into the very first alphabetical test in your test suite, such as 00000-supabase_test_helpers.sql. This will ensure that the test helpers are removed after your tests have run. for it to work, you need to create some fake tests at the bottom of the file for pgtap to not complain. Here's an example:


-- we have to run some tests to get this to pass as the first test file.
-- investigating options to make this better.  Maybe a dedicated test harness
-- but we dont' want these functions to always exist on the database.
BEGIN;

    select plan(7);
    select function_returns('tests', 'create_supabase_user', Array['text', 'text', 'text', 'jsonb'], 'uuid');
    select function_returns('tests', 'get_supabase_uid', Array['text'], 'uuid');
    select function_returns('tests', 'get_supabase_user', Array['text'], 'json');
    select function_returns('tests', 'authenticate_as', Array['text'], 'void');
    select function_returns('tests', 'clear_authentication', Array[null], 'void');
    select function_returns('tests', 'rls_enabled', Array['text', 'text'], 'text');
    select function_returns('tests', 'rls_enabled', Array['text'], 'text');
    select * from finish();
ROLLBACK;

Writing tests

Check out the docs below for available helpers. To view a comprehensive example, check out our blog tests.

Test Helpers

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

<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> <!-- END doctoc generated TOC please keep comment here to allow auto update --> <!-- include: supabase_test_helpers--0.0.2.sql -->

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.authenticate_as_service_role()

Clears authentication object and sets role to service_role.

Returns:

  • void

Example:

  SELECT tests.authenticate_as_service_role();

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;
<!-- /include: supabase_test_helpers--0.0.2.sql -->

Contributing

Yes, please! Anything you've found helpful for testing Supabase projects is welcome. To contribute:

  • Create a new version of supabase_test_helpers supabase_test_helpers--{major}-{minor}-{patch}.sql
  • New versions are intended to be a fresh install, so copy the contents of the previous version into the new version.
  • Add pgTAP compliant test functions to the new version
  • Comments should be added above each function, follow the examples in the file.
  • Create a migration file supabase_test_helpers--{oldMajor}-{oldMinor}-{oldPatch}--{newMajor}-{newMinor}-{newPatch}.sql to upgrade to the new version. Include ONLY your migration code, not the entire contents of the new version.
  • Add tests for your functions in tests/XX-your-function-name.sql
  • Submit a PR

Install

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

Downloads

  • 0 all time downloads
  • 0 downloads in the last 30 days
  • 0 downloads in the last 90 days
  • 0 downloads in the last 180 days