File: default_test.go

package info (click to toggle)
golang-golang-x-oauth2 0.15.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 748 kB
  • sloc: makefile: 15
file content (124 lines) | stat: -rw-r--r-- 4,000 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package google

import (
	"context"
	"testing"
)

var saJSONJWT = []byte(`{
  "type": "service_account",
  "project_id": "fake_project",
  "private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
  "private_key": "super secret key",
  "client_email": "gopher@developer.gserviceaccount.com",
  "client_id": "gopher.apps.googleusercontent.com",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gopher%40fake_project.iam.gserviceaccount.com"
}`)

var saJSONJWTUniverseDomain = []byte(`{
  "type": "service_account",
  "project_id": "fake_project",
  "universe_domain": "example.com",
  "private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
  "private_key": "super secret key",
  "client_email": "gopher@developer.gserviceaccount.com",
  "client_id": "gopher.apps.googleusercontent.com",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gopher%40fake_project.iam.gserviceaccount.com"
}`)

var userJSON = []byte(`{
  "client_id": "abc123.apps.googleusercontent.com",
  "client_secret": "shh",
  "refresh_token": "refreshing",
  "type": "authorized_user",
  "quota_project_id": "fake_project2"
}`)

var userJSONUniverseDomain = []byte(`{
  "client_id": "abc123.apps.googleusercontent.com",
  "client_secret": "shh",
  "refresh_token": "refreshing",
  "type": "authorized_user",
  "quota_project_id": "fake_project2",
  "universe_domain": "example.com"
}`)

func TestCredentialsFromJSONWithParams_SA(t *testing.T) {
	ctx := context.Background()
	scope := "https://www.googleapis.com/auth/cloud-platform"
	params := CredentialsParams{
		Scopes: []string{scope},
	}
	creds, err := CredentialsFromJSONWithParams(ctx, saJSONJWT, params)
	if err != nil {
		t.Fatal(err)
	}

	if want := "fake_project"; creds.ProjectID != want {
		t.Fatalf("got %q, want %q", creds.ProjectID, want)
	}
	if want := "googleapis.com"; creds.UniverseDomain() != want {
		t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
	}
}

func TestCredentialsFromJSONWithParams_SA_UniverseDomain(t *testing.T) {
	ctx := context.Background()
	scope := "https://www.googleapis.com/auth/cloud-platform"
	params := CredentialsParams{
		Scopes: []string{scope},
	}
	creds, err := CredentialsFromJSONWithParams(ctx, saJSONJWTUniverseDomain, params)
	if err != nil {
		t.Fatal(err)
	}

	if want := "fake_project"; creds.ProjectID != want {
		t.Fatalf("got %q, want %q", creds.ProjectID, want)
	}
	if want := "example.com"; creds.UniverseDomain() != want {
		t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
	}
}

func TestCredentialsFromJSONWithParams_User(t *testing.T) {
	ctx := context.Background()
	scope := "https://www.googleapis.com/auth/cloud-platform"
	params := CredentialsParams{
		Scopes: []string{scope},
	}
	creds, err := CredentialsFromJSONWithParams(ctx, userJSON, params)
	if err != nil {
		t.Fatal(err)
	}

	if want := "googleapis.com"; creds.UniverseDomain() != want {
		t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
	}
}

func TestCredentialsFromJSONWithParams_User_UniverseDomain(t *testing.T) {
	ctx := context.Background()
	scope := "https://www.googleapis.com/auth/cloud-platform"
	params := CredentialsParams{
		Scopes: []string{scope},
	}
	creds, err := CredentialsFromJSONWithParams(ctx, userJSONUniverseDomain, params)
	if err != nil {
		t.Fatal(err)
	}

	if want := "googleapis.com"; creds.UniverseDomain() != want {
		t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
	}
}