Skip to content

Commit 3bea651

Browse files
naveensrinivasanashearin
authored andcommitted
🌱 Included tests for accessor (ossf#3178)
* 🌱 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]> Signed-off-by: Allen Shearin <[email protected]>
1 parent 49e2863 commit 3bea651

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright 2023 OpenSSF Scorecard Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package tokens
15+
16+
import (
17+
"errors"
18+
"fmt"
19+
"log"
20+
"net/http"
21+
"net/rpc"
22+
"testing"
23+
)
24+
25+
//nolint:paralleltest
26+
func TestMakeTokenAccessor(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
useGitHubToken bool
30+
useServer bool
31+
}{
32+
{
33+
name: "GitHub Token",
34+
useGitHubToken: true,
35+
},
36+
{
37+
name: "No GitHub Token",
38+
useGitHubToken: false,
39+
},
40+
{
41+
name: "Server",
42+
useServer: true,
43+
},
44+
}
45+
t.Setenv("GITHUB_AUTH_TOKEN", "")
46+
t.Setenv("GITHUB_TOKEN", "")
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
switch {
50+
case tt.useGitHubToken:
51+
t.Helper()
52+
testToken(t)
53+
case tt.useServer:
54+
t.Helper()
55+
testServer(t)
56+
default:
57+
got := MakeTokenAccessor()
58+
if got != nil {
59+
t.Errorf("MakeTokenAccessor() = %v, want nil", got)
60+
}
61+
}
62+
})
63+
}
64+
}
65+
66+
func testToken(t *testing.T) {
67+
t.Helper()
68+
token := "test"
69+
t.Setenv("GITHUB_AUTH_TOKEN", token)
70+
got := MakeTokenAccessor()
71+
if got == nil {
72+
t.Errorf("MakeTokenAccessor() = nil, want not nil")
73+
}
74+
raccess, ok := got.(*roundRobinAccessor)
75+
if !ok {
76+
t.Errorf("MakeTokenAccessor() = %v, want *roundRobinAccessor", got)
77+
}
78+
if raccess.accessTokens[0] != token {
79+
t.Errorf("accessTokens[0] = %v, want %v", raccess.accessTokens[0], token)
80+
}
81+
}
82+
83+
func testServer(t *testing.T) {
84+
t.Helper()
85+
t.Setenv("GITHUB_AUTH_SERVER", "localhost:8080")
86+
server := startTestServer()
87+
t.Cleanup(func() {
88+
serverShutdown(server)
89+
})
90+
myRPCService := &MyRPCService{}
91+
rpc.Register(myRPCService) //nolint:errcheck
92+
server.Handler = nil
93+
rpc.HandleHTTP()
94+
got := MakeTokenAccessor()
95+
if got == nil {
96+
t.Errorf("MakeTokenAccessor() = nil, want not nil")
97+
}
98+
}
99+
100+
type MyRPCService struct {
101+
// Define your RPC service methods here
102+
}
103+
104+
func startTestServer() *http.Server {
105+
// Create a new server
106+
server := &http.Server{ //nolint:gosec
107+
Addr: ":8080",
108+
Handler: nil, // Use the default handler
109+
}
110+
111+
// Start the server in a separate goroutine
112+
go func() {
113+
fmt.Println("Starting server on http://localhost:8080")
114+
if err := server.ListenAndServe(); err != nil && !errors.Is(http.ErrServerClosed, err) {
115+
log.Fatal(err)
116+
}
117+
}()
118+
119+
return server
120+
}
121+
122+
func serverShutdown(server *http.Server) {
123+
err := server.Close()
124+
if err != nil {
125+
log.Fatalf("Error shutting down server: %s\n", err.Error())
126+
}
127+
128+
fmt.Println("Server gracefully stopped")
129+
}

0 commit comments

Comments
 (0)