github.com/golang/review@v0.0.0-20190122205339-266ee1edf5c3/git-codereview/api_test.go (about)

     1  // Copyright 2014 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  )
    12  
    13  var authTests = []struct {
    14  	netrc       string
    15  	cookiefile  string
    16  	user        string
    17  	password    string
    18  	cookieName  string
    19  	cookieValue string
    20  	died        bool
    21  }{
    22  	{
    23  		// If we specify the empty string here, git will store an empty
    24  		// value for the local http.cookiefile, and fall back to the global
    25  		// http.cookiefile, which will fail this test on any machine that has
    26  		// a global http.cookiefile configured. If we write a local, invalid
    27  		// value, git will try to load the local cookie file (and then fail
    28  		// later).
    29  		cookiefile: " ",
    30  		died:       true,
    31  	},
    32  	{
    33  		netrc:      "machine go.googlesource.com login u1 password pw\n",
    34  		cookiefile: " ", // prevent global fallback
    35  		user:       "u1",
    36  		password:   "pw",
    37  	},
    38  	{
    39  		cookiefile: "go.googlesource.com	TRUE	/	TRUE	2147483647	o2	git-u2=pw\n",
    40  		cookieName:  "o2",
    41  		cookieValue: "git-u2=pw",
    42  	},
    43  	{
    44  		cookiefile: ".googlesource.com	TRUE	/	TRUE	2147483647	o3	git-u3=pw\n",
    45  		cookieName:  "o3",
    46  		cookieValue: "git-u3=pw",
    47  	},
    48  	{
    49  		cookiefile: ".googlesource.com	TRUE	/	TRUE	2147483647	o4	WRONG\n" +
    50  			"go.googlesource.com	TRUE	/	TRUE	2147483647	o4	git-u4=pw\n",
    51  		cookieName:  "o4",
    52  		cookieValue: "git-u4=pw",
    53  	},
    54  	{
    55  		cookiefile: "go.googlesource.com	TRUE	/	TRUE	2147483647	o5	git-u5=pw\n" +
    56  			".googlesource.com	TRUE	/	TRUE	2147483647	o5	WRONG\n",
    57  		cookieName:  "o5",
    58  		cookieValue: "git-u5=pw",
    59  	},
    60  	{
    61  		netrc:      "machine go.googlesource.com login u6 password pw\n",
    62  		cookiefile: "BOGUS",
    63  		user:       "u6",
    64  		password:   "pw",
    65  	},
    66  	{
    67  		netrc: "BOGUS",
    68  		cookiefile: "go.googlesource.com	TRUE	/	TRUE	2147483647	o7	git-u7=pw\n",
    69  		cookieName:  "o7",
    70  		cookieValue: "git-u7=pw",
    71  	},
    72  	{
    73  		netrc:      "machine go.googlesource.com login u8 password pw\n",
    74  		cookiefile: "MISSING",
    75  		user:       "u8",
    76  		password:   "pw",
    77  	},
    78  }
    79  
    80  func TestLoadAuth(t *testing.T) {
    81  	gt := newGitTest(t)
    82  	defer gt.done()
    83  	gt.work(t)
    84  
    85  	defer os.Setenv("HOME", os.Getenv("HOME"))
    86  	os.Setenv("HOME", gt.client)
    87  
    88  	testHomeDir = gt.client
    89  	netrc := filepath.Join(gt.client, netrcName())
    90  	defer func() {
    91  		testHomeDir = ""
    92  	}()
    93  	trun(t, gt.client, "git", "config", "remote.origin.url", "https://go.googlesource.com/go")
    94  
    95  	for i, tt := range authTests {
    96  		t.Logf("#%d", i)
    97  		auth.user = ""
    98  		auth.password = ""
    99  		auth.cookieName = ""
   100  		auth.cookieValue = ""
   101  		trun(t, gt.client, "git", "config", "http.cookiefile", "XXX")
   102  		trun(t, gt.client, "git", "config", "--unset", "http.cookiefile")
   103  
   104  		remove(t, netrc)
   105  		remove(t, gt.client+"/.cookies")
   106  		if tt.netrc != "" {
   107  			write(t, netrc, tt.netrc)
   108  		}
   109  		if tt.cookiefile != "" {
   110  			if tt.cookiefile != "MISSING" {
   111  				write(t, gt.client+"/.cookies", tt.cookiefile)
   112  			}
   113  			trun(t, gt.client, "git", "config", "http.cookiefile", "~/.cookies")
   114  		}
   115  
   116  		// Run command via testMain to trap stdout, stderr, death.
   117  		if tt.died {
   118  			testMainDied(t, "test-loadAuth")
   119  		} else {
   120  			testMain(t, "test-loadAuth")
   121  		}
   122  
   123  		if auth.user != tt.user || auth.password != tt.password {
   124  			t.Errorf("#%d: have user, password = %q, %q, want %q, %q", i, auth.user, auth.password, tt.user, tt.password)
   125  		}
   126  		if auth.cookieName != tt.cookieName || auth.cookieValue != tt.cookieValue {
   127  			t.Errorf("#%d: have cookie name, value = %q, %q, want %q, %q", i, auth.cookieName, auth.cookieValue, tt.cookieName, tt.cookieValue)
   128  		}
   129  	}
   130  }
   131  
   132  func TestLoadGerritOrigin(t *testing.T) {
   133  	list := []struct {
   134  		origin, originUrl string
   135  
   136  		fail               bool
   137  		host, url, project string
   138  	}{
   139  		{
   140  			// *.googlesource.com
   141  			origin:    "",
   142  			originUrl: "https://go.googlesource.com/crypto",
   143  			host:      "go.googlesource.com",
   144  			url:       "https://go-review.googlesource.com",
   145  			project:   "crypto",
   146  		},
   147  		{
   148  			// Gerrit origin is set.
   149  			// Gerrit is hosted on a sub-domain.
   150  			origin:    "https://gerrit.mysite.com",
   151  			originUrl: "https://gerrit.mysite.com/projectA",
   152  			host:      "gerrit.mysite.com",
   153  			url:       "https://gerrit.mysite.com",
   154  			project:   "projectA",
   155  		},
   156  		{
   157  			// Gerrit origin is set.
   158  			// Gerrit is hosted under sub-path under "/gerrit".
   159  			origin:    "https://mysite.com/gerrit",
   160  			originUrl: "https://mysite.com/gerrit/projectA",
   161  			host:      "mysite.com",
   162  			url:       "https://mysite.com/gerrit",
   163  			project:   "projectA",
   164  		},
   165  		{
   166  			// Gerrit origin is set.
   167  			// Gerrit is hosted under sub-path under "/gerrit" and repo is under
   168  			// sub-folder.
   169  			origin:    "https://mysite.com/gerrit",
   170  			originUrl: "https://mysite.com/gerrit/sub/projectA",
   171  			host:      "mysite.com",
   172  			url:       "https://mysite.com/gerrit",
   173  			project:   "sub/projectA",
   174  		},
   175  	}
   176  
   177  	for _, item := range list {
   178  		auth.host = ""
   179  		auth.url = ""
   180  		auth.project = ""
   181  		err := loadGerritOriginInternal(item.origin, item.originUrl)
   182  		if err != nil && !item.fail {
   183  			t.Errorf("unexpected error from item %q %q: %v", item.origin, item.originUrl, err)
   184  			continue
   185  		}
   186  		if auth.host != item.host || auth.url != item.url || auth.project != item.project {
   187  			t.Errorf("want %q %q %q, got %q %q %q", item.host, item.url, item.project, auth.host, auth.url, auth.project)
   188  			continue
   189  		}
   190  		if item.fail {
   191  			continue
   192  		}
   193  		have := haveGerritInternal(item.origin, item.originUrl)
   194  		if !have {
   195  			t.Errorf("for %q %q expect haveGerrit() == true, but got false", item.origin, item.originUrl)
   196  		}
   197  	}
   198  }