From 4edb07802fdad892fa8d10f8fd47666b6ccc27c9 Mon Sep 17 00:00:00 2001 From: Naveen <172697+naveensrinivasan@users.noreply.github.com> Date: Wed, 21 Jun 2023 16:57:50 -0500 Subject: [PATCH] :seedling: Included tests for accessor (#3178) * :seedling: Included tests for accessor - Add test file for Token Accessor Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com> * Code review comments. Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com> --------- Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com> --- .../roundtripper/tokens/accessor_test.go | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 clients/githubrepo/roundtripper/tokens/accessor_test.go diff --git a/clients/githubrepo/roundtripper/tokens/accessor_test.go b/clients/githubrepo/roundtripper/tokens/accessor_test.go new file mode 100644 index 00000000000..9f2ed14511b --- /dev/null +++ b/clients/githubrepo/roundtripper/tokens/accessor_test.go @@ -0,0 +1,129 @@ +// Copyright 2023 OpenSSF Scorecard Authors +// +// Licensed 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 tokens + +import ( + "errors" + "fmt" + "log" + "net/http" + "net/rpc" + "testing" +) + +//nolint:paralleltest +func TestMakeTokenAccessor(t *testing.T) { + tests := []struct { + name string + useGitHubToken bool + useServer bool + }{ + { + name: "GitHub Token", + useGitHubToken: true, + }, + { + name: "No GitHub Token", + useGitHubToken: false, + }, + { + name: "Server", + useServer: true, + }, + } + t.Setenv("GITHUB_AUTH_TOKEN", "") + t.Setenv("GITHUB_TOKEN", "") + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + switch { + case tt.useGitHubToken: + t.Helper() + testToken(t) + case tt.useServer: + t.Helper() + testServer(t) + default: + got := MakeTokenAccessor() + if got != nil { + t.Errorf("MakeTokenAccessor() = %v, want nil", got) + } + } + }) + } +} + +func testToken(t *testing.T) { + t.Helper() + token := "test" + t.Setenv("GITHUB_AUTH_TOKEN", token) + got := MakeTokenAccessor() + if got == nil { + t.Errorf("MakeTokenAccessor() = nil, want not nil") + } + raccess, ok := got.(*roundRobinAccessor) + if !ok { + t.Errorf("MakeTokenAccessor() = %v, want *roundRobinAccessor", got) + } + if raccess.accessTokens[0] != token { + t.Errorf("accessTokens[0] = %v, want %v", raccess.accessTokens[0], token) + } +} + +func testServer(t *testing.T) { + t.Helper() + t.Setenv("GITHUB_AUTH_SERVER", "localhost:8080") + server := startTestServer() + t.Cleanup(func() { + serverShutdown(server) + }) + myRPCService := &MyRPCService{} + rpc.Register(myRPCService) //nolint:errcheck + server.Handler = nil + rpc.HandleHTTP() + got := MakeTokenAccessor() + if got == nil { + t.Errorf("MakeTokenAccessor() = nil, want not nil") + } +} + +type MyRPCService struct { + // Define your RPC service methods here +} + +func startTestServer() *http.Server { + // Create a new server + server := &http.Server{ //nolint:gosec + Addr: ":8080", + Handler: nil, // Use the default handler + } + + // Start the server in a separate goroutine + go func() { + fmt.Println("Starting server on http://localhost:8080") + if err := server.ListenAndServe(); err != nil && !errors.Is(http.ErrServerClosed, err) { + log.Fatal(err) + } + }() + + return server +} + +func serverShutdown(server *http.Server) { + err := server.Close() + if err != nil { + log.Fatalf("Error shutting down server: %s\n", err.Error()) + } + + fmt.Println("Server gracefully stopped") +}