Skip to content

Commit

Permalink
fix: unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
LordofAvernus committed Oct 26, 2023
1 parent 15b2853 commit ea3cc2e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 42 deletions.
4 changes: 2 additions & 2 deletions sqle/api/controller/v1/oauth2_ce.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// //go:build !enterprise
// // +build !enterprise
//go:build !enterprise
// +build !enterprise

package v1

Expand Down
4 changes: 2 additions & 2 deletions sqle/api/controller/v1/sync_instance_ce.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// //go:build !enterprise
// // +build !enterprise
//go:build !enterprise
// +build !enterprise

package v1

Expand Down
3 changes: 1 addition & 2 deletions sqle/api/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

dmsCommonJwt "github.com/actiontech/dms/pkg/dms-common/api/jwt"
"github.com/actiontech/sqle/sqle/api/controller"
"github.com/actiontech/sqle/sqle/dms"
"github.com/actiontech/sqle/sqle/model"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -66,7 +65,7 @@ func ScannerVerifier() echo.MiddlewareFunc {

projectUid, err := dms.GetPorjectUIDByName(context.TODO(), c.Param("project_name"))
if err != nil {
return controller.JSONBaseErrorReq(c, err)
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
apnInParam := c.Param("audit_plan_name")
if apnInToken != apnInParam {
Expand Down
55 changes: 45 additions & 10 deletions sqle/api/middleware/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,61 @@ package middleware

import (
"database/sql/driver"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"time"

sqlmock "github.com/DATA-DOG/go-sqlmock"
dmsCommon "github.com/actiontech/dms/pkg/dms-common/api/dms/v1"
dmsCommonJwt "github.com/actiontech/dms/pkg/dms-common/api/jwt"
"github.com/actiontech/sqle/sqle/api/controller"
"github.com/actiontech/sqle/sqle/model"
"github.com/jinzhu/gorm"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/stretchr/testify/assert"
)

func mockServer() *httptest.Server {
f := func(w http.ResponseWriter, r *http.Request) {
response := dmsCommon.ListProjectReply{
Data: []*dmsCommon.ListProject{{
ProjectUid: "700300",
Name: "default",
Archived: false,
}},
Total: 1,
}
res, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("reponse err ")

Check failure on line 37 in sqle/api/middleware/jwt_test.go

View workflow job for this annotation

GitHub Actions / lint

`reponse` is a misspelling of `response` (misspell)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")

_, err = w.Write(res)
if err != nil {
log.Printf("reponse err ")

Check failure on line 45 in sqle/api/middleware/jwt_test.go

View workflow job for this annotation

GitHub Actions / lint

`reponse` is a misspelling of `response` (misspell)
}
}
return httptest.NewServer(http.HandlerFunc(f))
}

func TestScannerVerifier(t *testing.T) {
server := mockServer()
defer server.Close()
controller.InitDMSServerAddress(server.URL)

e := echo.New()

apName := "test_audit_plan"
projectName := "test_project"
projectUID := "700300"
testUser := "test_user"

h := func(c echo.Context) error {
Expand All @@ -36,7 +71,7 @@ func TestScannerVerifier(t *testing.T) {
res := httptest.NewRecorder()
ctx := e.NewContext(req, res)
ctx.SetParamNames("audit_plan_name", "project_name")
ctx.SetParamValues(apName, projectName)
ctx.SetParamValues(apName, projectUID)
return ctx, res
}

Expand All @@ -63,8 +98,8 @@ func TestScannerVerifier(t *testing.T) {
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
model.InitMockStorage(mockDB)
mock.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.name = ? AND audit_plans.name = ?))").
WithArgs(projectName, apName).
mock.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_id = ? AND name = ?))").
WithArgs(projectUID, apName).
WillReturnRows(sqlmock.NewRows([]string{"name", "token"}).AddRow(driver.Value(testUser), "test-token"))
mock.ExpectClose()

Expand All @@ -84,8 +119,8 @@ func TestScannerVerifier(t *testing.T) {
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
model.InitMockStorage(mockDB)
mock.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.name = ? AND audit_plans.name = ?))").
WithArgs(projectName, apName).
mock.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_id = ? AND name = ?))").
WithArgs(projectUID, apName).
WillReturnError(gorm.ErrRecordNotFound)
assert.NoError(t, err)
model.InitMockStorage(mockDB)
Expand All @@ -107,8 +142,8 @@ func TestScannerVerifier(t *testing.T) {
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
model.InitMockStorage(mockDB)
mock.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.name = ? AND audit_plans.name = ?))").
WithArgs(projectName, apName).
mock.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_id = ? AND name = ?))").
WithArgs(projectUID, apName).
WillReturnRows(sqlmock.NewRows([]string{"name", "token"}).AddRow(testUser, token))
mock.ExpectClose()

