github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/git/credentials/step_git_credentials_test.go (about)

     1  // +build unit
     2  
     3  package credentials
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/MakeNowJust/heredoc"
    12  
    13  	"github.com/jenkins-x/jx-logging/pkg/log"
    14  
    15  	"github.com/olli-ai/jx/v2/pkg/auth"
    16  
    17  	"github.com/olli-ai/jx/v2/pkg/gits"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  func TestStepGitCredentials(t *testing.T) {
    23  	RegisterFailHandler(Fail)
    24  	RunSpecs(t, "Step Git Credentials Suite")
    25  }
    26  
    27  var _ = Describe("step git merge", func() {
    28  	var (
    29  		authSvc    auth.ConfigService
    30  		testWriter *os.File
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		By("capturing log output")
    35  		_, testWriter, _ = os.Pipe()
    36  		log.SetOutput(testWriter)
    37  		_ = log.SetLevel("info")
    38  
    39  		By("creating in memory auth service")
    40  		authSvc = auth.NewMemoryAuthConfigService()
    41  		cfg := auth.AuthConfig{
    42  			Servers: []*auth.AuthServer{
    43  				{
    44  					URL: "https://" + "github.com",
    45  					Users: []*auth.UserAuth{
    46  						{
    47  							GithubAppOwner: "jstrachan-gh-app",
    48  							Username:       "jstrachan",
    49  							ApiToken:       "lovelyLager",
    50  						},
    51  					},
    52  					Name:        "gh",
    53  					Kind:        gits.KindGitHub,
    54  					CurrentUser: "jstrachan",
    55  				},
    56  				{
    57  					URL: "http://" + "github.beescloud.com",
    58  					Users: []*auth.UserAuth{
    59  						{
    60  							Username: "rawlingsj",
    61  							ApiToken: "glassOfNice",
    62  						},
    63  					},
    64  					Name:        "bee",
    65  					Kind:        gits.KindGitHub,
    66  					CurrentUser: "rawlingsj",
    67  				},
    68  			},
    69  		}
    70  		authSvc.SetConfig(&cfg)
    71  	})
    72  
    73  	Context("#createGitCredentialsFile", func() {
    74  		var (
    75  			tmpDir  string
    76  			outFile string
    77  			err     error
    78  		)
    79  
    80  		BeforeEach(func() {
    81  			tmpDir, err = ioutil.TempDir("", "gitcredentials")
    82  			Expect(err).Should(BeNil())
    83  
    84  			outFile = filepath.Join(tmpDir, "credentials")
    85  
    86  		})
    87  
    88  		AfterEach(func() {
    89  			_ = os.RemoveAll(tmpDir)
    90  		})
    91  
    92  		It("successfully creates git credential file for known users", func() {
    93  
    94  			expected := heredoc.Doc(`https://jstrachan:lovelyLager@github.com
    95                                            http://rawlingsj:glassOfNice@github.beescloud.com
    96                                            https://rawlingsj:glassOfNice@github.beescloud.com
    97  			`)
    98  
    99  			options := &StepGitCredentialsOptions{
   100  				OutputFile: outFile,
   101  			}
   102  
   103  			credentials, err := options.CreateGitCredentialsFromAuthService(authSvc, false)
   104  			Expect(err).Should(BeNil())
   105  			err = options.createGitCredentialsFile(outFile, credentials)
   106  			Expect(err).Should(BeNil())
   107  
   108  			data, err := ioutil.ReadFile(outFile)
   109  			Expect(err).Should(BeNil())
   110  			actual := string(data)
   111  			Expect(actual).Should(Equal(expected))
   112  		})
   113  
   114  		It("successfully creates git credential file for GitHub App", func() {
   115  
   116  			expected := heredoc.Doc(`https://jstrachan:lovelyLager@github.com
   117  			`)
   118  
   119  			options := &StepGitCredentialsOptions{
   120  				OutputFile: outFile,
   121  				RepoOwner:  "jstrachan-gh-app",
   122  				GitKind:    gits.KindGitHub,
   123  			}
   124  
   125  			credentials, err := options.CreateGitCredentialsFromAuthService(authSvc, true)
   126  			Expect(err).Should(BeNil())
   127  			err = options.createGitCredentialsFile(outFile, credentials)
   128  			Expect(err).Should(BeNil())
   129  
   130  			data, err := ioutil.ReadFile(outFile)
   131  			Expect(err).Should(BeNil())
   132  			actual := string(data)
   133  			Expect(actual).Should(Equal(expected))
   134  		})
   135  	})
   136  })