github.com/prebid/prebid-server@v0.275.0/stored_requests/backends/file_fetcher/fetcher_test.go (about) 1 package file_fetcher 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "testing" 8 9 "github.com/prebid/prebid-server/stored_requests" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestFileFetcher(t *testing.T) { 14 fetcher, err := NewFileFetcher("./test") 15 if err != nil { 16 t.Errorf("Failed to create a Fetcher: %v", err) 17 } 18 19 storedReqs, storedImps, errs := fetcher.FetchRequests(context.Background(), []string{"1", "2"}, []string{"some-imp"}) 20 assertErrorCount(t, 0, errs) 21 22 validateStoredReqOne(t, storedReqs) 23 validateStoredReqTwo(t, storedReqs) 24 validateImp(t, storedImps) 25 } 26 27 func TestAccountFetcher(t *testing.T) { 28 fetcher, err := NewFileFetcher("./test") 29 assert.NoError(t, err, "Failed to create test fetcher") 30 31 account, errs := fetcher.FetchAccount(context.Background(), json.RawMessage(`{"events_enabled":true}`), "valid") 32 assertErrorCount(t, 0, errs) 33 assert.JSONEq(t, `{"disabled":false, "events_enabled":true, "id":"valid" }`, string(account)) 34 35 _, errs = fetcher.FetchAccount(context.Background(), json.RawMessage(`{"events_enabled":true}`), "nonexistent") 36 assertErrorCount(t, 1, errs) 37 assert.Error(t, errs[0]) 38 assert.Equal(t, stored_requests.NotFoundError{"nonexistent", "Account"}, errs[0]) 39 40 _, errs = fetcher.FetchAccount(context.Background(), json.RawMessage(`{"events_enabled"}`), "valid") 41 assertErrorCount(t, 1, errs) 42 assert.Error(t, errs[0]) 43 assert.Equal(t, fmt.Errorf("Invalid JSON Document"), errs[0]) 44 45 } 46 47 func TestInvalidDirectory(t *testing.T) { 48 _, err := NewFileFetcher("./nonexistant-directory") 49 if err == nil { 50 t.Errorf("There should be an error if we use a directory which doesn't exist.") 51 } 52 } 53 54 func validateStoredReqOne(t *testing.T, storedRequests map[string]json.RawMessage) { 55 value, hasID := storedRequests["1"] 56 if !hasID { 57 t.Fatalf("Expected stored request data to have id: %d", 1) 58 } 59 60 var req1Val map[string]string 61 if err := json.Unmarshal(value, &req1Val); err != nil { 62 t.Errorf("Failed to unmarshal 1: %v", err) 63 } 64 if len(req1Val) != 1 { 65 t.Errorf("Unexpected req1Val length. Expected %d, Got %d", 1, len(req1Val)) 66 } 67 data, hadKey := req1Val["test"] 68 if !hadKey { 69 t.Errorf("req1Val should have had a \"test\" key, but it didn't.") 70 } 71 if data != "foo" { 72 t.Errorf(`Bad data in "test" of stored request "1". Expected %s, Got %s`, "foo", data) 73 } 74 } 75 76 func validateStoredReqTwo(t *testing.T, storedRequests map[string]json.RawMessage) { 77 value, hasId := storedRequests["2"] 78 if !hasId { 79 t.Fatalf("Expected stored request map to have id: %d", 2) 80 } 81 82 var req2Val string 83 if err := json.Unmarshal(value, &req2Val); err != nil { 84 t.Errorf("Failed to unmarshal %d: %v", 2, err) 85 } 86 if req2Val != `esca"ped` { 87 t.Errorf(`Bad data in stored request "2". Expected %v, Got %s`, `esca"ped`, req2Val) 88 } 89 } 90 91 func validateImp(t *testing.T, storedImps map[string]json.RawMessage) { 92 value, hasId := storedImps["some-imp"] 93 if !hasId { 94 t.Fatal("Expected Stored Imp map to have id: some-imp") 95 } 96 97 var impVal map[string]bool 98 if err := json.Unmarshal(value, &impVal); err != nil { 99 t.Errorf("Failed to unmarshal some-imp: %v", err) 100 } 101 if len(impVal) != 1 { 102 t.Errorf("Unexpected impVal length. Expected %d, Got %d", 1, len(impVal)) 103 } 104 data, hadKey := impVal["imp"] 105 if !hadKey { 106 t.Errorf("some-imp should have had a \"imp\" key, but it didn't.") 107 } 108 if !data { 109 t.Errorf(`Bad data in "imp" of stored request "some-imp". Expected true, Got %t`, data) 110 } 111 } 112 113 func assertErrorCount(t *testing.T, num int, errs []error) { 114 t.Helper() 115 if len(errs) != num { 116 t.Errorf("Wrong number of errors. Expected %d. Got %d. Errors are %v", num, len(errs), errs) 117 } 118 } 119 120 func newCategoryFetcher(directory string) (stored_requests.CategoryFetcher, error) { 121 fetcher, err := NewFileFetcher(directory) 122 if err != nil { 123 return nil, err 124 } 125 catfetcher, ok := fetcher.(stored_requests.CategoryFetcher) 126 if !ok { 127 return nil, fmt.Errorf("Failed to type cast fetcher to CategoryFetcher") 128 } 129 return catfetcher, nil 130 } 131 132 func TestCategoriesFetcherWithPublisher(t *testing.T) { 133 fetcher, err := newCategoryFetcher("./test/category-mapping") 134 if err != nil { 135 t.Errorf("Failed to create a category Fetcher: %v", err) 136 } 137 category, err := fetcher.FetchCategories(nil, "test", "categories", "IAB1-1") 138 assert.Equal(t, nil, err, "Categories were loaded incorrectly") 139 assert.Equal(t, "Beverages", category, "Categories were loaded incorrectly") 140 } 141 142 func TestCategoriesFetcherWithoutPublisher(t *testing.T) { 143 fetcher, err := newCategoryFetcher("./test/category-mapping") 144 if err != nil { 145 t.Errorf("Failed to create a category Fetcher: %v", err) 146 } 147 category, err := fetcher.FetchCategories(nil, "test", "", "IAB1-1") 148 assert.Equal(t, nil, err, "Categories were loaded incorrectly") 149 assert.Equal(t, "VideoGames", category, "Categories were loaded incorrectly") 150 } 151 152 func TestCategoriesFetcherNoCategory(t *testing.T) { 153 fetcher, err := newCategoryFetcher("./test/category-mapping") 154 if err != nil { 155 t.Errorf("Failed to create a category Fetcher: %v", err) 156 } 157 _, fetchingErr := fetcher.FetchCategories(nil, "test", "", "IAB1-100") 158 assert.Equal(t, fmt.Errorf("Unable to find category for adserver 'test', publisherId: '', iab category: 'IAB1-100'"), 159 fetchingErr, "Categories were loaded incorrectly") 160 } 161 162 func TestCategoriesFetcherBrokenJson(t *testing.T) { 163 fetcher, err := newCategoryFetcher("./test/category-mapping") 164 if err != nil { 165 t.Errorf("Failed to create a category Fetcher: %v", err) 166 } 167 _, fetchingErr := fetcher.FetchCategories(nil, "test", "broken", "IAB1-100") 168 assert.Equal(t, fmt.Errorf("Unable to unmarshal categories for adserver: 'test', publisherId: 'broken'"), 169 fetchingErr, "Categories were loaded incorrectly") 170 } 171 172 func TestCategoriesFetcherNoCategoriesFile(t *testing.T) { 173 fetcher, err := newCategoryFetcher("./test/category-mapping") 174 if err != nil { 175 t.Errorf("Failed to create a category Fetcher: %v", err) 176 } 177 _, fetchingErr := fetcher.FetchCategories(nil, "test", "not_exists", "IAB1-100") 178 assert.Equal(t, fmt.Errorf("Unable to find mapping file for adserver: 'test', publisherId: 'not_exists'"), 179 fetchingErr, "Categories were loaded incorrectly") 180 }