From d51c28cf8bae7bdb5dc6c61c6eac3d6f9f40f497 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Mon, 20 Feb 2017 16:57:50 +0000 Subject: [PATCH] Fix tests and underlying bugs --- .../matrix-org/go-neb/services/imgur/imgur.go | 17 +++++++---- .../go-neb/services/imgur/imgur_test.go | 29 ++++++++++++++----- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go index c8a4bfd..913718f 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur.go @@ -114,15 +114,15 @@ type Service struct { func (s *Service) Commands(client *gomatrix.Client) []types.Command { return []types.Command{ types.Command{ - Path: []string{"imgur"}, + Path: []string{"imgur", "help"}, Command: func(roomID, userID string, args []string) (interface{}, error) { - return s.cmdImgurImgSearch(client, roomID, userID, args) + return usageMessage(), nil }, }, types.Command{ - Path: []string{"imgur", "help"}, + Path: []string{"imgur"}, Command: func(roomID, userID string, args []string) (interface{}, error) { - return usageMessage(), nil + return s.cmdImgurImgSearch(client, roomID, userID, args) }, }, } @@ -224,8 +224,10 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery } var searchResults imgurSearchResponse - if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil || searchResults.Data == nil { + if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil { return nil, nil, fmt.Errorf("No images found - %s", err.Error()) + } else if len(searchResults.Data) < 1 { + return nil, nil, fmt.Errorf("No images found") } log.Printf("%d results were returned from Imgur", len(searchResults.Data)) @@ -238,7 +240,10 @@ func (s *Service) text2imgImgur(query string) (*imgurGalleryImage, *imgurGallery } } if len(images) > 0 { - r := rand.Intn(len(images) - 1) + var r = 0 + if len(images) > 1 { + r = rand.Intn(len(images) - 1) + } return &images[r], nil, nil } diff --git a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go index 6fefae2..14a1f4a 100644 --- a/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go +++ b/src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go @@ -18,7 +18,8 @@ import ( func TestCommand(t *testing.T) { database.SetServiceDB(&database.NopStorage{}) clientID := "My ID" - imgurImageURL := "https://api.imgur.com/3/gallery/search" + imgurImageURL := "http://i.imgur.com/cat.jpg" + testSearchString := "Czechoslovakian bananna" // Mock the response from imgur imgurTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) { @@ -41,16 +42,30 @@ func TestCommand(t *testing.T) { // Check the search query var searchString = query.Get("q") - if !strings.HasPrefix(searchString, "image") { - t.Fatalf("Bad search string: got \"%s\"", searchString) + if searchString != testSearchString { + t.Fatalf("Bad search string - got: \"%s\", expected: \"%s\"", testSearchString, searchString) } - res := imgurGalleryImage{ + img := imgurGalleryImage{ Title: "A Cat", - Link: "http://i.imgur.com/cat.jpg", + Link: imgurImageURL, Type: "image/jpeg", } + imgJSON, err := json.Marshal(img) + if err != nil { + t.Fatalf("Failed to Marshal test image data - %s", err) + } + rawImageJSON := json.RawMessage(imgJSON) + + res := imgurSearchResponse{ + Data: []json.RawMessage{ + rawImageJSON, + }, + Success: true, + Status: 200, + } + b, err := json.Marshal(res) if err != nil { t.Fatalf("Failed to marshal imgur response - %s", err) @@ -98,8 +113,8 @@ func TestCommand(t *testing.T) { if len(cmds) != 2 { t.Fatalf("Unexpected number of commands: %d", len(cmds)) } - cmd := cmds[0] - _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{"image", "Czechoslovakian bananna"}) + cmd := cmds[1] + _, err = cmd.Command("!someroom:hyrule", "@navi:hyrule", []string{testSearchString}) if err != nil { t.Fatalf("Failed to process command: %s", err.Error()) }