Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
adds check for WIT; adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DhritiShikhar committed Dec 3, 2018
1 parent 65b3643 commit 02dca24
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 4 deletions.
4 changes: 3 additions & 1 deletion controller/trackerquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ func (c *TrackerqueryController) Create(ctx *app.CreateTrackerqueryContext) erro
SpaceID: *ctx.Payload.Data.Relationships.Space.Data.ID,
WorkItemTypeID: ctx.Payload.Data.Relationships.BaseType.Data.ID,
}
trackerQuery.ID = *ctx.Payload.Data.ID
if ctx.Payload.Data.ID != nil {
trackerQuery.ID = *ctx.Payload.Data.ID
}
tq, err := appl.TrackerQueries().Create(ctx.Context, trackerQuery)
if err != nil {
return errs.Wrapf(err, "failed to create tracker query %s", ctx.Payload.Data)
Expand Down
37 changes: 37 additions & 0 deletions controller/trackerquery_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/fabric8-services/fabric8-wit/app"
"github.com/fabric8-services/fabric8-wit/app/test"
. "github.com/fabric8-services/fabric8-wit/controller"
"github.com/fabric8-services/fabric8-wit/errors"
"github.com/fabric8-services/fabric8-wit/gormapplication"
"github.com/fabric8-services/fabric8-wit/gormtestsupport"
"github.com/fabric8-services/fabric8-wit/jsonapi"
Expand Down Expand Up @@ -304,6 +305,42 @@ func (rest *TestTrackerQueryREST) TestCreateTrackerQueryID() {
})
}

func (rest *TestTrackerQueryREST) TestInvalidWITinTrackerQuery() {
t := rest.T()
resource.Require(t, resource.Database)
rest.T().Run("nil WIT in trackerquery payload", func(t *testing.T) {
fxt := tf.NewTestFixture(t, rest.DB,
tf.SpaceTemplates(2),
tf.Spaces(1),
tf.Trackers(1),
)
svc, _, trackerQueryCtrl := rest.SecuredController()

tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, uuid.Nil)
_, err := test.CreateTrackerqueryBadRequest(t, svc.Context, svc, trackerQueryCtrl, &tqpayload)
assert.NotNil(t, err)
require.IsType(t, errors.BadParameterError{}, err)
})

rest.T().Run("disallow creation if WIT belongs to different spacetemplate", func(t *testing.T) {
fxt := tf.NewTestFixture(t, rest.DB,
tf.SpaceTemplates(2),
tf.Spaces(1),
tf.WorkItemTypes(1, func(fxt *tf.TestFixture, idx int) error {
fxt.WorkItemTypes[idx].SpaceTemplateID = fxt.SpaceTemplates[1].ID
return nil
}),
tf.Trackers(1),
)
svc, _, trackerQueryCtrl := rest.SecuredController()

tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, fxt.WorkItemTypes[0].ID)
_, err := test.CreateTrackerqueryBadRequest(t, svc.Context, svc, trackerQueryCtrl, &tqpayload)
assert.NotNil(t, err)
require.IsType(t, errors.BadParameterError{}, err)
})
}

