...

Source file src/golang.org/x/oauth2/google/google_test.go

Documentation: golang.org/x/oauth2/google

     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  	"net/http"
     9  	"net/http/httptest"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  var webJSONKey = []byte(`
    15  {
    16      "web": {
    17          "auth_uri": "https://google.com/o/oauth2/auth",
    18          "client_secret": "3Oknc4jS_wA2r9i",
    19          "token_uri": "https://google.com/o/oauth2/token",
    20          "client_email": "222-nprqovg5k43uum874cs9osjt2koe97g8@developer.gserviceaccount.com",
    21          "redirect_uris": ["https://www.example.com/oauth2callback"],
    22          "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/222-nprqovg5k43uum874cs9osjt2koe97g8@developer.gserviceaccount.com",
    23          "client_id": "222-nprqovg5k43uum874cs9osjt2koe97g8.apps.googleusercontent.com",
    24          "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    25          "javascript_origins": ["https://www.example.com"]
    26      }
    27  }`)
    28  
    29  var installedJSONKey = []byte(`{
    30    "installed": {
    31        "client_id": "222-installed.apps.googleusercontent.com",
    32        "redirect_uris": ["https://www.example.com/oauth2callback"]
    33      }
    34  }`)
    35  
    36  var jwtJSONKey = []byte(`{
    37    "private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
    38    "private_key": "super secret key",
    39    "client_email": "gopher@developer.gserviceaccount.com",
    40    "client_id": "gopher.apps.googleusercontent.com",
    41    "token_uri": "https://accounts.google.com/o/gophers/token",
    42    "type": "service_account",
    43    "audience": "https://testservice.googleapis.com/"
    44  }`)
    45  
    46  var jwtJSONKeyNoTokenURL = []byte(`{
    47    "private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
    48    "private_key": "super secret key",
    49    "client_email": "gopher@developer.gserviceaccount.com",
    50    "client_id": "gopher.apps.googleusercontent.com",
    51    "type": "service_account"
    52  }`)
    53  
    54  var jwtJSONKeyNoAudience = []byte(`{
    55    "private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
    56    "private_key": "super secret key",
    57    "client_email": "gopher@developer.gserviceaccount.com",
    58    "client_id": "gopher.apps.googleusercontent.com",
    59    "token_uri": "https://accounts.google.com/o/gophers/token",
    60    "type": "service_account"
    61  }`)
    62  
    63  func TestConfigFromJSON(t *testing.T) {
    64  	conf, err := ConfigFromJSON(webJSONKey, "scope1", "scope2")
    65  	if err != nil {
    66  		t.Error(err)
    67  	}
    68  	if got, want := conf.ClientID, "222-nprqovg5k43uum874cs9osjt2koe97g8.apps.googleusercontent.com"; got != want {
    69  		t.Errorf("ClientID = %q; want %q", got, want)
    70  	}
    71  	if got, want := conf.ClientSecret, "3Oknc4jS_wA2r9i"; got != want {
    72  		t.Errorf("ClientSecret = %q; want %q", got, want)
    73  	}
    74  	if got, want := conf.RedirectURL, "https://www.example.com/oauth2callback"; got != want {
    75  		t.Errorf("RedictURL = %q; want %q", got, want)
    76  	}
    77  	if got, want := strings.Join(conf.Scopes, ","), "scope1,scope2"; got != want {
    78  		t.Errorf("Scopes = %q; want %q", got, want)
    79  	}
    80  	if got, want := conf.Endpoint.AuthURL, "https://google.com/o/oauth2/auth"; got != want {
    81  		t.Errorf("AuthURL = %q; want %q", got, want)
    82  	}
    83  	if got, want := conf.Endpoint.TokenURL, "https://google.com/o/oauth2/token"; got != want {
    84  		t.Errorf("TokenURL = %q; want %q", got, want)
    85  	}
    86  }
    87  
    88  func TestConfigFromJSON_Installed(t *testing.T) {
    89  	conf, err := ConfigFromJSON(installedJSONKey)
    90  	if err != nil {
    91  		t.Error(err)
    92  	}
    93  	if got, want := conf.ClientID, "222-installed.apps.googleusercontent.com"; got != want {
    94  		t.Errorf("ClientID = %q; want %q", got, want)
    95  	}
    96  }
    97  
    98  func TestJWTConfigFromJSON(t *testing.T) {
    99  	conf, err := JWTConfigFromJSON(jwtJSONKey, "scope1", "scope2")
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  	if got, want := conf.Email, "gopher@developer.gserviceaccount.com"; got != want {
   104  		t.Errorf("Email = %q, want %q", got, want)
   105  	}
   106  	if got, want := string(conf.PrivateKey), "super secret key"; got != want {
   107  		t.Errorf("PrivateKey = %q, want %q", got, want)
   108  	}
   109  	if got, want := conf.PrivateKeyID, "268f54e43a1af97cfc71731688434f45aca15c8b"; got != want {
   110  		t.Errorf("PrivateKeyID = %q, want %q", got, want)
   111  	}
   112  	if got, want := strings.Join(conf.Scopes, ","), "scope1,scope2"; got != want {
   113  		t.Errorf("Scopes = %q; want %q", got, want)
   114  	}
   115  	if got, want := conf.TokenURL, "https://accounts.google.com/o/gophers/token"; got != want {
   116  		t.Errorf("TokenURL = %q; want %q", got, want)
   117  	}
   118  	if got, want := conf.Audience, "https://testservice.googleapis.com/"; got != want {
   119  		t.Errorf("Audience = %q; want %q", got, want)
   120  	}
   121  }
   122  
   123  func TestJWTConfigFromJSONNoTokenURL(t *testing.T) {
   124  	conf, err := JWTConfigFromJSON(jwtJSONKeyNoTokenURL, "scope1", "scope2")
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  	if got, want := conf.TokenURL, "https://oauth2.googleapis.com/token"; got != want {
   129  		t.Errorf("TokenURL = %q; want %q", got, want)
   130  	}
   131  }
   132  
   133  func TestJWTConfigFromJSONNoAudience(t *testing.T) {
   134  	conf, err := JWTConfigFromJSON(jwtJSONKeyNoAudience, "scope1", "scope2")
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  	if got, want := conf.Audience, ""; got != want {
   139  		t.Errorf("Audience = %q; want %q", got, want)
   140  	}
   141  }
   142  
   143  func TestComputeTokenSource(t *testing.T) {
   144  	tokenPath := "/computeMetadata/v1/instance/service-accounts/default/token"
   145  	tokenResponseBody := `{"access_token":"Sample.Access.Token","token_type":"Bearer","expires_in":3600}`
   146  	s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   147  		if r.URL.Path != tokenPath {
   148  			t.Errorf("got %s, want %s", r.URL.Path, tokenPath)
   149  		}
   150  		w.Write([]byte(tokenResponseBody))
   151  	}))
   152  	defer s.Close()
   153  	t.Setenv("GCE_METADATA_HOST", strings.TrimPrefix(s.URL, "http://"))
   154  	ts := ComputeTokenSource("")
   155  	_, err := ts.Token()
   156  	if err != nil {
   157  		t.Errorf("ts.Token() = %v", err)
   158  	}
   159  }
   160  

View as plain text