mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-14 16:07:42 +00:00
be more forgiving with reddit errors
This commit is contained in:
parent
840d644a4e
commit
2b03f4f884
5 changed files with 66 additions and 17 deletions
|
@ -26,6 +26,7 @@ func TestValidate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for scenario, tc := range tt {
|
for scenario, tc := range tt {
|
||||||
|
tc := tc
|
||||||
t.Run(scenario, func(t *testing.T) {
|
t.Run(scenario, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
|
@ -284,6 +284,23 @@ func (rc *Client) SubredditAbout(ctx context.Context, subreddit string, opts ...
|
||||||
return sr, nil
|
return sr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func obfuscate(tok string) string {
|
||||||
|
tl := len(tok)
|
||||||
|
if tl < 6 {
|
||||||
|
return "<SHORT>"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s...%s", tok[0:3], tok[tl-3:tl])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *AuthenticatedClient) ObfuscatedAccessToken() string {
|
||||||
|
return obfuscate(rac.accessToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rac *AuthenticatedClient) ObfuscatedRefreshToken() string {
|
||||||
|
return obfuscate(rac.refreshToken)
|
||||||
|
}
|
||||||
|
|
||||||
func (rac *AuthenticatedClient) request(ctx context.Context, r *Request, rh ResponseHandler, empty interface{}) (interface{}, error) {
|
func (rac *AuthenticatedClient) request(ctx context.Context, r *Request, rh ResponseHandler, empty interface{}) (interface{}, error) {
|
||||||
if rac.isRateLimited() {
|
if rac.isRateLimited() {
|
||||||
return nil, ErrRateLimited
|
return nil, ErrRateLimited
|
||||||
|
|
33
internal/reddit/client_test.go
Normal file
33
internal/reddit/client_test.go
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package reddit_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/christianselig/apollo-backend/internal/reddit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAuthenticatedClientObfuscatedToken(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
rc := reddit.NewClient("<SECRET>", "<SECRET>", nil, nil, 1)
|
||||||
|
|
||||||
|
type test struct {
|
||||||
|
have string
|
||||||
|
want string
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []test{
|
||||||
|
{"abc", "<SHORT>"},
|
||||||
|
{"abcdefghi", "abc...ghi"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
tc := tc
|
||||||
|
rac := rc.NewAuthenticatedClient("<ID>", "<REFRESH>", tc.have)
|
||||||
|
got := rac.ObfuscatedAccessToken()
|
||||||
|
|
||||||
|
assert.Equal(t, tc.want, got)
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,9 +5,10 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/christianselig/apollo-backend/internal/reddit"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/valyala/fastjson"
|
"github.com/valyala/fastjson"
|
||||||
|
|
||||||
|
"github.com/christianselig/apollo-backend/internal/reddit"
|
||||||
)
|
)
|
||||||
|
|
||||||
var pool = &fastjson.ParserPool{}
|
var pool = &fastjson.ParserPool{}
|
||||||
|
|
|
@ -173,29 +173,23 @@ func (lac *liveActivitiesConsumer) Consume(delivery rmq.Delivery) {
|
||||||
if la.TokenExpiresAt.Before(now.Add(5 * time.Minute)) {
|
if la.TokenExpiresAt.Before(now.Add(5 * time.Minute)) {
|
||||||
lac.logger.Debug("refreshing reddit token",
|
lac.logger.Debug("refreshing reddit token",
|
||||||
zap.String("live_activity#apns_token", at),
|
zap.String("live_activity#apns_token", at),
|
||||||
|
zap.String("reddit#id", la.RedditAccountID),
|
||||||
|
zap.String("reddit#access_token", rac.ObfuscatedAccessToken()),
|
||||||
|
zap.String("reddit#refresh_token", rac.ObfuscatedRefreshToken()),
|
||||||
)
|
)
|
||||||
|
|
||||||
tokens, err := rac.RefreshTokens(lac)
|
tokens, err := rac.RefreshTokens(lac)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != reddit.ErrOauthRevoked {
|
|
||||||
lac.logger.Error("failed to refresh reddit tokens",
|
lac.logger.Error("failed to refresh reddit tokens",
|
||||||
zap.Error(err),
|
zap.Error(err),
|
||||||
zap.String("live_activity#apns_token", at),
|
zap.String("live_activity#apns_token", at),
|
||||||
|
zap.String("reddit#id", la.RedditAccountID),
|
||||||
|
zap.String("reddit#access_token", rac.ObfuscatedAccessToken()),
|
||||||
|
zap.String("reddit#refresh_token", rac.ObfuscatedRefreshToken()),
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = lac.liveActivityRepo.Delete(lac, at)
|
|
||||||
if err != nil {
|
|
||||||
lac.logger.Error("failed to remove revoked account",
|
|
||||||
zap.Error(err),
|
|
||||||
zap.String("live_activity#apns_token", at),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update account
|
// Update account
|
||||||
la.AccessToken = tokens.AccessToken
|
la.AccessToken = tokens.AccessToken
|
||||||
la.RefreshToken = tokens.RefreshToken
|
la.RefreshToken = tokens.RefreshToken
|
||||||
|
@ -213,6 +207,9 @@ func (lac *liveActivitiesConsumer) Consume(delivery rmq.Delivery) {
|
||||||
lac.logger.Error("failed to fetch latest comments",
|
lac.logger.Error("failed to fetch latest comments",
|
||||||
zap.Error(err),
|
zap.Error(err),
|
||||||
zap.String("live_activity#apns_token", at),
|
zap.String("live_activity#apns_token", at),
|
||||||
|
zap.String("reddit#id", la.RedditAccountID),
|
||||||
|
zap.String("reddit#access_token", rac.ObfuscatedAccessToken()),
|
||||||
|
zap.String("reddit#refresh_token", rac.ObfuscatedRefreshToken()),
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue