package integration

import (
	"fmt"
	"net/http"
	"testing"

	"github.com/contiamo/dev/tests/integration/suite"
)

type Payload suite.Payload

func TestDataSourceDeleteExternal(t *testing.T) {
	s := suite.New(t)

	s.Run("prepare datasources", func(s *suite.Suite) {
		path := fmt.Sprintf("/hub/api/v1/%s/datasources", s.ProjectID)
		payload := Payload{
			"name":       "delete_me_external",
			"type":       "external",
			"technology": "postgresql",
			"connectionInfo": Payload{
				"host":     "metadb",
				"port":     "5433",
				"database": "hub",
				"user":     "user",
				"password": "localdev",
			},
		}
		resp := s.Post(path, payload)
		defer resp.Body.Close()
		s.Require().Equal(http.StatusCreated, resp.StatusCode, resp.Body)
	})

	s.Run("delete datasources", func(s *suite.Suite) {
		path := fmt.Sprintf("/hub/api/v1/%s/datasources/%s", s.ProjectID, "delete_me_external")
		resp := s.Delete(path)
		defer resp.Body.Close()
		s.Require().Equal(http.StatusNoContent, resp.StatusCode)
	})
}
