github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/test/login_test.go (about)

     1  package integration
     2  
     3  // Basic imports
     4  import (
     5  	"net/http"
     6  	"os"
     7  	"path/filepath"
     8  	"sync"
     9  	"testing"
    10  
    11  	"github.com/gin-gonic/gin"
    12  	gonanoid "github.com/matoous/go-nanoid/v2"
    13  	"github.com/spf13/cobra"
    14  	"github.com/stretchr/testify/require"
    15  	"github.com/stretchr/testify/suite"
    16  	clicmd "github.com/Redstoneguy129/cli/cmd"
    17  	"github.com/Redstoneguy129/cli/test/mocks/supabase"
    18  )
    19  
    20  type LoginTestSuite struct {
    21  	suite.Suite
    22  	tempDir string
    23  	cmd     *cobra.Command
    24  
    25  	ids     []string
    26  	headers []http.Header
    27  
    28  	mtx sync.RWMutex
    29  }
    30  
    31  // test functions
    32  func (suite *LoginTestSuite) TestLink() {
    33  	// run command
    34  	login, _, err := suite.cmd.Find([]string{"login"})
    35  	require.NoError(suite.T(), err)
    36  	key := "sbp_" + gonanoid.MustGenerate(supabase.KeyAlphabet, supabase.KeyLength)
    37  
    38  	// change stdin to read from a file
    39  	content := []byte(key)
    40  	tmpfile, err := os.CreateTemp(suite.tempDir, "key")
    41  	require.NoError(suite.T(), err)
    42  	defer os.Remove(tmpfile.Name()) // clean up
    43  
    44  	_, err = tmpfile.Write(content)
    45  	require.NoError(suite.T(), err)
    46  	_, err = tmpfile.Seek(0, 0)
    47  	require.NoError(suite.T(), err)
    48  
    49  	oldStdin := os.Stdin
    50  	defer func() { os.Stdin = oldStdin }()
    51  	os.Stdin = tmpfile
    52  
    53  	require.NoError(suite.T(), login.RunE(login, []string{}))
    54  
    55  	// check token is saved
    56  	home, err := os.UserHomeDir()
    57  	require.NoError(suite.T(), err)
    58  	_, err = os.Stat(filepath.Join(home, ".supabase/access-token"))
    59  	require.NoError(suite.T(), err)
    60  	token, err := os.ReadFile(filepath.Join(home, ".supabase/access-token"))
    61  	require.NoError(suite.T(), err)
    62  	require.Equal(suite.T(), key, string(token))
    63  }
    64  
    65  // hooks
    66  func (suite *LoginTestSuite) SetupTest() {
    67  	// init cli
    68  	suite.cmd = clicmd.GetRootCmd()
    69  	suite.tempDir = NewTempDir(Logger, TempDir)
    70  
    71  	// init supabase
    72  	init, _, err := suite.cmd.Find([]string{"init"})
    73  	require.NoError(suite.T(), err)
    74  	require.NoError(suite.T(), init.RunE(init, []string{}))
    75  
    76  	// implement mocks
    77  	SupaMock.FunctionsHandler = func(c *gin.Context) {
    78  		suite.addHeaders(c.Request.Header)
    79  		suite.addID(c.Params.ByName("id"))
    80  
    81  		c.JSON(http.StatusOK, gin.H{})
    82  	}
    83  }
    84  
    85  func (suite *LoginTestSuite) TeardownTest() {
    86  	require.NoError(suite.T(), os.Chdir(TempDir))
    87  }
    88  
    89  // In order for 'go test' to run this suite, we need to create
    90  // a normal test function and pass our suite to suite.Run
    91  func TestLoginTestSuite(t *testing.T) {
    92  	suite.Run(t, new(LoginTestSuite))
    93  }
    94  
    95  // helper functions
    96  func (suite *LoginTestSuite) addID(id string) {
    97  	suite.mtx.Lock()
    98  	defer suite.mtx.Unlock()
    99  	suite.ids = append(suite.ids, id)
   100  }
   101  
   102  func (suite *LoginTestSuite) addHeaders(headers http.Header) {
   103  	suite.mtx.Lock()
   104  	defer suite.mtx.Unlock()
   105  	suite.headers = append(suite.headers, headers)
   106  }