Skip to content

Commit

Permalink
🌱 Included tests for accessor (#3178)
Browse files Browse the repository at this point in the history
* 🌱 Included tests for accessor

- Add test file for Token Accessor

Signed-off-by: naveensrinivasan <[email protected]>

* Code review comments.

Signed-off-by: naveensrinivasan <[email protected]>

---------

Signed-off-by: naveensrinivasan <[email protected]>
  • Loading branch information
naveensrinivasan authored Jun 21, 2023
1 parent b4c197c commit 4edb078
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions clients/githubrepo/roundtripper/tokens/accessor_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 4edb078

Please sign in to comment.