2022-05-07 16:37:21 +00:00
|
|
|
package reddit_test
|
2021-08-14 17:42:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-05-07 16:37:21 +00:00
|
|
|
"context"
|
2021-08-14 17:42:28 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/DataDog/datadog-go/statsd"
|
2022-05-07 16:37:21 +00:00
|
|
|
"github.com/christianselig/apollo-backend/internal/reddit"
|
2022-03-26 17:29:58 +00:00
|
|
|
"github.com/go-redis/redismock/v8"
|
2021-08-14 17:42:28 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RoundTripFunc .
|
|
|
|
type RoundTripFunc func(req *http.Request) *http.Response
|
|
|
|
|
|
|
|
// RoundTrip .
|
|
|
|
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
return f(req), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewTestClient returns *http.Client with Transport replaced to avoid making real calls
|
|
|
|
func NewTestClient(fn RoundTripFunc) *http.Client {
|
2022-05-07 16:37:21 +00:00
|
|
|
return &http.Client{Transport: fn}
|
2021-08-14 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorResponse(t *testing.T) {
|
2022-05-07 16:37:21 +00:00
|
|
|
t.Parallel()
|
2022-03-26 17:29:58 +00:00
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
db, _ := redismock.NewClientMock()
|
2021-08-14 17:42:28 +00:00
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
errortests := map[string]struct {
|
|
|
|
call func(*reddit.AuthenticatedClient) error
|
2021-08-14 17:42:28 +00:00
|
|
|
|
|
|
|
status int
|
|
|
|
body string
|
|
|
|
err error
|
|
|
|
}{
|
2022-05-07 16:37:21 +00:00
|
|
|
"/api/v1/me 500 returns ServerError": {func(rac *reddit.AuthenticatedClient) error { _, err := rac.Me(ctx); return err }, 500, "", reddit.ServerError{500}},
|
|
|
|
"/api/v1/access_token 400 returns ErrOauthRevoked": {func(rac *reddit.AuthenticatedClient) error { _, err := rac.RefreshTokens(ctx); return err }, 400, "", reddit.ErrOauthRevoked},
|
|
|
|
"/api/v1/message/inbox 403 returns ErrOauthRevoked": {func(rac *reddit.AuthenticatedClient) error { _, err := rac.MessageInbox(ctx); return err }, 403, "", reddit.ErrOauthRevoked},
|
|
|
|
"/api/v1/message/unread 403 returns ErrOauthRevoked": {func(rac *reddit.AuthenticatedClient) error { _, err := rac.MessageUnread(ctx); return err }, 403, "", reddit.ErrOauthRevoked},
|
|
|
|
"/api/v1/me 403 returns ErrOauthRevoked": {func(rac *reddit.AuthenticatedClient) error { _, err := rac.Me(ctx); return err }, 403, "", reddit.ErrOauthRevoked},
|
2021-08-14 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
for scenario, tt := range errortests {
|
|
|
|
tt := tt
|
|
|
|
|
|
|
|
t.Run(scenario, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tc := NewTestClient(func(req *http.Request) *http.Response {
|
2021-08-14 17:42:28 +00:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: tt.status,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewBufferString(tt.body)),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
rc := reddit.NewClient("", "", &statsd.NoOpClient{}, db, 1, reddit.WithRetry(false), reddit.WithClient(tc))
|
|
|
|
rac := rc.NewAuthenticatedClient(reddit.SkipRateLimiting, "", "")
|
|
|
|
|
|
|
|
err := tt.call(rac)
|
2021-08-14 17:42:28 +00:00
|
|
|
|
|
|
|
assert.ErrorIs(t, err, tt.err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|