Compare commits

..

2 Commits

Author SHA1 Message Date
aca8605870 fix a make reservation error 2024-07-29 14:04:43 +02:00
6631288843 add more tests 2024-07-29 13:52:10 +02:00
3 changed files with 71 additions and 3 deletions

View File

@ -145,9 +145,16 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
data := make(map[string]interface{})
data["reservation"] = reservation
sd := reservation.StartDate.Format("2006-01-02")
ed := reservation.EndDate.Format("2006-01-02")
stringMap := make(map[string]string)
stringMap["start_date"] = sd
stringMap["end_date"] = ed
render.Template(w, r, "make-reservation.page.tmpl", &models.TemplateData{
Data: data,
Form: form,
StringMap: stringMap,
})
return
}

View File

@ -574,6 +574,64 @@ func Test_BookRoom(t *testing.T) {
}
}
// }}}
// {{{ Login
var loginTests = []struct {
name string
email string
expectedStatusCode int
expectedHTML string
expectedLocation string
}{
{"valid-credentials", "a@b.c", http.StatusSeeOther, "", "/admin/dashboard"},
{"invalid-credentials", "invalid@b.c", http.StatusSeeOther, "", "/user/login"},
{"invalid-data", "a@b", http.StatusOK, `action="/user/login"`, ""},
}
func TestLogin(t *testing.T) {
// range through all tests
for _, e := range loginTests {
postedData := url.Values{}
postedData.Add("email", e.email)
postedData.Add("password", "password")
// create request
req, _ := http.NewRequest("POST", "/user/login", strings.NewReader(postedData.Encode()))
ctx := getCtx(req)
req = req.WithContext(ctx)
// set the header
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()
// call the handler
handler := http.HandlerFunc(Repo.PostShowLogin)
handler.ServeHTTP(rr, req)
if rr.Code != e.expectedStatusCode {
t.Errorf("failed %s: expected code %d, but got %d", e.name, e.expectedStatusCode, rr.Code)
}
if e.expectedLocation != "" {
// get the URL from test
actualLoc, _ := rr.Result().Location()
if actualLoc.String() != e.expectedLocation {
t.Errorf("failed %s: expected location %s, but got location %s", e.name, e.expectedLocation, actualLoc.String())
}
}
// checking for expected values in HTML
if e.expectedHTML != "" {
// read the response body into a string
html := rr.Body.String()
if !strings.Contains(html, e.expectedHTML) {
t.Errorf("failed %s: expected html contains %s, but got html %s", e.name, e.expectedHTML, html)
}
}
}
}
// }}}
// {{{ Test Helpers

View File

@ -79,8 +79,11 @@ func (m *testDBRepo) UpdateUser(u models.User) error {
// Authenticate authenticates a user
func (m *testDBRepo) Authenticate(email, testPassword string) (int, string, error) {
if email == "a@b.c" {
return 1, "", nil
}
return 0, "", errors.New("deliberate errors")
}
// AllReservations returns a slice of all reservations
func (m *testDBRepo) AllReservations() ([]models.Reservation, error) {