github.com/jenkins-x/jx/v2@v2.1.155/pkg/gits/credentialhelper/git_credentialhelper_test.go (about)

     1  // +build unit
     2  
     3  package credentialhelper
     4  
     5  import (
     6  	"bytes"
     7  	"io"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/MakeNowJust/heredoc"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gstruct"
    16  )
    17  
    18  func TestGitCredentialHelper(t *testing.T) {
    19  	RegisterFailHandler(Fail)
    20  	RunSpecs(t, "GitCredentials Suite")
    21  }
    22  
    23  var _ = Describe("GitCredential", func() {
    24  	Context("#CreateGitCredential", func() {
    25  		It("successfully creates GitCredential", func() {
    26  			data := []string{
    27  				"Protocol=http",
    28  				"Host=github.com",
    29  				"Path=jenkins-x/jx",
    30  			}
    31  			credentials, err := CreateGitCredential(data)
    32  			Expect(err).Should(BeNil())
    33  			Expect(credentials).To(MatchAllFields(Fields{
    34  				"Protocol": Equal("http"),
    35  				"Host":     Equal("github.com"),
    36  				"Path":     Equal("jenkins-x/jx"),
    37  				"Username": BeEmpty(),
    38  				"Password": BeEmpty(),
    39  			}))
    40  		})
    41  
    42  		It("passing nil fails GitCredential creation", func() {
    43  			credentials, err := CreateGitCredential(nil)
    44  			Expect(err).ShouldNot(BeNil())
    45  			Expect(credentials).To(MatchAllFields(Fields{
    46  				"Protocol": BeEmpty(),
    47  				"Host":     BeEmpty(),
    48  				"Path":     BeEmpty(),
    49  				"Username": BeEmpty(),
    50  				"Password": BeEmpty(),
    51  			}))
    52  		})
    53  
    54  		It("missing key value pairs format fails GitCredential creation", func() {
    55  			data := []string{"foo"}
    56  			credentials, err := CreateGitCredential(data)
    57  			Expect(err).ShouldNot(BeNil())
    58  			Expect(credentials).To(MatchAllFields(Fields{
    59  				"Protocol": BeEmpty(),
    60  				"Host":     BeEmpty(),
    61  				"Path":     BeEmpty(),
    62  				"Username": BeEmpty(),
    63  				"Password": BeEmpty(),
    64  			}))
    65  		})
    66  	})
    67  
    68  	Context("#CreateGitCredentialFromURL", func() {
    69  		It("successfully creates GitCredential", func() {
    70  			credentials, err := CreateGitCredentialFromURL("http://github.com", "johndoe", "1234")
    71  			Expect(err).Should(BeNil())
    72  			Expect(credentials).To(MatchAllFields(Fields{
    73  				"Protocol": Equal("http"),
    74  				"Host":     Equal("github.com"),
    75  				"Path":     Equal(""),
    76  				"Username": Equal("johndoe"),
    77  				"Password": Equal("1234"),
    78  			}))
    79  		})
    80  	})
    81  
    82  	Context("#Clone", func() {
    83  		var (
    84  			testCredential GitCredential
    85  			err            error
    86  		)
    87  
    88  		BeforeEach(func() {
    89  			data := []string{
    90  				"Protocol=http",
    91  				"Host=github.com",
    92  				"Path=jenkins-x/jx",
    93  				"Username=johndoe",
    94  				"Password=1234",
    95  			}
    96  			testCredential, err = CreateGitCredential(data)
    97  			Expect(err).Should(BeNil())
    98  		})
    99  
   100  		It("successful clone", func() {
   101  			clone := testCredential.Clone()
   102  			Expect(&clone).ShouldNot(BeIdenticalTo(&testCredential))
   103  			Expect(clone).To(MatchAllFields(Fields{
   104  				"Protocol": Equal("http"),
   105  				"Host":     Equal("github.com"),
   106  				"Path":     Equal("jenkins-x/jx"),
   107  				"Username": Equal("johndoe"),
   108  				"Password": Equal("1234"),
   109  			}))
   110  		})
   111  
   112  		Context("#String", func() {
   113  			It("string representation of GitCredential has additional newline (needed by git credential protocol", func() {
   114  				credential, err := CreateGitCredentialFromURL("http://github.com/jenkins-x", "johndoe", "1234")
   115  				Expect(err).Should(BeNil())
   116  				expected := heredoc.Doc(`protocol=http
   117                                                host=github.com
   118                                                path=/jenkins-x
   119                                                username=johndoe
   120                                                password=1234
   121  			    `)
   122  				expected = expected + "\n"
   123  				Expect(credential.String()).Should(Equal(expected))
   124  			})
   125  		})
   126  	})
   127  })
   128  
   129  var _ = Describe("GitCredentialsHelper", func() {
   130  	Context("#CreateGitCredentialsHelper", func() {
   131  		var (
   132  			testIn          io.Reader
   133  			testOut         io.Writer
   134  			testCredentials []GitCredential
   135  		)
   136  
   137  		BeforeEach(func() {
   138  			testIn = strings.NewReader("")
   139  			testOut = bytes.NewBufferString("")
   140  			testCredentials = []GitCredential{}
   141  		})
   142  
   143  		It("fails with no input writer", func() {
   144  			helper, err := CreateGitCredentialsHelper(nil, testOut, testCredentials)
   145  			Expect(err).ShouldNot(BeNil())
   146  			Expect(helper).Should(BeNil())
   147  		})
   148  
   149  		It("fails with no output writer", func() {
   150  			helper, err := CreateGitCredentialsHelper(testIn, nil, testCredentials)
   151  			Expect(err).ShouldNot(BeNil())
   152  			Expect(helper).Should(BeNil())
   153  		})
   154  
   155  		It("fails with no credentials", func() {
   156  			helper, err := CreateGitCredentialsHelper(testIn, testOut, nil)
   157  			Expect(err).ShouldNot(BeNil())
   158  			Expect(helper).Should(BeNil())
   159  		})
   160  
   161  		It("succeeds when all parameters are specified", func() {
   162  			helper, err := CreateGitCredentialsHelper(testIn, testOut, testCredentials)
   163  			Expect(err).Should(BeNil())
   164  			Expect(helper).ShouldNot(BeNil())
   165  		})
   166  	})
   167  
   168  	Context("#Run", func() {
   169  		var (
   170  			testIn          io.Reader
   171  			testOut         *bytes.Buffer
   172  			testCredentials []GitCredential
   173  			helper          *GitCredentialsHelper
   174  			err             error
   175  		)
   176  
   177  		BeforeEach(func() {
   178  			in := heredoc.Doc(`protocol=https
   179                                      host=github.com
   180                                      username=jx-bot
   181              `)
   182  			testIn = strings.NewReader(in)
   183  			testOut = bytes.NewBufferString("")
   184  			testCredentials = []GitCredential{
   185  				{Protocol: "https", Host: "github.com", Username: "jx-bot", Password: "1234"},
   186  			}
   187  			helper, err = CreateGitCredentialsHelper(testIn, testOut, testCredentials)
   188  			Expect(err).Should(BeNil())
   189  		})
   190  
   191  		It("succeeds filling credentials", func() {
   192  			expected := heredoc.Doc(`protocol=https
   193                                      host=github.com
   194                                      path=
   195                                      username=jx-bot
   196                                      password=1234
   197              `)
   198  			expected = expected + "\n"
   199  			err = helper.Run("get")
   200  			Expect(err).Should(BeNil())
   201  			Expect(testOut.String()).Should(Equal(expected))
   202  		})
   203  	})
   204  })