Skip to content

Commit

Permalink
Add filtering by null for Auth field
Browse files Browse the repository at this point in the history
Fixes issue #9.
  • Loading branch information
davemachado committed Dec 16, 2018
1 parent 26d3462 commit f2d959f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func parseCategories(entries []Entry) []string {
func checkEntryMatches(entry Entry, request *SearchRequest) bool {
if strings.Contains(strings.ToLower(entry.API), strings.ToLower(request.Title)) &&
strings.Contains(strings.ToLower(entry.Description), strings.ToLower(request.Description)) &&
strings.Contains(strings.ToLower(entry.Auth), strings.ToLower(request.Auth)) &&
((strings.ToLower(request.Auth) == "null" && entry.Auth == "") || strings.Contains(strings.ToLower(entry.Auth), strings.ToLower(request.Auth))) &&
strings.Contains(strings.ToLower(entry.Cors), strings.ToLower(request.Cors)) &&
strings.Contains(strings.ToLower(entry.Category), strings.ToLower(request.Category)) {
if request.HTTPS == "" {
Expand Down
56 changes: 42 additions & 14 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,55 @@ func TestGetCategories(t *testing.T) {
func TestCheckEntryMatches(t *testing.T) {
entry := Entry{
API: "examplesAsAService",
Description: "provide classic examples of classic things",
Description: "provide classic examples",
Auth: "apiKey",
HTTPS: true,
Cors: "Unknown",
Link: "http:https://www.example.com",
Category: "Development",
}
search := &SearchRequest{}
if !checkEntryMatches(entry, search) {
t.Errorf("failed to match entry and search")
}
search.HTTPS = "true"
if !checkEntryMatches(entry, search) {
t.Errorf("failed to match entry and search")
entryEmptyAuth := Entry{
API: "examplesAsAService",
Description: "provide classic examples",
Auth: "",
HTTPS: true,
Cors: "Unknown",
Link: "http:https://www.example.com",
Category: "Development",
}
search.Auth = "OAuth"
if checkEntryMatches(entry, search) {
t.Errorf("failed to match entry and search")

testCases := []struct {
name string
entry Entry
search *SearchRequest
shouldPass bool
}{
{"Full search", entry, &SearchRequest{}, true},
{"Desc valid full", entry, &SearchRequest{Description: "provide classic examples"}, true},
{"Desc valid match", entry, &SearchRequest{Description: "provide class"}, true},
{"Desc invalid", entry, &SearchRequest{Description: "this will not match"}, false},
{"Auth valid full", entry, &SearchRequest{Auth: "apiKey"}, true},
{"Auth valid match", entry, &SearchRequest{Auth: "apiK"}, true},
{"Auth empty", entry, &SearchRequest{Auth: ""}, true},
{"Auth empty entry", entryEmptyAuth, &SearchRequest{Auth: ""}, true},
{"Auth null", entry, &SearchRequest{Auth: "null"}, false},
{"Auth null empty entry", entryEmptyAuth, &SearchRequest{Auth: "null"}, true},
{"Auth invalid", entry, &SearchRequest{Auth: "foo"}, false},
{"HTTPS true", entry, &SearchRequest{HTTPS: "1"}, true},
{"HTTPS false", entry, &SearchRequest{HTTPS: "false"}, false},
{"CORS valid full", entry, &SearchRequest{Cors: "unknown"}, true},
{"CORS valid match", entry, &SearchRequest{Cors: "unk"}, true},
{"CORS invalid", entry, &SearchRequest{Cors: "bar"}, false},
}
search.Cors = "unknown"
if checkEntryMatches(entry, search) {
t.Errorf("failed to match entry and search")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if checkEntryMatches(tc.entry, tc.search) != tc.shouldPass {
if tc.shouldPass {
t.Errorf("was expecting to pass, but failed")
} else {
t.Errorf("was expecting to fail, but passed")
}
}
})
}
}

0 comments on commit f2d959f

Please sign in to comment.