From 2b03f4f88461a9f6d5371c0f7f28faea0647d65d Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Wed, 26 Oct 2022 18:28:11 -0400 Subject: [PATCH] be more forgiving with reddit errors --- internal/domain/subreddit_test.go | 1 + internal/reddit/client.go | 17 +++++++++++++++ internal/reddit/client_test.go | 33 ++++++++++++++++++++++++++++++ internal/reddit/types_test.go | 3 ++- internal/worker/live_activities.go | 29 ++++++++++++-------------- 5 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 internal/reddit/client_test.go diff --git a/internal/domain/subreddit_test.go b/internal/domain/subreddit_test.go index 7ab7adb..e0745ab 100644 --- a/internal/domain/subreddit_test.go +++ b/internal/domain/subreddit_test.go @@ -26,6 +26,7 @@ func TestValidate(t *testing.T) { } for scenario, tc := range tt { + tc := tc t.Run(scenario, func(t *testing.T) { t.Parallel() diff --git a/internal/reddit/client.go b/internal/reddit/client.go index dc09692..843c6cf 100644 --- a/internal/reddit/client.go +++ b/internal/reddit/client.go @@ -284,6 +284,23 @@ func (rc *Client) SubredditAbout(ctx context.Context, subreddit string, opts ... return sr, nil } +func obfuscate(tok string) string { + tl := len(tok) + if tl < 6 { + return "" + } + + 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) { if rac.isRateLimited() { return nil, ErrRateLimited diff --git a/internal/reddit/client_test.go b/internal/reddit/client_test.go new file mode 100644 index 0000000..ac6e79b --- /dev/null +++ b/internal/reddit/client_test.go @@ -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("", "", nil, nil, 1) + + type test struct { + have string + want string + } + + tests := []test{ + {"abc", ""}, + {"abcdefghi", "abc...ghi"}, + } + + for _, tc := range tests { + tc := tc + rac := rc.NewAuthenticatedClient("", "", tc.have) + got := rac.ObfuscatedAccessToken() + + assert.Equal(t, tc.want, got) + } +} diff --git a/internal/reddit/types_test.go b/internal/reddit/types_test.go index 70fcd6a..d4debab 100644 --- a/internal/reddit/types_test.go +++ b/internal/reddit/types_test.go @@ -5,9 +5,10 @@ import ( "testing" "time" - "github.com/christianselig/apollo-backend/internal/reddit" "github.com/stretchr/testify/assert" "github.com/valyala/fastjson" + + "github.com/christianselig/apollo-backend/internal/reddit" ) var pool = &fastjson.ParserPool{} diff --git a/internal/worker/live_activities.go b/internal/worker/live_activities.go index 914e4e0..cb5c7b8 100644 --- a/internal/worker/live_activities.go +++ b/internal/worker/live_activities.go @@ -173,26 +173,20 @@ func (lac *liveActivitiesConsumer) Consume(delivery rmq.Delivery) { if la.TokenExpiresAt.Before(now.Add(5 * time.Minute)) { lac.logger.Debug("refreshing reddit token", 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) if err != nil { - if err != reddit.ErrOauthRevoked { - lac.logger.Error("failed to refresh reddit tokens", - zap.Error(err), - zap.String("live_activity#apns_token", at), - ) - 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), - ) - } - + lac.logger.Error("failed to refresh reddit tokens", + zap.Error(err), + 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 } @@ -213,6 +207,9 @@ func (lac *liveActivitiesConsumer) Consume(delivery rmq.Delivery) { lac.logger.Error("failed to fetch latest comments", zap.Error(err), 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 }