@@ -21,6 +21,7 @@ import (
21
21
"fmt"
22
22
"io/ioutil"
23
23
"log"
24
+ "net/http"
24
25
"os"
25
26
"strings"
26
27
"testing"
@@ -32,6 +33,11 @@ import (
32
33
"google.golang.org/api/transport"
33
34
)
34
35
36
+ const (
37
+ testProjectID = "mock-project-id"
38
+ testVersion = "test-version"
39
+ )
40
+
35
41
var (
36
42
testGetUserResponse []byte
37
43
testIDToken string
@@ -48,31 +54,22 @@ var (
48
54
AccessToken : "test.token" ,
49
55
}),
50
56
}
51
- testClock = & internal.MockClock {Timestamp : time .Now ()}
52
- testProjectID = "mock-project-id"
57
+ testClock = & internal.MockClock {Timestamp : time .Now ()}
53
58
)
54
59
55
60
func TestMain (m * testing.M ) {
56
61
var err error
57
62
testSigner , err = signerForTests (context .Background ())
58
- if err != nil {
59
- log .Fatalln (err )
60
- }
63
+ logFatal (err )
61
64
62
65
testIDTokenVerifier , err = idTokenVerifierForTests (context .Background ())
63
- if err != nil {
64
- log .Fatalln (err )
65
- }
66
+ logFatal (err )
66
67
67
68
testCookieVerifier , err = cookieVerifierForTests (context .Background ())
68
- if err != nil {
69
- log .Fatalln (err )
70
- }
69
+ logFatal (err )
71
70
72
71
testGetUserResponse , err = ioutil .ReadFile ("../testdata/get_user.json" )
73
- if err != nil {
74
- log .Fatalln (err )
75
- }
72
+ logFatal (err )
76
73
77
74
testIDToken = getIDToken (nil )
78
75
testSessionCookie = getSessionCookie (nil )
@@ -88,7 +85,7 @@ func TestNewClientWithServiceAccountCredentials(t *testing.T) {
88
85
Creds : creds ,
89
86
Opts : optsWithServiceAcct ,
90
87
ProjectID : creds .ProjectID ,
91
- Version : "test-version" ,
88
+ Version : testVersion ,
92
89
})
93
90
if err != nil {
94
91
t .Fatal (err )
@@ -103,19 +100,18 @@ func TestNewClientWithServiceAccountCredentials(t *testing.T) {
103
100
if err := checkCookieVerifier (client .cookieVerifier , creds .ProjectID ); err != nil {
104
101
t .Errorf ("NewClient().cookieVerifier: %v" , err )
105
102
}
103
+ if err := checkUserManagementClient (client , creds .ProjectID ); err != nil {
104
+ t .Errorf ("NewClient().userManagementClient: %v" , err )
105
+ }
106
106
if client .clock != internal .SystemClock {
107
107
t .Errorf ("NewClient().clock = %v; want = SystemClock" , client .clock )
108
108
}
109
- wantVersion := "Go/Admin/test-version"
110
- if client .version != wantVersion {
111
- t .Errorf ("NewClient().version = %q; want = %q" , client .version , wantVersion )
112
- }
113
109
}
114
110
115
111
func TestNewClientWithoutCredentials (t * testing.T ) {
116
112
conf := & internal.AuthConfig {
117
113
Opts : optsWithTokenSource ,
118
- Version : "test-version" ,
114
+ Version : testVersion ,
119
115
}
120
116
client , err := NewClient (context .Background (), conf )
121
117
if err != nil {
@@ -131,20 +127,19 @@ func TestNewClientWithoutCredentials(t *testing.T) {
131
127
if err := checkCookieVerifier (client .cookieVerifier , "" ); err != nil {
132
128
t .Errorf ("NewClient().cookieVerifier: %v" , err )
133
129
}
130
+ if err := checkUserManagementClient (client , "" ); err != nil {
131
+ t .Errorf ("NewClient().userManagementClient: %v" , err )
132
+ }
134
133
if client .clock != internal .SystemClock {
135
134
t .Errorf ("NewClient().clock = %v; want = SystemClock" , client .clock )
136
135
}
137
- wantVersion := "Go/Admin/test-version"
138
- if client .version != wantVersion {
139
- t .Errorf ("NewClient().version = %q; want = %q" , client .version , wantVersion )
140
- }
141
136
}
142
137
143
138
func TestNewClientWithServiceAccountID (t * testing.T ) {
144
139
conf := & internal.AuthConfig {
145
140
Opts : optsWithTokenSource ,
146
141
ServiceAccountID : "explicit-service-account" ,
147
- Version : "test-version" ,
142
+ Version : testVersion ,
148
143
}
149
144
client , err := NewClient (context .Background (), conf )
150
145
if err != nil {
@@ -160,13 +155,12 @@ func TestNewClientWithServiceAccountID(t *testing.T) {
160
155
if err := checkCookieVerifier (client .cookieVerifier , "" ); err != nil {
161
156
t .Errorf ("NewClient().cookieVerifier: %v" , err )
162
157
}
158
+ if err := checkUserManagementClient (client , "" ); err != nil {
159
+ t .Errorf ("NewClient().userManagementClient: %v" , err )
160
+ }
163
161
if client .clock != internal .SystemClock {
164
162
t .Errorf ("NewClient().clock = %v; want = SystemClock" , client .clock )
165
163
}
166
- wantVersion := "Go/Admin/test-version"
167
- if client .version != wantVersion {
168
- t .Errorf ("NewClient().version = %q; want = %q" , client .version , wantVersion )
169
- }
170
164
171
165
email , err := client .signer .Email (context .Background ())
172
166
if email != conf .ServiceAccountID || err != nil {
@@ -184,7 +178,7 @@ func TestNewClientWithUserCredentials(t *testing.T) {
184
178
conf := & internal.AuthConfig {
185
179
Creds : creds ,
186
180
Opts : []option.ClientOption {option .WithCredentials (creds )},
187
- Version : "test-version" ,
181
+ Version : testVersion ,
188
182
}
189
183
client , err := NewClient (context .Background (), conf )
190
184
if err != nil {
@@ -200,13 +194,12 @@ func TestNewClientWithUserCredentials(t *testing.T) {
200
194
if err := checkCookieVerifier (client .cookieVerifier , "" ); err != nil {
201
195
t .Errorf ("NewClient().cookieVerifier: %v" , err )
202
196
}
197
+ if err := checkUserManagementClient (client , "" ); err != nil {
198
+ t .Errorf ("NewClient().userManagementClient: %v" , err )
199
+ }
203
200
if client .clock != internal .SystemClock {
204
201
t .Errorf ("NewClient().clock = %v; want = SystemClock" , client .clock )
205
202
}
206
- wantVersion := "Go/Admin/test-version"
207
- if client .version != wantVersion {
208
- t .Errorf ("NewClient().version = %q; want = %q" , client .version , wantVersion )
209
- }
210
203
}
211
204
212
205
func TestNewClientWithMalformedCredentials (t * testing.T ) {
@@ -917,9 +910,7 @@ func getIDTokenWithKid(kid string, p mockIDTokenPayload) string {
917
910
payload : pCopy ,
918
911
}
919
912
token , err := info .Token (context .Background (), testSigner )
920
- if err != nil {
921
- log .Fatalln (err )
922
- }
913
+ logFatal (err )
923
914
return token
924
915
}
925
916
@@ -949,6 +940,31 @@ func checkCookieVerifier(tv *tokenVerifier, projectID string) error {
949
940
return nil
950
941
}
951
942
943
+ func checkUserManagementClient (client * Client , wantProjectID string ) error {
944
+ if client .baseURL != idToolkitV1Endpoint {
945
+ return fmt .Errorf ("baseURL = %q; want = %q" , client .baseURL , idToolkitV1Endpoint )
946
+ }
947
+ if client .projectID != wantProjectID {
948
+ return fmt .Errorf ("projectID = %q; want = %q" , client .projectID , wantProjectID )
949
+ }
950
+
951
+ req , err := http .NewRequest (http .MethodGet , "https://firebase.google.com" , nil )
952
+ if err != nil {
953
+ return err
954
+ }
955
+
956
+ for _ , opt := range client .httpClient .Opts {
957
+ opt (req )
958
+ }
959
+ version := req .Header .Get ("X-Client-Version" )
960
+ wantVersion := fmt .Sprintf ("Go/Admin/%s" , testVersion )
961
+ if version != wantVersion {
962
+ return fmt .Errorf ("version = %q; want = %q" , version , wantVersion )
963
+ }
964
+
965
+ return nil
966
+ }
967
+
952
968
func verifyCustomToken (ctx context.Context , token string , expected map [string ]interface {}, t * testing.T ) {
953
969
if err := testIDTokenVerifier .verifySignature (ctx , token ); err != nil {
954
970
t .Fatal (err )
@@ -996,3 +1012,9 @@ func verifyCustomToken(ctx context.Context, token string, expected map[string]in
996
1012
}
997
1013
}
998
1014
}
1015
+
1016
+ func logFatal (err error ) {
1017
+ if err != nil {
1018
+ log .Fatal (err )
1019
+ }
1020
+ }
0 commit comments