Expand All @@ -129,8 +164,8 @@ func TestScannerVerifier(t *testing.T) {
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
model.InitMockStorage(mockDB)
mock.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.name = ? AND audit_plans.name = ?))").
WithArgs(projectName, apName).
mock.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_id = ? AND name = ?))").
WithArgs(projectUID, apName).
WillReturnRows(sqlmock.NewRows([]string{"name", "token"}).AddRow(testUser, token))
mock.ExpectClose()

Expand Down
4 changes: 2 additions & 2 deletions sqle/api/middleware/license_qa.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// //go:build !release
// // +build !release
//go:build !release
// +build !release

package middleware

Expand Down
47 changes: 23 additions & 24 deletions sqle/model/audit_plan_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import (
func TestStorage_GetAuditPlansByReq(t *testing.T) {
// 1. test for common user
tableAndRowOfSQL := `
FROM audit_plans
LEFT JOIN users ON audit_plans.create_user_id = users.id
LEFT JOIN projects ON audit_plans.project_id = projects.id
WHERE audit_plans.deleted_at IS NULL
AND users.deleted_at IS NULL
AND (
users.login_name = ?
)
FROM
audit_plans
WHERE
audit_plans.deleted_at IS NULL
AND ( audit_plans.create_user_id = ? )
AND audit_plans.db_type = ?
`
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
Expand Down Expand Up @@ -47,11 +44,10 @@ func TestStorage_GetAuditPlansByReq(t *testing.T) {

// 2. test for admin user
tableAndRowOfSQL1 := `
FROM audit_plans
LEFT JOIN users ON audit_plans.create_user_id = users.id
LEFT JOIN projects ON audit_plans.project_id = projects.id
WHERE audit_plans.deleted_at IS NULL
AND users.deleted_at IS NULL
FROM
audit_plans
WHERE
audit_plans.deleted_at IS NULL
`
mockDB, mock, err = sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
Expand Down Expand Up @@ -111,29 +107,32 @@ func TestStorage_GetAuditPlanSQLsByReq(t *testing.T) {

func TestStorage_GetAuditPlanReportsByReq(t *testing.T) {
tableAndRowOfSQL := `
FROM audit_plan_reports_v2 AS reports
JOIN audit_plans ON audit_plans.id = reports.audit_plan_id
JOIN projects ON projects.id = audit_plans.project_id
WHERE reports.deleted_at IS NULL
AND audit_plans.deleted_at IS NULL
AND audit_plans.name = ?
AND projects.name = ?
ORDER BY reports.created_at DESC , reports.id DESC
FROM
audit_plan_reports_v2 AS reports
JOIN audit_plans ON audit_plans.id = reports.audit_plan_id
WHERE
reports.deleted_at IS NULL
AND audit_plans.deleted_at IS NULL
AND audit_plans.name = ?
AND audit_plans.project_id = ?
ORDER BY
reports.created_at DESC ,
reports.id DESC
`
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
defer mockDB.Close()
InitMockStorage(mockDB)
mock.ExpectPrepare(fmt.Sprintf(`SELECT reports.id, reports.score , reports.pass_rate, reports.audit_level, reports.created_at %v LIMIT ? OFFSET ?`, tableAndRowOfSQL)).
ExpectQuery().WithArgs("audit_plan_for_jave_repo", "project_1", 100, 10).WillReturnRows(sqlmock.NewRows([]string{
ExpectQuery().WithArgs("audit_plan_for_jave_repo", "1", 100, 10).WillReturnRows(sqlmock.NewRows([]string{
"id", "score", "pass_rate", "audit_level", "created_at"}).
AddRow("1", 100, 1, "normal", "2021-09-01T13:46:13+08:00"))

mock.ExpectPrepare(fmt.Sprintf(`SELECT COUNT(*) %v`, tableAndRowOfSQL)).
ExpectQuery().WithArgs("audit_plan_for_jave_repo", "project_1").WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow("2"))
ExpectQuery().WithArgs("audit_plan_for_jave_repo", "1").WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow("2"))
nameFields := map[string]interface{}{
"audit_plan_name": "audit_plan_for_jave_repo",
"project_name": "project_1",
"project_id": "1",
"limit": 100,
"offset": 10}
result, count, err := GetStorage().GetAuditPlanReportsByReq(nameFields)
Expand Down

0 comments on commit ea3cc2e

Please sign in to comment.