google_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. func TestConfigFromJSON(t *testing.T) {
  30. conf, err := ConfigFromJSON(webJSONKey, "scope1", "scope2")
  31. if err != nil {
  32. t.Error(err)
  33. }
  34. if got, want := conf.ClientID, "222-nprqovg5k43uum874cs9osjt2koe97g8.apps.googleusercontent.com"; got != want {
  35. t.Errorf("ClientID = %q; want %q", got, want)
  36. }
  37. if got, want := conf.ClientSecret, "3Oknc4jS_wA2r9i"; got != want {
  38. t.Errorf("ClientSecret = %q; want %q", got, want)
  39. }
  40. if got, want := conf.RedirectURL, "https://www.example.com/oauth2callback"; got != want {
  41. t.Errorf("RedictURL = %q; want %q", got, want)
  42. }
  43. if got, want := strings.Join(conf.Scopes, ","), "scope1,scope2"; got != want {
  44. t.Errorf("Scopes = %q; want %q", got, want)
  45. }
  46. if got, want := conf.Endpoint.AuthURL, "https://google.com/o/oauth2/auth"; got != want {
  47. t.Errorf("AuthURL = %q; want %q", got, want)
  48. }
  49. if got, want := conf.Endpoint.TokenURL, "https://google.com/o/oauth2/token"; got != want {
  50. t.Errorf("TokenURL = %q; want %q", got, want)
  51. }
  52. }
  53. func TestConfigFromJSON_Installed(t *testing.T) {
  54. conf, err := ConfigFromJSON(installedJSONKey)
  55. if err != nil {
  56. t.Error(err)
  57. }
  58. if got, want := conf.ClientID, "222-installed.apps.googleusercontent.com"; got != want {
  59. t.Errorf("ClientID = %q; want %q", got, want)
  60. }
  61. }