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