mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-13 07:27:43 +00:00
Merge pull request #71 from christianselig/feat/validate-subreddit-format
Add subreddit validation and de-register from old API
This commit is contained in:
commit
ec1b847ca4
4 changed files with 77 additions and 9 deletions
|
@ -183,6 +183,22 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
|
|||
_ = 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)
|
||||
}
|
||||
|
||||
|
|
|
@ -22,10 +22,11 @@ import (
|
|||
)
|
||||
|
||||
type api struct {
|
||||
logger *logrus.Logger
|
||||
statsd *statsd.Client
|
||||
reddit *reddit.Client
|
||||
apns *token.Token
|
||||
logger *logrus.Logger
|
||||
statsd *statsd.Client
|
||||
reddit *reddit.Client
|
||||
apns *token.Token
|
||||
httpClient *http.Client
|
||||
|
||||
accountRepo domain.AccountRepository
|
||||
deviceRepo domain.DeviceRepository
|
||||
|
@ -63,11 +64,14 @@ func NewAPI(ctx context.Context, logger *logrus.Logger, statsd *statsd.Client, r
|
|||
watcherRepo := repository.NewPostgresWatcher(pool)
|
||||
userRepo := repository.NewPostgresUser(pool)
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
return &api{
|
||||
logger: logger,
|
||||
statsd: statsd,
|
||||
reddit: reddit,
|
||||
apns: apns,
|
||||
logger: logger,
|
||||
statsd: statsd,
|
||||
reddit: reddit,
|
||||
apns: apns,
|
||||
httpClient: client,
|
||||
|
||||
accountRepo: accountRepo,
|
||||
deviceRepo: deviceRepo,
|
||||
|
|
|
@ -2,6 +2,8 @@ package domain
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
|
@ -20,9 +22,21 @@ func (sr *Subreddit) NormalizedName() string {
|
|||
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 {
|
||||
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)),
|
||||
)
|
||||
}
|
||||
|
|
34
internal/domain/subreddit_test.go
Normal file
34
internal/domain/subreddit_test.go
Normal 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)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue