grafana/pkg/services/sqlstore/transactions_test.go
idafurjes f40c1d0808
Chore: Add context apikey (#41698)
* Add context apikey

* Rename DeleteApiKeyCtx to DeleteApiKey
2021-11-18 09:50:03 +01:00

57 lines
1.4 KiB
Go

//go:build integration
// +build integration
package sqlstore
import (
"context"
"errors"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/require"
)
var ErrProvokedError = errors.New("testing error")
func TestTransaction(t *testing.T) {
ss := InitTestDB(t)
cmd := &models.AddApiKeyCommand{Key: "secret-key", Name: "key", OrgId: 1}
t.Run("can update key", func(t *testing.T) {
err := ss.AddAPIKey(context.Background(), cmd)
require.Nil(t, err)
err = ss.WithTransactionalDbSession(context.Background(), func(sess *DBSession) error {
return deleteAPIKey(sess, cmd.Result.Id, 1)
})
require.Nil(t, err)
query := &models.GetApiKeyByIdQuery{ApiKeyId: cmd.Result.Id}
err = ss.GetApiKeyById(context.Background(), query)
require.Equal(t, models.ErrInvalidApiKey, err)
})
t.Run("won't update if one handler fails", func(t *testing.T) {
err := ss.AddAPIKey(context.Background(), cmd)
require.Nil(t, err)
err = ss.WithTransactionalDbSession(context.Background(), func(sess *DBSession) error {
err := deleteAPIKey(sess, cmd.Result.Id, 1)
if err != nil {
return err
}
return ErrProvokedError
})
require.Equal(t, ErrProvokedError, err)
query := &models.GetApiKeyByIdQuery{ApiKeyId: cmd.Result.Id}
err = ss.GetApiKeyById(context.Background(), query)
require.Nil(t, err)
require.Equal(t, cmd.Result.Id, query.Result.Id)
})
}