...

Source file src/golang.org/x/oauth2/google/downscope/downscoping_test.go

Documentation: golang.org/x/oauth2/google/downscope

     1  // Copyright 2021 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 downscope
     6  
     7  import (
     8  	"context"
     9  	"io/ioutil"
    10  	"net/http"
    11  	"net/http/httptest"
    12  	"testing"
    13  
    14  	"golang.org/x/oauth2"
    15  )
    16  
    17  var (
    18  	standardReqBody  = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&options=%7B%22accessBoundary%22%3A%7B%22accessBoundaryRules%22%3A%5B%7B%22availableResource%22%3A%22test1%22%2C%22availablePermissions%22%3A%5B%22Perm1%22%2C%22Perm2%22%5D%7D%5D%7D%7D&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&subject_token=Mellon&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token"
    19  	standardRespBody = `{"access_token":"Open Sesame","expires_in":432,"issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer"}`
    20  )
    21  
    22  func Test_DownscopedTokenSource(t *testing.T) {
    23  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    24  		if r.Method != "POST" {
    25  			t.Errorf("Unexpected request method, %v is found", r.Method)
    26  		}
    27  		if r.URL.String() != "/" {
    28  			t.Errorf("Unexpected request URL, %v is found", r.URL)
    29  		}
    30  		body, err := ioutil.ReadAll(r.Body)
    31  		if err != nil {
    32  			t.Fatalf("Failed to read request body: %v", err)
    33  		}
    34  		if got, want := string(body), standardReqBody; got != want {
    35  			t.Errorf("Unexpected exchange payload: got %v but want %v,", got, want)
    36  		}
    37  		w.Header().Set("Content-Type", "application/json")
    38  		w.Write([]byte(standardRespBody))
    39  
    40  	}))
    41  	myTok := oauth2.Token{AccessToken: "Mellon"}
    42  	tmpSrc := oauth2.StaticTokenSource(&myTok)
    43  	rules := []AccessBoundaryRule{
    44  		{
    45  			AvailableResource:    "test1",
    46  			AvailablePermissions: []string{"Perm1", "Perm2"},
    47  		},
    48  	}
    49  	dts := downscopingTokenSource{
    50  		ctx: context.Background(),
    51  		config: DownscopingConfig{
    52  			RootSource: tmpSrc,
    53  			Rules:      rules,
    54  		},
    55  		identityBindingEndpoint: ts.URL,
    56  	}
    57  	_, err := dts.Token()
    58  	if err != nil {
    59  		t.Fatalf("NewDownscopedTokenSource failed with error: %v", err)
    60  	}
    61  }
    62  
    63  func Test_DownscopingConfig(t *testing.T) {
    64  	tests := []struct {
    65  		universeDomain string
    66  		want           string
    67  	}{
    68  		{"", "https://sts.googleapis.com/v1/token"},
    69  		{"googleapis.com", "https://sts.googleapis.com/v1/token"},
    70  		{"example.com", "https://sts.example.com/v1/token"},
    71  	}
    72  	for _, tt := range tests {
    73  		c := DownscopingConfig{
    74  			UniverseDomain: tt.universeDomain,
    75  		}
    76  		if got := c.identityBindingEndpoint(); got != tt.want {
    77  			t.Errorf("got %q, want %q", got, tt.want)
    78  		}
    79  	}
    80  }
    81  

View as plain text