github.com/google/osv-scalibr@v0.4.1/veles/secrets/github/oauth_validator_test.go (about) 1 // Copyright 2025 Google LLC 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 15 package github_test 16 17 import ( 18 "context" 19 "net/http" 20 "net/http/httptest" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 "github.com/google/go-cmp/cmp/cmpopts" 25 "github.com/google/osv-scalibr/veles" 26 "github.com/google/osv-scalibr/veles/secrets/github" 27 "github.com/google/osv-scalibr/veles/secrets/github/mockgithub" 28 ) 29 30 const oauthValidatorTestKey = `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` 31 32 func TestOAuthTokenValidator(t *testing.T) { 33 cancelledContext, cancel := context.WithCancel(t.Context()) 34 cancel() 35 36 mockGithubServer := func(code int) *httptest.Server { 37 return mockgithub.Server(t, github.UserValidationEndpoint, code, oauthValidatorTestKey) 38 } 39 40 cases := []struct { 41 name string 42 token string 43 server *httptest.Server 44 want veles.ValidationStatus 45 wantErr error 46 //nolint:containedctx 47 ctx context.Context 48 }{ 49 { 50 name: "cancelled_context", 51 ctx: cancelledContext, 52 server: mockGithubServer(http.StatusOK), 53 want: veles.ValidationFailed, 54 wantErr: cmpopts.AnyError, 55 }, 56 { 57 name: "valid_key", 58 token: oauthValidatorTestKey, 59 server: mockGithubServer(http.StatusOK), 60 want: veles.ValidationValid, 61 }, 62 { 63 name: "invalid_key_unauthorized", 64 token: "random_string", 65 server: mockGithubServer(http.StatusUnauthorized), 66 want: veles.ValidationInvalid, 67 }, 68 { 69 name: "server_error", 70 server: mockGithubServer(http.StatusInternalServerError), 71 want: veles.ValidationFailed, 72 wantErr: cmpopts.AnyError, 73 }, 74 { 75 name: "bad_gateway", 76 server: mockGithubServer(http.StatusBadGateway), 77 want: veles.ValidationFailed, 78 wantErr: cmpopts.AnyError, 79 }, 80 } 81 82 for _, tt := range cases { 83 t.Run(tt.name, func(t *testing.T) { 84 if tt.ctx == nil { 85 tt.ctx = t.Context() 86 } 87 88 // Create a validator with a mock client 89 validator := github.NewOAuthTokenValidator() 90 validator.HTTPC = tt.server.Client() 91 validator.Endpoint = tt.server.URL + github.UserValidationEndpoint 92 93 // Create a test key 94 key := github.OAuthToken{Token: tt.token} 95 96 // Test validation 97 got, err := validator.Validate(tt.ctx, key) 98 99 if !cmp.Equal(tt.wantErr, err, cmpopts.EquateErrors()) { 100 t.Fatalf("Validate() error: %v, want %v", err, tt.wantErr) 101 } 102 103 if tt.want != got { 104 t.Errorf("Validate(): got: %v, want: %v", got, tt.want) 105 } 106 }) 107 } 108 }