Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(test): add test for getting projects (#482)

Danny Xu8516961d9fa7
diff --git a/tests/start_test.go b/tests/start_test.go
index e33647f4a2..d0abb13417 100644
--- a/tests/start_test.go
+++ b/tests/start_test.go
@@ -16,4 +16,13 @@ func TestServiceStart(t *testing.T) {
if err := ctl.Login(); err != nil {
t.Fatal(err)
}
+
+ projects, err := ctl.getProjects()
+ if err != nil {
+ t.Fatal(err)
+ }
+ // Test seed should have more than one project.
+ if len(projects) <= 1 {
+ t.Errorf("unexpected number of projects %v", len(projects))
+ }
}
diff --git a/tests/tests.go b/tests/tests.go
index 5745c99c8b..90c9a4b92b 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -3,11 +3,15 @@ package tests
import (
"context"
"fmt"
+ "io"
"net/http"
+ "reflect"
"strings"
"time"
+ "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/bin/server/cmd"
+ "github.com/google/jsonapi"
)
var (
@@ -109,3 +113,39 @@ func (ctl *controller) Login() error {
return nil
}
+
+// get sends a GET client request.
+func (ctl *controller) get(shortURL string) (io.ReadCloser, error) {
+ url := fmt.Sprintf("%s%s", rootURL, shortURL)
+ req, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ return nil, fmt.Errorf("fail to create a new GET request(%q), error %w", url, err)
+ }
+ req.Header.Set("Cookie", ctl.cookie)
+ resp, err := ctl.client.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("fail to send a GET request(%q), error %w", url, err)
+ }
+ return resp.Body, nil
+}
+
+func (ctl *controller) getProjects() ([]*api.Project, error) {
+ body, err := ctl.get("/project")
+ if err != nil {
+ return nil, err
+ }
+
+ var projects []*api.Project
+ ps, err := jsonapi.UnmarshalManyPayload(body, reflect.TypeOf(new(api.Project)))
+ if err != nil {
+ return nil, fmt.Errorf("fail to unmarshal get project response, error %w", err)
+ }
+ for _, p := range ps {
+ project, ok := p.(*api.Project)
+ if !ok {
+ return nil, fmt.Errorf("fail to convert project")
+ }
+ projects = append(projects, project)
+ }
+ return projects, nil
+}