Skip to content

Commit

Permalink
Improve Imgur tests (#289)
Browse files Browse the repository at this point in the history
This also ensures that we handle bad API responses better
  • Loading branch information
pajlada authored Mar 20, 2022
1 parent 0362f87 commit f435c3e
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 106 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Dev: Improve YouTube tests. (#284)
- Dev: Resolver Check now returns a context. (#287)
- Dev: Improve Wikipedia tests. (#286)
- Dev: Improve Imgur tests. (#289)

## 1.2.3

Expand Down
11 changes: 2 additions & 9 deletions internal/resolvers/imgur/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package imgur

import (
"bytes"
"fmt"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -85,6 +84,7 @@ func finalizeMiniImage(mini *miniImage) {
mini.Link = linkURL.String()
} else {
log.Println("[IMGUR] Error making smaller thumbnail for image:", err, mini)
mini.Link = ""
}
}
}
Expand All @@ -95,18 +95,11 @@ func finalizeMiniImage(mini *miniImage) {
}
}

func internalServerError(message string) (*resolver.Response, time.Duration, error) {
return &resolver.Response{
Status: http.StatusInternalServerError,
Message: "imgur resolver error: " + resolver.CleanResponse(message),
}, cache.NoSpecialDur, nil
}

func buildTooltip(miniData miniImage) (*resolver.Response, time.Duration, error) {
var tooltip bytes.Buffer

if err := imageTooltipTemplate.Execute(&tooltip, &miniData); err != nil {
return internalServerError(fmt.Sprintf("Error building template: %s", err.Error()))
return resolver.Errorf("Imgur template error: %s", err)
}

return &resolver.Response{
Expand Down
79 changes: 0 additions & 79 deletions internal/resolvers/imgur/helpers_test.go

This file was deleted.

10 changes: 9 additions & 1 deletion internal/resolvers/imgur/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/config"
"github.com/Chatterino/api/pkg/resolver"
"github.com/koffeinsource/go-imgur"
)

var (
Expand All @@ -24,5 +25,12 @@ func Initialize(ctx context.Context, cfg config.APIConfig, pool db.Pool, resolve
return
}

*resolvers = append(*resolvers, NewResolver(ctx, cfg, pool))
imgurClient := &imgur.Client{
HTTPClient: resolver.HTTPClient(),
Log: &NullLogger{},
ImgurClientID: cfg.ImgurClientID,
RapidAPIKEY: "",
}

*resolvers = append(*resolvers, NewResolver(ctx, cfg, pool, imgurClient))
}
38 changes: 38 additions & 0 deletions internal/resolvers/imgur/initialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package imgur

import (
"context"
"testing"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/config"
"github.com/Chatterino/api/pkg/resolver"
qt "github.com/frankban/quicktest"
"github.com/pashagolub/pgxmock"
)

func TestInitialize(t *testing.T) {
ctx := logger.OnContext(context.Background(), logger.NewTest())
c := qt.New(t)

pool, err := pgxmock.NewPool()
c.Assert(err, qt.IsNil)

c.Run("No credentials", func(c *qt.C) {
cfg := config.APIConfig{}
customResolvers := []resolver.Resolver{}
c.Assert(customResolvers, qt.HasLen, 0)
Initialize(ctx, cfg, pool, &customResolvers)
c.Assert(customResolvers, qt.HasLen, 0)
})

c.Run("Credentials", func(c *qt.C) {
cfg := config.APIConfig{
ImgurClientID: "test",
}
customResolvers := []resolver.Resolver{}
c.Assert(customResolvers, qt.HasLen, 0)
Initialize(ctx, cfg, pool, &customResolvers)
c.Assert(customResolvers, qt.HasLen, 1)
})
}
33 changes: 24 additions & 9 deletions internal/resolvers/imgur/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"time"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/cache"
"github.com/Chatterino/api/pkg/resolver"
"github.com/Chatterino/api/pkg/utils"
Expand All @@ -16,12 +17,23 @@ type Loader struct {
}

func (l *Loader) Load(ctx context.Context, urlString string, r *http.Request) (*resolver.Response, time.Duration, error) {
log := logger.FromContext(ctx)

genericInfo, _, err := l.apiClient.GetInfoFromURL(urlString)
if err != nil {
return &resolver.Response{
Status: http.StatusOK,
Tooltip: "Error getting imgur API information for URL",
}, cache.NoSpecialDur, resolver.ErrDontHandle
log.Warnw("Error getting imgur info from URL",
"url", urlString,
"error", err,
)
return nil, cache.NoSpecialDur, resolver.ErrDontHandle
}

if genericInfo == nil {
log.Warnw("Missing imgur info",
"url", urlString,
"error", err,
)
return nil, cache.NoSpecialDur, resolver.ErrDontHandle
}

var miniData miniImage
Expand Down Expand Up @@ -59,14 +71,17 @@ func (l *Loader) Load(ctx context.Context, urlString string, r *http.Request) (*
miniData.Title = ptr.Title
miniData.Description = ptr.Description
} else {
return &resolver.Response{
Status: http.StatusOK,
Tooltip: "Error getting imgur API information for URL",
}, cache.NoSpecialDur, nil
log.Warnw("Missing relevant imgur response",
"url", urlString,
)

return nil, resolver.NoSpecialDur, resolver.ErrDontHandle
}

// Proxy imgur thumbnails
miniData.Link = utils.FormatThumbnailURL(l.baseURL, r, miniData.Link)
if miniData.Link != "" {
miniData.Link = utils.FormatThumbnailURL(l.baseURL, r, miniData.Link)
}

return buildTooltip(miniData)
}
11 changes: 3 additions & 8 deletions internal/resolvers/imgur/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,10 @@ func (r *Resolver) Name() string {
return "imgur"
}

func NewResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool) *Resolver {
func NewResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool, imgurClient ImgurClient) *Resolver {
loader := &Loader{
baseURL: cfg.BaseURL,
apiClient: &imgur.Client{
HTTPClient: resolver.HTTPClient(),
Log: &NullLogger{},
ImgurClientID: cfg.ImgurClientID,
RapidAPIKEY: "",
},
baseURL: cfg.BaseURL,
apiClient: imgurClient,
}

r := &Resolver{
Expand Down
Loading

0 comments on commit f435c3e

Please sign in to comment.