You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

88 lines
2.6 KiB

  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package internal
  15. import (
  16. "testing"
  17. "github.com/google/go-cmp/cmp"
  18. "golang.org/x/net/context"
  19. "golang.org/x/oauth2"
  20. "golang.org/x/oauth2/google"
  21. )
  22. type dummyTokenSource struct {
  23. oauth2.TokenSource
  24. }
  25. func TestTokenSource(t *testing.T) {
  26. ctx := context.Background()
  27. // Pass in a TokenSource, get it back.
  28. ts := &dummyTokenSource{}
  29. ds := &DialSettings{TokenSource: ts}
  30. got, err := Creds(ctx, ds)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. want := &google.DefaultCredentials{TokenSource: ts}
  35. if !cmp.Equal(got, want) {
  36. t.Error("did not get the same TokenSource back")
  37. }
  38. // Load a valid JSON file. No way to really test the contents; we just
  39. // verify that there is no error.
  40. ds = &DialSettings{CredentialsFile: "service-account.json"}
  41. if _, err := Creds(ctx, ds); err != nil {
  42. t.Errorf("got %v, wanted no error", err)
  43. }
  44. // If both a file and TokenSource are passed, the file takes precedence
  45. // (existing behavior).
  46. // TODO(jba): make this an error?
  47. ds = &DialSettings{
  48. TokenSource: ts,
  49. CredentialsFile: "service-account.json",
  50. }
  51. got, err = Creds(ctx, ds)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if cmp.Equal(got, want) {
  56. t.Error("got the same TokenSource back, wanted one from the JSON file")
  57. }
  58. // TODO(jba): find a way to test the call to google.DefaultTokenSource.
  59. }
  60. const validRefeshTokenJSON = `{
  61. "client_id": "764-aaaaa.apps.googleusercontent.com",
  62. "client_secret": "d-988888888",
  63. "refresh_token": "1/88888aaaaaaaaa",
  64. "type": "authorized_user"
  65. }`
  66. const validServiceAccountJSON = `{
  67. "type": "service_account",
  68. "project_id": "dumba-504",
  69. "private_key_id": "adsfsdd",
  70. "private_key": "-----BEGIN PRIVATE KEY-----\n\n-----END PRIVATE KEY-----\n",
  71. "client_email": "dumba-504@appspot.gserviceaccount.com",
  72. "client_id": "111",
  73. "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  74. "token_uri": "https://accounts.google.com/o/oauth2/token",
  75. "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  76. "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dumba-504%40appspot.gserviceaccount.com"
  77. }`