generated from VLADIMIR/template
add email
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package game_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestManualTeamEmail(t *testing.T) {
|
||||
for _, email := range []string{"captain@example.org", "captain+team@example.org"} {
|
||||
if !validCaptainEmail(email) {
|
||||
t.Errorf("rejected %q", email)
|
||||
}
|
||||
}
|
||||
for _, email := range []string{"invalid", "Captain <captain@example.org>", "a@example.org\r\nBcc: other@example.org", strings.Repeat("a", 250) + "@example.org"} {
|
||||
service := &GameService{}
|
||||
if _, err := service.AddTeam(context.Background(), 1, 1, "Team", email); err == nil {
|
||||
t.Errorf("accepted invalid email %q", email)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,12 +51,16 @@ func (r *Registration) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validCaptainEmail(email string) bool {
|
||||
address, err := mail.ParseAddress(email)
|
||||
return err == nil && address.Address == email && len(email) <= 254
|
||||
}
|
||||
|
||||
func (r *Registration) Validate() error {
|
||||
r.Name = strings.TrimSpace(r.Name)
|
||||
r.Email = strings.TrimSpace(r.Email)
|
||||
address, err := mail.ParseAddress(r.Email)
|
||||
if r.Name == "" || !utf8.ValidString(r.Name) || strings.ContainsRune(r.Name, 0) || utf8.RuneCountInString(r.Name) > 255 ||
|
||||
err != nil || address.Address != r.Email || len(r.Email) > 254 || r.ParticipantsCount <= 0 {
|
||||
!validCaptainEmail(r.Email) || r.ParticipantsCount <= 0 {
|
||||
return ErrInvalidRegistration
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"evening_detective_server/internal/repos/team_answers_repo"
|
||||
"evening_detective_server/internal/repos/teams_repo"
|
||||
"evening_detective_server/internal/services/scenarios_service"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -217,7 +218,12 @@ func (s *GameService) AddTeam(
|
||||
creatorId int,
|
||||
gameId int,
|
||||
name string,
|
||||
captainEmail string,
|
||||
) (*Team, error) {
|
||||
captainEmail = strings.TrimSpace(captainEmail)
|
||||
if captainEmail != "" && !validCaptainEmail(captainEmail) {
|
||||
return nil, errors.New("Укажите корректную почту капитана")
|
||||
}
|
||||
password, err := s.passwordGenerator.Generate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -226,7 +232,7 @@ func (s *GameService) AddTeam(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id, err := s.teamsRepo.AddTeamWithParticipation(ctx, creatorId, gameId, name, password, !time.Now().Before(game.StartAt.Add(-24*time.Hour)))
|
||||
id, err := s.teamsRepo.AddTeamWithParticipation(ctx, creatorId, gameId, name, password, captainEmail, !time.Now().Before(game.StartAt.Add(-24*time.Hour)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user