github.com/supabase/cli@v1.168.1/internal/utils/tenant/gotrue_test.go (about)

     1  package tenant
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/supabase/cli/pkg/fetcher"
    11  	"gopkg.in/h2non/gock.v1"
    12  )
    13  
    14  var mockApi = TenantAPI{Fetcher: fetcher.NewFetcher(
    15  	"http://127.0.0.1",
    16  )}
    17  
    18  func TestGotrueVersion(t *testing.T) {
    19  	t.Run("gets gotrue version", func(t *testing.T) {
    20  		// Setup mock api
    21  		defer gock.OffAll()
    22  		gock.New("http://127.0.0.1").
    23  			Get("/auth/v1/health").
    24  			Reply(http.StatusOK).
    25  			JSON(HealthResponse{Version: "v2.92.1"})
    26  		// Run test
    27  		version, err := mockApi.GetGotrueVersion(context.Background())
    28  		// Check error
    29  		assert.NoError(t, err)
    30  		assert.Equal(t, "v2.92.1", version)
    31  	})
    32  
    33  	t.Run("throws error on network error", func(t *testing.T) {
    34  		// Setup mock api
    35  		defer gock.OffAll()
    36  		gock.New("http://127.0.0.1").
    37  			Get("/auth/v1/health").
    38  			ReplyError(errors.New("network error"))
    39  		// Run test
    40  		version, err := mockApi.GetGotrueVersion(context.Background())
    41  		// Check error
    42  		assert.ErrorContains(t, err, "network error")
    43  		assert.Empty(t, version)
    44  	})
    45  
    46  	t.Run("throws error on missing version", func(t *testing.T) {
    47  		// Setup mock api
    48  		defer gock.OffAll()
    49  		gock.New("http://127.0.0.1").
    50  			Get("/auth/v1/health").
    51  			Reply(http.StatusOK).
    52  			JSON(HealthResponse{})
    53  		// Run test
    54  		version, err := mockApi.GetGotrueVersion(context.Background())
    55  		// Check error
    56  		assert.ErrorIs(t, err, errGotrueVersion)
    57  		assert.Empty(t, version)
    58  	})
    59  }