From 9f0238028df57dcbbe249850fbf451ae43608a30 Mon Sep 17 00:00:00 2001 From: FantasyRL <1845983502@qq.com> Date: Wed, 18 Dec 2024 14:33:35 +0800 Subject: [PATCH 1/9] fix: api.common and api.version logic error --- api/handler/api/common_service.go | 7 +++--- api/handler/api/version_service.go | 24 +++++++-------------- api/handler/custom/launch_screen_service.go | 3 --- api/handler/custom/url_service.go | 6 ------ 4 files changed, 11 insertions(+), 29 deletions(-) diff --git a/api/handler/api/common_service.go b/api/handler/api/common_service.go index 83df466f..8c3082bb 100644 --- a/api/handler/api/common_service.go +++ b/api/handler/api/common_service.go @@ -29,7 +29,6 @@ import ( "github.com/west2-online/fzuhelper-server/api/rpc" "github.com/west2-online/fzuhelper-server/kitex_gen/common" "github.com/west2-online/fzuhelper-server/pkg/errno" - "github.com/west2-online/fzuhelper-server/pkg/logger" ) // GetCSS . @@ -39,7 +38,6 @@ func GetCSS(ctx context.Context, c *app.RequestContext) { var req api.GetCSSRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetCSS: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -47,6 +45,7 @@ func GetCSS(ctx context.Context, c *app.RequestContext) { css, err := rpc.GetCSSRPC(ctx, &common.GetCSSRequest{}) if err != nil { pack.RespError(c, err) + return } c.Data(consts.StatusOK, "text/css", *css) } @@ -58,7 +57,6 @@ func GetHtml(ctx context.Context, c *app.RequestContext) { var req api.GetHtmlRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetHtml: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -66,6 +64,7 @@ func GetHtml(ctx context.Context, c *app.RequestContext) { html, err := rpc.GetHtmlRPC(ctx, &common.GetHtmlRequest{}) if err != nil { pack.RespError(c, err) + return } c.Data(consts.StatusOK, "text/html", *html) } @@ -77,7 +76,6 @@ func GetUserAgreement(ctx context.Context, c *app.RequestContext) { var req api.GetUserAgreementRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetUserAgreement: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -85,6 +83,7 @@ func GetUserAgreement(ctx context.Context, c *app.RequestContext) { userAgreement, err := rpc.GetUserAgreementRPC(ctx, &common.GetUserAgreementRequest{}) if err != nil { pack.RespError(c, err) + return } c.Data(consts.StatusOK, "text/html", *userAgreement) } diff --git a/api/handler/api/version_service.go b/api/handler/api/version_service.go index 6d88f528..e1f51c38 100644 --- a/api/handler/api/version_service.go +++ b/api/handler/api/version_service.go @@ -21,14 +21,12 @@ package api import ( "context" + "github.com/cloudwego/hertz/pkg/app" + "github.com/cloudwego/hertz/pkg/protocol/consts" "github.com/west2-online/fzuhelper-server/api/pack" "github.com/west2-online/fzuhelper-server/api/rpc" "github.com/west2-online/fzuhelper-server/kitex_gen/version" "github.com/west2-online/fzuhelper-server/pkg/errno" - "github.com/west2-online/fzuhelper-server/pkg/logger" - - "github.com/cloudwego/hertz/pkg/app" - "github.com/cloudwego/hertz/pkg/protocol/consts" api "github.com/west2-online/fzuhelper-server/api/model/api" ) @@ -40,7 +38,6 @@ func Login(ctx context.Context, c *app.RequestContext) { var req api.LoginRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.Login: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -61,7 +58,6 @@ func UploadVersion(ctx context.Context, c *app.RequestContext) { var req api.UploadRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.UploadVersion: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -91,7 +87,6 @@ func UploadParams(ctx context.Context, c *app.RequestContext) { var req api.UploadParamsRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.UploadParams: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -115,7 +110,6 @@ func DownloadReleaseApk(ctx context.Context, c *app.RequestContext) { var req api.DownloadReleaseApkRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.DownloadReleaseApk: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -123,6 +117,7 @@ func DownloadReleaseApk(ctx context.Context, c *app.RequestContext) { reUrl, err := rpc.DownloadReleaseApkRPC(ctx, &version.DownloadReleaseApkRequest{}) if err != nil { pack.RespError(c, err) + return } c.Redirect(consts.StatusFound, []byte(*reUrl)) @@ -135,7 +130,6 @@ func DownloadBetaApk(ctx context.Context, c *app.RequestContext) { var req api.DownloadBetaApkRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.DownloadBetaApk: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -143,6 +137,7 @@ func DownloadBetaApk(ctx context.Context, c *app.RequestContext) { reUrl, err := rpc.DownloadBetaApkRPC(ctx, &version.DownloadBetaApkRequest{}) if err != nil { pack.RespError(c, err) + return } c.Redirect(consts.StatusFound, []byte(*reUrl)) @@ -155,7 +150,6 @@ func GetReleaseVersion(ctx context.Context, c *app.RequestContext) { var req api.GetReleaseVersionRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetReleaseVersion: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -182,7 +176,6 @@ func GetBetaVersion(ctx context.Context, c *app.RequestContext) { var req api.GetBetaVersionRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetBetaVersion: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -209,7 +202,6 @@ func GetSetting(ctx context.Context, c *app.RequestContext) { var req api.GetSettingRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetSetting: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -225,6 +217,7 @@ func GetSetting(ctx context.Context, c *app.RequestContext) { }) if err != nil { pack.RespError(c, err) + return } resp.CloudSetting = rpcResp.CloudSetting @@ -238,7 +231,6 @@ func GetTest(ctx context.Context, c *app.RequestContext) { var req api.GetTestRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetTest: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -255,6 +247,7 @@ func GetTest(ctx context.Context, c *app.RequestContext) { }) if err != nil { pack.RespError(c, err) + return } resp.CloudSetting = rpcResp.CloudSetting @@ -268,7 +261,6 @@ func GetCloud(ctx context.Context, c *app.RequestContext) { var req api.GetCloudRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetCloud: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -277,6 +269,7 @@ func GetCloud(ctx context.Context, c *app.RequestContext) { rpcResp, err := rpc.GetCloudRPC(ctx, &version.GetCloudRequest{}) if err != nil { pack.RespError(c, err) + return } resp.CloudSetting = rpcResp.CloudSetting @@ -291,7 +284,6 @@ func SetCloud(ctx context.Context, c *app.RequestContext) { var req api.SetCloudRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.SetCloud: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -314,7 +306,6 @@ func GetDump(ctx context.Context, c *app.RequestContext) { var req api.GetDumpRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetDump: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -324,6 +315,7 @@ func GetDump(ctx context.Context, c *app.RequestContext) { rpcResp, err := rpc.GetDumpRPC(ctx, &version.GetDumpRequest{}) if err != nil { pack.RespError(c, err) + return } resp.Data = rpcResp.Data diff --git a/api/handler/custom/launch_screen_service.go b/api/handler/custom/launch_screen_service.go index 483d3d86..55d1ed52 100644 --- a/api/handler/custom/launch_screen_service.go +++ b/api/handler/custom/launch_screen_service.go @@ -26,7 +26,6 @@ import ( "github.com/west2-online/fzuhelper-server/api/rpc" "github.com/west2-online/fzuhelper-server/kitex_gen/launch_screen" "github.com/west2-online/fzuhelper-server/pkg/errno" - "github.com/west2-online/fzuhelper-server/pkg/logger" ) // MobileGetImage . @@ -36,7 +35,6 @@ func MobileGetImage(ctx context.Context, c *app.RequestContext) { var req api.MobileGetImageRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.MobileGetImage: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -65,7 +63,6 @@ func AddImagePointTime(ctx context.Context, c *app.RequestContext) { var req api.AddImagePointTimeRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.AddImagePointTime: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } diff --git a/api/handler/custom/url_service.go b/api/handler/custom/url_service.go index f637c361..c0accb43 100644 --- a/api/handler/custom/url_service.go +++ b/api/handler/custom/url_service.go @@ -32,7 +32,6 @@ import ( "github.com/west2-online/fzuhelper-server/api/rpc" "github.com/west2-online/fzuhelper-server/pkg/base" "github.com/west2-online/fzuhelper-server/pkg/errno" - "github.com/west2-online/fzuhelper-server/pkg/logger" ) var ( @@ -49,7 +48,6 @@ func APILogin(ctx context.Context, c *app.RequestContext) { var req api.LoginRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.APILogin: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -74,7 +72,6 @@ func UploadVersionInfo(ctx context.Context, c *app.RequestContext) { var req api.UploadRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.UploadVersion: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -108,7 +105,6 @@ func GetUploadParams(ctx context.Context, c *app.RequestContext) { var req api.UploadParamsRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.UploadParams: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -135,7 +131,6 @@ func GetReleaseVersionModify(ctx context.Context, c *app.RequestContext) { var req api.GetReleaseVersionRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetReleaseVersionModify: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } @@ -161,7 +156,6 @@ func GetBetaVersionModify(ctx context.Context, c *app.RequestContext) { var req api.GetBetaVersionRequest err = c.BindAndValidate(&req) if err != nil { - logger.Errorf("api.GetBetaVersionModify: BindAndValidate error %v", err) pack.RespError(c, errno.ParamError.WithError(err)) return } From 28196d918274cb38bdb7cd83071462a67602acd0 Mon Sep 17 00:00:00 2001 From: FantasyRL <1845983502@qq.com> Date: Wed, 18 Dec 2024 15:09:58 +0800 Subject: [PATCH 2/9] fix: hard-coding error in tests --- api/handler/api/paper_service_test.go | 9 +++++---- api/handler/api/version_service.go | 4 ++-- api/handler/custom/url_service.go | 3 +-- internal/common/service/get_agreement_test.go | 6 ++++-- internal/common/service/get_css_test.go | 6 ++++-- internal/common/service/get_html_test.go | 6 ++++-- internal/launch_screen/service/create_image_test.go | 3 ++- internal/launch_screen/service/delete_image_test.go | 3 ++- internal/launch_screen/service/update_image_test.go | 3 ++- internal/version/service/get_setting_test.go | 3 ++- internal/version/service/test_get_setting_test.go | 3 ++- internal/version/service/upload_version_test.go | 3 ++- pkg/base/pack.go | 1 - 13 files changed, 32 insertions(+), 21 deletions(-) diff --git a/api/handler/api/paper_service_test.go b/api/handler/api/paper_service_test.go index aad2a915..5d654202 100644 --- a/api/handler/api/paper_service_test.go +++ b/api/handler/api/paper_service_test.go @@ -19,6 +19,7 @@ package api import ( "context" "errors" + "github.com/west2-online/fzuhelper-server/pkg/errno" "net/http" "strings" "testing" @@ -60,7 +61,7 @@ func TestGetDownloadUrl(t *testing.T) { { Name: "BindAndValidateError", ExpectedError: false, - ExpectedResult: `{"code":"20001","message":"parameter error, 'filepath' field is a 'required' parameter` + + ExpectedResult: `{"code":"20001","message":"` + errno.ParamError.ErrorMsg + `, 'filepath' field is a 'required' parameter` + `, but the request body does not have this parameter 'filepath'"}`, Url: "/api/v1/paper/download", }, @@ -165,7 +166,7 @@ func TestListDirFiles(t *testing.T) { { Name: "EmptyPath", ExpectedError: false, - ExpectedResult: `{"code":"20001","message":"parameter error, path is empty"}`, + ExpectedResult: `{"code":"20001","message":"` + errno.ParamError.ErrorMsg + `, path is empty"}`, ExpectUpYunResult: nil, Path: "", }, @@ -224,7 +225,7 @@ func TestGetDownloadUrlForAndroid(t *testing.T) { { Name: "BindAndValidateError", ExpectedError: false, - ExpectedResult: `{"code":20001,"data":null,"msg":"parameter error, 'filepath' field is a 'required' parameter,` + + ExpectedResult: `{"code":20001,"data":null,"msg":"` + errno.ParamError.ErrorMsg + `, 'filepath' field is a 'required' parameter,` + ` but the request body does not have this parameter 'filepath'"}`, Url: "/api/v1/paper/download", }, @@ -329,7 +330,7 @@ func TestListDirFilesForAndroid(t *testing.T) { { Name: "EmptyPath", ExpectedError: false, - ExpectedResult: `{"code":20001,"data":null,"msg":"parameter error, path is empty"}`, + ExpectedResult: `{"code":20001,"data":null,"msg":"` + errno.ParamError.ErrorMsg + `, path is empty"}`, ExpectUpYunResult: nil, Path: "", }, diff --git a/api/handler/api/version_service.go b/api/handler/api/version_service.go index e1f51c38..cedc9c81 100644 --- a/api/handler/api/version_service.go +++ b/api/handler/api/version_service.go @@ -23,12 +23,12 @@ import ( "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/protocol/consts" + + api "github.com/west2-online/fzuhelper-server/api/model/api" "github.com/west2-online/fzuhelper-server/api/pack" "github.com/west2-online/fzuhelper-server/api/rpc" "github.com/west2-online/fzuhelper-server/kitex_gen/version" "github.com/west2-online/fzuhelper-server/pkg/errno" - - api "github.com/west2-online/fzuhelper-server/api/model/api" ) // Login . diff --git a/api/handler/custom/url_service.go b/api/handler/custom/url_service.go index c0accb43..12d28dbf 100644 --- a/api/handler/custom/url_service.go +++ b/api/handler/custom/url_service.go @@ -22,14 +22,13 @@ import ( "context" "net/http" - "github.com/west2-online/fzuhelper-server/kitex_gen/version" - "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/protocol/consts" "github.com/west2-online/fzuhelper-server/api/model/api" "github.com/west2-online/fzuhelper-server/api/pack" "github.com/west2-online/fzuhelper-server/api/rpc" + "github.com/west2-online/fzuhelper-server/kitex_gen/version" "github.com/west2-online/fzuhelper-server/pkg/base" "github.com/west2-online/fzuhelper-server/pkg/errno" ) diff --git a/internal/common/service/get_agreement_test.go b/internal/common/service/get_agreement_test.go index d8ac369a..30ac0a1f 100644 --- a/internal/common/service/get_agreement_test.go +++ b/internal/common/service/get_agreement_test.go @@ -18,6 +18,8 @@ package service import ( "fmt" + "github.com/west2-online/fzuhelper-server/pkg/errno" + "strconv" "testing" "github.com/bytedance/mockey" @@ -48,9 +50,9 @@ func TestGetUserAgreement(t *testing.T) { { name: "FileNotFound", mockFileResult: nil, - mockFileError: fmt.Errorf("file not found"), + mockFileError: errno.UpcloudError, expectedResult: nil, - expectedError: fmt.Errorf("CommonService.GetUserAgreement error:file not found"), + expectedError: fmt.Errorf("CommonService.GetUserAgreement error:[" + strconv.Itoa(errno.BizFileUploadErrorCode) + "] " + errno.UpcloudError.ErrorMsg), }, } diff --git a/internal/common/service/get_css_test.go b/internal/common/service/get_css_test.go index 2a93718c..6ff127f1 100644 --- a/internal/common/service/get_css_test.go +++ b/internal/common/service/get_css_test.go @@ -18,6 +18,8 @@ package service import ( "fmt" + "github.com/west2-online/fzuhelper-server/pkg/errno" + "strconv" "testing" "github.com/bytedance/mockey" @@ -48,9 +50,9 @@ func TestGetCSS(t *testing.T) { { name: "FileNotFound", mockFileResult: nil, - mockFileError: fmt.Errorf("file not found"), + mockFileError: errno.UpcloudError, expectedResult: nil, - expectedError: fmt.Errorf("CommonService.GetCSS error:file not found"), + expectedError: fmt.Errorf("CommonService.GetCSS error:[" + strconv.Itoa(errno.BizFileUploadErrorCode) + "] " + errno.UpcloudError.ErrorMsg), }, } diff --git a/internal/common/service/get_html_test.go b/internal/common/service/get_html_test.go index c0eacfac..7c74af85 100644 --- a/internal/common/service/get_html_test.go +++ b/internal/common/service/get_html_test.go @@ -18,6 +18,8 @@ package service import ( "fmt" + "github.com/west2-online/fzuhelper-server/pkg/errno" + "strconv" "testing" "github.com/bytedance/mockey" @@ -48,9 +50,9 @@ func TestGetHtml(t *testing.T) { { name: "FileNotFound", mockFileResult: nil, - mockFileError: fmt.Errorf("file not found"), + mockFileError: errno.UpcloudError, expectedResult: nil, - expectedError: fmt.Errorf("CommonService.GetHtml error:file not found"), + expectedError: fmt.Errorf("CommonService.GetHtml error:[" + strconv.Itoa(errno.BizFileUploadErrorCode) + "] " + errno.UpcloudError.ErrorMsg), }, } diff --git a/internal/launch_screen/service/create_image_test.go b/internal/launch_screen/service/create_image_test.go index b296965e..b1077c75 100644 --- a/internal/launch_screen/service/create_image_test.go +++ b/internal/launch_screen/service/create_image_test.go @@ -18,6 +18,7 @@ package service import ( "context" + "strconv" "testing" "time" @@ -110,7 +111,7 @@ func TestLaunchScreenService_CreateImage(t *testing.T) { if tc.expectingError { assert.Nil(t, result) - assert.EqualError(t, err, "LaunchScreenService.CreateImage error:[40006] upload to upcloud error") + assert.EqualError(t, err, "LaunchScreenService.CreateImage error:["+strconv.Itoa(errno.BizFileUploadErrorCode)+"] "+errno.UpcloudError.ErrorMsg) } else { assert.NoError(t, err) assert.Equal(t, tc.expectedResult, result) diff --git a/internal/launch_screen/service/delete_image_test.go b/internal/launch_screen/service/delete_image_test.go index 49da07e0..4704096e 100644 --- a/internal/launch_screen/service/delete_image_test.go +++ b/internal/launch_screen/service/delete_image_test.go @@ -18,6 +18,7 @@ package service import ( "context" + "strconv" "testing" "time" @@ -94,7 +95,7 @@ func TestLaunchScreenService_DeleteImage(t *testing.T) { err := launchScreenService.DeleteImage(req.PictureId) if tc.expectingError { - assert.EqualError(t, err, "LaunchScreen.DeleteImage error: [40006] upload to upcloud error") + assert.EqualError(t, err, "LaunchScreen.DeleteImage error: ["+strconv.Itoa(errno.BizFileUploadErrorCode)+"] "+errno.UpcloudError.ErrorMsg) } else { assert.NoError(t, err) } diff --git a/internal/launch_screen/service/update_image_test.go b/internal/launch_screen/service/update_image_test.go index 46b1e95d..72e33082 100644 --- a/internal/launch_screen/service/update_image_test.go +++ b/internal/launch_screen/service/update_image_test.go @@ -18,6 +18,7 @@ package service import ( "context" + "strconv" "testing" "time" @@ -136,7 +137,7 @@ func TestLaunchScreenService_UpdateImagePath(t *testing.T) { if !tc.mockIsExist { assert.EqualError(t, err, "LaunchScreenService.UpdateImagePath db.GetImageById error: record not found") } else { - assert.EqualError(t, err, "LaunchScreenService.UpdateImagePath error: [40006] upload to upcloud error") + assert.EqualError(t, err, "LaunchScreenService.UpdateImagePath error: ["+strconv.Itoa(errno.BizFileUploadErrorCode)+"] "+errno.UpcloudError.ErrorMsg) } } else { assert.NoError(t, err) diff --git a/internal/version/service/get_setting_test.go b/internal/version/service/get_setting_test.go index bef8083d..dbd41ae7 100644 --- a/internal/version/service/get_setting_test.go +++ b/internal/version/service/get_setting_test.go @@ -19,6 +19,7 @@ package service import ( "encoding/json" "fmt" + "strconv" "testing" "github.com/bytedance/mockey" @@ -77,7 +78,7 @@ func TestGetCloudSetting(t *testing.T) { mockCriteria: &pack.Plan{Name: strPtr("Non-Matching Plan")}, mockPlanList: []pack.Plan{{Name: strPtr("Other Plan"), Plan: json.RawMessage(mockPlanResult)}}, expectedResult: nil, - expectedError: fmt.Errorf("VersionService.GetCloudSetting error:[40001] no matching plan"), + expectedError: fmt.Errorf("VersionService.GetCloudSetting error:[" + strconv.Itoa(int(errno.NoMatchingPlanError.ErrorCode)) + "] " + errno.NoMatchingPlanError.ErrorMsg), }, } diff --git a/internal/version/service/test_get_setting_test.go b/internal/version/service/test_get_setting_test.go index 6cda800b..cdd28e96 100644 --- a/internal/version/service/test_get_setting_test.go +++ b/internal/version/service/test_get_setting_test.go @@ -19,6 +19,7 @@ package service import ( "encoding/json" "fmt" + "strconv" "testing" "github.com/bytedance/mockey" @@ -67,7 +68,7 @@ func TestTestSetting(t *testing.T) { mockCriteria: &pack.Plan{Name: strPtr("Non-Matching Plan")}, mockPlanList: []pack.Plan{{Name: strPtr("Other Plan"), Plan: json.RawMessage(mockPlanResult)}}, expectedResult: nil, - expectedError: fmt.Errorf("VersionService.TestSetting error:[40001] no matching plan"), + expectedError: fmt.Errorf("VersionService.TestSetting error:[" + strconv.Itoa(int(errno.NoMatchingPlanError.ErrorCode)) + "] " + errno.NoMatchingPlanError.ErrorMsg), }, } diff --git a/internal/version/service/upload_version_test.go b/internal/version/service/upload_version_test.go index f89b55e2..213f3f0a 100644 --- a/internal/version/service/upload_version_test.go +++ b/internal/version/service/upload_version_test.go @@ -17,6 +17,7 @@ limitations under the License. package service import ( + "github.com/west2-online/fzuhelper-server/pkg/errno" "testing" "github.com/bytedance/mockey" @@ -100,7 +101,7 @@ func TestUploadVersion(t *testing.T) { Type: "invalidType", }, expectedError: true, - expectedErrorInfo: "parameter error", + expectedErrorInfo: errno.ParamError.ErrorMsg, }, } diff --git a/pkg/base/pack.go b/pkg/base/pack.go index 3d6868e6..83bea810 100644 --- a/pkg/base/pack.go +++ b/pkg/base/pack.go @@ -18,7 +18,6 @@ package base import ( "errors" - "github.com/west2-online/fzuhelper-server/kitex_gen/model" "github.com/west2-online/fzuhelper-server/pkg/errno" "github.com/west2-online/fzuhelper-server/pkg/logger" From a69cd1eda8baf09da16988823f5de9603c2aa598 Mon Sep 17 00:00:00 2001 From: FantasyRL <1845983502@qq.com> Date: Wed, 18 Dec 2024 15:19:41 +0800 Subject: [PATCH 3/9] fix: lint error --- api/handler/api/paper_service_test.go | 2 +- internal/common/service/get_agreement_test.go | 8 ++++++-- internal/common/service/get_css_test.go | 8 ++++++-- internal/common/service/get_html_test.go | 4 ++-- internal/version/service/get_setting_test.go | 6 +++++- internal/version/service/test_get_setting_test.go | 6 +++++- internal/version/service/upload_version_test.go | 2 +- pkg/base/pack.go | 1 + pkg/base/pack_test.go | 6 ------ 9 files changed, 27 insertions(+), 16 deletions(-) diff --git a/api/handler/api/paper_service_test.go b/api/handler/api/paper_service_test.go index 5d654202..856f50ac 100644 --- a/api/handler/api/paper_service_test.go +++ b/api/handler/api/paper_service_test.go @@ -19,7 +19,6 @@ package api import ( "context" "errors" - "github.com/west2-online/fzuhelper-server/pkg/errno" "net/http" "strings" "testing" @@ -35,6 +34,7 @@ import ( "github.com/west2-online/fzuhelper-server/api/rpc" "github.com/west2-online/fzuhelper-server/kitex_gen/model" "github.com/west2-online/fzuhelper-server/kitex_gen/paper" + "github.com/west2-online/fzuhelper-server/pkg/errno" ) func TestGetDownloadUrl(t *testing.T) { diff --git a/internal/common/service/get_agreement_test.go b/internal/common/service/get_agreement_test.go index 30ac0a1f..7d89c08b 100644 --- a/internal/common/service/get_agreement_test.go +++ b/internal/common/service/get_agreement_test.go @@ -18,13 +18,14 @@ package service import ( "fmt" - "github.com/west2-online/fzuhelper-server/pkg/errno" "strconv" + "strings" "testing" "github.com/bytedance/mockey" "github.com/stretchr/testify/assert" + "github.com/west2-online/fzuhelper-server/pkg/errno" "github.com/west2-online/fzuhelper-server/pkg/upyun" ) @@ -52,7 +53,10 @@ func TestGetUserAgreement(t *testing.T) { mockFileResult: nil, mockFileError: errno.UpcloudError, expectedResult: nil, - expectedError: fmt.Errorf("CommonService.GetUserAgreement error:[" + strconv.Itoa(errno.BizFileUploadErrorCode) + "] " + errno.UpcloudError.ErrorMsg), + expectedError: fmt.Errorf("%s", strings.Join([]string{ + "CommonService.GetUserAgreement error:[", + strconv.Itoa(errno.BizFileUploadErrorCode), "] ", errno.UpcloudError.ErrorMsg, + }, "")), }, } diff --git a/internal/common/service/get_css_test.go b/internal/common/service/get_css_test.go index 6ff127f1..c4ee1b82 100644 --- a/internal/common/service/get_css_test.go +++ b/internal/common/service/get_css_test.go @@ -18,13 +18,14 @@ package service import ( "fmt" - "github.com/west2-online/fzuhelper-server/pkg/errno" "strconv" + "strings" "testing" "github.com/bytedance/mockey" "github.com/stretchr/testify/assert" + "github.com/west2-online/fzuhelper-server/pkg/errno" "github.com/west2-online/fzuhelper-server/pkg/upyun" ) @@ -52,7 +53,10 @@ func TestGetCSS(t *testing.T) { mockFileResult: nil, mockFileError: errno.UpcloudError, expectedResult: nil, - expectedError: fmt.Errorf("CommonService.GetCSS error:[" + strconv.Itoa(errno.BizFileUploadErrorCode) + "] " + errno.UpcloudError.ErrorMsg), + expectedError: fmt.Errorf("%s", strings.Join([]string{ + "CommonService.GetCSS error:[", + strconv.Itoa(errno.BizFileUploadErrorCode), "] ", errno.UpcloudError.ErrorMsg, + }, "")), }, } diff --git a/internal/common/service/get_html_test.go b/internal/common/service/get_html_test.go index 7c74af85..60541c54 100644 --- a/internal/common/service/get_html_test.go +++ b/internal/common/service/get_html_test.go @@ -18,13 +18,13 @@ package service import ( "fmt" - "github.com/west2-online/fzuhelper-server/pkg/errno" "strconv" "testing" "github.com/bytedance/mockey" "github.com/stretchr/testify/assert" + "github.com/west2-online/fzuhelper-server/pkg/errno" "github.com/west2-online/fzuhelper-server/pkg/upyun" ) @@ -52,7 +52,7 @@ func TestGetHtml(t *testing.T) { mockFileResult: nil, mockFileError: errno.UpcloudError, expectedResult: nil, - expectedError: fmt.Errorf("CommonService.GetHtml error:[" + strconv.Itoa(errno.BizFileUploadErrorCode) + "] " + errno.UpcloudError.ErrorMsg), + expectedError: fmt.Errorf("%s", "CommonService.GetHtml error:["+strconv.Itoa(errno.BizFileUploadErrorCode)+"] "+errno.UpcloudError.ErrorMsg), }, } diff --git a/internal/version/service/get_setting_test.go b/internal/version/service/get_setting_test.go index dbd41ae7..6ceaab2e 100644 --- a/internal/version/service/get_setting_test.go +++ b/internal/version/service/get_setting_test.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "strconv" + "strings" "testing" "github.com/bytedance/mockey" @@ -78,7 +79,10 @@ func TestGetCloudSetting(t *testing.T) { mockCriteria: &pack.Plan{Name: strPtr("Non-Matching Plan")}, mockPlanList: []pack.Plan{{Name: strPtr("Other Plan"), Plan: json.RawMessage(mockPlanResult)}}, expectedResult: nil, - expectedError: fmt.Errorf("VersionService.GetCloudSetting error:[" + strconv.Itoa(int(errno.NoMatchingPlanError.ErrorCode)) + "] " + errno.NoMatchingPlanError.ErrorMsg), + expectedError: fmt.Errorf("%s", strings.Join([]string{ + "VersionService.GetCloudSetting error:[", + strconv.Itoa(int(errno.NoMatchingPlanError.ErrorCode)), "] ", errno.NoMatchingPlanError.ErrorMsg, + }, "")), }, } diff --git a/internal/version/service/test_get_setting_test.go b/internal/version/service/test_get_setting_test.go index cdd28e96..44681892 100644 --- a/internal/version/service/test_get_setting_test.go +++ b/internal/version/service/test_get_setting_test.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "strconv" + "strings" "testing" "github.com/bytedance/mockey" @@ -68,7 +69,10 @@ func TestTestSetting(t *testing.T) { mockCriteria: &pack.Plan{Name: strPtr("Non-Matching Plan")}, mockPlanList: []pack.Plan{{Name: strPtr("Other Plan"), Plan: json.RawMessage(mockPlanResult)}}, expectedResult: nil, - expectedError: fmt.Errorf("VersionService.TestSetting error:[" + strconv.Itoa(int(errno.NoMatchingPlanError.ErrorCode)) + "] " + errno.NoMatchingPlanError.ErrorMsg), + expectedError: fmt.Errorf("%s", strings.Join([]string{ + "VersionService.TestSetting error:[", + strconv.Itoa(int(errno.NoMatchingPlanError.ErrorCode)), "] ", errno.NoMatchingPlanError.ErrorMsg, + }, "")), }, } diff --git a/internal/version/service/upload_version_test.go b/internal/version/service/upload_version_test.go index 213f3f0a..6fb52886 100644 --- a/internal/version/service/upload_version_test.go +++ b/internal/version/service/upload_version_test.go @@ -17,13 +17,13 @@ limitations under the License. package service import ( - "github.com/west2-online/fzuhelper-server/pkg/errno" "testing" "github.com/bytedance/mockey" "github.com/stretchr/testify/assert" "github.com/west2-online/fzuhelper-server/kitex_gen/version" + "github.com/west2-online/fzuhelper-server/pkg/errno" "github.com/west2-online/fzuhelper-server/pkg/upyun" "github.com/west2-online/fzuhelper-server/pkg/utils" ) diff --git a/pkg/base/pack.go b/pkg/base/pack.go index 83bea810..3d6868e6 100644 --- a/pkg/base/pack.go +++ b/pkg/base/pack.go @@ -18,6 +18,7 @@ package base import ( "errors" + "github.com/west2-online/fzuhelper-server/kitex_gen/model" "github.com/west2-online/fzuhelper-server/pkg/errno" "github.com/west2-online/fzuhelper-server/pkg/logger" diff --git a/pkg/base/pack_test.go b/pkg/base/pack_test.go index 085298d0..5862098b 100644 --- a/pkg/base/pack_test.go +++ b/pkg/base/pack_test.go @@ -45,12 +45,6 @@ func TestBuildSuccessResp(t *testing.T) { assert.Equal(t, errno.Success.ErrorMsg, r.Msg) } -func TestLogError(t *testing.T) { - LogError(nil) - LogError(fmt.Errorf("ok")) - LogError(errno.Success) -} - func TestBuildRespAndLog(t *testing.T) { nilError := BuildBaseResp(nil) assert.Equal(t, int64(errno.SuccessCode), nilError.Code) From 856914ac146a3d182b9f6e8d2eaf5d9ef472d3af Mon Sep 17 00:00:00 2001 From: jiuxia211 <2064166368@qq.com> Date: Wed, 18 Dec 2024 20:14:12 +0800 Subject: [PATCH 4/9] fix: ci test --- internal/version/service/download_apk_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/version/service/download_apk_test.go b/internal/version/service/download_apk_test.go index 9f31cc69..fdd31de0 100644 --- a/internal/version/service/download_apk_test.go +++ b/internal/version/service/download_apk_test.go @@ -60,7 +60,7 @@ func TestDownloadBetaApk(t *testing.T) { mockError: nil, expectedUrl: "", expectingError: true, - expectedErrorInfo: `VersionService.DownloadBetaApk error:"Syntax error at index 1: invalid char\n\n\tinvalid json\n\t.^..........\n"`, + expectedErrorInfo: `VersionService.DownloadBetaApk error:"Syntax error at index 0: invalid chars\n\n\tinvalid json\n\t^...........\n"`, }, } @@ -128,7 +128,7 @@ func TestDownloadReleaseApk(t *testing.T) { mockError: nil, expectedUrl: "", expectingError: true, - expectedErrorInfo: `VersionService.DownloadReleaseApk error:"Syntax error at index 1: invalid char\n\n\tinvalid json\n\t.^..........\n"`, + expectedErrorInfo: `VersionService.DownloadReleaseApk error:"Syntax error at index 0: invalid chars\n\n\tinvalid json\n\t^...........\n"`, }, } From f36fc2d36c3c5ae4ef0446ee5037098ba38a713a Mon Sep 17 00:00:00 2001 From: jiuxia211 <2064166368@qq.com> Date: Wed, 18 Dec 2024 20:26:43 +0800 Subject: [PATCH 5/9] fix: ci test again --- internal/version/service/download_apk_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/version/service/download_apk_test.go b/internal/version/service/download_apk_test.go index fdd31de0..9f31cc69 100644 --- a/internal/version/service/download_apk_test.go +++ b/internal/version/service/download_apk_test.go @@ -60,7 +60,7 @@ func TestDownloadBetaApk(t *testing.T) { mockError: nil, expectedUrl: "", expectingError: true, - expectedErrorInfo: `VersionService.DownloadBetaApk error:"Syntax error at index 0: invalid chars\n\n\tinvalid json\n\t^...........\n"`, + expectedErrorInfo: `VersionService.DownloadBetaApk error:"Syntax error at index 1: invalid char\n\n\tinvalid json\n\t.^..........\n"`, }, } @@ -128,7 +128,7 @@ func TestDownloadReleaseApk(t *testing.T) { mockError: nil, expectedUrl: "", expectingError: true, - expectedErrorInfo: `VersionService.DownloadReleaseApk error:"Syntax error at index 0: invalid chars\n\n\tinvalid json\n\t^...........\n"`, + expectedErrorInfo: `VersionService.DownloadReleaseApk error:"Syntax error at index 1: invalid char\n\n\tinvalid json\n\t.^..........\n"`, }, } From 218e3e70a93880dea03b4966e9779dda94877e73 Mon Sep 17 00:00:00 2001 From: jiuxia211 <2064166368@qq.com> Date: Wed, 18 Dec 2024 20:36:42 +0800 Subject: [PATCH 6/9] fix: ci test again again --- internal/course/service/get_course_list_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/course/service/get_course_list_test.go b/internal/course/service/get_course_list_test.go index 9201b724..f85ee0b7 100644 --- a/internal/course/service/get_course_list_test.go +++ b/internal/course/service/get_course_list_test.go @@ -141,6 +141,7 @@ func TestCourseService_GetCourseList(t *testing.T) { LoginData: mockLoginData, Term: "202401", } + defer mockey.UnPatchAll() for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { mockey.Mock((*jwch.Student).GetTerms).Return(tc.mockTerms, tc.mockError).Build() @@ -150,7 +151,7 @@ func TestCourseService_GetCourseList(t *testing.T) { mockey.Mock((*jwch.Student).GetSemesterCourses).Return(tc.mockCourses, tc.mockError).Build() } mockey.Mock((*CourseService).putCourseListToDatabase).Return(tc.mockPutToDbError).Build() - defer mockey.UnPatchAll() + mockClientSet := new(base.ClientSet) mockClientSet.SFClient = new(utils.Snowflake) mockClientSet.DBClient = new(db.Database) From dfaf4a1f4c6a536ce6b593c4af6f3b1811dd8914 Mon Sep 17 00:00:00 2001 From: jiuxia211 <2064166368@qq.com> Date: Wed, 18 Dec 2024 20:51:06 +0800 Subject: [PATCH 7/9] fix: ci test again again again --- .../course/service/get_course_list_test.go | 85 ++++++++++--------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/internal/course/service/get_course_list_test.go b/internal/course/service/get_course_list_test.go index f85ee0b7..f572c5be 100644 --- a/internal/course/service/get_course_list_test.go +++ b/internal/course/service/get_course_list_test.go @@ -34,16 +34,6 @@ import ( ) func TestCourseService_GetCourseList(t *testing.T) { - type testCase struct { - name string - mockTerms *jwch.Term - mockCourses []*jwch.Course - mockError error - mockPutToDbError error - expectedResult []*jwch.Course - expectingError bool - expectedErrorMsg string - } mockTerm := &jwch.Term{ Terms: []string{"202401"}, @@ -94,42 +84,63 @@ func TestCourseService_GetCourseList(t *testing.T) { }, } + type testCase struct { + name string + mockTerms *jwch.Term + mockCourses []*jwch.Course + mockError error + mockPutToDbError error + expectedResult []*jwch.Course + expectingError bool + expectedErrorMsg string + mockTermsReturn *jwch.Term + mockTermsError error + mockCoursesReturn []*jwch.Course + mockCoursesError error + } + // Test cases testCases := []testCase{ { - name: "GetCourseListSuccess", - mockTerms: mockTerm, - mockCourses: mockCourses, - expectedResult: mockCourses, - expectingError: false, + name: "GetCourseListSuccess", + mockTerms: mockTerm, + mockCourses: mockCourses, + expectedResult: mockCourses, + expectingError: false, + mockTermsReturn: mockTerm, + mockCoursesReturn: mockCourses, }, { - name: "GetCourseListInvalidTerm", - mockTerms: mockTerm, - mockCourses: nil, - mockError: nil, - expectedResult: nil, - expectingError: true, - expectedErrorMsg: "Invalid term", + name: "GetCourseListInvalidTerm", + mockTerms: mockTerm, + mockCourses: nil, + expectedResult: nil, + expectingError: true, + expectedErrorMsg: "Invalid term", + mockTermsReturn: mockTerm, + mockCoursesReturn: nil, + mockCoursesError: fmt.Errorf("Invalid term"), }, { name: "GetCourseListGetTermsFailed", mockTerms: nil, mockCourses: nil, - mockError: fmt.Errorf("Get terms failed"), expectedResult: nil, expectingError: true, expectedErrorMsg: "Get terms failed", + mockTermsReturn: nil, + mockTermsError: fmt.Errorf("Get terms failed"), }, { - name: "GetCourseListGetCoursesFailed", - mockTerms: mockTerm, - mockCourses: nil, - mockError: nil, - mockPutToDbError: fmt.Errorf("put course list to db failed"), - expectedResult: nil, - expectingError: true, - expectedErrorMsg: "Get semester courses failed", + name: "GetCourseListGetCoursesFailed", + mockTerms: mockTerm, + mockCourses: nil, + expectedResult: nil, + expectingError: true, + expectedErrorMsg: "Get semester courses failed", + mockTermsReturn: mockTerm, + mockCoursesReturn: nil, + mockCoursesError: fmt.Errorf("Get semester courses failed"), }, } @@ -142,14 +153,11 @@ func TestCourseService_GetCourseList(t *testing.T) { Term: "202401", } defer mockey.UnPatchAll() + for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - mockey.Mock((*jwch.Student).GetTerms).Return(tc.mockTerms, tc.mockError).Build() - if tc.mockCourses == nil { - mockey.Mock((*jwch.Student).GetSemesterCourses).Return(nil, fmt.Errorf("Invalid term")).Build() - } else { - mockey.Mock((*jwch.Student).GetSemesterCourses).Return(tc.mockCourses, tc.mockError).Build() - } + mockey.PatchConvey(tc.name, t, func() { + mockey.Mock((*jwch.Student).GetTerms).Return(tc.mockTermsReturn, tc.mockTermsError).Build() + mockey.Mock((*jwch.Student).GetSemesterCourses).Return(tc.mockCoursesReturn, tc.mockCoursesError).Build() mockey.Mock((*CourseService).putCourseListToDatabase).Return(tc.mockPutToDbError).Build() mockClientSet := new(base.ClientSet) @@ -170,5 +178,6 @@ func TestCourseService_GetCourseList(t *testing.T) { assert.Equal(t, tc.expectedResult, result) } }) + } } From 21dfc0df9ab57ee3c485ee03257394c59159f202 Mon Sep 17 00:00:00 2001 From: jiuxia211 <2064166368@qq.com> Date: Wed, 18 Dec 2024 20:57:34 +0800 Subject: [PATCH 8/9] fix: lint --- internal/course/service/get_course_list_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/internal/course/service/get_course_list_test.go b/internal/course/service/get_course_list_test.go index f572c5be..225ee8fa 100644 --- a/internal/course/service/get_course_list_test.go +++ b/internal/course/service/get_course_list_test.go @@ -34,7 +34,6 @@ import ( ) func TestCourseService_GetCourseList(t *testing.T) { - mockTerm := &jwch.Term{ Terms: []string{"202401"}, ViewState: "viewstate123", @@ -88,7 +87,6 @@ func TestCourseService_GetCourseList(t *testing.T) { name string mockTerms *jwch.Term mockCourses []*jwch.Course - mockError error mockPutToDbError error expectedResult []*jwch.Course expectingError bool @@ -178,6 +176,5 @@ func TestCourseService_GetCourseList(t *testing.T) { assert.Equal(t, tc.expectedResult, result) } }) - } } From e569666c14cb40e11be8c13e38cfd75b52a32b45 Mon Sep 17 00:00:00 2001 From: FantasyRL <1845983502@qq.com> Date: Wed, 18 Dec 2024 21:16:28 +0800 Subject: [PATCH 9/9] fix: CI test again --- api/mw/token_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/mw/token_test.go b/api/mw/token_test.go index ca18c285..a2eb9ae8 100644 --- a/api/mw/token_test.go +++ b/api/mw/token_test.go @@ -28,6 +28,8 @@ import ( "github.com/west2-online/fzuhelper-server/pkg/constants" ) +const secretKeyExample = "MC4CAQAwBQYDK2VwBCIEIPAfnWESrQOyNXXHpMqx2xcgXXvPqvuSup4YLqIjMpI3" + // TestCreateExpiredToken 是一个特殊测试,旨在生成一个过期的 Token func TestCreateExpiredToken(t *testing.T) { // 默认生成时间是一年一个月七天前的 @@ -46,7 +48,7 @@ func TestCreateExpiredToken(t *testing.T) { } tokenStruct := jwt.NewWithClaims(jwt.SigningMethodEdDSA, claims) key, err := jwt.ParseEdPrivateKeyFromPEM([]byte(fmt.Sprintf("%v\n%v\n%v", "-----BEGIN PRIVATE KEY-----", - "此处需要修改为私钥", + secretKeyExample, "-----END PRIVATE KEY-----"))) if err != nil { t.Errorf("parse private key failed, err: %v", err)