Remove org methods from sqlstore interface (#56358)

* Remove org methods from sqlstore interface

* Remove some mocks

* Fix some tests
This commit is contained in:
idafurjes
2022-10-05 15:47:56 +02:00
committed by GitHub
parent ad2a1dd680
commit bc7a383252
11 changed files with 50 additions and 76 deletions
+5 -4
View File
@@ -127,7 +127,7 @@ func (hs *HTTPServer) getOrgHelper(ctx context.Context, orgID int64) response.Re
// 409: conflictError
// 500: internalServerError
func (hs *HTTPServer) CreateOrg(c *models.ReqContext) response.Response {
cmd := models.CreateOrgCommand{}
cmd := org.CreateOrgCommand{}
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
@@ -136,8 +136,9 @@ func (hs *HTTPServer) CreateOrg(c *models.ReqContext) response.Response {
return response.Error(http.StatusForbidden, "Access denied", nil)
}
cmd.UserId = c.UserID
if err := hs.SQLStore.CreateOrg(c.Req.Context(), &cmd); err != nil {
cmd.UserID = c.UserID
result, err := hs.orgService.CreateWithMember(c.Req.Context(), &cmd)
if err != nil {
if errors.Is(err, models.ErrOrgNameTaken) {
return response.Error(http.StatusConflict, "Organization name taken", err)
}
@@ -147,7 +148,7 @@ func (hs *HTTPServer) CreateOrg(c *models.ReqContext) response.Response {
metrics.MApiOrgCreate.Inc()
return response.JSON(http.StatusOK, &util.DynMap{
"orgId": cmd.Result.Id,
"orgId": result.ID,
"message": "Organization created",
})
}
+1 -1
View File
@@ -194,7 +194,7 @@ func TestAPIEndpoint_PutCurrentOrgAddress_AccessControl(t *testing.T) {
// `/api/orgs/` endpoints test
// setupOrgsDBForAccessControlTests creates orgs up until orgID and fake user as member of org
func setupOrgsDBForAccessControlTests(t *testing.T, db sqlstore.Store, c accessControlScenarioContext, orgID int64) {
func setupOrgsDBForAccessControlTests(t *testing.T, db *sqlstore.SQLStore, c accessControlScenarioContext, orgID int64) {
t.Helper()
setInitCtxSignedInViewer(c.initCtx)
u := *c.initCtx.SignedInUser
+2 -2
View File
@@ -122,8 +122,8 @@ func createUser(db sqlstore.Store, orgId int64, t *testing.T) int64 {
return user.ID
}
func setupTeamTestScenario(userCount int, db sqlstore.Store, t *testing.T) int64 {
teamService := teamimpl.ProvideService(db.(*sqlstore.SQLStore), setting.NewCfg()) // FIXME
func setupTeamTestScenario(userCount int, db *sqlstore.SQLStore, t *testing.T) int64 {
teamService := teamimpl.ProvideService(db, setting.NewCfg()) // FIXME
user, err := db.CreateUser(context.Background(), user.CreateUserCommand{SkipOrgSetup: true, Login: testUserLogin})
require.NoError(t, err)
testOrg, err := db.CreateOrgWithMember("TestOrg", user.ID)
+10 -7
View File
@@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
@@ -286,26 +287,28 @@ func (hs *HTTPServer) GetUserOrgList(c *models.ReqContext) response.Response {
}
func (hs *HTTPServer) getUserOrgList(ctx context.Context, userID int64) response.Response {
query := models.GetUserOrgListQuery{UserId: userID}
query := org.GetUserOrgListQuery{UserID: userID}
if err := hs.SQLStore.GetUserOrgList(ctx, &query); err != nil {
result, err := hs.orgService.GetUserOrgList(ctx, &query)
if err != nil {
return response.Error(500, "Failed to get user organizations", err)
}
return response.JSON(http.StatusOK, query.Result)
return response.JSON(http.StatusOK, result)
}
func (hs *HTTPServer) validateUsingOrg(ctx context.Context, userID int64, orgID int64) bool {
query := models.GetUserOrgListQuery{UserId: userID}
query := org.GetUserOrgListQuery{UserID: userID}
if err := hs.SQLStore.GetUserOrgList(ctx, &query); err != nil {
result, err := hs.orgService.GetUserOrgList(ctx, &query)
if err != nil {
return false
}
// validate that the org id in the list
valid := false
for _, other := range query.Result {
if other.OrgId == orgID {
for _, other := range result {
if other.OrgID == orgID {
valid = true
}
}