Compare commits
2 Commits
99c2eec759
...
aca8605870
Author | SHA1 | Date | |
---|---|---|---|
aca8605870 | |||
6631288843 |
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user