v.io/jiri@v0.0.0-20160715023856-abfb8b131290/gerrit/gerrit_test.go (about)

     1  // Copyright 2015 The Vanadium 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 gerrit
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestParseQueryResults(t *testing.T) {
    15  	input := `)]}'
    16  	[
    17  		{
    18  			"change_id": "I26f771cebd6e512b89e98bec1fadfa1cb2aad6e8",
    19  			"current_revision": "3654e38b2f80a5410ea94f1d7321477d89cac391",
    20  			"project": "vanadium",
    21  			"owner": {
    22  				"_account_id": 1234,
    23  				"name": "John Doe",
    24  				"email": "john.doe@example.com"
    25  			},
    26  			"revisions": {
    27  				"3654e38b2f80a5410ea94f1d7321477d89cac391": {
    28  					"fetch": {
    29  						"http": {
    30  							"ref": "refs/changes/40/4440/1"
    31  						}
    32  					}
    33  				}
    34  			}
    35  		},
    36  		{
    37  			"change_id": "I26f771cebd6e512b89e98bec1fadfa1cb2aad6e8",
    38  			"current_revision": "3654e38b2f80a5410ea94f1d7321477d89cac391",
    39  			"labels": {
    40  				"Code-Review": {},
    41  				"Verified": {}
    42  			},
    43  			"project": "vanadium",
    44  			"owner": {
    45  				"_account_id": 1234,
    46  				"name": "John Doe",
    47  				"email": "john.doe@example.com"
    48  			},
    49  			"topic": "test",
    50  			"revisions": {
    51  				"3654e38b2f80a5410ea94f1d7321477d89cac391": {
    52  					"fetch": {
    53  						"http": {
    54  							"ref": "refs/changes/40/4440/1"
    55  						}
    56  					},
    57  					"commit": {
    58  						"message": "MultiPart: 1/3\nPresubmitTest: none"
    59  					}
    60  				}
    61  			}
    62  		},
    63  		{
    64  			"change_id": "I35d83f8adae5b7db1974062fdc744f700e456677",
    65  			"current_revision": "b60413712472f1b576c7be951c4de309c6edaa53",
    66  			"project": "tools",
    67  			"owner": {
    68  				"_account_id": 1234,
    69  				"name": "John Doe",
    70  				"email": "john.doe@example.com"
    71  			},
    72  			"revisions": {
    73  				"b60413712472f1b576c7be951c4de309c6edaa53": {
    74  					"fetch": {
    75  						"http": {
    76  							"ref": "refs/changes/43/4443/1"
    77  						}
    78  					},
    79  					"commit": {
    80  						"message": "this change is great.\nPresubmitTest: none"
    81  					}
    82  				}
    83  			}
    84  		}
    85  	]
    86  	`
    87  	expectedFields := []struct {
    88  		ref           string
    89  		project       string
    90  		ownerEmail    string
    91  		multiPart     *MultiPartCLInfo
    92  		presubmitType PresubmitTestType
    93  	}{
    94  		{
    95  			ref:           "refs/changes/40/4440/1",
    96  			project:       "vanadium",
    97  			ownerEmail:    "john.doe@example.com",
    98  			multiPart:     nil,
    99  			presubmitType: PresubmitTestTypeAll,
   100  		},
   101  		{
   102  			ref:        "refs/changes/40/4440/1",
   103  			project:    "vanadium",
   104  			ownerEmail: "john.doe@example.com",
   105  			multiPart: &MultiPartCLInfo{
   106  				Topic: "test",
   107  				Index: 1,
   108  				Total: 3,
   109  			},
   110  			presubmitType: PresubmitTestTypeNone,
   111  		},
   112  		{
   113  			ref:           "refs/changes/43/4443/1",
   114  			project:       "tools",
   115  			ownerEmail:    "john.doe@example.com",
   116  			multiPart:     nil,
   117  			presubmitType: PresubmitTestTypeNone,
   118  		},
   119  	}
   120  
   121  	got, err := parseQueryResults(strings.NewReader(input))
   122  	if err != nil {
   123  		t.Fatalf("%v", err)
   124  	}
   125  	for i, curChange := range got {
   126  		f := expectedFields[i]
   127  		if want, got := f.ref, curChange.Reference(); want != got {
   128  			t.Fatalf("%d: want: %q, got: %q", i, want, got)
   129  		}
   130  		if want, got := f.project, curChange.Project; want != got {
   131  			t.Fatalf("%d: want: %q, got: %q", i, want, got)
   132  		}
   133  		if want, got := f.ownerEmail, curChange.OwnerEmail(); want != got {
   134  			t.Fatalf("%d: want: %q, got: %q", i, want, got)
   135  		}
   136  		if want, got := f.multiPart, curChange.MultiPart; !reflect.DeepEqual(want, got) {
   137  			t.Fatalf("%d: want:\n%#v\ngot:\n%#v\n", i, want, got)
   138  		}
   139  		if want, got := f.presubmitType, curChange.PresubmitTest; want != got {
   140  			t.Fatalf("%d: want: %q, got: %q", i, want, got)
   141  		}
   142  	}
   143  }
   144  
   145  func TestParseMultiPartMatch(t *testing.T) {
   146  	type testCase struct {
   147  		str             string
   148  		expectNoMatches bool
   149  		expectedIndex   string
   150  		expectedTotal   string
   151  	}
   152  	testCases := []testCase{
   153  		testCase{
   154  			str:             "message...\nMultiPart: a/3",
   155  			expectNoMatches: true,
   156  		},
   157  		testCase{
   158  			str:             "message...\n1/3",
   159  			expectNoMatches: true,
   160  		},
   161  		testCase{
   162  			str:           "message...\nMultiPart:1/2",
   163  			expectedIndex: "1",
   164  			expectedTotal: "2",
   165  		},
   166  		testCase{
   167  			str:           "message...\nMultiPart: 1/2",
   168  			expectedIndex: "1",
   169  			expectedTotal: "2",
   170  		},
   171  		testCase{
   172  			str:           "message...\nMultiPart: 1 /2",
   173  			expectedIndex: "1",
   174  			expectedTotal: "2",
   175  		},
   176  		testCase{
   177  			str:           "message...\nMultiPart: 1/ 2",
   178  			expectedIndex: "1",
   179  			expectedTotal: "2",
   180  		},
   181  		testCase{
   182  			str:           "message...\nMultiPart: 1 / 2",
   183  			expectedIndex: "1",
   184  			expectedTotal: "2",
   185  		},
   186  		testCase{
   187  			str:           "message...\nMultiPart: 123/234",
   188  			expectedIndex: "123",
   189  			expectedTotal: "234",
   190  		},
   191  	}
   192  	for _, test := range testCases {
   193  		multiPartCLInfo, _ := parseMultiPartMatch(test.str)
   194  		if test.expectNoMatches && multiPartCLInfo != nil {
   195  			t.Fatalf("want no matches, got %v", multiPartCLInfo)
   196  		}
   197  		if !test.expectNoMatches && multiPartCLInfo == nil {
   198  			t.Fatalf("want matches, got no matches")
   199  		}
   200  		if !test.expectNoMatches {
   201  			if want, got := test.expectedIndex, fmt.Sprintf("%d", multiPartCLInfo.Index); want != got {
   202  				t.Fatalf("want 'index' %q, got %q", want, got)
   203  			}
   204  			if want, got := test.expectedTotal, fmt.Sprintf("%d", multiPartCLInfo.Total); want != got {
   205  				t.Fatalf("want 'total' %q, got %q", want, got)
   206  			}
   207  		}
   208  	}
   209  }
   210  
   211  func TestParseValidGitCookieFile(t *testing.T) {
   212  	// Valid content.
   213  	gitCookieFileContent := `
   214  vanadium.googlesource.com	FALSE	/	TRUE	2147483647	o	git-johndoe.example.com=12345
   215  vanadium-review.googlesource.com	FALSE	/	TRUE	2147483647	o	git-johndoe.example.com=54321
   216  .googlesource.com	FALSE	/	TRUE	2147483647	o	git-johndoe.example.com=12321
   217  	`
   218  	got, err := parseGitCookieFile(strings.NewReader(gitCookieFileContent))
   219  	expected := map[string]*credentials{
   220  		"vanadium.googlesource.com": &credentials{
   221  			username: "git-johndoe.example.com",
   222  			password: "12345",
   223  		},
   224  		"vanadium-review.googlesource.com": &credentials{
   225  			username: "git-johndoe.example.com",
   226  			password: "54321",
   227  		},
   228  		".googlesource.com": &credentials{
   229  			username: "git-johndoe.example.com",
   230  			password: "12321",
   231  		},
   232  	}
   233  	if err != nil {
   234  		t.Fatalf("want no errors, got: %v", err)
   235  	}
   236  	if !reflect.DeepEqual(expected, got) {
   237  		t.Fatalf("want: %#v, got: %#v", expected, got)
   238  	}
   239  }
   240  
   241  func TestParseInvalidGitCookieFile(t *testing.T) {
   242  	// Content with invalid entries which should be skipped.
   243  	gitCookieFileContentWithInvalidEntries := `
   244  vanadium.googlesource.com	FALSE	/	TRUE	2147483647	o	git-johndoe.example.com
   245  vanadium-review.googlesource.com FALSE / TRUE 2147483647 o git-johndoe.example.com=54321
   246  vanadium.googlesource.com	FALSE	/	TRUE	2147483647	o	git-johndoe.example.com=12345
   247  vanadium-review.googlesource.com	FALSE	/	TRUE	2147483647	o
   248  	`
   249  	got, err := parseGitCookieFile(strings.NewReader(gitCookieFileContentWithInvalidEntries))
   250  	expected := map[string]*credentials{
   251  		"vanadium.googlesource.com": &credentials{
   252  			username: "git-johndoe.example.com",
   253  			password: "12345",
   254  		},
   255  	}
   256  	if err != nil {
   257  		t.Fatalf("want no errors, got: %v", err)
   258  	}
   259  	if !reflect.DeepEqual(expected, got) {
   260  		t.Fatalf("want: %#v, got: %#v", expected, got)
   261  	}
   262  }
   263  
   264  func TestParseValidNetRcFile(t *testing.T) {
   265  	// Valid content.
   266  	netrcFileContent := `
   267  machine vanadium.googlesource.com login git-johndoe.example.com password 12345
   268  machine vanadium-review.googlesource.com login git-johndoe.example.com password 54321
   269  	`
   270  	got, err := parseNetrcFile(strings.NewReader(netrcFileContent))
   271  	expected := map[string]*credentials{
   272  		"vanadium.googlesource.com": &credentials{
   273  			username: "git-johndoe.example.com",
   274  			password: "12345",
   275  		},
   276  		"vanadium-review.googlesource.com": &credentials{
   277  			username: "git-johndoe.example.com",
   278  			password: "54321",
   279  		},
   280  	}
   281  	if err != nil {
   282  		t.Fatalf("want no errors, got: %v", err)
   283  	}
   284  	if !reflect.DeepEqual(expected, got) {
   285  		t.Fatalf("want: %#v, got: %#v", expected, got)
   286  	}
   287  }
   288  
   289  func TestParseInvalidNetRcFile(t *testing.T) {
   290  	// Content with invalid entries which should be skipped.
   291  	netRcFileContentWithInvalidEntries := `
   292  machine vanadium.googlesource.com login git-johndoe.example.com password
   293  machine_blah vanadium3.googlesource.com login git-johndoe.example.com password 12345
   294  machine vanadium2.googlesource.com login_blah git-johndoe.example.com password 12345
   295  machine vanadium4.googlesource.com login git-johndoe.example.com password_blah 12345
   296  machine vanadium-review.googlesource.com login git-johndoe.example.com password 54321
   297  	`
   298  	got, err := parseNetrcFile(strings.NewReader(netRcFileContentWithInvalidEntries))
   299  	expected := map[string]*credentials{
   300  		"vanadium-review.googlesource.com": &credentials{
   301  			username: "git-johndoe.example.com",
   302  			password: "54321",
   303  		},
   304  	}
   305  	if err != nil {
   306  		t.Fatalf("want no errors, got: %v", err)
   307  	}
   308  	if !reflect.DeepEqual(expected, got) {
   309  		t.Fatalf("want: %#v, got: %#v", expected, got)
   310  	}
   311  }
   312  
   313  func TestParseRefString(t *testing.T) {
   314  	type testCase struct {
   315  		ref              string
   316  		expectErr        bool
   317  		expectedCL       int
   318  		expectedPatchSet int
   319  	}
   320  	testCases := []testCase{
   321  		// Normal case
   322  		testCase{
   323  			ref:              "ref/changes/12/3412/2",
   324  			expectedCL:       3412,
   325  			expectedPatchSet: 2,
   326  		},
   327  		// Error cases
   328  		testCase{
   329  			ref:       "ref/123",
   330  			expectErr: true,
   331  		},
   332  		testCase{
   333  			ref:       "ref/changes/12/a/2",
   334  			expectErr: true,
   335  		},
   336  		testCase{
   337  			ref:       "ref/changes/12/3412/a",
   338  			expectErr: true,
   339  		},
   340  	}
   341  	for _, test := range testCases {
   342  		cl, patchset, err := ParseRefString(test.ref)
   343  		if test.expectErr {
   344  			if err == nil {
   345  				t.Fatalf("want errors, got: %v", err)
   346  			}
   347  		} else {
   348  			if err != nil {
   349  				t.Fatalf("want no errors, got: %v", err)
   350  			}
   351  			if cl != test.expectedCL {
   352  				t.Fatalf("want %v, got %v", test.expectedCL, cl)
   353  			}
   354  			if patchset != test.expectedPatchSet {
   355  				t.Fatalf("want %v, got %v", test.expectedPatchSet, patchset)
   356  			}
   357  		}
   358  	}
   359  }
   360  
   361  // TODO(jsimsa): Add a test for the hostCredentials function that
   362  // exercises the logic that reads the .netrc and git cookie files.