Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

128 рядки
5.6 KiB

  1. // Copyright 2017 Google LLC
  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. "context"
  17. "testing"
  18. "github.com/google/go-cmp/cmp"
  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. // If both a file and TokenSource are passed, the file takes precedence
  39. // (existing behavior).
  40. // TODO(jba): make this an error?
  41. ds = &DialSettings{
  42. TokenSource: ts,
  43. CredentialsFile: "service-account.json",
  44. }
  45. got, err = Creds(ctx, ds)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. if cmp.Equal(got, want) {
  50. t.Error("got the same TokenSource back, wanted one from the JSON file")
  51. }
  52. // TODO(jba): find a way to test the call to google.DefaultTokenSource.
  53. }
  54. func TestDefaultServiceAccount(t *testing.T) {
  55. ctx := context.Background()
  56. // Load a valid JSON file. No way to really test the contents; we just
  57. // verify that there is no error.
  58. ds := &DialSettings{CredentialsFile: "service-account.json"}
  59. if _, err := Creds(ctx, ds); err != nil {
  60. t.Errorf("got %v, wanted no error", err)
  61. }
  62. // Load valid JSON. No way to really test the contents; we just
  63. // verify that there is no error.
  64. ds = &DialSettings{CredentialsJSON: []byte(validServiceAccountJSON)}
  65. if _, err := Creds(ctx, ds); err != nil {
  66. t.Errorf("got %v, wanted no error", err)
  67. }
  68. }
  69. func TestJWTWithAudience(t *testing.T) {
  70. ctx := context.Background()
  71. // Load a valid JSON file. No way to really test the contents; we just
  72. // verify that there is no error.
  73. ds := &DialSettings{CredentialsFile: "service-account.json", Audiences: []string{"foo"}}
  74. if _, err := Creds(ctx, ds); err != nil {
  75. t.Errorf("got %v, wanted no error", err)
  76. }
  77. // Load valid JSON. No way to really test the contents; we just
  78. // verify that there is no error.
  79. ds = &DialSettings{CredentialsJSON: []byte(validServiceAccountJSON), Audiences: []string{"foo"}}
  80. if _, err := Creds(ctx, ds); err != nil {
  81. t.Errorf("got %v, wanted no error", err)
  82. }
  83. }
  84. func TestOAuth(t *testing.T) {
  85. ctx := context.Background()
  86. // Load a valid JSON file. No way to really test the contents; we just
  87. // verify that there is no error.
  88. ds := &DialSettings{CredentialsFile: "service-account.json", Scopes: []string{"foo"}}
  89. if _, err := Creds(ctx, ds); err != nil {
  90. t.Errorf("got %v, wanted no error", err)
  91. }
  92. // Load valid JSON. No way to really test the contents; we just
  93. // verify that there is no error.
  94. ds = &DialSettings{CredentialsJSON: []byte(validServiceAccountJSON), Scopes: []string{"foo"}}
  95. if _, err := Creds(ctx, ds); err != nil {
  96. t.Errorf("got %v, wanted no error", err)
  97. }
  98. }
  99. const validServiceAccountJSON = `{
  100. "type": "service_account",
  101. "project_id": "dumba-504",
  102. "private_key_id": "adsfsdd",
  103. "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDczcmZh9zhcDjd\nc+a1bcNu06QA+PGwjAZygTy9ays8qTLi4J8zWSjxgb18ZgYnv1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qqaMt00qYyRiqfKKM8rJVjvMEwC5qI1OXzL2CIC3qJIW3wXl0PyQmjG\nYQpHuWFYuGUS4ZZGNB8O1rzLDyA3r3i6jLmaRG/09D0TM9joCr6HdtkBAoGBAIi6\n1p3nw/MeA1520uligiOMpAqIYTBr9e3QvWgeOwKRwjic09hN+T2SdAewTiP7Ov8l\n3dC3P4aWtQR6HzAnHQrJQkJhHNd3uKJjnpvC0iPsGfKl1ND5k5niu/hdZsZHarvq\n+lBqtzSP9yNStkv63dI3YliHoIIcijBdpp1u5qXBAoGBAOLrmvUKnx4NLEcauQ0e\naHndQ/6y4ie6knn1iJsJdYNJnYh9RKqDPTgpi8DbE2eb5JNkBQl0nSBMl74+MhVl\nMKBPVprkv7p3BdxoanpsncY14TUnzWIngkH21Rk0gqE3t/iJ7xnCTSv8qv3yYDj3\nL54zu6Y9GbjLgn6BtfhLHG4v\n-----END PRIVATE KEY-----\n",
  104. "client_email": "dumba-504@appspot.gserviceaccount.com",
  105. "client_id": "111",
  106. "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  107. "token_uri": "https://accounts.google.com/o/oauth2/token",
  108. "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  109. "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dumba-504%40appspot.gserviceaccount.com"
  110. }`