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

Commit

Permalink
Refactor search_blackbox_test.go (#2148)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarifibrahim committed Jul 13, 2018
1 parent f13e094 commit cb85aa7
Showing 1 changed file with 34 additions and 110 deletions.
144 changes: 34 additions & 110 deletions controller/search_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import (
"github.com/fabric8-services/fabric8-wit/ptr"
"github.com/fabric8-services/fabric8-wit/rendering"
"github.com/fabric8-services/fabric8-wit/resource"
"github.com/fabric8-services/fabric8-wit/rest"
"github.com/fabric8-services/fabric8-wit/search"
"github.com/fabric8-services/fabric8-wit/space"
testsupport "github.com/fabric8-services/fabric8-wit/test"
tf "github.com/fabric8-services/fabric8-wit/test/testfixture"
"github.com/fabric8-services/fabric8-wit/workitem"
Expand All @@ -47,8 +45,6 @@ func TestSearchController(t *testing.T) {
type searchControllerTestSuite struct {
gormtestsupport.DBTestSuite
svc *goa.Service
testIdentity account.Identity
wiRepo *workitem.GormWorkItemRepository
controller *SearchController
spaceBlackBoxTestConfiguration *config.Registry
testDir string
Expand All @@ -60,13 +56,10 @@ func (s *searchControllerTestSuite) SetupTest() {
// create a test identity
testIdentity, err := testsupport.CreateTestIdentity(s.DB, "searchControllerTestSuite user", "test provider")
require.NoError(s.T(), err)
s.testIdentity = *testIdentity

s.wiRepo = workitem.NewWorkItemRepository(s.DB)
spaceBlackBoxTestConfiguration, err := config.Get()
require.NoError(s.T(), err)
s.spaceBlackBoxTestConfiguration = spaceBlackBoxTestConfiguration
s.svc = testsupport.ServiceAsUser("WorkItemComment-Service", s.testIdentity)
s.svc = testsupport.ServiceAsUser("WorkItemComment-Service", *testIdentity)
s.controller = NewSearchController(s.svc, s.GormDB, spaceBlackBoxTestConfiguration)
}

Expand Down Expand Up @@ -208,34 +201,6 @@ func (s *searchControllerTestSuite) TestUnwantedCharactersRelatedToSearchLogic()
assert.Empty(s.T(), sr.Data)
}

func (s *searchControllerTestSuite) getWICreatePayload() *app.CreateWorkitemsPayload {
spaceID := space.SystemSpace
spaceRelatedURL := rest.AbsoluteURL(&http.Request{Host: "api.service.domain.org"}, app.SpaceHref(spaceID.String()))
witRelatedURL := rest.AbsoluteURL(&http.Request{Host: "api.service.domain.org"}, app.WorkitemtypeHref(workitem.SystemTask.String()))
c := app.CreateWorkitemsPayload{
Data: &app.WorkItem{
Type: APIStringTypeWorkItem,
Attributes: map[string]interface{}{},
Relationships: &app.WorkItemRelationships{
BaseType: &app.RelationBaseType{
Data: &app.BaseTypeData{
Type: APIStringTypeWorkItemType,
ID: workitem.SystemTask,
},
Links: &app.GenericLinks{
Self: &witRelatedURL,
Related: &witRelatedURL,
},
},
Space: app.NewSpaceRelation(spaceID, spaceRelatedURL),
},
},
}
c.Data.Attributes[workitem.SystemTitle] = "Title"
c.Data.Attributes[workitem.SystemState] = workitem.SystemStateNew
return &c
}

func getServiceAsUser(testIdentity account.Identity) *goa.Service {
return testsupport.ServiceAsUser("TestSearch-Service", testIdentity)
}
Expand Down Expand Up @@ -273,10 +238,10 @@ func (s *searchControllerTestSuite) searchByURL(customHost, queryString string)
}

// verifySearchByKnownURLs performs actual tests on search result and knwonURL map
func (s *searchControllerTestSuite) verifySearchByKnownURLs(wi *app.WorkItemSingle, host, searchQuery string) {
func (s *searchControllerTestSuite) verifySearchByKnownURLs(wi *workitem.WorkItem, host, searchQuery string) {
result := s.searchByURL(host, searchQuery)
assert.NotEmpty(s.T(), result.Data)
assert.Equal(s.T(), *wi.Data.ID, *result.Data[0].ID)
assert.Equal(s.T(), wi.ID, *result.Data[0].ID)

known := search.GetAllRegisteredURLs()
require.NotNil(s.T(), known)
Expand All @@ -286,22 +251,17 @@ func (s *searchControllerTestSuite) verifySearchByKnownURLs(wi *app.WorkItemSing
}

// TestAutoRegisterHostURL checks if client's host is neatly registered as a KnwonURL or not
// Uses helper functions verifySearchByKnownURLs, searchByURL, getWICreatePayload
// Uses helper functions verifySearchByKnownURLs, searchByURL
func (s *searchControllerTestSuite) TestAutoRegisterHostURL() {
wiCtrl := NewWorkitemsController(s.svc, s.GormDB, s.Configuration)
// create a WI, search by `list view URL` of newly created item
//fxt := tf.NewTestFixture(s.T(), s.DB, tf.Spaces(1))
newWI := s.getWICreatePayload()
_, wi := test.CreateWorkitemsCreated(s.T(), s.svc.Context, s.svc, wiCtrl, space.SystemSpace, newWI)
require.NotNil(s.T(), wi)
fxt := tf.NewTestFixture(s.T(), s.DB, tf.CreateWorkItemEnvironment(), tf.WorkItems(1))
customHost := "own.domain.one"
queryString := fmt.Sprintf("http://%s/work-item/list/detail/%d", customHost, wi.Data.Attributes[workitem.SystemNumber])
s.verifySearchByKnownURLs(wi, customHost, queryString)
queryString := fmt.Sprintf("http://%s/work-item/list/detail/%d", customHost, fxt.WorkItems[0].Fields[workitem.SystemNumber])
s.verifySearchByKnownURLs(fxt.WorkItems[0], customHost, queryString)

// Search by `board view URL` of newly created item
customHost2 := "own.domain.two"
queryString2 := fmt.Sprintf("http://%s/work-item/board/detail/%d", customHost2, wi.Data.Attributes[workitem.SystemNumber])
s.verifySearchByKnownURLs(wi, customHost2, queryString2)
queryString2 := fmt.Sprintf("http://%s/work-item/board/detail/%d", customHost2, fxt.WorkItems[0].Fields[workitem.SystemNumber])
s.verifySearchByKnownURLs(fxt.WorkItems[0], customHost2, queryString2)
}

func (s *searchControllerTestSuite) TestSearchWorkItemsSpaceContext() {
Expand Down Expand Up @@ -408,76 +368,40 @@ func (s *searchControllerTestSuite) TestSearchFilter() {

func (s *searchControllerTestSuite) TestSearchByWorkItemTypeGroup() {
s.T().Run(http.StatusText(http.StatusOK), func(t *testing.T) {
// given
// given work items of different types and in different states
type testWI struct {
Title string
WorkItemTypeID uuid.UUID
State string
}
testWIs := []testWI{
{"closed feature", workitem.SystemFeature, workitem.SystemStateClosed},
{"open feature", workitem.SystemFeature, workitem.SystemStateOpen},
{"closed bug", workitem.SystemBug, workitem.SystemStateClosed},
{"open bug", workitem.SystemBug, workitem.SystemStateOpen},
{"open experience", workitem.SystemExperience, workitem.SystemStateOpen},
{"closed experience", workitem.SystemExperience, workitem.SystemStateClosed},
{"open task", workitem.SystemTask, workitem.SystemStateOpen},
{"closed task", workitem.SystemTask, workitem.SystemStateClosed},
{"open scenario", workitem.SystemScenario, workitem.SystemStateOpen},
{"closed scenario", workitem.SystemScenario, workitem.SystemStateClosed},
{"open fundamental", workitem.SystemFundamental, workitem.SystemStateOpen},
{"closed fundamental", workitem.SystemFundamental, workitem.SystemStateClosed},
}

fxt := tf.NewTestFixture(t, s.DB,
tf.Spaces(1, func(fxt *tf.TestFixture, idx int) error {
fxt.Spaces[idx].SpaceTemplateID = spacetemplate.SystemLegacyTemplateID
return nil
}),
tf.CreateWorkItemEnvironment(),
// TODO(kwk): Decide if these type groups should go to CreateWorkItemEnvironment()
tf.WorkItemTypeGroups(4, func(fxt *tf.TestFixture, idx int) error {
witg := fxt.WorkItemTypeGroups[idx]
switch idx {
case 0:
witg.Name = "Scenarios"
witg.TypeList = []uuid.UUID{
workitem.SystemScenario,
workitem.SystemFundamental,
workitem.SystemPapercuts,
}
case 1:
witg.Name = "Experiences"
witg.TypeList = []uuid.UUID{
workitem.SystemExperience,
workitem.SystemValueProposition,
}
case 2:
witg.Name = "Requirements"
witg.TypeList = []uuid.UUID{
workitem.SystemFeature,
workitem.SystemBug,
}
case 3:
witg.Name = "Execution"
witg.TypeList = []uuid.UUID{
workitem.SystemTask,
workitem.SystemBug,
workitem.SystemFeature,
}
}
tf.WorkItems(len(testWIs), func(fxt *tf.TestFixture, idx int) error {
fxt.WorkItems[idx].Type = testWIs[idx].WorkItemTypeID
fxt.WorkItems[idx].Fields[workitem.SystemTitle] = testWIs[idx].Title
fxt.WorkItems[idx].Fields[workitem.SystemState] = testWIs[idx].State
return nil
}),
)
svc := testsupport.ServiceAsUser("TestUpdateWI-Service", *fxt.Identities[0])
workitemsCtrl := NewWorkitemsController(svc, s.GormDB, s.Configuration)
// given work items of different types and in different states
type testWI struct {
Title string
WorkItemTypeID uuid.UUID
State string
SpaceID uuid.UUID
}
testWIs := []testWI{
{"closed feature", workitem.SystemFeature, workitem.SystemStateClosed, fxt.Spaces[0].ID},
{"open feature", workitem.SystemFeature, workitem.SystemStateOpen, fxt.Spaces[0].ID},
{"closed bug", workitem.SystemBug, workitem.SystemStateClosed, fxt.Spaces[0].ID},
{"open bug", workitem.SystemBug, workitem.SystemStateOpen, fxt.Spaces[0].ID},
{"open experience", workitem.SystemExperience, workitem.SystemStateOpen, fxt.Spaces[0].ID},
{"closed experience", workitem.SystemExperience, workitem.SystemStateClosed, fxt.Spaces[0].ID},
{"open task", workitem.SystemTask, workitem.SystemStateOpen, fxt.Spaces[0].ID},
{"closed task", workitem.SystemTask, workitem.SystemStateClosed, fxt.Spaces[0].ID},
{"open scenario", workitem.SystemScenario, workitem.SystemStateOpen, fxt.Spaces[0].ID},
{"closed scenario", workitem.SystemScenario, workitem.SystemStateClosed, fxt.Spaces[0].ID},
{"open fundamental", workitem.SystemFundamental, workitem.SystemStateOpen, fxt.Spaces[0].ID},
{"closed fundamental", workitem.SystemFundamental, workitem.SystemStateClosed, fxt.Spaces[0].ID},
}
for _, wi := range testWIs {
payload := minimumRequiredCreateWithTypeAndSpace(wi.WorkItemTypeID, wi.SpaceID)
payload.Data.Attributes[workitem.SystemTitle] = wi.Title
payload.Data.Attributes[workitem.SystemState] = wi.State
_, _ = test.CreateWorkitemsCreated(t, svc.Context, svc, workitemsCtrl, wi.SpaceID, &payload)
}

// helper function that checks if the given to be found work item titles
// exist in the result list that originate from a search query.
Expand Down

0 comments on commit cb85aa7

Please sign in to comment.