golang.org/x/oauth2@v0.18.0/google/sdk_test.go (about)

     1  // Copyright 2015 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 google
     6  
     7  import (
     8  	"reflect"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestSDKConfig(t *testing.T) {
    14  	sdkConfigPath = func() (string, error) {
    15  		return "testdata/gcloud", nil
    16  	}
    17  
    18  	tests := []struct {
    19  		account     string
    20  		accessToken string
    21  		err         bool
    22  	}{
    23  		{"", "bar_access_token", false},
    24  		{"foo@example.com", "foo_access_token", false},
    25  		{"bar@example.com", "bar_access_token", false},
    26  		{"baz@serviceaccount.example.com", "", true},
    27  	}
    28  	for _, tt := range tests {
    29  		c, err := NewSDKConfig(tt.account)
    30  		if got, want := err != nil, tt.err; got != want {
    31  			if !tt.err {
    32  				t.Errorf("got %v, want nil", err)
    33  			} else {
    34  				t.Errorf("got nil, want error")
    35  			}
    36  			continue
    37  		}
    38  		if err != nil {
    39  			continue
    40  		}
    41  		tok := c.initialToken
    42  		if tok == nil {
    43  			t.Errorf("got nil, want %q", tt.accessToken)
    44  			continue
    45  		}
    46  		if tok.AccessToken != tt.accessToken {
    47  			t.Errorf("got %q, want %q", tok.AccessToken, tt.accessToken)
    48  		}
    49  	}
    50  }
    51  
    52  func TestParseINI(t *testing.T) {
    53  	tests := []struct {
    54  		ini  string
    55  		want map[string]map[string]string
    56  	}{
    57  		{
    58  			`root = toor
    59  [foo]
    60  bar = hop
    61  ini = nin
    62  `,
    63  			map[string]map[string]string{
    64  				"":    {"root": "toor"},
    65  				"foo": {"bar": "hop", "ini": "nin"},
    66  			},
    67  		},
    68  		{
    69  			"\t  extra \t =  whitespace  \t\r\n \t [everywhere] \t \r\n  here \t =  \t there  \t \r\n",
    70  			map[string]map[string]string{
    71  				"":           {"extra": "whitespace"},
    72  				"everywhere": {"here": "there"},
    73  			},
    74  		},
    75  		{
    76  			`[empty]
    77  [section]
    78  empty=
    79  `,
    80  			map[string]map[string]string{
    81  				"":        {},
    82  				"empty":   {},
    83  				"section": {"empty": ""},
    84  			},
    85  		},
    86  		{
    87  			`ignore
    88  [invalid
    89  =stuff
    90  ;comment=true
    91  `,
    92  			map[string]map[string]string{
    93  				"": {},
    94  			},
    95  		},
    96  	}
    97  	for _, tt := range tests {
    98  		result, err := parseINI(strings.NewReader(tt.ini))
    99  		if err != nil {
   100  			t.Errorf("parseINI(%q) error %v, want: no error", tt.ini, err)
   101  			continue
   102  		}
   103  		if !reflect.DeepEqual(result, tt.want) {
   104  			t.Errorf("parseINI(%q) = %#v, want: %#v", tt.ini, result, tt.want)
   105  		}
   106  	}
   107  }