github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/test/link_test.go (about) 1 package integration 2 3 // Basic imports 4 import ( 5 "context" 6 "fmt" 7 "net/http" 8 "os" 9 "sync" 10 "testing" 11 12 clicmd "github.com/Redstoneguy129/cli/cmd" 13 "github.com/Redstoneguy129/cli/internal/utils" 14 "github.com/Redstoneguy129/cli/pkg/api" 15 "github.com/Redstoneguy129/cli/test/mocks/supabase" 16 "github.com/gin-gonic/gin" 17 gonanoid "github.com/matoous/go-nanoid/v2" 18 "github.com/spf13/cobra" 19 "github.com/stretchr/testify/require" 20 "github.com/stretchr/testify/suite" 21 ) 22 23 type LinkTestSuite struct { 24 suite.Suite 25 tempDir string 26 cmd *cobra.Command 27 28 ids []string 29 headers []http.Header 30 31 mtx sync.RWMutex 32 } 33 34 // test functions 35 func (suite *LinkTestSuite) TestLink() { 36 // run command 37 link, _, err := suite.cmd.Find([]string{"link"}) 38 link.SetContext(context.Background()) 39 require.NoError(suite.T(), err) 40 41 id := gonanoid.MustGenerate(supabase.IDAlphabet, supabase.IDLength) 42 require.NoError(suite.T(), link.Flags().Set("project-ref", id)) 43 require.NoError(suite.T(), link.Flags().Set("password", "postgres")) 44 45 require.NoError(suite.T(), link.RunE(link, []string{})) 46 47 // check request details 48 suite.mtx.RLock() 49 defer suite.mtx.RUnlock() 50 require.Contains(suite.T(), suite.ids, id) 51 require.Contains(suite.T(), suite.headers, http.Header{ 52 "Authorization": []string{fmt.Sprintf("Bearer %s", supabase.AccessToken)}, 53 "Accept-Encoding": []string{"gzip"}, 54 "User-Agent": []string{"Go-http-client/1.1"}, 55 }) 56 _, err = os.Stat(utils.ProjectRefPath) 57 require.NoError(suite.T(), err) 58 ref, err := os.ReadFile(utils.ProjectRefPath) 59 require.NoError(suite.T(), err) 60 require.Equal(suite.T(), id, string(ref)) 61 } 62 63 // hooks 64 func (suite *LinkTestSuite) SetupTest() { 65 // init cli 66 suite.cmd = clicmd.GetRootCmd() 67 suite.tempDir = NewTempDir(Logger, TempDir) 68 69 // init supabase 70 init, _, err := suite.cmd.Find([]string{"init"}) 71 require.NoError(suite.T(), err) 72 require.NoError(suite.T(), init.RunE(init, []string{})) 73 74 // implement mocks 75 SupaMock.FunctionsHandler = func(c *gin.Context) { 76 suite.addHeaders(c.Request.Header) 77 suite.addID(c.Params.ByName("id")) 78 79 c.JSON(http.StatusOK, []api.FunctionResponse{}) 80 } 81 } 82 83 func (suite *LinkTestSuite) TeardownTest() { 84 require.NoError(suite.T(), os.Chdir(TempDir)) 85 } 86 87 // In order for 'go test' to run this suite, we need to create 88 // a normal test function and pass our suite to suite.Run 89 func TestLinkTestSuite(t *testing.T) { 90 // suite.Run(t, new(LinkTestSuite)) 91 } 92 93 // helper functions 94 func (suite *LinkTestSuite) addID(id string) { 95 suite.mtx.Lock() 96 defer suite.mtx.Unlock() 97 suite.ids = append(suite.ids, id) 98 } 99 100 func (suite *LinkTestSuite) addHeaders(headers http.Header) { 101 suite.mtx.Lock() 102 defer suite.mtx.Unlock() 103 suite.headers = append(suite.headers, headers) 104 }