This is an automated email from the ASF dual-hosted git repository.

LinkinStars pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/answer.git


The following commit(s) were added to refs/heads/dev by this push:
     new a570e3aa test: cover connector POST state handling
a570e3aa is described below

commit a570e3aa6da48d9afd1ab95a5310755c0503d1da
Author: LinkinStars <[email protected]>
AuthorDate: Wed Oct 21 16:47:07 2026 +0800

    test: cover connector POST state handling
---
 internal/controller/connector_controller.go      |  7 +++-
 internal/controller/connector_controller_test.go | 50 ++++++++++++++++++++++++
 internal/router/plugin_api_router_test.go        | 39 ++++++++++++++++++
 3 files changed, 95 insertions(+), 1 deletion(-)

diff --git a/internal/controller/connector_controller.go 
b/internal/controller/connector_controller.go
index c81e644f..9ea04ece 100644
--- a/internal/controller/connector_controller.go
+++ b/internal/controller/connector_controller.go
@@ -175,7 +175,7 @@ func (cc *ConnectorController) ConnectorRedirect(connector 
plugin.Connector) (fn
                        ctx.Redirect(http.StatusFound, "/50x")
                        return
                }
-               if requirer, ok := connector.(plugin.ConnectorStateRequired); 
ok && requirer.ConnectorRequireState() && stateInfo == nil {
+               if connectorRequiresState(connector) && stateInfo == nil {
                        log.Errorf("missing or invalid connector oauth state 
for provider %s", connector.ConnectorSlugName())
                        ctx.Redirect(http.StatusFound, "/50x")
                        return
@@ -209,6 +209,11 @@ func (cc *ConnectorController) ConnectorRedirect(connector 
plugin.Connector) (fn
        }
 }
 
+func connectorRequiresState(connector any) bool {
+       requirer, ok := connector.(plugin.ConnectorStateRequired)
+       return ok && requirer.ConnectorRequireState()
+}
+
 // ConnectorsInfo get all enabled connectors
 // @Summary get all enabled connectors
 // @Description get all enabled connectors
diff --git a/internal/controller/connector_controller_test.go 
b/internal/controller/connector_controller_test.go
new file mode 100644
index 00000000..6fc219be
--- /dev/null
+++ b/internal/controller/connector_controller_test.go
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package controller
+
+import "testing"
+
+type stateRequiringConnector struct {
+       required bool
+}
+
+func (c stateRequiringConnector) ConnectorRequireState() bool {
+       return c.required
+}
+
+func TestConnectorRequiresState(t *testing.T) {
+       tests := []struct {
+               name      string
+               connector any
+               want      bool
+       }{
+               {name: "legacy connector", connector: struct{}{}, want: false},
+               {name: "connector opts out", connector: 
stateRequiringConnector{required: false}, want: false},
+               {name: "connector opts in", connector: 
stateRequiringConnector{required: true}, want: true},
+       }
+
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       if got := connectorRequiresState(tt.connector); got != 
tt.want {
+                               t.Fatalf("connectorRequiresState() = %t, want 
%t", got, tt.want)
+                       }
+               })
+       }
+}
diff --git a/internal/router/plugin_api_router_test.go 
b/internal/router/plugin_api_router_test.go
new file mode 100644
index 00000000..4ca1157e
--- /dev/null
+++ b/internal/router/plugin_api_router_test.go
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package router
+
+import (
+       "testing"
+
+       "github.com/gin-gonic/gin"
+)
+
+func TestUnAuthConnectorRouterRegistersPostRedirect(t *testing.T) {
+       r := gin.New()
+       pluginRouter := NewPluginAPIRouter(nil, nil, nil, nil, nil, nil)
+       pluginRouter.RegisterUnAuthConnectorRouter(r.Group(""))
+
+       for _, route := range r.Routes() {
+               if route.Method == "POST" && route.Path == 
"/connector/redirect/:name" {
+                       return
+               }
+       }
+       t.Fatal("POST connector redirect route was not registered")
+}

Reply via email to