Skip to content

Commit

Permalink
fix: validate imdb lists ids
Browse files Browse the repository at this point in the history
  • Loading branch information
cecobask committed Jul 31, 2024
1 parent ab61739 commit 7d6aaba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"regexp"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -121,6 +122,9 @@ func (c *Config) Validate() error {
default:
return fmt.Errorf("field 'IMDB_AUTH' must be one of: %s", strings.Join(validIMDbAuthMethods(), ", "))
}
if err := c.validateListIdentifiers(); err != nil {
return fmt.Errorf("field 'IMDB_LISTS' is invalid: %w", err)
}
if isNilOrEmpty(c.Trakt.Email) {
return fmt.Errorf("field 'TRAKT_EMAIL' is required")
}
Expand All @@ -142,6 +146,16 @@ func (c *Config) Validate() error {
return c.checkDummies()
}

func (c *Config) validateListIdentifiers() error {
re := regexp.MustCompile(`^ls[0-9]{9}$`)
for _, id := range *c.IMDb.Lists {
if ok := re.MatchString(id); !ok {
return fmt.Errorf("valid list id starts with ls and is followed by 9 digits, but got %s", id)
}
}
return nil
}

func (c *Config) WriteFile(path string) error {
data, err := c.koanf.Marshal(yaml.Parser())
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func TestConfig_Validate(t *testing.T) {
clientID = "clientID"
clientSecret = "clientSecret"
cookieAtMain = "cookieAtMain"
lists = make([]string, 0)
)

type fields struct {
Expand All @@ -148,6 +149,7 @@ func TestConfig_Validate(t *testing.T) {
Auth: pointer(IMDbAuthMethodCredentials),
Email: &email,
Password: &password,
Lists: &lists,
},
Trakt: Trakt{
Email: &email,
Expand Down Expand Up @@ -229,13 +231,29 @@ func TestConfig_Validate(t *testing.T) {
assertions.Contains(err.Error(), "IMDB_AUTH")
},
},
{
name: "invalid IMDb.Lists",
fields: fields{
IMDb: IMDb{
Auth: pointer(IMDbAuthMethodCredentials),
Email: &email,
Password: &password,
Lists: pointer([]string{"invalid"}),
},
},
assertions: func(assertions *assert.Assertions, err error) {
assertions.NotNil(err)
assertions.Contains(err.Error(), "IMDB_LISTS")
},
},
{
name: "missing Trakt.Email",
fields: fields{
IMDb: IMDb{
Auth: pointer(IMDbAuthMethodCredentials),
Email: &email,
Password: &password,
Lists: &lists,
},
Trakt: Trakt{
Email: nil,
Expand All @@ -253,6 +271,7 @@ func TestConfig_Validate(t *testing.T) {
Auth: pointer(IMDbAuthMethodCredentials),
Email: &email,
Password: &password,
Lists: &lists,
},
Trakt: Trakt{
Email: &email,
Expand All @@ -271,6 +290,7 @@ func TestConfig_Validate(t *testing.T) {
Auth: pointer(IMDbAuthMethodCredentials),
Email: &email,
Password: &password,
Lists: &lists,
},
Trakt: Trakt{
Email: &email,
Expand All @@ -290,6 +310,7 @@ func TestConfig_Validate(t *testing.T) {
Auth: pointer(IMDbAuthMethodCredentials),
Email: &email,
Password: &password,
Lists: &lists,
},
Trakt: Trakt{
Email: &email,
Expand All @@ -310,6 +331,7 @@ func TestConfig_Validate(t *testing.T) {
Auth: pointer(IMDbAuthMethodCredentials),
Email: &email,
Password: &password,
Lists: &lists,
},
Trakt: Trakt{
Email: &email,
Expand All @@ -333,6 +355,7 @@ func TestConfig_Validate(t *testing.T) {
Auth: pointer(IMDbAuthMethodCredentials),
Email: &email,
Password: &password,
Lists: &lists,
},
Trakt: Trakt{
Email: &email,
Expand Down

0 comments on commit 7d6aaba

Please sign in to comment.