From 9290372ba082942856be15bf368ce2f268819f38 Mon Sep 17 00:00:00 2001 From: donald1218 Date: Thu, 18 Jan 2024 08:06:56 +0000 Subject: [PATCH] add router_auth_check_test.go --- go.mod | 2 +- go.sum | 4 +- .../api_n1_n2_message_collection_document.go | 5 - .../api_am_policy_control_update_notify.go | 5 - internal/util/router_auth_check_test.go | 91 +++++++++++++++++++ 5 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 internal/util/router_auth_check_test.go diff --git a/go.mod b/go.mod index fcaf93a2..29d05e89 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/free5gc/aper v1.0.5-0.20230614030933-c73735898582 github.com/free5gc/nas v1.1.2-0.20230828074825-175b09665828 github.com/free5gc/ngap v1.0.7-0.20230614061954-9c128114ab1f - github.com/free5gc/openapi v1.0.7-0.20231216094313-e15a4ff046f6 + github.com/free5gc/openapi v1.0.7-0.20240117084712-52ad99299693 github.com/free5gc/sctp v0.0.0-20231121085449-400a702ea7f9 github.com/free5gc/util v1.0.5-0.20231001095115-433858e5be94 github.com/gin-contrib/cors v1.3.1 diff --git a/go.sum b/go.sum index 67a78a35..5606434f 100644 --- a/go.sum +++ b/go.sum @@ -70,8 +70,8 @@ github.com/free5gc/nas v1.1.2-0.20230828074825-175b09665828/go.mod h1:fjWwpyp7/w github.com/free5gc/ngap v1.0.7-0.20230614061954-9c128114ab1f h1:wgXjoknZ7JJoZ72J15g/f2/0DgdCpfcTg189lnhUPuY= github.com/free5gc/ngap v1.0.7-0.20230614061954-9c128114ab1f/go.mod h1:lKA1sLTYM3CGEBhZVxkGGJIkai5+Bvy2yHIMhb7Vx/k= github.com/free5gc/openapi v1.0.6/go.mod h1:iw/N0E+FlX44EEx24IBi2EdZW8v+bkj3ETWPGnlK9DI= -github.com/free5gc/openapi v1.0.7-0.20231216094313-e15a4ff046f6 h1:8P/wOkTAQMgZJe9pUUNSTE5PWeAdlMrsU9kLsI+VAVE= -github.com/free5gc/openapi v1.0.7-0.20231216094313-e15a4ff046f6/go.mod h1:qv9KqEucoZSeENPRFGxfTe+33ZWYyiYFx1Rj+H0DoWA= +github.com/free5gc/openapi v1.0.7-0.20240117084712-52ad99299693 h1:gFyYBsErQAkx4OVHXYqjO0efO9gPWydQavQcjU0CkHY= +github.com/free5gc/openapi v1.0.7-0.20240117084712-52ad99299693/go.mod h1:qv9KqEucoZSeENPRFGxfTe+33ZWYyiYFx1Rj+H0DoWA= github.com/free5gc/sctp v0.0.0-20231121085449-400a702ea7f9 h1:L02UI8oODfXgH1fGzWWuWF4zyze4IScEFm20q3PKZdE= github.com/free5gc/sctp v0.0.0-20231121085449-400a702ea7f9/go.mod h1:Nr81VlvMkBHZsCbWPXjosBh+SWLdeEyz8o0OrS110Ic= github.com/free5gc/util v1.0.5-0.20231001095115-433858e5be94 h1:tNylIqH/m5Kq+3KuC+jjXGl06Y6EmM8yq61ZUgNrPBY= diff --git a/internal/sbi/communication/api_n1_n2_message_collection_document.go b/internal/sbi/communication/api_n1_n2_message_collection_document.go index a76f427e..f0c0f152 100644 --- a/internal/sbi/communication/api_n1_n2_message_collection_document.go +++ b/internal/sbi/communication/api_n1_n2_message_collection_document.go @@ -25,11 +25,6 @@ import ( // N1N2MessageTransfer - Namf_Communication N1N2 Message Transfer (UE Specific) service Operation func HTTPN1N2MessageTransfer(c *gin.Context) { - auth_err := authorizationCheck(c) - if auth_err != nil { - c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()}) - return - } var n1n2MessageTransferRequest models.N1N2MessageTransferRequest n1n2MessageTransferRequest.JsonData = new(models.N1N2MessageTransferReqData) diff --git a/internal/sbi/httpcallback/api_am_policy_control_update_notify.go b/internal/sbi/httpcallback/api_am_policy_control_update_notify.go index abe6c3b1..9ac404ba 100644 --- a/internal/sbi/httpcallback/api_am_policy_control_update_notify.go +++ b/internal/sbi/httpcallback/api_am_policy_control_update_notify.go @@ -61,11 +61,6 @@ func HTTPAmPolicyControlUpdateNotifyUpdate(c *gin.Context) { } func HTTPAmPolicyControlUpdateNotifyTerminate(c *gin.Context) { - auth_err := authorizationCheck(c) - if auth_err != nil { - c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()}) - return - } var terminationNotification models.TerminationNotification requestBody, err := c.GetRawData() diff --git a/internal/util/router_auth_check_test.go b/internal/util/router_auth_check_test.go new file mode 100644 index 00000000..256d4d40 --- /dev/null +++ b/internal/util/router_auth_check_test.go @@ -0,0 +1,91 @@ +package util + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/pkg/errors" +) + +const ( + Valid = "valid" + Invalid = "invalid" +) + +type mockAMFContext struct{} + +func newMockAMFContext() *mockAMFContext { + return &mockAMFContext{} +} + +func (m *mockAMFContext) AuthorizationCheck(token string, serviceName string) error { + if token == Valid { + return nil + } + + return errors.New("invalid token") +} + +func TestRouterAuthorizationCheck_Check(t *testing.T) { + // Mock gin.Context + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + var err error + c.Request, err = http.NewRequest("GET", "/", nil) + if err != nil { + t.Errorf("error on http request: %+v", err) + } + + type Args struct { + token string + } + type Want struct { + statusCode int + } + + tests := []struct { + name string + args Args + want Want + }{ + { + name: "Valid Token", + args: Args{ + token: Valid, + }, + want: Want{ + statusCode: http.StatusOK, + }, + }, + { + name: "Invalid Token", + args: Args{ + token: Invalid, + }, + want: Want{ + statusCode: http.StatusUnauthorized, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w = httptest.NewRecorder() + c, _ = gin.CreateTestContext(w) + c.Request, err = http.NewRequest("GET", "/", nil) + if err != nil { + t.Errorf("error on http request: %+v", err) + } + c.Request.Header.Set("Authorization", tt.args.token) + + rac := NewRouterAuthorizationCheck("testService") + rac.Check(c, newMockAMFContext()) + if w.Code != tt.want.statusCode { + t.Errorf("StatusCode should be %d, but got %d", tt.want.statusCode, w.Code) + } + }) + } +} \ No newline at end of file