add some tests

This commit is contained in:
vinchent 2024-07-28 15:48:03 +02:00
parent 623291541a
commit 99c2eec759
3 changed files with 68 additions and 33 deletions

View File

@ -83,14 +83,14 @@ func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
if !ok {
m.App.Session.Put(r.Context(), "error", "can't get reservation from session")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
room, err := m.DB.GetRoomById(res.RoomID)
if err != nil {
m.App.Session.Put(r.Context(), "error", "can't find room")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@ -118,13 +118,13 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
reservation, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
if !ok {
m.App.Session.Put(r.Context(), "error", "can't get reservation from session")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
if err := r.ParseForm(); err != nil {
m.App.Session.Put(r.Context(), "error", "can't parse form")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@ -155,7 +155,7 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
newReservationID, err := m.DB.InsertReservation(reservation)
if err != nil {
m.App.Session.Put(r.Context(), "error", "can't insert reservation into database")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@ -173,7 +173,7 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
err = m.DB.InsertRoomRestriction(restriction)
if err != nil {
m.App.Session.Put(r.Context(), "error", "can't insert room restriction into database")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@ -224,7 +224,7 @@ func (m *Repository) ReservationSummary(w http.ResponseWriter, r *http.Request)
if !ok {
m.App.ErrorLog.Println("connot get item from session")
m.App.Session.Put(r.Context(), "error", "Can't get reservation from session")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
m.App.Session.Remove(r.Context(), "reservation")
@ -254,7 +254,7 @@ func (m *Repository) Availability(w http.ResponseWriter, r *http.Request) {
func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
m.App.Session.Put(r.Context(), "error", "can't parse form")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
start := r.Form.Get("start")
@ -264,20 +264,20 @@ func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
startDate, err := time.Parse(layout, start)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse start date")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/availability", http.StatusSeeOther)
return
}
endDate, err := time.Parse(layout, end)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse end date")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/availability", http.StatusSeeOther)
return
}
rooms, err := m.DB.SearchAvailabilityForAllRooms(startDate, endDate)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't connect to database")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/availability", http.StatusSeeOther)
return
}
@ -402,7 +402,7 @@ func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) {
roomID, err := strconv.Atoi(exploded[2])
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse roomID")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/availability", http.StatusSeeOther)
return
}
m.App.Session.Get(r.Context(), "reservation")
@ -410,7 +410,7 @@ func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) {
res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
if !ok {
m.App.Session.Put(r.Context(), "error", "Can't get reservation from session")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/availability", http.StatusSeeOther)
return
}
@ -432,19 +432,19 @@ func (m *Repository) BookRoom(w http.ResponseWriter, r *http.Request) {
startDate, err := time.Parse(layout, sd)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse start date")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/availability", http.StatusSeeOther)
return
}
endDate, err := time.Parse(layout, ed)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse end date")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/availability", http.StatusSeeOther)
return
}
room, err := m.DB.GetRoomById(roomID)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Can't parse roomId")
http.Redirect(w, r, "/availability", http.StatusTemporaryRedirect)
http.Redirect(w, r, "/availability", http.StatusSeeOther)
return
}
res.RoomID = roomID

View File

