github.com/supabase/cli@v1.168.1/internal/login/login_test.go (about)

     1  package login
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/spf13/afero"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  	"github.com/supabase/cli/internal/testing/apitest"
    15  	"github.com/supabase/cli/internal/utils"
    16  	"github.com/supabase/cli/internal/utils/credentials"
    17  	"github.com/zalando/go-keyring"
    18  	"gopkg.in/h2non/gock.v1"
    19  )
    20  
    21  type MockEncryption struct {
    22  	token     string
    23  	publicKey string
    24  }
    25  
    26  func (enc *MockEncryption) encodedPublicKey() string {
    27  	return enc.publicKey
    28  }
    29  
    30  func (enc *MockEncryption) decryptAccessToken(accessToken string, publicKey string, nonce string) (string, error) {
    31  	return enc.token, nil
    32  }
    33  
    34  func TestLoginCommand(t *testing.T) {
    35  	keyring.MockInit()
    36  
    37  	t.Run("accepts --token flag and validates provided value", func(t *testing.T) {
    38  		token := string(apitest.RandomAccessToken(t))
    39  		assert.NoError(t, Run(context.Background(), os.Stdout, RunParams{
    40  			Token: token,
    41  			Fsys:  afero.NewMemMapFs(),
    42  		}))
    43  		saved, err := credentials.Get(utils.AccessTokenKey)
    44  		assert.NoError(t, err)
    45  		assert.Equal(t, token, saved)
    46  	})
    47  
    48  	t.Run("goes through automated flow successfully", func(t *testing.T) {
    49  		r, w, err := os.Pipe()
    50  		require.NoError(t, err)
    51  
    52  		sessionId := "random_session_id"
    53  		token := string(apitest.RandomAccessToken(t))
    54  		tokenName := "random_token_name"
    55  		publicKey := "random_public_key"
    56  
    57  		defer gock.OffAll()
    58  
    59  		gock.New(utils.GetSupabaseAPIHost()).
    60  			Get("/platform/cli/login/" + sessionId).
    61  			Reply(200).
    62  			JSON(map[string]any{
    63  				"id":           "0b0d48f6-878b-4190-88d7-2ca33ed800bc",
    64  				"created_at":   "2023-03-28T13:50:14.464Z",
    65  				"access_token": "picklerick",
    66  				"public_key":   "iddqd",
    67  				"nonce":        "idkfa",
    68  			})
    69  
    70  		enc := &MockEncryption{publicKey: publicKey, token: token}
    71  		runParams := RunParams{
    72  			TokenName:  tokenName,
    73  			SessionId:  sessionId,
    74  			Fsys:       afero.NewMemMapFs(),
    75  			Encryption: enc,
    76  		}
    77  		assert.NoError(t, Run(context.Background(), w, runParams))
    78  		w.Close()
    79  
    80  		var out bytes.Buffer
    81  		_, _ = io.Copy(&out, r)
    82  
    83  		expectedBrowserUrl := fmt.Sprintf("%s/cli/login?session_id=%s&token_name=%s&public_key=%s", utils.GetSupabaseDashboardURL(), sessionId, tokenName, publicKey)
    84  		assert.Contains(t, out.String(), expectedBrowserUrl)
    85  
    86  		saved, err := credentials.Get(utils.AccessTokenKey)
    87  		assert.NoError(t, err)
    88  		assert.Equal(t, token, saved)
    89  		assert.Empty(t, apitest.ListUnmatchedRequests())
    90  	})
    91  }