Merge pull request #71 from christianselig/feat/validate-subreddit-format

Add subreddit validation and de-register from old API
This commit is contained in:
André Medeiros 2022-05-07 11:21:41 -04:00 committed by GitHub
commit ec1b847ca4
4 changed files with 77 additions and 9 deletions

View file

@ -183,6 +183,22 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
_ = a.accountRepo.Disassociate(ctx, &acc, &dev) _ = a.accountRepo.Disassociate(ctx, &acc, &dev)
} }
go func(apns string) {
url := fmt.Sprintf("https://apollopushserver.xyz/api/new-server-addition?apns_token=%s", apns)
req, err := http.NewRequest("POST", url, nil)
req.Header.Set("Authentication", "Bearer 98g5j89aurqwfcsp9khlnvgd38fa15")
if err != nil {
a.logger.WithFields(logrus.Fields{
"apns": apns,
}).Error(err)
return
}
resp, _ := a.httpClient.Do(req)
resp.Body.Close()
}(apns)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }

View file

@ -26,6 +26,7 @@ type api struct {
statsd *statsd.Client statsd *statsd.Client
reddit *reddit.Client reddit *reddit.Client
apns *token.Token apns *token.Token
httpClient *http.Client
accountRepo domain.AccountRepository accountRepo domain.AccountRepository
deviceRepo domain.DeviceRepository deviceRepo domain.DeviceRepository
@ -63,11 +64,14 @@ func NewAPI(ctx context.Context, logger *logrus.Logger, statsd *statsd.Client, r
watcherRepo := repository.NewPostgresWatcher(pool) watcherRepo := repository.NewPostgresWatcher(pool)
userRepo := repository.NewPostgresUser(pool) userRepo := repository.NewPostgresUser(pool)
client := &http.Client{}
return &api{ return &api{
logger: logger, logger: logger,
statsd: statsd, statsd: statsd,
reddit: reddit, reddit: reddit,
apns: apns, apns: apns,
httpClient: client,
accountRepo: accountRepo, accountRepo: accountRepo,
deviceRepo: deviceRepo, deviceRepo: deviceRepo,

View file

@ -2,6 +2,8 @@ package domain
import ( import (
"context" "context"
"errors"
"regexp"
"strings" "strings"
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
@ -20,9 +22,21 @@ func (sr *Subreddit) NormalizedName() string {
return strings.ToLower(sr.Name) return strings.ToLower(sr.Name)
} }
func validPrefix(value interface{}) error {
s, _ := value.(string)
if len(s) < 2 {
return nil
}
if s[1] != '_' || s[0] != 'u' {
return nil
}
return errors.New("invalid subreddit format")
}
func (sr *Subreddit) Validate() error { func (sr *Subreddit) Validate() error {
return validation.ValidateStruct(sr, return validation.ValidateStruct(sr,
validation.Field(&sr.Name, validation.Required, validation.Length(3, 32)), validation.Field(&sr.Name, validation.Required, validation.Length(3, 32), validation.By(validPrefix), validation.Match(regexp.MustCompile(`^[a-zA-Z0-9]\w{1,19}$`))),
validation.Field(&sr.SubredditID, validation.Required, validation.Length(4, 9)), validation.Field(&sr.SubredditID, validation.Required, validation.Length(4, 9)),
) )
} }

View file

@ -0,0 +1,34 @@
package domain
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValidate(t *testing.T) {
tests := map[string]struct {
subreddit Subreddit
err error
}{
"invalid subreddit prefix": {Subreddit{Name: "u_iamthatis"}, errors.New("invalid subreddit format")},
"valid subreddit": {Subreddit{Name: "pics", SubredditID: "abcd"}, nil},
"valid subreddit starting with u": {Subreddit{Name: "urcool", SubredditID: "abcd"}, nil},
"valid subreddit with _": {Subreddit{Name: "p_i_x_a_r", SubredditID: "abcd"}, nil},
}
for scenario, tc := range tests {
t.Run(scenario, func(t *testing.T) {
err := tc.subreddit.Validate()
if tc.err == nil {
require.NoError(t, err)
return
}
assert.Error(t, err)
})
}
}