func TestShootingDuration(t *testing.T) { v := New() var shooting *booking_shooting.Shooting { shooting = createValidShooting() shooting.Duration = null.NewInt(int64(MINIMAL_DURATION), false) if err := AssertThat(v.ValidateShooting(shooting), NotNilValue()); err != nil { t.Fatal(err) } } { shooting = createValidShooting() shooting.Duration = null.NewInt(int64(time.Minute*5), true) if err := AssertThat(v.ValidateShooting(shooting), NotNilValue()); err != nil { t.Fatal(err) } } { shooting = createValidShooting() shooting.Duration = null.NewInt(int64(MINIMAL_DURATION), true) if err := AssertThat(v.ValidateShooting(shooting), NilValue()); err != nil { t.Fatal(err) } } }
func TestSendMail(t *testing.T) { e := eventbus.New() counter := 0 var sendMail SendMail = func(mail *booking_mail.Mail, config booking_mail_config.Config) error { counter++ return nil } mailConfig := booking_mail_config.New(nil, nil) userList := createUserList([]booking_user.User{{Email: "*****@*****.**"}, {Email: "*****@*****.**"}}, nil) getModel := createGetModel(&booking_model.Model{Email: "*****@*****.**"}, nil) getDate := createGetDate(&booking_date.Date{}, nil) formatDate := createFormatDate("today", nil) b := New(sendMail, mailConfig, userList, getModel, getDate, formatDate, createProtocol()) if err := e.RegisterHandler(b.HandleBookedEvent); err != nil { t.Fatal(err) } if err := AssertThat(counter, Is(0)); err != nil { t.Fatal(err) } if err := e.Publish(*booking_booked_event.New(booking_shooting.Shooting{DateId: null.NewInt(1, true), ModelId: null.NewInt(1, true)})); err != nil { t.Fatal(err) } if err := AssertThat(counter, Is(3)); err != nil { t.Fatal(err) } }
func Create(db *pg.Session, email, pw string, userJsonData *UserJsonData) (*Model, error) { // encode password var saltedPassword password.Salted err := saltedPassword.Encode(pw) if err != nil { return nil, errors.New("Could not encode password") } // create new user structure user := &Model{ Id: null.NewInt(0, false), State: UserStateActive, Email: email, Password: saltedPassword, } // fill user structure with additional data err = user.JsonData.Encode(userJsonData) if err != nil { return nil, fmt.Errorf("Failed to marshal json data: %s", err) } // create new user in database err = db.Create(user) if err != nil { return nil, fmt.Errorf("Could not persist user: %s", err) } return user, nil }
func createToken(db *pg.Session, userId int64) (*Model, error) { // generate key key, err := random.New(32) if err != nil { return nil, err } // create new reset token structure resetToken := &Model{ Id: null.NewInt(0, false), State: ResetTokenActive, Key: key, Expiration: time.Now().Add(time.Minute * 10), UserId: userId, } // create new user in database err = db.Create(resetToken) if err != nil { return nil, fmt.Errorf("Could not persist reset token: %s", err) } return resetToken, nil }
func TestBuildModelConfirmationMail(t *testing.T) { expectedContent := `Hallo model_firstname model_lastname, Du hast gerade erfolgreich einen Termin für dein Shooting "shooting_name" gebucht. Termin: shooting_date Ort: shooting_location shooting_location_url Thema: shooting_theme shooting_theme_url Bis bald -- Benjamin Borbe Fotografie Mobil: 0178-1970808, Web https://www.benjamin-borbe.de ` getModel := createGetModel(&booking_model.Model{FirstName: "model_firstname", LastName: "model_lastname", Email: "*****@*****.**"}, nil) getDate := createGetDate(&booking_date.Date{}, nil) userList := createUserList([]booking_user.User{}, nil) sendMail := createSendMail(nil) mailConfig := booking_mail_config.New(nil, nil) formatDate := createFormatDate("shooting_date", nil) b := New(sendMail, mailConfig, userList, getModel, getDate, formatDate, createProtocol()) recipient := "*****@*****.**" mail, err := b.buildModelConfirmationMail(&booking_shooting.Shooting{Theme: "shooting_theme", ThemeUrl: "shooting_theme_url", Location: "shooting_location", LocationUrl: "shooting_location_url", Name: "shooting_name", DateId: null.NewInt(1, true), ModelId: null.NewInt(1, true)}, recipient) if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } if err := AssertThat(mail, NotNilValue()); err != nil { t.Fatal(err) } if err := AssertThat(mail.Recipient, Is(recipient)); err != nil { t.Fatal(err) } if err := AssertThat(mail.Subject, Is("Bestätigung Deiner Shooting Buchung")); err != nil { t.Fatal(err) } if err := AssertThat(mail.Content, Is(expectedContent)); err != nil { t.Fatal(err) } }
func (b *builder) AddModelId(modelId int64) { b.protocol.ModelId = null.NewInt(modelId, true) }
func (b *builder) AddUserId(userId int64) { b.protocol.UserId = null.NewInt(userId, true) }
func createValidShooting() *booking_shooting.Shooting { return &booking_shooting.Shooting{ Name: "Foo", Duration: null.NewInt(int64(MINIMAL_DURATION), true), } }
func TestFindWithoutShootingAndInFuture(t *testing.T) { var err error var dates []booking_date.Date database := createDatabase() dateStorage := createStorage(database) shootingStorage := shooting_storage.New(database) if err = dateStorage.Truncate(); err != nil { t.Fatal(err) } now := time.Now() // no dates { dates, err = dateStorage.FindWithoutShootingAndInFuture() if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } if err := AssertThat(len(dates), Is(0)); err != nil { t.Fatal(err) } } // one date without shooting { d := &booking_date.Date{ Start: now.Add(24 * time.Hour), End: now.Add(25 * time.Hour), } _, err = dateStorage.Create(d) if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } dates, err = dateStorage.FindWithoutShootingAndInFuture() if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } if err := AssertThat(len(dates), Is(1)); err != nil { t.Fatal(err) } } // two dates one with shooting { d := &booking_date.Date{ Start: now.Add(24 * time.Hour), End: now.Add(25 * time.Hour), } d, err = dateStorage.Create(d) if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } _, err = shootingStorage.Create(&booking_shooting.Shooting{Name: "test", DateId: null.NewInt(d.Id, true)}) if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } dates, err = dateStorage.FindWithoutShootingAndInFuture() if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } if err := AssertThat(len(dates), Is(1)); err != nil { t.Fatal(err) } } // three dates one with shooting { d := &booking_date.Date{ Start: now.Add(-32 * time.Hour), End: now.Add(-30 * time.Hour), } d, err = dateStorage.Create(d) if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } dates, err = dateStorage.FindWithoutShootingAndInFuture() if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } if err := AssertThat(len(dates), Is(1)); err != nil { t.Fatal(err) } } }
func TestBookableDatesForShooting(t *testing.T) { var err error var list []booking_date.Date database := createDatabase() dateStorage := booking_date_storage.New(database) if err = dateStorage.Truncate(); err != nil { t.Fatal(err) } now, err := time.Parse(time.RFC3339, fmt.Sprintf("%d-01-02T15:04:05+01:00", time.Now().Year()+1)) if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } // create date d := &booking_date.Date{ Start: now.Add(3 * time.Hour), End: now.Add(4 * time.Hour), } _, err = dateStorage.Create(d) if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } // eq { d := New(dateStorage, createGetShooting(&booking_shooting.Shooting{Duration: null.NewInt(int64(time.Hour), true)}, nil), createGetUserIdsForShooting(make([]int64, 0), nil), createGetUserIdsForDate(make([]int64, 0), nil)) list, err = d.BookableDatesForShooting(1337) if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } if err := AssertThat(list, NotNilValue()); err != nil { t.Fatal(err) } if err := AssertThat(len(list), Is(1)); err != nil { t.Fatal(err) } } // gt { d := New(dateStorage, createGetShooting(&booking_shooting.Shooting{Duration: null.NewInt(int64(time.Hour+time.Minute), true)}, nil), createGetUserIdsForShooting(make([]int64, 0), nil), createGetUserIdsForDate(make([]int64, 0), nil)) list, err = d.BookableDatesForShooting(1337) if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } if err := AssertThat(list, NotNilValue()); err != nil { t.Fatal(err) } if err := AssertThat(len(list), Is(0)); err != nil { t.Fatal(err) } } // lt { d := New(dateStorage, createGetShooting(&booking_shooting.Shooting{Duration: null.NewInt(int64(time.Hour-time.Minute), true)}, nil), createGetUserIdsForShooting(make([]int64, 0), nil), createGetUserIdsForDate(make([]int64, 0), nil)) list, err = d.BookableDatesForShooting(1337) if err := AssertThat(err, NilValue()); err != nil { t.Fatal(err) } if err := AssertThat(list, NotNilValue()); err != nil { t.Fatal(err) } if err := AssertThat(len(list), Is(1)); err != nil { t.Fatal(err) } } }