Skip to content

Commit

Permalink
chart tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jan 28, 2024
1 parent b404989 commit b2b46b0
Showing 1 changed file with 141 additions and 0 deletions.
141 changes: 141 additions & 0 deletions app/pkg/chart_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package pkg

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

func TestGetChartErrors(t *testing.T) {

BeforeEach()
defer AfterEach()
e := echo.New()
e.GET("/chart", func(c echo.Context) error {
return NewChartHandler().Get(c)
})

server := httptest.NewServer(e)
defer server.Close()

type TestCase struct {
Name string
Query string
Status int
ContentType string
ResponseContains string
}
testCases := []TestCase{
{
Name: "404",
Query: `?org=org&repo=repo&branch=branch&type=type`,
Status: http.StatusNotFound,
ContentType: "application/json; charset=UTF-8",
},
{
Name: "422",
Query: `?repo=repo&branch=branch&type=type`,
ContentType: "application/json; charset=UTF-8",
Status: http.StatusUnprocessableEntity,
},
{
Name: "422",
Query: `?org=org&branch=branch&type=type`,
ContentType: "application/json; charset=UTF-8",
Status: http.StatusUnprocessableEntity,
},
{
Name: "422",
Query: `?org=org&repo=repo&branch=branch`,
ContentType: "application/json; charset=UTF-8",
Status: http.StatusUnprocessableEntity,
},
}

for _, tc := range testCases {
url := server.URL + "/chart" + tc.Query
resp, err := http.Get(url)

assert.NoError(t, err)
assert.Equal(t, tc.Status, resp.StatusCode)
assert.Equal(t, tc.ContentType, resp.Header.Get("Content-Type"))
if tc.ResponseContains != "" {
data, _ := io.ReadAll(resp.Body)
defer resp.Body.Close()
assert.Contains(t, string(data), tc.ResponseContains)
}
}
}

func TestGetChartOK(t *testing.T) {

BeforeEach()
defer AfterEach()
e := echo.New()
e.GET("/chart", func(c echo.Context) error {
return NewChartHandler().Get(c)
})

server := httptest.NewServer(e)
defer server.Close()

type TestCase struct {
Name string
PreUploadRequest *UploadRequest
Query string
Status int
ContentType string
ResponseContains string
}
testCases := []TestCase{
{
Name: "200",
PreUploadRequest: &UploadRequest{
Org: "org",
Repo: "repo",
Type: "type",
Branch: "branch",
Commit: "commit",
},
Query: `?org=org&repo=repo&branch=branch&type=type&output=svg`,
Status: http.StatusOK,
ContentType: "image/svg+xml",
ResponseContains: `<svg xmlns=`,
},
{
Name: "200",
PreUploadRequest: &UploadRequest{
Org: "org",
Repo: "repo",
Type: "type",
Branch: "branch",
Commit: "commit",
Score: "1999",
},
Query: `?org=org&repo=repo&branch=branch&type=type`,
Status: http.StatusOK,
ContentType: "image/png",
},
}

for _, tc := range testCases {
c, err := NewUpload().Post(tc.PreUploadRequest)
assert.NotNil(t, c)
assert.NoError(t, err)
url := server.URL + "/chart" + tc.Query
resp, err := http.Get(url)

assert.NoError(t, err)
assert.Equal(t, tc.Status, resp.StatusCode)
assert.Equal(t, tc.ContentType, resp.Header.Get("Content-Type"))
if tc.ResponseContains != "" {
data, _ := io.ReadAll(resp.Body)
defer resp.Body.Close()
assert.Contains(t, string(data), tc.ResponseContains)
}
}
}

0 comments on commit b2b46b0

Please sign in to comment.