generic_oauth_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package social
  2. import (
  3. "github.com/grafana/grafana/pkg/infra/log"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "testing"
  6. )
  7. func TestSearchJSONForEmail(t *testing.T) {
  8. Convey("Given a generic OAuth provider", t, func() {
  9. provider := SocialGenericOAuth{
  10. SocialBase: &SocialBase{
  11. log: log.New("generic_oauth_test"),
  12. },
  13. }
  14. tests := []struct {
  15. Name string
  16. UserInfoJSONResponse []byte
  17. EmailAttributePath string
  18. ExpectedResult string
  19. }{
  20. {
  21. Name: "Given an invalid user info JSON response",
  22. UserInfoJSONResponse: []byte("{"),
  23. EmailAttributePath: "attributes.email",
  24. ExpectedResult: "",
  25. },
  26. {
  27. Name: "Given an empty user info JSON response and empty JMES path",
  28. UserInfoJSONResponse: []byte{},
  29. EmailAttributePath: "",
  30. ExpectedResult: "",
  31. },
  32. {
  33. Name: "Given an empty user info JSON response and valid JMES path",
  34. UserInfoJSONResponse: []byte{},
  35. EmailAttributePath: "attributes.email",
  36. ExpectedResult: "",
  37. },
  38. {
  39. Name: "Given a simple user info JSON response and valid JMES path",
  40. UserInfoJSONResponse: []byte(`{
  41. "attributes": {
  42. "email": "grafana@localhost"
  43. }
  44. }`),
  45. EmailAttributePath: "attributes.email",
  46. ExpectedResult: "grafana@localhost",
  47. },
  48. {
  49. Name: "Given a user info JSON response with e-mails array and valid JMES path",
  50. UserInfoJSONResponse: []byte(`{
  51. "attributes": {
  52. "emails": ["grafana@localhost", "admin@localhost"]
  53. }
  54. }`),
  55. EmailAttributePath: "attributes.emails[0]",
  56. ExpectedResult: "grafana@localhost",
  57. },
  58. {
  59. Name: "Given a nested user info JSON response and valid JMES path",
  60. UserInfoJSONResponse: []byte(`{
  61. "identities": [
  62. {
  63. "userId": "grafana@localhost"
  64. },
  65. {
  66. "userId": "admin@localhost"
  67. }
  68. ]
  69. }`),
  70. EmailAttributePath: "identities[0].userId",
  71. ExpectedResult: "grafana@localhost",
  72. },
  73. }
  74. for _, test := range tests {
  75. provider.emailAttributePath = test.EmailAttributePath
  76. Convey(test.Name, func() {
  77. actualResult := provider.searchJSONForEmail(test.UserInfoJSONResponse)
  78. So(actualResult, ShouldEqual, test.ExpectedResult)
  79. })
  80. }
  81. })
  82. }