func newCreateTrackerQueryPayload(spaceID uuid.UUID, trackerID uuid.UUID, witID uuid.UUID) app.CreateTrackerqueryPayload {
trackerQueryID := uuid.NewV4()
return app.CreateTrackerqueryPayload{
Expand Down
2 changes: 1 addition & 1 deletion remoteworkitem/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *Scheduler) ScheduleAllQueries(ctx context.Context, accessTokens map[str

func fetchTrackerQueries(db *gorm.DB) []TrackerSchedule {
tsList := []TrackerSchedule{}
err := db.Table("tracker_queries").Select("trackers.id as tracker_id, trackers.url, trackers.type as tracker_type, tracker_queries.query, tracker_queries.schedule, tracker_queries.space_id, tracker_queries.work_item_type_id as work_item_type").Joins("left join trackers on tracker_queries.tracker_id = trackers.id").Where("trackers.deleted_at is NULL AND tracker_queries.deleted_at is NULL").Scan(&tsList).Error
err := db.Table("tracker_queries").Select("trackers.id as tracker_id, trackers.url, trackers.type as tracker_type, tracker_queries.query, tracker_queries.schedule, tracker_queries.space_id, tracker_queries.work_item_type_id as work_item_type_id").Joins("left join trackers on tracker_queries.tracker_id = trackers.id").Where("trackers.deleted_at is NULL AND tracker_queries.deleted_at is NULL").Scan(&tsList).Error
if err != nil {
log.Error(nil, map[string]interface{}{
"err": err,
Expand Down
25 changes: 23 additions & 2 deletions remoteworkitem/trackerquery_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/fabric8-services/fabric8-wit/application/repository"
"github.com/fabric8-services/fabric8-wit/log"
"github.com/fabric8-services/fabric8-wit/workitem"
"github.com/goadesign/goa"

"github.com/fabric8-services/fabric8-wit/errors"
Expand All @@ -21,12 +22,18 @@ const trackerQueriesTableName = "tracker_queries"

// GormTrackerQueryRepository implements TrackerRepository using gorm
type GormTrackerQueryRepository struct {
db *gorm.DB
db *gorm.DB
witr *workitem.GormWorkItemTypeRepository
wir *workitem.GormWorkItemRepository
}

// NewTrackerQueryRepository constructs a TrackerQueryRepository
func NewTrackerQueryRepository(db *gorm.DB) *GormTrackerQueryRepository {
return &GormTrackerQueryRepository{db}
return &GormTrackerQueryRepository{
db: db,
witr: workitem.NewWorkItemTypeRepository(db),
wir: workitem.NewWorkItemRepository(db),
}
}

// TrackerQueryRepository encapsulate storage & retrieval of tracker queries
Expand All @@ -42,6 +49,20 @@ type TrackerQueryRepository interface {
// Create creates a new tracker query in the repository
// returns BadParameterError, ConversionError or InternalError
func (r *GormTrackerQueryRepository) Create(ctx context.Context, tq TrackerQuery) (*TrackerQuery, error) {
wiType, err := r.witr.Load(ctx, tq.WorkItemTypeID)
if err != nil {
return nil, errors.NewBadParameterError("WorkItemTypeID", tq.WorkItemTypeID)
}

allowedWIT, err := r.wir.CheckTypeAndSpaceShareTemplate(ctx, wiType, tq.SpaceID)
if err != nil {
return nil, err

}
if !allowedWIT {
return nil, err
}

if err := r.db.Create(&tq).Error; err != nil {
return nil, errors.NewInternalError(ctx, r.db.Error)
}
Expand Down
66 changes: 66 additions & 0 deletions remoteworkitem/trackerquery_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,69 @@ func (test *TestTrackerQueryRepository) TestTrackerQueryList() {
require.True(t, len(trackerqueries2) >= 2)
assert.Equal(t, trackerqueries2[1], trackerqueries3[1])
}

func (test *TestTrackerQueryRepository) TestTrackerQueryValidWIT() {
t := test.T()
resource.Require(t, resource.Database)

t.Run("valid WIT - success", func(t *testing.T) {
req := &http.Request{Host: "localhost"}
params := url.Values{}
ctx := goa.NewContext(context.Background(), nil, req, params)

tracker := remoteworkitem.Tracker{
URL: "http://issues.jboss.com",
Type: remoteworkitem.ProviderJira,
}
err := test.trackerRepo.Create(ctx, &tracker)
fxt := tf.NewTestFixture(t, test.DB, tf.Spaces(1), tf.WorkItemTypes(1))

tq := remoteworkitem.TrackerQuery{
Query: "abc",
Schedule: "xyz",
TrackerID: tracker.ID,
SpaceID: fxt.Spaces[0].ID,
WorkItemTypeID: fxt.WorkItemTypes[0].ID,
}
res, err := test.queryRepo.Create(ctx, tq)
require.NoError(t, err)
require.NotNil(t, res)

res2, err := test.queryRepo.Load(ctx, res.ID)
require.NoError(t, err)
assert.Equal(t, res.ID, res2.ID)
})

t.Run("invalid WIT - fail", func(t *testing.T) {
req := &http.Request{Host: "localhost"}
params := url.Values{}
ctx := goa.NewContext(context.Background(), nil, req, params)

tracker := remoteworkitem.Tracker{
URL: "http://issues.jboss.com",
Type: remoteworkitem.ProviderJira,
}
err := test.trackerRepo.Create(ctx, &tracker)
fxt := tf.NewTestFixture(t, test.DB,
tf.SpaceTemplates(2),
tf.Spaces(1),
tf.WorkItemTypes(1, func(fxt *tf.TestFixture, idx int) error {
fxt.WorkItemTypes[idx].SpaceTemplateID = fxt.SpaceTemplates[1].ID
return nil
}),
tf.Trackers(1),
)

tq := remoteworkitem.TrackerQuery{
Query: "abc",
Schedule: "xyz",
TrackerID: tracker.ID,
SpaceID: fxt.Spaces[0].ID,
WorkItemTypeID: fxt.WorkItemTypes[0].ID,
}
res, err := test.queryRepo.Create(ctx, tq)
assert.NotNil(t, err)
require.IsType(t, errors.BadParameterError{}, err)
assert.Nil(t, res)
})
}

0 comments on commit 02dca24

Please sign in to comment.