github.com/google/osv-scalibr@v0.4.1/clients/datasource/maven_settings_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 datasource_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"github.com/google/go-cmp/cmp/cmpopts"
    22  	"github.com/google/osv-scalibr/clients/datasource"
    23  )
    24  
    25  func TestParseMavenSettings(t *testing.T) {
    26  	t.Setenv("MAVEN_SETTINGS_TEST_USR", "UsErNaMe")
    27  	t.Setenv("MAVEN_SETTINGS_TEST_PWD", "P455W0RD")
    28  	t.Setenv("MAVEN_SETTINGS_TEST_SID", "my-cool-server")
    29  	t.Setenv("MAVEN_SETTINGS_TEST_NIL", "")
    30  	want := datasource.MavenSettingsXML{
    31  		Servers: []datasource.MavenSettingsXMLServer{
    32  			{
    33  				ID:       "server1",
    34  				Username: "user",
    35  				Password: "pass",
    36  			},
    37  			{
    38  				ID:       "server2",
    39  				Username: "UsErNaMe",
    40  				Password: "~~P455W0RD~~",
    41  			},
    42  			{
    43  				ID:       "my-cool-server",
    44  				Username: "${env.maven_settings_test_usr}-",
    45  				Password: "${env.MAVEN_SETTINGS_TEST_BAD}",
    46  			},
    47  		},
    48  	}
    49  
    50  	got := datasource.ParseMavenSettings("./testdata/maven_settings/settings.xml")
    51  
    52  	if diff := cmp.Diff(want, got); diff != "" {
    53  		t.Errorf("ParseMavenSettings() (-want +got):\n%s", diff)
    54  	}
    55  }
    56  
    57  func TestMakeMavenAuth(t *testing.T) {
    58  	globalSettings := datasource.MavenSettingsXML{
    59  		Servers: []datasource.MavenSettingsXMLServer{
    60  			{
    61  				ID:       "global",
    62  				Username: "global-user",
    63  				Password: "global-pass",
    64  			},
    65  			{
    66  				ID:       "overwrite1",
    67  				Username: "original-user",
    68  				Password: "original-pass",
    69  			},
    70  			{
    71  				ID:       "overwrite2",
    72  				Username: "user-to-be-deleted",
    73  				// no password
    74  			},
    75  		},
    76  	}
    77  	userSettings := datasource.MavenSettingsXML{
    78  		Servers: []datasource.MavenSettingsXMLServer{
    79  			{
    80  				ID:       "user",
    81  				Username: "user",
    82  				Password: "pass",
    83  			},
    84  			{
    85  				ID:       "overwrite1",
    86  				Username: "new-user",
    87  				Password: "new-pass",
    88  			},
    89  			{
    90  				ID: "overwrite2",
    91  				// no username
    92  				Password: "lone-password",
    93  			},
    94  		},
    95  	}
    96  
    97  	wantSupportedMethods := []datasource.HTTPAuthMethod{datasource.AuthDigest, datasource.AuthBasic}
    98  	want := map[string]*datasource.HTTPAuthentication{
    99  		"global": {
   100  			SupportedMethods: wantSupportedMethods,
   101  			AlwaysAuth:       false,
   102  			Username:         "global-user",
   103  			Password:         "global-pass",
   104  		},
   105  		"user": {
   106  			SupportedMethods: wantSupportedMethods,
   107  			AlwaysAuth:       false,
   108  			Username:         "user",
   109  			Password:         "pass",
   110  		},
   111  		"overwrite1": {
   112  			SupportedMethods: wantSupportedMethods,
   113  			AlwaysAuth:       false,
   114  			Username:         "new-user",
   115  			Password:         "new-pass",
   116  		},
   117  		"overwrite2": {
   118  			SupportedMethods: wantSupportedMethods,
   119  			AlwaysAuth:       false,
   120  			Username:         "",
   121  			Password:         "lone-password",
   122  		},
   123  	}
   124  
   125  	got := datasource.MakeMavenAuth(globalSettings, userSettings)
   126  	if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(datasource.HTTPAuthentication{})); diff != "" {
   127  		t.Errorf("MakeMavenAuth() (-want +got):\n%s", diff)
   128  	}
   129  }