flamingo.me/flamingo-commerce/v3@v3.11.0/product/interfaces/templatefunctions/findProducts_test.go (about) 1 package templatefunctions 2 3 import ( 4 "net/http" 5 "net/url" 6 "testing" 7 8 "flamingo.me/flamingo-commerce/v3/product/application" 9 "flamingo.me/flamingo-commerce/v3/search/domain" 10 "flamingo.me/flamingo/v3/framework/web" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 // TestFilterProcessingIsAllowed - Test different combinations of filter processing 15 func TestFilterProcessingIsAllowed(t *testing.T) { 16 17 // Case 1: only blacklist filled, everything not on the blacklist should be fine 18 filterConstrains := make(map[string]string) 19 filterConstrains["blackList"] = "blacklisted" 20 filterConstrains["whiteList"] = "" 21 filterProcessing := buildFilterProcessing(nil, filterConstrains) 22 23 assert.True(t, filterProcessing.isAllowed("allowedKey")) 24 assert.False(t, filterProcessing.isAllowed("blacklisted")) 25 26 // Case 2: only whitelist filled, everything noy on the whitelist should be bad 27 filterConstrains["blackList"] = "" 28 filterConstrains["whiteList"] = "allowedKey" 29 filterProcessing = buildFilterProcessing(nil, filterConstrains) 30 31 assert.True(t, filterProcessing.isAllowed("allowedKey")) 32 assert.False(t, filterProcessing.isAllowed("blacklisted")) 33 34 // Case 3: both lists are empty, everything is allowed 35 filterConstrains["blackList"] = "" 36 filterConstrains["whiteList"] = "" 37 filterProcessing = buildFilterProcessing(nil, filterConstrains) 38 39 assert.True(t, filterProcessing.isAllowed("allowedKey")) 40 assert.True(t, filterProcessing.isAllowed("blacklisted")) 41 42 // Case 4: both lists are filled, blacklist is ignored, because whitelisting has a higher prio 43 filterConstrains["blackList"] = "blacklisted,notAllowed, notAllowedWithSpace" 44 filterConstrains["whiteList"] = "allowedKey, allowedWithSpace,allowedNoSpace,blacklisted" 45 filterProcessing = buildFilterProcessing(nil, filterConstrains) 46 47 assert.True(t, filterProcessing.isAllowed("allowedKey")) 48 assert.True(t, filterProcessing.isAllowed("allowedWithSpace")) 49 assert.True(t, filterProcessing.isAllowed("allowedNoSpace")) 50 assert.True(t, filterProcessing.isAllowed("blacklisted")) 51 assert.False(t, filterProcessing.isAllowed("notAllowedWithSpace")) 52 assert.False(t, filterProcessing.isAllowed("notOnAnyList")) 53 } 54 55 // TestFilterProcessingModifyResultRemoveBlacklisted - Test modification of the searchResult 56 func TestFilterProcessingModifyResultRemoveBlacklisted(t *testing.T) { 57 searchResult := buildSearchResult() 58 assert.Len(t, searchResult.Facets, 2) 59 filterConstrains := make(map[string]string) 60 filterConstrains["blackList"] = "disallowed" 61 62 filterProcessing := buildFilterProcessing(nil, filterConstrains) 63 64 newResult := filterProcessing.modifyResult(searchResult) 65 assert.Len(t, newResult.Facets, 1) 66 assert.Len(t, searchResult.SearchMeta.SelectedFacets, 1) 67 assert.Equal(t, newResult.Facets["allowed"].Name, "allowed") 68 } 69 70 // TestFilterProcessingModifyResultOnlyKeepWhitelisted - Test modification of the searchResult 71 func TestFilterProcessingModifyResultOnlyKeepWhitelisted(t *testing.T) { 72 searchResult := buildSearchResult() 73 assert.Len(t, searchResult.Facets, 2) 74 assert.Len(t, searchResult.SearchMeta.SelectedFacets, 2) 75 filterConstrains := make(map[string]string) 76 filterConstrains["whiteList"] = "allowed" 77 78 filterProcessing := buildFilterProcessing(nil, filterConstrains) 79 80 newResult := filterProcessing.modifyResult(searchResult) 81 assert.Len(t, newResult.Facets, 1) 82 assert.Len(t, searchResult.SearchMeta.SelectedFacets, 1) 83 assert.Equal(t, newResult.Facets["allowed"].Name, "allowed") 84 } 85 86 // helper function for test cases 87 func buildSearchResult() *application.SearchResult { 88 searchResult := application.SearchResult{} 89 90 // adding a facet, one allowed, on to remove 91 facetAllowed := domain.Facet{Name: "allowed"} 92 facetDisallowed := domain.Facet{Name: "disallowed"} 93 facetCollection := domain.FacetCollection{} 94 facetCollection["allowed"] = facetAllowed 95 facetCollection["disallowed"] = facetDisallowed 96 97 searchResult.Facets = facetCollection 98 99 var selectedFacets []domain.Facet 100 selectedFacets = append(selectedFacets, facetAllowed) 101 selectedFacets = append(selectedFacets, facetDisallowed) 102 searchResult.SearchMeta.SelectedFacets = selectedFacets 103 104 return &searchResult 105 } 106 107 // helper function for test cases 108 func buildFilterProcessing(keyValueFilter map[string][]string, filterConstrains map[string]string) filterProcessing { 109 requestURL := url.URL{} 110 webRequest := web.CreateRequest(&http.Request{ 111 URL: &requestURL, 112 }, nil) 113 114 // we do not yet support multiple namespaces 115 namespace := "" 116 117 filterProcessing := newFilterProcessing(webRequest, namespace, nil, keyValueFilter, filterConstrains, nil) 118 return filterProcessing 119 }