Skylm808 commented on code in PR #125: URL: https://github.com/apache/dubbo-go-pixiu-samples/pull/125#discussion_r2995339883
########## auth/saml/test/pixiu_test.go: ########## @@ -0,0 +1,139 @@ +/* + * 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 saml + +import ( + "io" + "net/http" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +func sampleFile(rel string) string { + return filepath.Join("..", filepath.FromSlash(rel)) +} + +func TestSampleAssetsExist(t *testing.T) { + assets := []string{ + "pixiu/conf.yaml", + "server/app/server.go", + "docker/docker-compose.yml", + "README.md", + "README_CN.md", + "certs/sp.crt", + "certs/sp.key", + } + + for _, asset := range assets { + if _, err := os.Stat(sampleFile(asset)); err != nil { + t.Fatalf("expected sample asset %s: %v", asset, err) + } + } +} + +func TestPixiuConfigContainsSAMLFilter(t *testing.T) { + data, err := os.ReadFile(sampleFile("pixiu/conf.yaml")) + if err != nil { + t.Fatalf("read pixiu config: %v", err) + } + + content := string(data) + required := []string{ + "dgp.filter.http.auth.saml", + "acs_url:", + "metadata_url:", + "idp_metadata_url:", + "allow_idp_initiated: true", + "forward_attributes:", + "X-User-Email", + "X-User-Name", + "/app", + } + + for _, token := range required { + if !strings.Contains(content, token) { + t.Fatalf("expected pixiu config to contain %q", token) + } + } +} + +func TestMetadataEndpoint(t *testing.T) { + client := &http.Client{Timeout: 5 * time.Second} + req, err := http.NewRequest(http.MethodGet, "http://localhost:8888/saml/metadata", nil) + if err != nil { + t.Fatalf("create request: %v", err) + } + + resp, err := client.Do(req) + if err != nil { + t.Fatalf("request metadata endpoint: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + t.Fatalf("expected 200 from metadata endpoint, got %d", resp.StatusCode) + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatalf("read metadata response: %v", err) + } + + text := string(body) + if !strings.Contains(text, "pixiu-saml-sp") { + t.Fatalf("expected metadata to contain entity ID, got %s", text) + } + if !strings.Contains(text, "AssertionConsumerService") { + t.Fatalf("expected metadata to contain ACS endpoint, got %s", text) + } +} + +func TestProtectedRouteRedirectsToIDP(t *testing.T) { + client := &http.Client{ + Timeout: 5 * time.Second, + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } + + req, err := http.NewRequest(http.MethodGet, "http://localhost:8888/app", nil) + if err != nil { + t.Fatalf("create request: %v", err) + } + + resp, err := client.Do(req) + if err != nil { + t.Fatalf("request protected route: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusFound && resp.StatusCode != http.StatusSeeOther { + t.Fatalf("expected redirect from protected route, got %d", resp.StatusCode) + } + + location := resp.Header.Get("Location") + if location == "" { + t.Fatalf("expected redirect location header") + } + if !strings.Contains(location, "18080") && !strings.Contains(location, "/protocol/saml") { Review Comment: 我修正了这里的判断逻辑。现在要求 Location 同时包含 18080 和 /protocol/saml,避免 http://localhost:18080/not-saml 或其他主机上的 /protocol/saml/... 这类误判。另外我把判断抽成了 isKeycloakSAMLRedirect,并补了对应测试用例。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
