github.com/supabase/cli@v1.168.1/internal/orgs/create/create_test.go (about)

     1  package create
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/supabase/cli/internal/testing/apitest"
    11  	"github.com/supabase/cli/internal/utils"
    12  	"github.com/supabase/cli/pkg/api"
    13  	"gopkg.in/h2non/gock.v1"
    14  )
    15  
    16  func TestOrganizationCreateCommand(t *testing.T) {
    17  	orgName := "Test Organization"
    18  
    19  	t.Run("create an organization", func(t *testing.T) {
    20  		// Setup valid access token
    21  		token := apitest.RandomAccessToken(t)
    22  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    23  		// Flush pending mocks after test execution
    24  		defer gock.OffAll()
    25  		gock.New(utils.DefaultApiHost).
    26  			Post("/v1/organizations").
    27  			Reply(http.StatusCreated).
    28  			JSON(api.OrganizationResponseV1{
    29  				Id:   "combined-fuchsia-lion",
    30  				Name: orgName,
    31  			})
    32  		// Run test
    33  		assert.NoError(t, Run(context.Background(), orgName))
    34  		// Validate api
    35  		assert.Empty(t, apitest.ListUnmatchedRequests())
    36  	})
    37  
    38  	t.Run("throws error on network error", func(t *testing.T) {
    39  		// Setup valid access token
    40  		token := apitest.RandomAccessToken(t)
    41  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    42  		// Flush pending mocks after test execution
    43  		defer gock.OffAll()
    44  		gock.New(utils.DefaultApiHost).
    45  			Post("/v1/organizations").
    46  			ReplyError(errors.New("network error"))
    47  		// Run test
    48  		assert.Error(t, Run(context.Background(), orgName))
    49  		// Validate api
    50  		assert.Empty(t, apitest.ListUnmatchedRequests())
    51  	})
    52  
    53  	t.Run("throws error on server unavailable", func(t *testing.T) {
    54  		// Setup valid access token
    55  		token := apitest.RandomAccessToken(t)
    56  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    57  		// Flush pending mocks after test execution
    58  		defer gock.OffAll()
    59  		gock.New(utils.DefaultApiHost).
    60  			Post("/v1/organizations").
    61  			Reply(http.StatusServiceUnavailable).
    62  			JSON(map[string]string{"message": "unavailable"})
    63  		// Run test
    64  		assert.Error(t, Run(context.Background(), orgName))
    65  		// Validate api
    66  		assert.Empty(t, apitest.ListUnmatchedRequests())
    67  	})
    68  }