github.com/supabase/cli@v1.168.1/internal/sso/get/get_test.go (about)

     1  package get
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/supabase/cli/internal/testing/apitest"
    10  	"github.com/supabase/cli/internal/utils"
    11  	"gopkg.in/h2non/gock.v1"
    12  )
    13  
    14  func TestSSOProvidersShowCommand(t *testing.T) {
    15  	t.Run("show provider", func(t *testing.T) {
    16  		// Setup valid access token
    17  		token := apitest.RandomAccessToken(t)
    18  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    19  
    20  		// Flush pending mocks after test execution
    21  		defer gock.OffAll()
    22  
    23  		projectRef := "abcdefghijklmnopqrst"
    24  		providerId := "0b0d48f6-878b-4190-88d7-2ca33ed800bc"
    25  
    26  		gock.New(utils.DefaultApiHost).
    27  			Get("/v1/projects/" + projectRef + "/config/auth/sso/providers/" + providerId).
    28  			Reply(200).
    29  			JSON(map[string]any{
    30  				"id":         providerId,
    31  				"created_at": "2023-03-28T13:50:14.464Z",
    32  				"updated_at": "2023-03-28T13:50:14.464Z",
    33  				"saml": map[string]any{
    34  					"id":           "8682fcf4-4056-455c-bd93-f33295604929",
    35  					"metadata_url": "https://example.com",
    36  					"metadata_xml": "<?xml version=\"2.0\"?>",
    37  					"entity_id":    "https://example.com",
    38  					"attribute_mapping": map[string]any{
    39  						"keys": map[string]any{
    40  							"a": map[string]any{
    41  								"name": "xyz",
    42  								"names": []string{
    43  									"x",
    44  									"y",
    45  									"z",
    46  								},
    47  								"default": 3,
    48  							},
    49  						},
    50  					},
    51  					"created_at": "2023-03-28T13:50:14.464Z",
    52  					"updated_at": "2023-03-28T13:50:14.464Z",
    53  				},
    54  				"domains": []map[string]any{
    55  					{
    56  						"id":         "9484591c-a203-4500-bea7-d0aaa845e2f5",
    57  						"domain":     "example.com",
    58  						"created_at": "2023-03-28T13:50:14.464Z",
    59  						"updated_at": "2023-03-28T13:50:14.464Z",
    60  					},
    61  				},
    62  			})
    63  
    64  		// Run test
    65  		assert.NoError(t, Run(context.Background(), projectRef, providerId, utils.OutputPretty))
    66  		// Validate api
    67  		assert.Empty(t, apitest.ListUnmatchedRequests())
    68  	})
    69  
    70  	t.Run("show provider that does not exist", func(t *testing.T) {
    71  		// Setup valid access token
    72  		token := apitest.RandomAccessToken(t)
    73  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    74  
    75  		// Flush pending mocks after test execution
    76  		defer gock.OffAll()
    77  
    78  		projectRef := "abcdefghijklmnopqrst"
    79  		providerId := "0b0d48f6-878b-4190-88d7-2ca33ed800bc"
    80  
    81  		gock.New(utils.DefaultApiHost).
    82  			Get("/v1/projects/" + projectRef + "/config/auth/sso/providers/" + providerId).
    83  			Reply(404).
    84  			JSON(map[string]string{})
    85  
    86  		err := Run(context.Background(), projectRef, providerId, utils.OutputPretty)
    87  
    88  		// Run test
    89  		assert.Error(t, err)
    90  		assert.Equal(t, err.Error(), fmt.Sprintf("An identity provider with ID %q could not be found.", providerId))
    91  
    92  		// Validate api
    93  		assert.Empty(t, apitest.ListUnmatchedRequests())
    94  	})
    95  }