Skip to content

Commit

Permalink
fix: prevent users from passing watchlist id to the imdb lists config
Browse files Browse the repository at this point in the history
resolves #63
  • Loading branch information
cecobask committed Aug 24, 2024
1 parent 1584180 commit 1704a12
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 97 deletions.
3 changes: 3 additions & 0 deletions internal/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func (s *Syncer) hydrate() error {
}

func (s *Syncer) syncLists() error {
if !*s.conf.Watchlist {
s.logger.Info("skipping watchlist sync")
}
for _, list := range s.user.imdbLists {
traktListSlug := entities.InferTraktListSlug(list.ListName)
diff := entities.ListDifference(list, s.user.traktLists[list.ListID])
Expand Down
5 changes: 4 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type TraktClientInterface interface {
ListItemsAdd(listID string, items entities.TraktItems) error
ListItemsRemove(listID string, items entities.TraktItems) error
ListAdd(listID, listName string) error
ListRemove(listID string) error
RatingsGet() (entities.TraktItems, error)
RatingsAdd(items entities.TraktItems) error
RatingsRemove(items entities.TraktItems) error
Expand Down Expand Up @@ -121,3 +120,7 @@ type TraktListNotFoundError struct {
func (e *TraktListNotFoundError) Error() string {
return fmt.Sprintf("list with id %s could not be found", e.Slug)
}

func pointer[T any](v T) *T {
return &v
}
15 changes: 11 additions & 4 deletions pkg/client/imdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,21 @@ func (c *IMDbClient) hydrate() error {
return fmt.Errorf("failure extracting watchlist id from href: %w", err)
}
c.config.watchlistID = watchlistID
if len(*c.config.Lists) == 0 {
lids, err := c.lidsScrape()
lids := slices.DeleteFunc(*c.config.Lists, func(lid string) bool {
if lid == watchlistID {
c.logger.Warn("removing watchlist id from provided lists; please use config option SYNC_WATCHLIST instead")
return true
}
return false
})
if len(lids) == 0 {
lids, err = c.lidsScrape()
if err != nil {
return fmt.Errorf("failure scraping list ids: %w", err)
}
c.config.Lists = &lids
}
c.logger.Info("hydrated imdb client", slog.String("username", username), slog.String("userID", userID), slog.String("watchlistID", watchlistID), slog.Any("lists", c.config.Lists))
c.config.Lists = &lids
c.logger.Info("hydrated imdb client", slog.String("username", username), slog.String("userID", userID), slog.String("watchlistID", watchlistID), slog.Any("lists", lids))
return nil
}

Expand Down
16 changes: 0 additions & 16 deletions pkg/client/trakt.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,22 +554,6 @@ func (tc *TraktClient) ListAdd(listID, listName string) error {
return nil
}

func (tc *TraktClient) ListRemove(listID string) error {
response, err := tc.doRequest(requestFields{
Method: http.MethodDelete,
BasePath: traktPathBaseAPI,
Endpoint: fmt.Sprintf(traktPathUserList, tc.config.username, listID),
Body: http.NoBody,
Headers: tc.defaultApiHeaders(),
})
if err != nil {
return err
}
defer response.Body.Close()
tc.logger.Info(fmt.Sprintf("removed trakt list %s", listID))
return nil
}

func (tc *TraktClient) RatingsGet() (entities.TraktItems, error) {
response, err := tc.doRequest(requestFields{
Method: http.MethodGet,
Expand Down
80 changes: 4 additions & 76 deletions pkg/client/trakt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func buildTestTraktClient(config traktConfig) *TraktClient {
}
}

func stringPointer(s string) *string {
return &s
}

var (
dummyUsername = "cecobask"
dummyListID = "watched"
Expand All @@ -41,10 +37,10 @@ var (
dummyUserCode = "0e887e88"
dummyDeviceCode = "4eca8122d271cf8a17f96b00326d2e83c8e699ee8cb836f9d812aa71cb535b6b"
dummyAppConfigTrakt = appconfig.Trakt{
Email: stringPointer(""),
Password: stringPointer(""),
ClientID: stringPointer(""),
ClientSecret: stringPointer(""),
Email: pointer(""),
Password: pointer(""),
ClientID: pointer(""),
ClientSecret: pointer(""),
}
dummyConfig = traktConfig{
Trakt: dummyAppConfigTrakt,
Expand Down Expand Up @@ -1006,74 +1002,6 @@ func TestTraktClient_ListAdd(t *testing.T) {
}
}

func TestTraktClient_ListRemove(t *testing.T) {
type fields struct {
config traktConfig
}
type args struct {
listID string
}
tests := []struct {
name string
fields fields
args args
requirements func()
assertions func(*assert.Assertions, error)
}{
{
name: "successfully remove list",
fields: fields{
config: dummyConfig,
},
args: args{
listID: dummyListID,
},
requirements: func() {
httpmock.RegisterResponder(
http.MethodDelete,
fmt.Sprintf(traktPathBaseAPI+traktPathUserList, dummyUsername, dummyListID),
httpmock.NewJsonResponderOrPanic(http.StatusNoContent, nil),
)
},
assertions: func(assertions *assert.Assertions, err error) {
assertions.NoError(err)
},
},
{
name: "failure removing list",
fields: fields{
config: dummyConfig,
},
args: args{
listID: dummyListID,
},
requirements: func() {
httpmock.RegisterResponder(
http.MethodDelete,
fmt.Sprintf(traktPathBaseAPI+traktPathUserList, dummyUsername, dummyListID),
httpmock.NewJsonResponderOrPanic(http.StatusInternalServerError, nil),
)
},
assertions: func(assertions *assert.Assertions, err error) {
assertions.Error(err)
var apiError *ApiError
assertions.True(errors.As(err, &apiError))
assertions.Equal(http.StatusInternalServerError, apiError.StatusCode)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
tt.requirements()
c := buildTestTraktClient(tt.fields.config)
err := c.ListRemove(tt.args.listID)
tt.assertions(assert.New(t), err)
})
}
}

func TestTraktClient_RatingsGet(t *testing.T) {
type fields struct {
config traktConfig
Expand Down

0 comments on commit 1704a12

Please sign in to comment.