mirror of https://github.com/grafana/grafana
OAuth: Support JMES path lookup when retrieving user email (#14683)
Add support for fetching e-mail with JMES path Signed-off-by: Bob Shannon <bobs@dropbox.com>pull/18716/head
parent
35b74a99a8
commit
056dbc7012
@ -0,0 +1,86 @@ |
||||
package social |
||||
|
||||
import ( |
||||
"github.com/grafana/grafana/pkg/infra/log" |
||||
. "github.com/smartystreets/goconvey/convey" |
||||
"testing" |
||||
) |
||||
|
||||
func TestSearchJSONForEmail(t *testing.T) { |
||||
Convey("Given a generic OAuth provider", t, func() { |
||||
provider := SocialGenericOAuth{ |
||||
SocialBase: &SocialBase{ |
||||
log: log.New("generic_oauth_test"), |
||||
}, |
||||
} |
||||
|
||||
tests := []struct { |
||||
Name string |
||||
UserInfoJSONResponse []byte |
||||
EmailAttributePath string |
||||
ExpectedResult string |
||||
}{ |
||||
{ |
||||
Name: "Given an invalid user info JSON response", |
||||
UserInfoJSONResponse: []byte("{"), |
||||
EmailAttributePath: "attributes.email", |
||||
ExpectedResult: "", |
||||
}, |
||||
{ |
||||
Name: "Given an empty user info JSON response and empty JMES path", |
||||
UserInfoJSONResponse: []byte{}, |
||||
EmailAttributePath: "", |
||||
ExpectedResult: "", |
||||
}, |
||||
{ |
||||
Name: "Given an empty user info JSON response and valid JMES path", |
||||
UserInfoJSONResponse: []byte{}, |
||||
EmailAttributePath: "attributes.email", |
||||
ExpectedResult: "", |
||||
}, |
||||
{ |
||||
Name: "Given a simple user info JSON response and valid JMES path", |
||||
UserInfoJSONResponse: []byte(`{ |
||||
"attributes": { |
||||
"email": "grafana@localhost" |
||||
} |
||||
}`), |
||||
EmailAttributePath: "attributes.email", |
||||
ExpectedResult: "grafana@localhost", |
||||
}, |
||||
{ |
||||
Name: "Given a user info JSON response with e-mails array and valid JMES path", |
||||
UserInfoJSONResponse: []byte(`{ |
||||
"attributes": { |
||||
"emails": ["grafana@localhost", "admin@localhost"] |
||||
} |
||||
}`), |
||||
EmailAttributePath: "attributes.emails[0]", |
||||
ExpectedResult: "grafana@localhost", |
||||
}, |
||||
{ |
||||
Name: "Given a nested user info JSON response and valid JMES path", |
||||
UserInfoJSONResponse: []byte(`{ |
||||
"identities": [ |
||||
{ |
||||
"userId": "grafana@localhost" |
||||
}, |
||||
{ |
||||
"userId": "admin@localhost" |
||||
} |
||||
] |
||||
}`), |
||||
EmailAttributePath: "identities[0].userId", |
||||
ExpectedResult: "grafana@localhost", |
||||
}, |
||||
} |
||||
|
||||
for _, test := range tests { |
||||
provider.emailAttributePath = test.EmailAttributePath |
||||
Convey(test.Name, func() { |
||||
actualResult := provider.searchJSONForEmail(test.UserInfoJSONResponse) |
||||
So(actualResult, ShouldEqual, test.ExpectedResult) |
||||
}) |
||||
} |
||||
}) |
||||
} |
||||
Loading…
Reference in new issue