@ -47,6 +47,13 @@ var theTests = []struct {
{"ms", "/majors-suite", "GET", http.StatusOK},
{"sa", "/availability", "GET", http.StatusOK},
{"contact", "/contact", "GET", http.StatusOK},
{"non-existant", "/some/link", "GET", http.StatusNotFound},
{"login", "/user/login", "GET", http.StatusOK},
{"logout", "/user/logout", "GET", http.StatusOK},
{"dashboard", "/admin/dashboard", "GET", http.StatusOK},
{"all reservations", "/admin/reservations-all", "GET", http.StatusOK},
{"new reservations", "/admin/reservations-new", "GET", http.StatusOK},
{"show reservation", "/admin/reservations/new/1/show", "GET", http.StatusOK},
}
func TestHandlers(t *testing.T) {
@ -75,8 +82,8 @@ var makeReservationTests = []struct {
expectedStatusCode int
}{
{"ok", 1, http.StatusOK},
{"no session", 0, http.StatusTemporaryRedirect},
{"non-existant room", 100, http.StatusTemporaryRedirect},
{"no session", 0, http.StatusSeeOther},
{"non-existant room", 100, http.StatusSeeOther},
}
func TestRepository_MakeReservation(t *testing.T) {
@ -141,9 +148,9 @@ var postMakeReservationTests = []struct {
{key: "start_date", value: "2050-01-01"},
{key: "end_date", value: "2050-01-02"},
},
http.StatusTemporaryRedirect,
http.StatusSeeOther,
},
{"no_post_data", []postData{}, http.StatusTemporaryRedirect},
{"no_post_data", []postData{}, http.StatusSeeOther},
{
"missing first name",
[]postData{
@ -193,7 +200,7 @@ var postMakeReservationTests = []struct {
{key: "start_date", value: "2050-01-01"},
{key: "end_date", value: "2050-01-02"},
},
http.StatusTemporaryRedirect,
http.StatusSeeOther,
},
{
"insert room restriction error",
@ -206,7 +213,7 @@ var postMakeReservationTests = []struct {
{key: "start_date", value: "2050-01-01"},
{key: "end_date", value: "2050-01-02"},
},
http.StatusTemporaryRedirect,
http.StatusSeeOther,
},
}
@ -374,7 +381,7 @@ var reservationSummaryTests = []struct {
expectedStatusCode int
}{
{"ok", true, http.StatusOK},
{"nok", false, http.StatusTemporaryRedirect},
{"nok", false, http.StatusSeeOther},
}
func Test_ReservationSummary(t *testing.T) {
@ -426,7 +433,7 @@ var postAvailabilityTests = []struct {
{"database error", []postData{
{key: "start", value: "2050-01-03"},
{key: "end", value: "2050-01-04"},
}, http.StatusTemporaryRedirect},
}, http.StatusSeeOther},
{"no availability", []postData{
{key: "start", value: "2050-01-05"},
{key: "end", value: "2050-01-06"},
@ -434,12 +441,12 @@ var postAvailabilityTests = []struct {
{"wrong start date", []postData{
{key: "start", value: "2050-05"},
{key: "end", value: "2050-01-06"},
}, http.StatusTemporaryRedirect},
}, http.StatusSeeOther},
{"wrong end date", []postData{
{key: "start", value: "2050-01-05"},
{key: "end", value: "01-06"},
}, http.StatusTemporaryRedirect},
{"wrong end date", []postData{}, http.StatusTemporaryRedirect},
}, http.StatusSeeOther},
{"wrong end date", []postData{}, http.StatusSeeOther},
}
func Test_PostAvailability(t *testing.T) {
@ -481,8 +488,8 @@ var chooseRoomTests = []struct {
expectedStatusCode int
}{
{"ok", "/choose-room/1", true, http.StatusSeeOther},
{"wrong room id", "/choose-room/1wrong", true, http.StatusTemporaryRedirect},
{"no session", "/choose-room/1", false, http.StatusTemporaryRedirect},
{"wrong room id", "/choose-room/1wrong", true, http.StatusSeeOther},
{"no session", "/choose-room/1", false, http.StatusSeeOther},
}
func Test_ChooseRoom(t *testing.T) {
@ -531,17 +538,17 @@ var bookRoomTests = []struct {
{key: "id", value: "1"},
{key: "s", value: "20-01-01"},
{key: "e", value: "2050-01-02"},
}, http.StatusTemporaryRedirect},
}, http.StatusSeeOther},
{"wrong end date", []postData{
{key: "id", value: "1"},
{key: "s", value: "2050-01-01"},
{key: "e", value: "2050-0-02"},
}, http.StatusTemporaryRedirect},
}, http.StatusSeeOther},
{"wrong room id", []postData{
{key: "id", value: "w"},
{key: "s", value: "2050-01-01"},
{key: "e", value: "2050-01-02"},
}, http.StatusTemporaryRedirect},
}, http.StatusSeeOther},
}
func Test_BookRoom(t *testing.T) {

View File

@ -19,7 +19,12 @@ import (
"github.com/go-chi/chi/v5/middleware"
)
var functions = template.FuncMap{}
var functions = template.FuncMap{
"humanDate": render.HumanDate,
"formatDate": render.FormatDate,
"iterate": render.Iterate,
"add": render.Add,
}
var (
app config.AppConfig
@ -28,6 +33,10 @@ var (
func TestMain(m *testing.M) {
gob.Register(models.Reservation{})
gob.Register(models.User{})
gob.Register(models.Room{})
gob.Register(models.Restriction{})
gob.Register(map[string]int{})
// change this to true when in production
app.InProduction = false
@ -85,6 +94,25 @@ func getRoutes() http.Handler {
mux.Post("/make-reservation", Repo.PostMakeReservation)
mux.Get("/reservation-summary", Repo.ReservationSummary)
mux.Get("/choose-room/{id}", Repo.ChooseRoom)
mux.Get("/book-room", Repo.BookRoom)
mux.Get("/user/login", Repo.ShowLogin)
mux.Post("/user/login", Repo.PostShowLogin)
mux.Get("/user/logout", Repo.Logout)
mux.Get("/admin/dashboard", Repo.AdminDashboard)
mux.Get("/admin/reservations-new", Repo.AdminNewReservations)
mux.Get("/admin/reservations-all", Repo.AdminAllReservations)
mux.Get("/admin/reservations-calendar", Repo.AdminReservationsCalendar)
mux.Post("/admin/reservations-calendar", Repo.AdminPostReservationsCalendar)
mux.Get("/admin/process-reservation/{src}/{id}/do", Repo.AdminProcessReservation)
mux.Get("/admin/delete-reservation/{src}/{id}/do", Repo.AdminDeleteReservation)
mux.Get("/admin/reservations/{src}/{id}/show", Repo.AdminShowReservation)
mux.Post("/admin/reservations/{src}/{id}", Repo.AdminPostShowReservation)
fileServer := http.FileServer(http.Dir("./static/"))
mux.Handle("/static/*", http.StripPrefix("/static", fileServer))