add some tests
This commit is contained in:
parent
623291541a
commit
99c2eec759
@ -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)
|
res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
|
||||||
if !ok {
|
if !ok {
|
||||||
m.App.Session.Put(r.Context(), "error", "can't get reservation 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)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
room, err := m.DB.GetRoomById(res.RoomID)
|
room, err := m.DB.GetRoomById(res.RoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "can't find room")
|
m.App.Session.Put(r.Context(), "error", "can't find room")
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
return
|
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)
|
reservation, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
|
||||||
if !ok {
|
if !ok {
|
||||||
m.App.Session.Put(r.Context(), "error", "can't get reservation 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)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.ParseForm(); err != nil {
|
if err := r.ParseForm(); err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "can't parse form")
|
m.App.Session.Put(r.Context(), "error", "can't parse form")
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +155,7 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
|
|||||||
newReservationID, err := m.DB.InsertReservation(reservation)
|
newReservationID, err := m.DB.InsertReservation(reservation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "can't insert reservation into database")
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,7 +173,7 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
|
|||||||
err = m.DB.InsertRoomRestriction(restriction)
|
err = m.DB.InsertRoomRestriction(restriction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "can't insert room restriction into database")
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,7 +224,7 @@ func (m *Repository) ReservationSummary(w http.ResponseWriter, r *http.Request)
|
|||||||
if !ok {
|
if !ok {
|
||||||
m.App.ErrorLog.Println("connot get item from session")
|
m.App.ErrorLog.Println("connot get item from session")
|
||||||
m.App.Session.Put(r.Context(), "error", "Can't get reservation 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")
|
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) {
|
func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := r.ParseForm(); err != nil {
|
if err := r.ParseForm(); err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "can't parse form")
|
m.App.Session.Put(r.Context(), "error", "can't parse form")
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
start := r.Form.Get("start")
|
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)
|
startDate, err := time.Parse(layout, start)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "Can't parse start date")
|
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
|
return
|
||||||
}
|
}
|
||||||
endDate, err := time.Parse(layout, end)
|
endDate, err := time.Parse(layout, end)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "Can't parse end date")
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rooms, err := m.DB.SearchAvailabilityForAllRooms(startDate, endDate)
|
rooms, err := m.DB.SearchAvailabilityForAllRooms(startDate, endDate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "Can't connect to database")
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -402,7 +402,7 @@ func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) {
|
|||||||
roomID, err := strconv.Atoi(exploded[2])
|
roomID, err := strconv.Atoi(exploded[2])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "Can't parse roomID")
|
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
|
return
|
||||||
}
|
}
|
||||||
m.App.Session.Get(r.Context(), "reservation")
|
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)
|
res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
|
||||||
if !ok {
|
if !ok {
|
||||||
m.App.Session.Put(r.Context(), "error", "Can't get reservation from session")
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -432,19 +432,19 @@ func (m *Repository) BookRoom(w http.ResponseWriter, r *http.Request) {
|
|||||||
startDate, err := time.Parse(layout, sd)
|
startDate, err := time.Parse(layout, sd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "Can't parse start date")
|
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
|
return
|
||||||
}
|
}
|
||||||
endDate, err := time.Parse(layout, ed)
|
endDate, err := time.Parse(layout, ed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "Can't parse end date")
|
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
|
return
|
||||||
}
|
}
|
||||||
room, err := m.DB.GetRoomById(roomID)
|
room, err := m.DB.GetRoomById(roomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.App.Session.Put(r.Context(), "error", "Can't parse roomId")
|
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
|
return
|
||||||
}
|
}
|
||||||
res.RoomID = roomID
|
res.RoomID = roomID
|
||||||
|
@ -47,6 +47,13 @@ var theTests = []struct {
|
|||||||
{"ms", "/majors-suite", "GET", http.StatusOK},
|
{"ms", "/majors-suite", "GET", http.StatusOK},
|
||||||
{"sa", "/availability", "GET", http.StatusOK},
|
{"sa", "/availability", "GET", http.StatusOK},
|
||||||
{"contact", "/contact", "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) {
|
func TestHandlers(t *testing.T) {
|
||||||
@ -75,8 +82,8 @@ var makeReservationTests = []struct {
|
|||||||
expectedStatusCode int
|
expectedStatusCode int
|
||||||
}{
|
}{
|
||||||
{"ok", 1, http.StatusOK},
|
{"ok", 1, http.StatusOK},
|
||||||
{"no session", 0, http.StatusTemporaryRedirect},
|
{"no session", 0, http.StatusSeeOther},
|
||||||
{"non-existant room", 100, http.StatusTemporaryRedirect},
|
{"non-existant room", 100, http.StatusSeeOther},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRepository_MakeReservation(t *testing.T) {
|
func TestRepository_MakeReservation(t *testing.T) {
|
||||||
@ -141,9 +148,9 @@ var postMakeReservationTests = []struct {
|
|||||||
{key: "start_date", value: "2050-01-01"},
|
{key: "start_date", value: "2050-01-01"},
|
||||||
{key: "end_date", value: "2050-01-02"},
|
{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",
|
"missing first name",
|
||||||
[]postData{
|
[]postData{
|
||||||
@ -193,7 +200,7 @@ var postMakeReservationTests = []struct {
|
|||||||
{key: "start_date", value: "2050-01-01"},
|
{key: "start_date", value: "2050-01-01"},
|
||||||
{key: "end_date", value: "2050-01-02"},
|
{key: "end_date", value: "2050-01-02"},
|
||||||
},
|
},
|
||||||
http.StatusTemporaryRedirect,
|
http.StatusSeeOther,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"insert room restriction error",
|
"insert room restriction error",
|
||||||
@ -206,7 +213,7 @@ var postMakeReservationTests = []struct {
|
|||||||
{key: "start_date", value: "2050-01-01"},
|
{key: "start_date", value: "2050-01-01"},
|
||||||
{key: "end_date", value: "2050-01-02"},
|
{key: "end_date", value: "2050-01-02"},
|
||||||
},
|
},
|
||||||
http.StatusTemporaryRedirect,
|
http.StatusSeeOther,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -374,7 +381,7 @@ var reservationSummaryTests = []struct {
|
|||||||
expectedStatusCode int
|
expectedStatusCode int
|
||||||
}{
|
}{
|
||||||
{"ok", true, http.StatusOK},
|
{"ok", true, http.StatusOK},
|
||||||
{"nok", false, http.StatusTemporaryRedirect},
|
{"nok", false, http.StatusSeeOther},
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_ReservationSummary(t *testing.T) {
|
func Test_ReservationSummary(t *testing.T) {
|
||||||
@ -426,7 +433,7 @@ var postAvailabilityTests = []struct {
|
|||||||
{"database error", []postData{
|
{"database error", []postData{
|
||||||
{key: "start", value: "2050-01-03"},
|
{key: "start", value: "2050-01-03"},
|
||||||
{key: "end", value: "2050-01-04"},
|
{key: "end", value: "2050-01-04"},
|
||||||
}, http.StatusTemporaryRedirect},
|
}, http.StatusSeeOther},
|
||||||
{"no availability", []postData{
|
{"no availability", []postData{
|
||||||
{key: "start", value: "2050-01-05"},
|
{key: "start", value: "2050-01-05"},
|
||||||
{key: "end", value: "2050-01-06"},
|
{key: "end", value: "2050-01-06"},
|
||||||
@ -434,12 +441,12 @@ var postAvailabilityTests = []struct {
|
|||||||
{"wrong start date", []postData{
|
{"wrong start date", []postData{
|
||||||
{key: "start", value: "2050-05"},
|
{key: "start", value: "2050-05"},
|
||||||
{key: "end", value: "2050-01-06"},
|
{key: "end", value: "2050-01-06"},
|
||||||
}, http.StatusTemporaryRedirect},
|
}, http.StatusSeeOther},
|
||||||
{"wrong end date", []postData{
|
{"wrong end date", []postData{
|
||||||
{key: "start", value: "2050-01-05"},
|
{key: "start", value: "2050-01-05"},
|
||||||
{key: "end", value: "01-06"},
|
{key: "end", value: "01-06"},
|
||||||
}, http.StatusTemporaryRedirect},
|
}, http.StatusSeeOther},
|
||||||
{"wrong end date", []postData{}, http.StatusTemporaryRedirect},
|
{"wrong end date", []postData{}, http.StatusSeeOther},
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_PostAvailability(t *testing.T) {
|
func Test_PostAvailability(t *testing.T) {
|
||||||
@ -481,8 +488,8 @@ var chooseRoomTests = []struct {
|
|||||||
expectedStatusCode int
|
expectedStatusCode int
|
||||||
}{
|
}{
|
||||||
{"ok", "/choose-room/1", true, http.StatusSeeOther},
|
{"ok", "/choose-room/1", true, http.StatusSeeOther},
|
||||||
{"wrong room id", "/choose-room/1wrong", true, http.StatusTemporaryRedirect},
|
{"wrong room id", "/choose-room/1wrong", true, http.StatusSeeOther},
|
||||||
{"no session", "/choose-room/1", false, http.StatusTemporaryRedirect},
|
{"no session", "/choose-room/1", false, http.StatusSeeOther},
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_ChooseRoom(t *testing.T) {
|
func Test_ChooseRoom(t *testing.T) {
|
||||||
@ -531,17 +538,17 @@ var bookRoomTests = []struct {
|
|||||||
{key: "id", value: "1"},
|
{key: "id", value: "1"},
|
||||||
{key: "s", value: "20-01-01"},
|
{key: "s", value: "20-01-01"},
|
||||||
{key: "e", value: "2050-01-02"},
|
{key: "e", value: "2050-01-02"},
|
||||||
}, http.StatusTemporaryRedirect},
|
}, http.StatusSeeOther},
|
||||||
{"wrong end date", []postData{
|
{"wrong end date", []postData{
|
||||||
{key: "id", value: "1"},
|
{key: "id", value: "1"},
|
||||||
{key: "s", value: "2050-01-01"},
|
{key: "s", value: "2050-01-01"},
|
||||||
{key: "e", value: "2050-0-02"},
|
{key: "e", value: "2050-0-02"},
|
||||||
}, http.StatusTemporaryRedirect},
|
}, http.StatusSeeOther},
|
||||||
{"wrong room id", []postData{
|
{"wrong room id", []postData{
|
||||||
{key: "id", value: "w"},
|
{key: "id", value: "w"},
|
||||||
{key: "s", value: "2050-01-01"},
|
{key: "s", value: "2050-01-01"},
|
||||||
{key: "e", value: "2050-01-02"},
|
{key: "e", value: "2050-01-02"},
|
||||||
}, http.StatusTemporaryRedirect},
|
}, http.StatusSeeOther},
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_BookRoom(t *testing.T) {
|
func Test_BookRoom(t *testing.T) {
|
||||||
|
@ -19,7 +19,12 @@ import (
|
|||||||
"github.com/go-chi/chi/v5/middleware"
|
"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 (
|
var (
|
||||||
app config.AppConfig
|
app config.AppConfig
|
||||||
@ -28,6 +33,10 @@ var (
|
|||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
gob.Register(models.Reservation{})
|
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
|
// change this to true when in production
|
||||||
app.InProduction = false
|
app.InProduction = false
|
||||||
|
|
||||||
@ -85,6 +94,25 @@ func getRoutes() http.Handler {
|
|||||||
mux.Post("/make-reservation", Repo.PostMakeReservation)
|
mux.Post("/make-reservation", Repo.PostMakeReservation)
|
||||||
mux.Get("/reservation-summary", Repo.ReservationSummary)
|
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/"))
|
fileServer := http.FileServer(http.Dir("./static/"))
|
||||||
mux.Handle("/static/*", http.StripPrefix("/static", fileServer))
|
mux.Handle("/static/*", http.StripPrefix("/static", fileServer))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user