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.
 
 
 

117 lines
4.1 KiB

  1. // Copyright 2015 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. package google
  5. import (
  6. "strings"
  7. "testing"
  8. )
  9. var webJSONKey = []byte(`
  10. {
  11. "web": {
  12. "auth_uri": "https://google.com/o/oauth2/auth",
  13. "client_secret": "3Oknc4jS_wA2r9i",
  14. "token_uri": "https://google.com/o/oauth2/token",
  15. "client_email": "222-nprqovg5k43uum874cs9osjt2koe97g8@developer.gserviceaccount.com",
  16. "redirect_uris": ["https://www.example.com/oauth2callback"],
  17. "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/222-nprqovg5k43uum874cs9osjt2koe97g8@developer.gserviceaccount.com",
  18. "client_id": "222-nprqovg5k43uum874cs9osjt2koe97g8.apps.googleusercontent.com",
  19. "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  20. "javascript_origins": ["https://www.example.com"]
  21. }
  22. }`)
  23. var installedJSONKey = []byte(`{
  24. "installed": {
  25. "client_id": "222-installed.apps.googleusercontent.com",
  26. "redirect_uris": ["https://www.example.com/oauth2callback"]
  27. }
  28. }`)
  29. var jwtJSONKey = []byte(`{
  30. "private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
  31. "private_key": "super secret key",
  32. "client_email": "gopher@developer.gserviceaccount.com",
  33. "client_id": "gopher.apps.googleusercontent.com",
  34. "token_uri": "https://accounts.google.com/o/gophers/token",
  35. "type": "service_account"
  36. }`)
  37. var jwtJSONKeyNoTokenURL = []byte(`{
  38. "private_key_id": "268f54e43a1af97cfc71731688434f45aca15c8b",
  39. "private_key": "super secret key",
  40. "client_email": "gopher@developer.gserviceaccount.com",
  41. "client_id": "gopher.apps.googleusercontent.com",
  42. "type": "service_account"
  43. }`)
  44. func TestConfigFromJSON(t *testing.T) {
  45. conf, err := ConfigFromJSON(webJSONKey, "scope1", "scope2")
  46. if err != nil {
  47. t.Error(err)
  48. }
  49. if got, want := conf.ClientID, "222-nprqovg5k43uum874cs9osjt2koe97g8.apps.googleusercontent.com"; got != want {
  50. t.Errorf("ClientID = %q; want %q", got, want)
  51. }
  52. if got, want := conf.ClientSecret, "3Oknc4jS_wA2r9i"; got != want {
  53. t.Errorf("ClientSecret = %q; want %q", got, want)
  54. }
  55. if got, want := conf.RedirectURL, "https://www.example.com/oauth2callback"; got != want {
  56. t.Errorf("RedictURL = %q; want %q", got, want)
  57. }
  58. if got, want := strings.Join(conf.Scopes, ","), "scope1,scope2"; got != want {
  59. t.Errorf("Scopes = %q; want %q", got, want)
  60. }
  61. if got, want := conf.Endpoint.AuthURL, "https://google.com/o/oauth2/auth"; got != want {
  62. t.Errorf("AuthURL = %q; want %q", got, want)
  63. }
  64. if got, want := conf.Endpoint.TokenURL, "https://google.com/o/oauth2/token"; got != want {
  65. t.Errorf("TokenURL = %q; want %q", got, want)
  66. }
  67. }
  68. func TestConfigFromJSON_Installed(t *testing.T) {
  69. conf, err := ConfigFromJSON(installedJSONKey)
  70. if err != nil {
  71. t.Error(err)
  72. }
  73. if got, want := conf.ClientID, "222-installed.apps.googleusercontent.com"; got != want {
  74. t.Errorf("ClientID = %q; want %q", got, want)
  75. }
  76. }
  77. func TestJWTConfigFromJSON(t *testing.T) {
  78. conf, err := JWTConfigFromJSON(jwtJSONKey, "scope1", "scope2")
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if got, want := conf.Email, "gopher@developer.gserviceaccount.com"; got != want {
  83. t.Errorf("Email = %q, want %q", got, want)
  84. }
  85. if got, want := string(conf.PrivateKey), "super secret key"; got != want {
  86. t.Errorf("PrivateKey = %q, want %q", got, want)
  87. }
  88. if got, want := conf.PrivateKeyID, "268f54e43a1af97cfc71731688434f45aca15c8b"; got != want {
  89. t.Errorf("PrivateKeyID = %q, want %q", got, want)
  90. }
  91. if got, want := strings.Join(conf.Scopes, ","), "scope1,scope2"; got != want {
  92. t.Errorf("Scopes = %q; want %q", got, want)
  93. }
  94. if got, want := conf.TokenURL, "https://accounts.google.com/o/gophers/token"; got != want {
  95. t.Errorf("TokenURL = %q; want %q", got, want)
  96. }
  97. }
  98. func TestJWTConfigFromJSONNoTokenURL(t *testing.T) {
  99. conf, err := JWTConfigFromJSON(jwtJSONKeyNoTokenURL, "scope1", "scope2")
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. if got, want := conf.TokenURL, "https://oauth2.googleapis.com/token"; got != want {
  104. t.Errorf("TokenURL = %q; want %q", got, want)
  105. }
  106. }