Browse Source

Fix tests and underlying bugs

pull/158/head
Richard Lewis 7 years ago
parent
commit
d51c28cf8b
  1. 17
      src/github.com/matrix-org/go-neb/services/imgur/imgur.go
  2. 29
      src/github.com/matrix-org/go-neb/services/imgur/imgur_test.go

17
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
}

29
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())
}

Loading…
Cancel
Save