Skip to content

Commit

Permalink
Improve BetterTTV emote tests (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada committed Mar 13, 2022
1 parent ec8a83d commit a70c387
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 88 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
## Unreleased

- Breaking: Resolver caches are now stored in PostgreSQL. See [docs/build.md](./docs/build.md) for prerequisite instructions. (#271)
- Fix: SevenTV emotes now resolve correctly.
- Fix: SevenTV emotes now resolve correctly. (#281)
- Dev: Improve BetterTTV emote tests. (#282)
- Minor: BetterTTV cache key changed from plural to singular form. (#282)

## 1.2.3

Expand Down
2 changes: 1 addition & 1 deletion internal/resolvers/betterttv/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func testServer() *httptest.Server {

w.Header().Set("Content-Type", "application/json")

if emote == "bad_json" {
if emote == "bad" {
w.Write([]byte("xD"))
} else if response, ok = data[emote]; !ok {
http.Error(w, http.StatusText(404), 404)
Expand Down
54 changes: 0 additions & 54 deletions internal/resolvers/betterttv/emote_loader_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package betterttv

import (
"context"
"net/url"
"testing"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/utils"
qt "github.com/frankban/quicktest"
)
Expand Down Expand Up @@ -47,55 +45,3 @@ func TestBuildURL(t *testing.T) {
})
}
}

func TestLoad(t *testing.T) {
ctx := logger.OnContext(context.Background(), logger.NewTest())
c := qt.New(t)
ts := testServer()
defer ts.Close()
loader := NewEmoteLoader(utils.MustParseURL(ts.URL + "/3/emotes/"))

type tc struct {
emoteHash string
expectedTooltip string
expectedMessage string
}

tests := []tc{
{
emoteHash: "kkona",
expectedTooltip: `<div style="text-align: left;"><b>KKona</b><br><b>Global BetterTTV Emote</b><br><b>By:</b> zneix</div>`,
},
{
emoteHash: "kkona_html",
expectedTooltip: `<div style="text-align: left;"><b>&lt;b&gt;KKona&lt;/b&gt;</b><br><b>Global BetterTTV Emote</b><br><b>By:</b> &lt;b&gt;zneix&lt;/b&gt;</div>`,
},
{
emoteHash: "forsenga",
expectedTooltip: `<div style="text-align: left;"><b>forsenGa</b><br><b>Shared BetterTTV Emote</b><br><b>By:</b> pajlada</div>`,
},
{
emoteHash: "forsenga_html",
expectedTooltip: `<div style="text-align: left;"><b>&lt;b&gt;forsenGa&lt;/b&gt;</b><br><b>Shared BetterTTV Emote</b><br><b>By:</b> &lt;b&gt;pajlada&lt;/b&gt;</div>`,
},
{
emoteHash: "bad_json",
expectedMessage: `betterttv api unmarshal error: invalid character &#39;x&#39; looking for beginning of value`,
},
}

for _, test := range tests {
c.Run(test.emoteHash, func(c *qt.C) {
response, _, err := loader.Load(ctx, test.emoteHash, nil)

c.Assert(err, qt.IsNil)
c.Assert(response, qt.Not(qt.IsNil))

cleanTooltip, unescapeErr := url.PathUnescape(response.Tooltip)
c.Assert(unescapeErr, qt.IsNil)

c.Assert(cleanTooltip, qt.Equals, test.expectedTooltip)
c.Assert(response.Message, qt.Equals, test.expectedMessage)
})
}
}
2 changes: 1 addition & 1 deletion internal/resolvers/betterttv/emote_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewEmoteResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool, e
emoteLoader := NewEmoteLoader(emoteAPIURL)

r := &EmoteResolver{
emoteCache: cache.NewPostgreSQLCache(ctx, cfg, pool, "betterttv:emotes", resolver.NewResponseMarshaller(emoteLoader), 1*time.Hour),
emoteCache: cache.NewPostgreSQLCache(ctx, cfg, pool, "betterttv:emote", resolver.NewResponseMarshaller(emoteLoader), 1*time.Hour),
}

return r
Expand Down
44 changes: 13 additions & 31 deletions internal/resolvers/betterttv/emote_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package betterttv

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/config"
"github.com/Chatterino/api/pkg/utils"
qt "github.com/frankban/quicktest"
"github.com/go-chi/chi/v5"
"github.com/golang/mock/gomock"
"github.com/jackc/pgx/v4"
"github.com/pashagolub/pgxmock"
Expand All @@ -28,30 +25,7 @@ func TestEmoteResolver(t *testing.T) {
pool, _ := pgxmock.NewPool()

cfg := config.APIConfig{}
r := chi.NewRouter()
r.Get("/3/emotes/{emote}", func(w http.ResponseWriter, r *http.Request) {
emote := chi.URLParam(r, "emote")

var response *EmoteAPIResponse
var ok bool

w.Header().Set("Content-Type", "application/json")

if emote == "500" {
// response = &resolver.Response{
// Status: http.StatusInternalServerError,
// Message: "betterttv http request error "
// }
} else if response, ok = data[emote]; !ok {
http.Error(w, http.StatusText(404), 404)
return
}

b, _ := json.Marshal(&response)

w.Write(b)
})
ts := httptest.NewServer(r)
ts := testServer()
defer ts.Close()
emoteAPIURL := utils.MustParseURL(ts.URL + "/3/emotes/")

Expand Down Expand Up @@ -173,7 +147,7 @@ func TestEmoteResolver(t *testing.T) {
c.Run(test.label, func(c *qt.C) {
rows := pgxmock.NewRows([]string{"value"}).AddRow(test.expectedBytes)
pool.ExpectQuery("SELECT").
WithArgs("betterttv:emotes:" + test.inputEmoteHash).
WithArgs("betterttv:emote:" + test.inputEmoteHash).
WillReturnRows(rows)
outputBytes, outputError := resolver.Run(ctx, test.inputURL, test.inputReq)
c.Assert(outputError, qt.Equals, test.expectedError)
Expand All @@ -195,21 +169,29 @@ func TestEmoteResolver(t *testing.T) {

tests := []runTest{
{
label: "Matching link - not cached",
label: "Emote",
inputURL: utils.MustParseURL("https://betterttv.com/emotes/566ca04265dbbdab32ec054b"),
inputEmoteHash: "566ca04265dbbdab32ec054b",
inputReq: nil,
expectedBytes: []byte(`{"status":200,"thumbnail":"https://cdn.betterttv.net/emote/566ca04265dbbdab32ec054b/3x","tooltip":"%3Cdiv%20style=%22text-align:%20left%3B%22%3E%3Cb%3EKKona%3C%2Fb%3E%3Cbr%3E%3Cb%3EGlobal%20BetterTTV%20Emote%3C%2Fb%3E%3Cbr%3E%3Cb%3EBy:%3C%2Fb%3E%20zneix%3C%2Fdiv%3E"}`),
expectedError: nil,
},
{
label: "Matching link - 404",
label: "404",
inputURL: utils.MustParseURL("https://betterttv.com/emotes/404"),
inputEmoteHash: "404",
inputReq: nil,
expectedBytes: []byte(`{"status":404,"message":"No BetterTTV emote with this hash found"}`),
expectedError: nil,
},
{
label: "Bad JSON",
inputURL: utils.MustParseURL("https://betterttv.com/emotes/bad"),
inputEmoteHash: "bad",
inputReq: nil,
expectedBytes: []byte(`{"status":500,"message":"betterttv api unmarshal error: invalid character \u0026#39;x\u0026#39; looking for beginning of value"}`),
expectedError: nil,
},
}

const q = `SELECT value FROM cache WHERE key=$1`
Expand All @@ -218,7 +200,7 @@ func TestEmoteResolver(t *testing.T) {
c.Run(test.label, func(c *qt.C) {
pool.ExpectQuery("SELECT").WillReturnError(pgx.ErrNoRows)
pool.ExpectExec("INSERT INTO cache").
WithArgs("betterttv:emotes:"+test.inputEmoteHash, test.expectedBytes, pgxmock.AnyArg()).
WithArgs("betterttv:emote:"+test.inputEmoteHash, test.expectedBytes, pgxmock.AnyArg()).
WillReturnResult(pgxmock.NewResult("INSERT", 1))
outputBytes, outputError := resolver.Run(ctx, test.inputURL, test.inputReq)
c.Assert(outputError, qt.Equals, test.expectedError)
Expand Down

0 comments on commit a70c387

Please sign in to comment.