API: Rewrite tests from goconvey (#29091)
* API: Rewrite tests from goconvey Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Fix test Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Fix tests Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
@@ -9,11 +9,11 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/jwt"
|
||||
)
|
||||
@@ -22,238 +22,245 @@ var (
|
||||
token map[string]interface{}
|
||||
)
|
||||
|
||||
func TestAccessToken(t *testing.T) {
|
||||
Convey("Plugin with JWT token auth route", t, func() {
|
||||
pluginRoute := &plugins.AppPluginRoute{
|
||||
Path: "pathwithjwttoken1",
|
||||
URL: "https://api.jwt.io/some/path",
|
||||
Method: "GET",
|
||||
JwtTokenAuth: &plugins.JwtTokenAuth{
|
||||
Url: "https://login.server.com/{{.JsonData.tenantId}}/oauth2/token",
|
||||
Scopes: []string{
|
||||
"https://www.testapi.com/auth/monitoring.read",
|
||||
"https://www.testapi.com/auth/cloudplatformprojects.readonly",
|
||||
},
|
||||
Params: map[string]string{
|
||||
"token_uri": "{{.JsonData.tokenUri}}",
|
||||
"client_email": "{{.JsonData.clientEmail}}",
|
||||
"private_key": "{{.SecureJsonData.privateKey}}",
|
||||
},
|
||||
func TestAccessToken_pluginWithJWTTokenAuthRoute(t *testing.T) {
|
||||
pluginRoute := &plugins.AppPluginRoute{
|
||||
Path: "pathwithjwttoken1",
|
||||
URL: "https://api.jwt.io/some/path",
|
||||
Method: "GET",
|
||||
JwtTokenAuth: &plugins.JwtTokenAuth{
|
||||
Url: "https://login.server.com/{{.JsonData.tenantId}}/oauth2/token",
|
||||
Scopes: []string{
|
||||
"https://www.testapi.com/auth/monitoring.read",
|
||||
"https://www.testapi.com/auth/cloudplatformprojects.readonly",
|
||||
},
|
||||
}
|
||||
|
||||
templateData := templateData{
|
||||
JsonData: map[string]interface{}{
|
||||
"clientEmail": "test@test.com",
|
||||
"tokenUri": "login.url.com/token",
|
||||
Params: map[string]string{
|
||||
"token_uri": "{{.JsonData.tokenUri}}",
|
||||
"client_email": "{{.JsonData.clientEmail}}",
|
||||
"private_key": "{{.SecureJsonData.privateKey}}",
|
||||
},
|
||||
SecureJsonData: map[string]string{
|
||||
"privateKey": "testkey",
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
ds := &models.DataSource{Id: 1, Version: 2}
|
||||
templateData := templateData{
|
||||
JsonData: map[string]interface{}{
|
||||
"clientEmail": "test@test.com",
|
||||
"tokenUri": "login.url.com/token",
|
||||
},
|
||||
SecureJsonData: map[string]string{
|
||||
"privateKey": "testkey",
|
||||
},
|
||||
}
|
||||
|
||||
Convey("should fetch token using jwt private key", func() {
|
||||
getTokenSource = func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
|
||||
return &oauth2.Token{AccessToken: "abc"}, nil
|
||||
}
|
||||
provider := newAccessTokenProvider(ds, pluginRoute)
|
||||
token, err := provider.getJwtAccessToken(context.Background(), templateData)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(token, ShouldEqual, "abc")
|
||||
setUp := func(t *testing.T, fn func(*jwt.Config, context.Context) (*oauth2.Token, error)) {
|
||||
origFn := getTokenSource
|
||||
t.Cleanup(func() {
|
||||
getTokenSource = origFn
|
||||
})
|
||||
|
||||
Convey("should set jwt config values", func() {
|
||||
getTokenSource = func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
|
||||
So(conf.Email, ShouldEqual, "test@test.com")
|
||||
So(conf.PrivateKey, ShouldResemble, []byte("testkey"))
|
||||
So(len(conf.Scopes), ShouldEqual, 2)
|
||||
So(conf.Scopes[0], ShouldEqual, "https://www.testapi.com/auth/monitoring.read")
|
||||
So(conf.Scopes[1], ShouldEqual, "https://www.testapi.com/auth/cloudplatformprojects.readonly")
|
||||
So(conf.TokenURL, ShouldEqual, "login.url.com/token")
|
||||
getTokenSource = fn
|
||||
}
|
||||
|
||||
return &oauth2.Token{AccessToken: "abc"}, nil
|
||||
}
|
||||
ds := &models.DataSource{Id: 1, Version: 2}
|
||||
|
||||
provider := newAccessTokenProvider(ds, pluginRoute)
|
||||
_, err := provider.getJwtAccessToken(context.Background(), templateData)
|
||||
So(err, ShouldBeNil)
|
||||
t.Run("should fetch token using JWT private key", func(t *testing.T) {
|
||||
setUp(t, func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
|
||||
return &oauth2.Token{AccessToken: "abc"}, nil
|
||||
})
|
||||
provider := newAccessTokenProvider(ds, pluginRoute)
|
||||
token, err := provider.getJwtAccessToken(context.Background(), templateData)
|
||||
require.NoError(t, err)
|
||||
|
||||
Convey("should use cached token on second call", func() {
|
||||
getTokenSource = func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
|
||||
return &oauth2.Token{
|
||||
AccessToken: "abc",
|
||||
Expiry: time.Now().Add(1 * time.Minute)}, nil
|
||||
}
|
||||
provider := newAccessTokenProvider(ds, pluginRoute)
|
||||
token1, err := provider.getJwtAccessToken(context.Background(), templateData)
|
||||
So(err, ShouldBeNil)
|
||||
So(token1, ShouldEqual, "abc")
|
||||
|
||||
getTokenSource = func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
|
||||
return &oauth2.Token{AccessToken: "error: cache not used"}, nil
|
||||
}
|
||||
token2, err := provider.getJwtAccessToken(context.Background(), templateData)
|
||||
So(err, ShouldBeNil)
|
||||
So(token2, ShouldEqual, "abc")
|
||||
})
|
||||
assert.Equal(t, "abc", token)
|
||||
})
|
||||
|
||||
Convey("Plugin with token auth route", t, func() {
|
||||
apiHandler := http.NewServeMux()
|
||||
server := httptest.NewServer(apiHandler)
|
||||
defer server.Close()
|
||||
t.Run("should set JWT config values", func(t *testing.T) {
|
||||
setUp(t, func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
|
||||
assert.Equal(t, "test@test.com", conf.Email)
|
||||
assert.Equal(t, []byte("testkey"), conf.PrivateKey)
|
||||
assert.Equal(t, 2, len(conf.Scopes))
|
||||
assert.Equal(t, "https://www.testapi.com/auth/monitoring.read", conf.Scopes[0])
|
||||
assert.Equal(t, "https://www.testapi.com/auth/cloudplatformprojects.readonly", conf.Scopes[1])
|
||||
assert.Equal(t, "login.url.com/token", conf.TokenURL)
|
||||
|
||||
pluginRoute := &plugins.AppPluginRoute{
|
||||
Path: "pathwithtokenauth1",
|
||||
URL: "",
|
||||
Method: "GET",
|
||||
TokenAuth: &plugins.JwtTokenAuth{
|
||||
Url: server.URL + "/oauth/token",
|
||||
Scopes: []string{
|
||||
"https://www.testapi.com/auth/monitoring.read",
|
||||
"https://www.testapi.com/auth/cloudplatformprojects.readonly",
|
||||
},
|
||||
Params: map[string]string{
|
||||
"grant_type": "client_credentials",
|
||||
"client_id": "{{.JsonData.client_id}}",
|
||||
"client_secret": "{{.SecureJsonData.client_secret}}",
|
||||
"audience": "{{.JsonData.audience}}",
|
||||
"client_name": "datasource_plugin",
|
||||
},
|
||||
return &oauth2.Token{AccessToken: "abc"}, nil
|
||||
})
|
||||
|
||||
provider := newAccessTokenProvider(ds, pluginRoute)
|
||||
_, err := provider.getJwtAccessToken(context.Background(), templateData)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("should use cached token on second call", func(t *testing.T) {
|
||||
setUp(t, func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
|
||||
return &oauth2.Token{
|
||||
AccessToken: "abc",
|
||||
Expiry: time.Now().Add(1 * time.Minute)}, nil
|
||||
})
|
||||
provider := newAccessTokenProvider(ds, pluginRoute)
|
||||
token1, err := provider.getJwtAccessToken(context.Background(), templateData)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "abc", token1)
|
||||
|
||||
getTokenSource = func(conf *jwt.Config, ctx context.Context) (*oauth2.Token, error) {
|
||||
return &oauth2.Token{AccessToken: "error: cache not used"}, nil
|
||||
}
|
||||
token2, err := provider.getJwtAccessToken(context.Background(), templateData)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "abc", token2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccessToken_pluginWithTokenAuthRoute(t *testing.T) {
|
||||
apiHandler := http.NewServeMux()
|
||||
server := httptest.NewServer(apiHandler)
|
||||
defer server.Close()
|
||||
|
||||
pluginRoute := &plugins.AppPluginRoute{
|
||||
Path: "pathwithtokenauth1",
|
||||
URL: "",
|
||||
Method: "GET",
|
||||
TokenAuth: &plugins.JwtTokenAuth{
|
||||
Url: server.URL + "/oauth/token",
|
||||
Scopes: []string{
|
||||
"https://www.testapi.com/auth/monitoring.read",
|
||||
"https://www.testapi.com/auth/cloudplatformprojects.readonly",
|
||||
},
|
||||
Params: map[string]string{
|
||||
"grant_type": "client_credentials",
|
||||
"client_id": "{{.JsonData.client_id}}",
|
||||
"client_secret": "{{.SecureJsonData.client_secret}}",
|
||||
"audience": "{{.JsonData.audience}}",
|
||||
"client_name": "datasource_plugin",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
templateData := templateData{
|
||||
JsonData: map[string]interface{}{
|
||||
"client_id": "my_client_id",
|
||||
"audience": "www.example.com",
|
||||
},
|
||||
SecureJsonData: map[string]string{
|
||||
"client_secret": "my_secret",
|
||||
},
|
||||
}
|
||||
|
||||
var authCalls int
|
||||
apiHandler.HandleFunc("/oauth/token", func(w http.ResponseWriter, req *http.Request) {
|
||||
err := json.NewEncoder(w).Encode(token)
|
||||
require.NoError(t, err)
|
||||
authCalls++
|
||||
})
|
||||
|
||||
t.Run("Should parse token, with different fields and types", func(t *testing.T) {
|
||||
type tokenTestDescription struct {
|
||||
desc string
|
||||
expiresIn interface{}
|
||||
expiresOn interface{}
|
||||
expectedExpiresOn int64
|
||||
}
|
||||
|
||||
templateData := templateData{
|
||||
JsonData: map[string]interface{}{
|
||||
"client_id": "my_client_id",
|
||||
"audience": "www.example.com",
|
||||
mockTimeNow(time.Now())
|
||||
defer resetTimeNow()
|
||||
provider := newAccessTokenProvider(&models.DataSource{}, pluginRoute)
|
||||
|
||||
testCases := []tokenTestDescription{
|
||||
{
|
||||
desc: "token with expires_in in string format",
|
||||
expiresIn: "3600",
|
||||
expiresOn: nil,
|
||||
expectedExpiresOn: timeNow().Unix() + 3600,
|
||||
},
|
||||
SecureJsonData: map[string]string{
|
||||
"client_secret": "my_secret",
|
||||
{
|
||||
desc: "token with expires_in in int format",
|
||||
expiresIn: 3600,
|
||||
expiresOn: nil,
|
||||
expectedExpiresOn: timeNow().Unix() + 3600,
|
||||
},
|
||||
{
|
||||
desc: "token with expires_on in string format",
|
||||
expiresOn: strconv.FormatInt(timeNow().Add(86*time.Minute).Unix(), 10),
|
||||
expiresIn: nil,
|
||||
expectedExpiresOn: timeNow().Add(86 * time.Minute).Unix(),
|
||||
},
|
||||
{
|
||||
desc: "token with expires_on in int format",
|
||||
expiresOn: timeNow().Add(86 * time.Minute).Unix(),
|
||||
expiresIn: nil,
|
||||
expectedExpiresOn: timeNow().Add(86 * time.Minute).Unix(),
|
||||
},
|
||||
{
|
||||
desc: "token with both expires_on and expires_in, should prioritize expiresOn",
|
||||
expiresIn: 5200,
|
||||
expiresOn: timeNow().Add(1 * time.Hour).Unix(),
|
||||
expectedExpiresOn: timeNow().Add(1 * time.Hour).Unix(),
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.desc, func(t *testing.T) {
|
||||
clearTokenCache()
|
||||
// reset the httphandler counter
|
||||
authCalls = 0
|
||||
|
||||
var authCalls int
|
||||
apiHandler.HandleFunc("/oauth/token", func(w http.ResponseWriter, req *http.Request) {
|
||||
err := json.NewEncoder(w).Encode(token)
|
||||
require.NoError(t, err)
|
||||
authCalls++
|
||||
})
|
||||
token = map[string]interface{}{
|
||||
"access_token": "2YotnFZFEjr1zCsicMWpAA",
|
||||
"token_type": "example",
|
||||
"refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
|
||||
}
|
||||
|
||||
Convey("Should parse token, with different fields and types", func() {
|
||||
type tokenTestDescription struct {
|
||||
desc string
|
||||
expiresIn interface{}
|
||||
expiresOn interface{}
|
||||
expectedExpiresOn int64
|
||||
}
|
||||
if testCase.expiresIn != nil {
|
||||
token["expires_in"] = testCase.expiresIn
|
||||
}
|
||||
|
||||
mockTimeNow(time.Now())
|
||||
defer resetTimeNow()
|
||||
provider := newAccessTokenProvider(&models.DataSource{}, pluginRoute)
|
||||
if testCase.expiresOn != nil {
|
||||
token["expires_on"] = testCase.expiresOn
|
||||
}
|
||||
|
||||
testCases := []tokenTestDescription{
|
||||
{
|
||||
desc: "token with expires_in in string format",
|
||||
expiresIn: "3600",
|
||||
expiresOn: nil,
|
||||
expectedExpiresOn: timeNow().Unix() + 3600,
|
||||
},
|
||||
{
|
||||
desc: "token with expires_in in int format",
|
||||
expiresIn: 3600,
|
||||
expiresOn: nil,
|
||||
expectedExpiresOn: timeNow().Unix() + 3600,
|
||||
},
|
||||
{
|
||||
desc: "token with expires_on in string format",
|
||||
expiresOn: strconv.FormatInt(timeNow().Add(86*time.Minute).Unix(), 10),
|
||||
expiresIn: nil,
|
||||
expectedExpiresOn: timeNow().Add(86 * time.Minute).Unix(),
|
||||
},
|
||||
{
|
||||
desc: "token with expires_on in int format",
|
||||
expiresOn: timeNow().Add(86 * time.Minute).Unix(),
|
||||
expiresIn: nil,
|
||||
expectedExpiresOn: timeNow().Add(86 * time.Minute).Unix(),
|
||||
},
|
||||
{
|
||||
desc: "token with both expires_on and expires_in, should prioritize expiresOn",
|
||||
expiresIn: 5200,
|
||||
expiresOn: timeNow().Add(1 * time.Hour).Unix(),
|
||||
expectedExpiresOn: timeNow().Add(1 * time.Hour).Unix(),
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
Convey(testCase.desc, func() {
|
||||
clearTokenCache()
|
||||
// reset the httphandler counter
|
||||
authCalls = 0
|
||||
accessToken, err := provider.getAccessToken(templateData)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, token["access_token"], accessToken)
|
||||
|
||||
token = map[string]interface{}{
|
||||
"access_token": "2YotnFZFEjr1zCsicMWpAA",
|
||||
"token_type": "example",
|
||||
"refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
|
||||
}
|
||||
// getAccessToken should use internal cache
|
||||
accessToken, err = provider.getAccessToken(templateData)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, token["access_token"], accessToken)
|
||||
assert.Equal(t, 1, authCalls)
|
||||
|
||||
if testCase.expiresIn != nil {
|
||||
token["expires_in"] = testCase.expiresIn
|
||||
}
|
||||
tokenCache.Lock()
|
||||
v, ok := tokenCache.cache[provider.getAccessTokenCacheKey()]
|
||||
tokenCache.Unlock()
|
||||
|
||||
if testCase.expiresOn != nil {
|
||||
token["expires_on"] = testCase.expiresOn
|
||||
}
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, testCase.expectedExpiresOn, v.ExpiresOn.Unix())
|
||||
assert.Equal(t, token["access_token"], v.AccessToken)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
accessToken, err := provider.getAccessToken(templateData)
|
||||
So(err, ShouldBeNil)
|
||||
So(accessToken, ShouldEqual, token["access_token"])
|
||||
t.Run("Should refetch token on expire", func(t *testing.T) {
|
||||
clearTokenCache()
|
||||
// reset the httphandler counter
|
||||
authCalls = 0
|
||||
|
||||
// getAccessToken should use internal cache
|
||||
accessToken, err = provider.getAccessToken(templateData)
|
||||
So(err, ShouldBeNil)
|
||||
So(accessToken, ShouldEqual, token["access_token"])
|
||||
So(authCalls, ShouldEqual, 1)
|
||||
mockTimeNow(time.Now())
|
||||
defer resetTimeNow()
|
||||
provider := newAccessTokenProvider(&models.DataSource{}, pluginRoute)
|
||||
|
||||
tokenCache.Lock()
|
||||
v, ok := tokenCache.cache[provider.getAccessTokenCacheKey()]
|
||||
tokenCache.Unlock()
|
||||
token = map[string]interface{}{
|
||||
"access_token": "2YotnFZFEjr1zCsicMWpAA",
|
||||
"token_type": "3600",
|
||||
"refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
|
||||
}
|
||||
accessToken, err := provider.getAccessToken(templateData)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, token["access_token"], accessToken)
|
||||
|
||||
So(ok, ShouldBeTrue)
|
||||
So(v.ExpiresOn.Unix(), ShouldEqual, testCase.expectedExpiresOn)
|
||||
So(v.AccessToken, ShouldEqual, token["access_token"])
|
||||
})
|
||||
}
|
||||
})
|
||||
mockTimeNow(timeNow().Add(3601 * time.Second))
|
||||
|
||||
Convey("Should refetch token on expire", func() {
|
||||
clearTokenCache()
|
||||
// reset the httphandler counter
|
||||
authCalls = 0
|
||||
|
||||
mockTimeNow(time.Now())
|
||||
defer resetTimeNow()
|
||||
provider := newAccessTokenProvider(&models.DataSource{}, pluginRoute)
|
||||
|
||||
token = map[string]interface{}{
|
||||
"access_token": "2YotnFZFEjr1zCsicMWpAA",
|
||||
"token_type": "3600",
|
||||
"refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
|
||||
}
|
||||
accessToken, err := provider.getAccessToken(templateData)
|
||||
So(err, ShouldBeNil)
|
||||
So(accessToken, ShouldEqual, token["access_token"])
|
||||
|
||||
mockTimeNow(timeNow().Add(3601 * time.Second))
|
||||
|
||||
accessToken, err = provider.getAccessToken(templateData)
|
||||
So(err, ShouldBeNil)
|
||||
So(accessToken, ShouldEqual, token["access_token"])
|
||||
So(authCalls, ShouldEqual, 2)
|
||||
})
|
||||
accessToken, err = provider.getAccessToken(templateData)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, token["access_token"], accessToken)
|
||||
assert.Equal(t, 2, authCalls)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user