From 2ee0e1e916863e670d0dc501df5d4b780423b565 Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Sun, 21 Jan 2024 14:23:23 +0900 Subject: [PATCH] added tests and fixes --- .github/workflows/build.yml | 2 +- action.yml | 2 - app/db/database.go | 6 +- app/main.go | 2 +- app/models/coverage.go | 15 ++-- app/models/org.go | 23 ++++-- app/models/repo.go | 26 ++++--- app/models/type.go | 24 +++++-- app/models/user.go | 25 ++++--- app/pkg/badge.go | 6 +- app/pkg/badge_handler.go | 2 - app/pkg/badge_handler_test.go | 125 +++++++++++++++++++++++++++++---- app/pkg/chart.go | 12 ++-- app/pkg/destroy_handler.go | 9 ++- app/pkg/setup_test.go | 6 +- app/pkg/upload_handler.go | 20 ++++-- app/pkg/upload_handler_test.go | 89 +++++++++++++++++++++++ 17 files changed, 318 insertions(+), 76 deletions(-) create mode 100644 app/pkg/upload_handler_test.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7bf13b2..a840e51 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: go-version: [latest] - os: [ubuntu-latest, macos-latest] + os: [ubuntu-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 diff --git a/action.yml b/action.yml index 4521775..aaa818e 100644 --- a/action.yml +++ b/action.yml @@ -16,8 +16,6 @@ inputs: description: "Type of score" metric: description: "Metric of score" - value: - description: "Value of Score" destroy: description: "Destroy all" default: 'false' diff --git a/app/db/database.go b/app/db/database.go index 4e07cb9..b66df72 100644 --- a/app/db/database.go +++ b/app/db/database.go @@ -66,7 +66,7 @@ func configureSQL(sqlDB *sql.DB) { sqlDB.SetConnMaxLifetime(time.Hour * time.Duration(1)) } -func Migrate(command string, embedMigrations embed.FS) error { +func Migrate(command string, embedMigrations embed.FS, dir string) error { if command == "create" { create() return nil @@ -93,8 +93,10 @@ func Migrate(command string, embedMigrations embed.FS) error { if err != nil { panic(err) } + goose.SetBaseFS(embedMigrations) - err = goose.RunContext(ctx, command, driver, "migrations") + + err = goose.RunContext(ctx, command, driver, dir) if err != nil { panic(err) } diff --git a/app/main.go b/app/main.go index 9fcbb95..4d5df21 100644 --- a/app/main.go +++ b/app/main.go @@ -43,7 +43,7 @@ func main() { db.SetupDatabase() if f.migrate != "" { - err := db.Migrate(f.migrate, embedMigrations) + err := db.Migrate(f.migrate, embedMigrations, "pkg/migrations") if err != nil { pkg.Logger().Error(err) } diff --git a/app/models/coverage.go b/app/models/coverage.go index bb55cd1..a51a7e9 100644 --- a/app/models/coverage.go +++ b/app/models/coverage.go @@ -165,7 +165,7 @@ func (c *Coverage) GetLatestBranchScore(orgName string, repoName string, branchN AND t.name = @typeName ORDER BY - c.created_at DESC + c.created_at DESC, c.id DESC LIMIT 1; ` err := db.Db().Raw( @@ -519,7 +519,10 @@ func (c *Coverage) Create( score float32) (*Coverage, error) { var ret Coverage - query := `INSERT INTO + branchName = strings.TrimSpace(branchName) + commit = strings.TrimSpace(commit) + + insertQ := `INSERT INTO coverages ( org_id, repo_id, @@ -541,7 +544,7 @@ func (c *Coverage) Create( @score )` err := db.Db().Raw( - query, + insertQ, sql.Named("org_id", orgID), sql.Named("repo_id", repoID), sql.Named("user_id", userID), @@ -552,6 +555,10 @@ func (c *Coverage) Create( sql.Named("score", score)). Scan(&ret).Error + if err != nil { + return &ret, err + } + return &ret, err } func (c *Coverage) SoftDeleteCoverages(orgID int64, repoID int64, branches string) error { @@ -565,7 +572,7 @@ func (c *Coverage) SoftDeleteCoverages(orgID int64, repoID int64, branches strin AND repo_id = @repo_id AND - branch_name IN (@branches) + branch_name NOT IN (@branches) ` err := db.Db().Exec( query, diff --git a/app/models/org.go b/app/models/org.go index a38eef7..25f74da 100644 --- a/app/models/org.go +++ b/app/models/org.go @@ -18,7 +18,7 @@ func (Org) TableName() string { return "orgs" } -func (o *Org) Get(name string) (Org, error) { +func (o *Org) Get(name string) (*Org, error) { var ret Org query := `SELECT * FROM orgs WHERE name = @name LIMIT 1` @@ -29,17 +29,28 @@ func (o *Org) Get(name string) (Org, error) { ret.Name = strings.TrimSpace(ret.Name) - return ret, err + return &ret, err } -func (o *Org) Create(name string) (Org, error) { +func (o *Org) Create(name string) (*Org, error) { var ret Org + name = strings.TrimSpace(name) - query := `INSERT INTO orgs (name) VALUES (@name)` + insertQ := `INSERT INTO orgs (name) VALUES (@name)` err := db.Db().Raw( - query, + insertQ, + sql.Named("name", name)). + Scan(&ret).Error + + if err != nil { + return &ret, err + } + + selectQ := `SELECT * FROM orgs WHERE name = @name LIMIT 1` + err = db.Db().Raw( + selectQ, sql.Named("name", name)). Scan(&ret).Error - return ret, err + return &ret, err } diff --git a/app/models/repo.go b/app/models/repo.go index 5eb74f8..4b16257 100644 --- a/app/models/repo.go +++ b/app/models/repo.go @@ -18,8 +18,9 @@ type Repo struct { func (Repo) TableName() string { return "repos" } -func (r *Repo) Get(orgID int64, name string) (Repo, error) { +func (r *Repo) Get(orgID int64, name string) (*Repo, error) { var ret Repo + name = strings.TrimSpace(name) query := `SELECT * FROM repos WHERE org_id = @org_id AND name = @name LIMIT 1` err := db.Db().Raw( @@ -28,20 +29,29 @@ func (r *Repo) Get(orgID int64, name string) (Repo, error) { sql.Named("name", name)). Scan(&ret).Error - ret.Name = strings.TrimSpace(ret.Name) - - return ret, err + return &ret, err } -func (r *Repo) Create(orgID int64, name string) (Repo, error) { +func (r *Repo) Create(orgID int64, name string) (*Repo, error) { var ret Repo + name = strings.TrimSpace(name) - query := `INSERT INTO repos (org_id, name) VALUES (@org_id, @name)` + insertQ := `INSERT INTO repos (org_id, name) VALUES (@org_id, @name)` err := db.Db().Raw( - query, + insertQ, sql.Named("org_id", orgID), sql.Named("name", name)). Scan(&ret).Error - return ret, err + if err != nil { + return &ret, err + } + selectQ := `SELECT * FROM repos WHERE org_id = @orgID AND name = @name LIMIT 1` + err = db.Db().Raw( + selectQ, + sql.Named("orgID", orgID), + sql.Named("name", name)). + Scan(&ret).Error + + return &ret, err } diff --git a/app/models/type.go b/app/models/type.go index 2976fce..3264461 100644 --- a/app/models/type.go +++ b/app/models/type.go @@ -22,8 +22,9 @@ const ( TYPE_NUMBER_OF_CONTRIBUTORS = "number-of-contributors" ) -func (t *Type) Get(name string) (Type, error) { +func (t *Type) Get(name string) (*Type, error) { var ret Type + name = strings.TrimSpace(name) query := `SELECT * FROM types WHERE name = @name LIMIT 1` err := db.Db().Raw( @@ -34,19 +35,30 @@ func (t *Type) Get(name string) (Type, error) { ret.Metric = strings.TrimSpace(ret.Metric) ret.Name = strings.TrimSpace(ret.Name) - return ret, err + return &ret, err } -func (t *Type) Create(name string, metric string) (Type, error) { +func (t *Type) Create(name string, metric string) (*Type, error) { var ret Type - query := `INSERT INTO types (name, metric) VALUES (@name, @metric)` + name = strings.TrimSpace(name) + metric = strings.TrimSpace(metric) + insertQ := `INSERT INTO types (name, metric) VALUES (@name, @metric)` err := db.Db().Raw( - query, + insertQ, sql.Named("name", name), sql.Named("metric", metric)). Scan(&ret).Error + if err != nil { + return &ret, err + } + + selectQ := `SELECT * FROM types WHERE name = @name LIMIT 1` + err = db.Db().Raw( + selectQ, + sql.Named("name", name)). + Scan(&ret).Error - return ret, err + return &ret, err } func (t *Type) GetTypesFor(orgName string, repoName string) ([]Type, error) { diff --git a/app/models/user.go b/app/models/user.go index 08c2eee..11d821c 100644 --- a/app/models/user.go +++ b/app/models/user.go @@ -22,7 +22,7 @@ func (User) TableName() string { return "users" } -func (u *User) Get(name string) (User, error) { +func (u *User) Get(name string) (*User, error) { var ret User query := `SELECT * FROM users WHERE name = @name LIMIT 1` @@ -31,19 +31,28 @@ func (u *User) Get(name string) (User, error) { sql.Named("name", name)). Scan(&ret).Error - ret.Name = strings.TrimSpace(ret.Name) - - return ret, err + return &ret, err } -func (u *User) Create(name string) (User, error) { +func (u *User) Create(name string) (*User, error) { var ret User + name = strings.TrimSpace(name) - query := `INSERT INTO users (name) VALUES (@name)` + insertQ := `INSERT INTO users (name) VALUES (@name)` err := db.Db().Raw( - query, + insertQ, + sql.Named("name", name)). + Scan(&ret).Error + + if err != nil { + return &ret, err + } + + selectQ := `SELECT * FROM users WHERE name = @name LIMIT 1` + err = db.Db().Raw( + selectQ, sql.Named("name", name)). Scan(&ret).Error - return ret, err + return &ret, err } diff --git a/app/pkg/badge.go b/app/pkg/badge.go index 01e0627..1f50fcc 100644 --- a/app/pkg/badge.go +++ b/app/pkg/badge.go @@ -18,7 +18,7 @@ func NewBadge() *Badge { return &Badge{} } -func (b *Badge) Get(req *BadgeRequest, t models.Type) ([]byte, error) { +func (b *Badge) Get(req *BadgeRequest, t *models.Type) ([]byte, error) { if t.Name == models.TYPE_AVERAGE_PR_DAYS { num, err := b.coverageModel.GetAveragePRDays(req.Org, req.Repo) @@ -61,9 +61,9 @@ func (b *Badge) Get404(req *BadgeRequest) ([]byte, error) { return badge.RenderBytes(req.Branch+"|"+req.Type, "404", "#fff", "white", "red") } -func (b *Badge) GetType(name string) (models.Type, error) { +func (b *Badge) GetType(name string) (*models.Type, error) { if (name == models.TYPE_AVERAGE_PR_DAYS) || (name == models.TYPE_NUMBER_OF_CONTRIBUTORS) { - return models.Type{ + return &models.Type{ Name: name, }, nil } diff --git a/app/pkg/badge_handler.go b/app/pkg/badge_handler.go index 7483075..a86091c 100644 --- a/app/pkg/badge_handler.go +++ b/app/pkg/badge_handler.go @@ -22,8 +22,6 @@ type BadgeRequest struct { Repo string `json:"repo" query:"repo" validate:"required,ascii,excludes=/" message:"repo is required"` Branch string `json:"branch" query:"branch" validate:"required,ascii,excludes=/" message:"ascii branch is required"` Type string `json:"type" query:"type" validate:"ascii,required,excludes=/" message:"ascii type is required"` - Style string `json:"style" query:"style" default:"badge" validate:"oneof=badge chart table" message:"style must be badge or chart"` - Color string `json:"color" query:"color" default:"blue" validate:"oneof=blue red green orange" message:"color must be blue, red, green or orange"` } func (h *BadgeHandler) Get(c echo.Context) error { diff --git a/app/pkg/badge_handler_test.go b/app/pkg/badge_handler_test.go index cc3ed46..7ce185d 100644 --- a/app/pkg/badge_handler_test.go +++ b/app/pkg/badge_handler_test.go @@ -10,12 +10,78 @@ import ( "github.com/stretchr/testify/assert" ) -func TestGetBadge(t *testing.T) { - e := echo.New() +func TestGetBadgeErrors(t *testing.T) { BeforeEach() defer AfterEach() + e := echo.New() + e.GET("/badge", func(c echo.Context) error { + return NewBadgeHandler().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.StatusOK, + ContentType: "image/svg+xml", + ResponseContains: `404`, + }, + { + 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&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 + "/badge" + 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 TestGetBadgeOK(t *testing.T) { + + BeforeEach() + defer AfterEach() + e := echo.New() e.GET("/badge", func(c echo.Context) error { return NewBadgeHandler().Get(c) }) @@ -24,28 +90,59 @@ func TestGetBadge(t *testing.T) { defer server.Close() type TestCase struct { - Name string - Query string - ExpectedStatus int - ExpectedResponseContains string + Name string + PreUploadRequest *UploadRequest + Query string + Status int + ContentType string + ResponseContains string } testCases := []TestCase{ { - Name: "404", - Query: `?org=org&repo=repo&branch=branch&type=type`, - ExpectedStatus: http.StatusOK, - ExpectedResponseContains: `404`, + Name: "200", + PreUploadRequest: &UploadRequest{ + Org: "org", + Repo: "repo", + Type: "type", + Branch: "branch", + Commit: "commit", + }, + Query: `?org=org&repo=repo&branch=branch&type=type`, + Status: http.StatusOK, + ContentType: "image/svg+xml", + ResponseContains: `0`, + }, + { + 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/svg+xml", + ResponseContains: `2.0k`, }, } for _, tc := range testCases { + c, err := NewUpload().Post(tc.PreUploadRequest) + assert.NotNil(t, c) + assert.NoError(t, err) url := server.URL + "/badge" + tc.Query resp, err := http.Get(url) - data, _ := io.ReadAll(resp.Body) assert.NoError(t, err) - assert.Contains(t, string(data), tc.ExpectedResponseContains) - assert.Equal(t, tc.ExpectedStatus, resp.StatusCode) - assert.Equal(t, "image/svg+xml", resp.Header.Get("Content-Type")) + 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) + } } } diff --git a/app/pkg/chart.go b/app/pkg/chart.go index 650285a..ca84cb1 100644 --- a/app/pkg/chart.go +++ b/app/pkg/chart.go @@ -25,11 +25,11 @@ func NewChart() *Chart { } } -func (e *Chart) GetType(name string) (models.Type, error) { +func (e *Chart) GetType(name string) (*models.Type, error) { return e.typeModel.Get(name) } -func (e *Chart) GetInstaChartForBranch(req *ChartRequest, t models.Type) ([]byte, error) { +func (e *Chart) GetInstaChartForBranch(req *ChartRequest, t *models.Type) ([]byte, error) { cReq := e.makeChartRequest(req, t) line := instachart.NewLineChart() xData := []string{} @@ -51,7 +51,7 @@ func (e *Chart) GetInstaChartForBranch(req *ChartRequest, t models.Type) ([]byte return line.Get(xData, yyData, names, cReq) } -func (e *Chart) GetInstaChartForUser(req *ChartRequest, t models.Type) ([]byte, error) { +func (e *Chart) GetInstaChartForUser(req *ChartRequest, t *models.Type) ([]byte, error) { cReq := e.makeChartRequest(req, t) line := instachart.NewLineChart() xData := []string{} @@ -73,7 +73,7 @@ func (e *Chart) GetInstaChartForUser(req *ChartRequest, t models.Type) ([]byte, return line.Get(xData, yyData, names, cReq) } -func (e *Chart) GetInstaChartForBranches(req *ChartRequest, t models.Type) ([]byte, error) { +func (e *Chart) GetInstaChartForBranches(req *ChartRequest, t *models.Type) ([]byte, error) { cReq := e.makeChartRequest(req, t) bar := instachart.NewBarChart() @@ -109,7 +109,7 @@ func (e *Chart) GetInstaChartForBranches(req *ChartRequest, t models.Type) ([]by return bar.GetStacked(xData, yyData, zzData, names, cReq) } -func (e *Chart) GetInstaChartForUsers(req *ChartRequest, t models.Type) ([]byte, error) { +func (e *Chart) GetInstaChartForUsers(req *ChartRequest, t *models.Type) ([]byte, error) { cReq := e.makeChartRequest(req, t) bar := instachart.NewBarChart() @@ -146,7 +146,7 @@ func (e *Chart) GetInstaChartForUsers(req *ChartRequest, t models.Type) ([]byte, return bar.GetStacked(xData, yyData, zzData, names, cReq) } -func (e *Chart) makeChartRequest(req *ChartRequest, t models.Type) *instachart.ChartRequest { +func (e *Chart) makeChartRequest(req *ChartRequest, t *models.Type) *instachart.ChartRequest { cReq := &instachart.ChartRequest{ Output: req.Output, Metric: t.Metric, diff --git a/app/pkg/destroy_handler.go b/app/pkg/destroy_handler.go index 52dbd83..e01ccd8 100644 --- a/app/pkg/destroy_handler.go +++ b/app/pkg/destroy_handler.go @@ -2,6 +2,7 @@ package pkg import ( "net/http" + "os" "github.com/labstack/echo/v4" "github.com/mcuadros/go-defaults" @@ -38,9 +39,11 @@ func (h *DestroyHandler) Post(c echo.Context) error { return echo.NewHTTPError(http.StatusUnprocessableEntity, msgs) } - // if err := h.github.VerifyGithubToken(c.Request().Header.Get("Authorization"), req.Org, req.Repo, req.Type); err != nil { - // return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) - // } + if os.Getenv("GITHUB_API") != "" { + if err := h.github.VerifyGithubToken(c.Request().Header.Get("Authorization"), req.Org, req.Repo, req.Type); err != nil { + return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) + } + } err = h.Destroy.Delete(*req) if err != nil { diff --git a/app/pkg/setup_test.go b/app/pkg/setup_test.go index a301283..8805854 100644 --- a/app/pkg/setup_test.go +++ b/app/pkg/setup_test.go @@ -13,13 +13,13 @@ var embedMigrations embed.FS func BeforeEach() { os.Setenv("DATABASE_DSN", "root:@tcp(127.0.0.1:3306)/") os.Setenv("DATABASE_NAME", "coverituptest") - db.Migrate("create", embedMigrations) + db.Migrate("create", embedMigrations, "migrations") db.SetupDatabase() - db.Migrate("up", embedMigrations) + db.Migrate("up", embedMigrations, "migrations") } func AfterEach() { os.Setenv("DATABASE_DSN", "root:@tcp(127.0.0.1:3306)/") os.Setenv("DATABASE_NAME", "coverituptest") - db.Migrate("drop", embedMigrations) + db.Migrate("drop", embedMigrations, "migrations") } diff --git a/app/pkg/upload_handler.go b/app/pkg/upload_handler.go index d871955..53af4b6 100644 --- a/app/pkg/upload_handler.go +++ b/app/pkg/upload_handler.go @@ -2,6 +2,7 @@ package pkg import ( "net/http" + "os" "github.com/labstack/echo/v4" "github.com/mcuadros/go-defaults" @@ -9,11 +10,13 @@ import ( type UploadHandler struct { upload *Upload + github *Github } func NewUploadHandler() *UploadHandler { return &UploadHandler{ upload: NewUpload(), + github: NewGithub(), } } @@ -22,12 +25,12 @@ type UploadRequest struct { Repo string `json:"repo" query:"repo" validate:"required,ascii,excludes=/,max=255" message:"repo is required"` User string `json:"user" query:"user" validate:"required,ascii,excludes=/,max=255" message:"user is required"` Type string `json:"type" query:"type" validate:"required,ascii,required,excludes=/,max=32" message:"ascii type is required"` - Metric string `json:"metric" query:"metric" validate:"ascii,max=3" message:"ascii metric is required"` Branch string `json:"branch" query:"branch" validate:"required,ascii,max=255" message:"ascii branch is required"` - Commit string `json:"commit" query:"commit" validate:"ascii,excludes=/,max=255" message:"ascii commit is required"` - Score string `json:"score" query:"score" validate:"ascii,excludes=/,max=12" message:"ascii score is required"` + Commit string `json:"commit" query:"commit" validate:"required,ascii,max=255" message:"ascii commit is required"` + Score string `json:"score" query:"score" validate:"ascii,max=12" message:"ascii score is required"` + Metric string `json:"metric" query:"metric" validate:"ascii,max=3" message:"ascii metric is required"` Branches string `json:"branches" query:"branches" validate:"ascii" message:"ascii branches is required"` - PRNum string `json:"pr_num" query:"pr_num" validate:"ascii,excludes=/,max=4" message:"ascii pr_num is required"` + PRNum string `json:"pr_num" query:"pr_num" validate:"ascii,max=4" message:"ascii pr_num is required"` } func (h *UploadHandler) Post(c echo.Context) error { @@ -42,9 +45,12 @@ func (h *UploadHandler) Post(c echo.Context) error { return echo.NewHTTPError(http.StatusUnprocessableEntity, msgs) } - // if err := h.github.VerifyGithubToken(c.Request().Header.Get("Authorization"), req.Org, req.Repo, req.Type); err != nil { - // return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) - // } + // set by default to api.github and "" on tests + if os.Getenv("GITHUB_API") != "" { + if err := h.github.VerifyGithubToken(c.Request().Header.Get("Authorization"), req.Org, req.Repo, req.Type); err != nil { + return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) + } + } res, err := h.upload.Post(req) if err != nil { diff --git a/app/pkg/upload_handler_test.go b/app/pkg/upload_handler_test.go new file mode 100644 index 0000000..be5fde9 --- /dev/null +++ b/app/pkg/upload_handler_test.go @@ -0,0 +1,89 @@ +package pkg + +import ( + "bytes" + "net/http" + "net/http/httptest" + "testing" + + "github.com/labstack/echo/v4" + "github.com/stretchr/testify/assert" +) + +func TestPostUploadErrors(t *testing.T) { + + BeforeEach() + defer AfterEach() + e := echo.New() + e.POST("/upload", func(c echo.Context) error { + return NewUploadHandler().Post(c) + }) + + server := httptest.NewServer(e) + defer server.Close() + + type TestCase struct { + Name string + Body []byte + Status int + } + testCases := []TestCase{ + { + Name: "422", + Body: []byte(`{"repo":"repo","branch":"branch","type":"type"}`), + Status: http.StatusUnprocessableEntity, + }, + { + Name: "422", + Body: []byte(`{"org":"org", "repo":"repo","branch":"branch","type":"type"}`), + Status: http.StatusUnprocessableEntity, + }, + { + Name: "422", + Body: []byte(`{"org":"org","repo":"repo","user": "user", "branch":"branch","type":"type"}`), + Status: http.StatusUnprocessableEntity, + }, + } + + for _, tc := range testCases { + url := server.URL + "/upload" + resp, err := http.Post(url, "application/json", bytes.NewBuffer(tc.Body)) + + assert.NoError(t, err) + assert.Equal(t, tc.Status, resp.StatusCode) + } +} + +func TestPostUploadOK(t *testing.T) { + + BeforeEach() + defer AfterEach() + e := echo.New() + e.POST("/upload", func(c echo.Context) error { + return NewUploadHandler().Post(c) + }) + + server := httptest.NewServer(e) + defer server.Close() + + type TestCase struct { + Name string + Body []byte + Status int + } + testCases := []TestCase{ + { + Name: "200", + Body: []byte(`{"org":"org","repo":"repo","user": "user", "branch":"branch","type":"type","commit":"commit"}`), + Status: http.StatusOK, + }, + } + + for _, tc := range testCases { + url := server.URL + "/upload" + resp, err := http.Post(url, "application/json", bytes.NewBuffer(tc.Body)) + + assert.NoError(t, err) + assert.Equal(t, tc.Status, resp.StatusCode) + } +}