github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/utils/auth_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package utils provides generic utility functions.package utils
     5  package utils
     6  
     7  import (
     8  	"testing"
     9  
    10  	mocks "github.com/Racer159/jackal/src/test/mocks"
    11  	"github.com/go-git/go-git/v5/plumbing/transport/http"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestCredentialParser(t *testing.T) {
    16  	credentialsFile := &mocks.MockReadCloser{
    17  		MockData: []byte(
    18  			`https://wayne:password@github.com/
    19  bad line
    20  <really bad="line"/>
    21  https://wayne:p%40ss%20word%2520@jackal.dev
    22  http://google.com`,
    23  		),
    24  	}
    25  
    26  	expectedCreds := []Credential{
    27  		{
    28  			Path: "github.com",
    29  			Auth: http.BasicAuth{
    30  				Username: "wayne",
    31  				Password: "password",
    32  			},
    33  		},
    34  		{
    35  			Path: "jackal.dev",
    36  			Auth: http.BasicAuth{
    37  				Username: "wayne",
    38  				Password: "p@ss word%20",
    39  			},
    40  		},
    41  		{
    42  			Path: "google.com",
    43  			Auth: http.BasicAuth{
    44  				Username: "",
    45  				Password: "",
    46  			},
    47  		},
    48  	}
    49  
    50  	gitCredentials := credentialParser(credentialsFile)
    51  	require.Equal(t, expectedCreds, gitCredentials)
    52  }
    53  
    54  func TestNetRCParser(t *testing.T) {
    55  
    56  	netrcFile := &mocks.MockReadCloser{
    57  		MockData: []byte(
    58  			`# top of file comment
    59  machine github.com
    60  	login wayne
    61      password password
    62  
    63   machine jackal.dev login wayne password p@s#sword%20
    64  
    65  macdef macro-name
    66  touch file
    67   echo "I am a script and can do anything password fun or login info yay!"
    68  
    69  machine google.com #comment password fun and login info!
    70  
    71  default
    72    login anonymous
    73  	password password`,
    74  		),
    75  	}
    76  
    77  	expectedCreds := []Credential{
    78  		{
    79  			Path: "github.com",
    80  			Auth: http.BasicAuth{
    81  				Username: "wayne",
    82  				Password: "password",
    83  			},
    84  		},
    85  		{
    86  			Path: "jackal.dev",
    87  			Auth: http.BasicAuth{
    88  				Username: "wayne",
    89  				Password: "p@s#sword%20",
    90  			},
    91  		},
    92  		{
    93  			Path: "google.com",
    94  			Auth: http.BasicAuth{
    95  				Username: "",
    96  				Password: "",
    97  			},
    98  		},
    99  		{
   100  			Path: "",
   101  			Auth: http.BasicAuth{
   102  				Username: "anonymous",
   103  				Password: "password",
   104  			},
   105  		},
   106  	}
   107  
   108  	netrcCredentials := netrcParser(netrcFile)
   109  	require.Equal(t, expectedCreds, netrcCredentials)
   110  }