2024-07-01 20:34:16 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-07-13 14:35:17 +00:00
|
|
|
"context"
|
2024-07-14 09:49:42 +00:00
|
|
|
"fmt"
|
2024-07-13 14:35:17 +00:00
|
|
|
"go-udemy-web-1/internal/models"
|
|
|
|
"log"
|
2024-07-01 20:34:16 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2024-07-14 09:49:42 +00:00
|
|
|
"strings"
|
2024-07-01 20:34:16 +00:00
|
|
|
"testing"
|
2024-07-14 09:49:42 +00:00
|
|
|
"time"
|
2024-07-01 20:34:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type postData struct {
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
var theTests = []struct {
|
|
|
|
name string
|
|
|
|
url string
|
|
|
|
method string
|
|
|
|
expectedStatusCode int
|
|
|
|
}{
|
2024-07-13 14:47:23 +00:00
|
|
|
{"home", "/", "GET", http.StatusOK},
|
|
|
|
{"about", "/about", "GET", http.StatusOK},
|
|
|
|
{"gq", "/generals-quarters", "GET", http.StatusOK},
|
|
|
|
{"ms", "/majors-suite", "GET", http.StatusOK},
|
|
|
|
{"sa", "/availability", "GET", http.StatusOK},
|
|
|
|
{"contact", "/contact", "GET", http.StatusOK},
|
|
|
|
|
2024-07-13 14:35:17 +00:00
|
|
|
// {"post-search-avail", "/availability", "POST", []postData{
|
|
|
|
// {key: "start", value: "2020-01-01"},
|
|
|
|
// {key: "end", value: "2020-01-02"},
|
|
|
|
// }, http.StatusOK},
|
|
|
|
// {"post-search-avail-json", "/availability-json", "POST", []postData{
|
|
|
|
// {key: "start", value: "2020-01-01"},
|
|
|
|
// {key: "end", value: "2020-01-02"},
|
|
|
|
// }, http.StatusOK},
|
|
|
|
// {"make-reservation", "/make-reservation", "POST", []postData{
|
|
|
|
// {key: "first_name", value: "John"},
|
|
|
|
// {key: "last_name", value: "Smith"},
|
|
|
|
// {key: "email", value: "me@here.com"},
|
|
|
|
// {key: "phone", value: "12345"},
|
|
|
|
// }, http.StatusOK},
|
2024-07-01 20:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandlers(t *testing.T) {
|
|
|
|
routes := getRoutes()
|
|
|
|
ts := httptest.NewTLSServer(routes)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
for _, e := range theTests {
|
2024-07-13 14:47:23 +00:00
|
|
|
resp, err := ts.Client().Get(ts.URL + e.url)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != e.expectedStatusCode {
|
2024-07-14 09:59:43 +00:00
|
|
|
t.Errorf("for %s, expected %d but got %d\n", e.name, e.expectedStatusCode, resp.StatusCode)
|
2024-07-01 20:34:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-13 14:35:17 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
// {{{ Make Reservation Tests
|
2024-07-13 14:35:17 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
var makeReservationTests = []struct {
|
|
|
|
name string
|
|
|
|
roomID int
|
|
|
|
expectedStatusCode int
|
|
|
|
}{
|
|
|
|
{"ok", 1, http.StatusOK},
|
|
|
|
{"no session", 0, http.StatusTemporaryRedirect},
|
|
|
|
{"non-existant room", 100, http.StatusTemporaryRedirect},
|
|
|
|
}
|
2024-07-13 14:35:17 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
func TestRepository_MakeReservation(t *testing.T) {
|
|
|
|
for _, test := range makeReservationTests {
|
|
|
|
reservation := models.Reservation{
|
|
|
|
RoomID: test.roomID,
|
|
|
|
Room: models.Room{
|
|
|
|
ID: test.roomID,
|
|
|
|
RoomName: "Room name",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req, _ := http.NewRequest("GET", "/make-reservation", nil)
|
|
|
|
ctx := getCtx(req)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
if test.roomID == 0 {
|
|
|
|
session.Put(ctx, "reservation", nil)
|
|
|
|
} else {
|
|
|
|
session.Put(ctx, "reservation", reservation)
|
|
|
|
}
|
|
|
|
handler := http.HandlerFunc(Repo.MakeReservation)
|
2024-07-13 14:35:17 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
handler.ServeHTTP(rr, req)
|
2024-07-13 14:35:17 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
if rr.Code != test.expectedStatusCode {
|
2024-07-14 09:59:43 +00:00
|
|
|
t.Errorf("for %s, reservation handler returned response code: got %d, wanted %d\n",
|
2024-07-14 09:49:42 +00:00
|
|
|
test.name, rr.Code, http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-13 14:35:17 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
// }}}
|
|
|
|
// {{{ PostMakeReservation tests
|
2024-07-13 14:35:17 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
var postMakeReservationTests = []struct {
|
|
|
|
name string
|
2024-07-14 09:59:43 +00:00
|
|
|
reservationInfo []postData
|
2024-07-14 09:49:42 +00:00
|
|
|
roomID int
|
2024-07-13 14:35:17 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
expectedStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"ok",
|
2024-07-14 09:59:43 +00:00
|
|
|
[]postData{
|
|
|
|
{key: "first_name", value: "John"},
|
|
|
|
{key: "last_name", value: "Smith"},
|
|
|
|
{key: "email", value: "john@smith.com"},
|
|
|
|
{key: "phone", value: "1234"},
|
|
|
|
{key: "room_id", value: "1"},
|
|
|
|
{key: "start_date", value: "2050-01-01"},
|
|
|
|
{key: "end_date", value: "2050-01-02"},
|
2024-07-14 09:49:42 +00:00
|
|
|
},
|
|
|
|
1, http.StatusSeeOther,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"no_session",
|
2024-07-14 09:59:43 +00:00
|
|
|
[]postData{
|
|
|
|
{key: "first_name", value: "John"},
|
|
|
|
{key: "last_name", value: "Smith"},
|
|
|
|
{key: "email", value: "john@smith.com"},
|
|
|
|
{key: "phone", value: "1234"},
|
|
|
|
{key: "room_id", value: "0"},
|
|
|
|
{key: "start_date", value: "2050-01-01"},
|
|
|
|
{key: "end_date", value: "2050-01-02"},
|
2024-07-14 09:49:42 +00:00
|
|
|
},
|
|
|
|
0, http.StatusTemporaryRedirect,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"no_post_data",
|
2024-07-14 09:59:43 +00:00
|
|
|
[]postData{},
|
2024-07-14 09:49:42 +00:00
|
|
|
0, http.StatusTemporaryRedirect,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"missing first name",
|
2024-07-14 09:59:43 +00:00
|
|
|
[]postData{
|
|
|
|
{key: "last_name", value: "Smith"},
|
|
|
|
{key: "email", value: "john@smith.com"},
|
|
|
|
{key: "phone", value: "1234"},
|
|
|
|
{key: "room_id", value: "0"},
|
|
|
|
{key: "start_date", value: "2050-01-01"},
|
|
|
|
{key: "end_date", value: "2050-01-02"},
|
2024-07-14 09:49:42 +00:00
|
|
|
},
|
|
|
|
1, http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"wrong first name",
|
2024-07-14 09:59:43 +00:00
|
|
|
[]postData{
|
|
|
|
{key: "first_name", value: "J"},
|
|
|
|
{key: "last_name", value: "Smith"},
|
|
|
|
{key: "email", value: "john@smith.com"},
|
|
|
|
{key: "phone", value: "1234"},
|
|
|
|
{key: "room_id", value: "0"},
|
|
|
|
{key: "start_date", value: "2050-01-01"},
|
|
|
|
{key: "end_date", value: "2050-01-02"},
|
2024-07-14 09:49:42 +00:00
|
|
|
},
|
|
|
|
1, http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"wrong email",
|
2024-07-14 09:59:43 +00:00
|
|
|
[]postData{
|
|
|
|
{key: "first_name", value: "John"},
|
|
|
|
{key: "last_name", value: "Smith"},
|
|
|
|
{key: "email", value: "john@smith"},
|
|
|
|
{key: "phone", value: "1234"},
|
|
|
|
{key: "room_id", value: "0"},
|
|
|
|
{key: "start_date", value: "2050-01-01"},
|
|
|
|
{key: "end_date", value: "2050-01-02"},
|
2024-07-14 09:49:42 +00:00
|
|
|
},
|
|
|
|
1, http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"insert reservation error",
|
2024-07-14 09:59:43 +00:00
|
|
|
[]postData{
|
|
|
|
{key: "first_name", value: "John"},
|
|
|
|
{key: "last_name", value: "Smith"},
|
|
|
|
{key: "email", value: "john@smith.com"},
|
|
|
|
{key: "phone", value: "1234"},
|
|
|
|
{key: "room_id", value: "2"},
|
|
|
|
{key: "start_date", value: "2050-01-01"},
|
|
|
|
{key: "end_date", value: "2050-01-02"},
|
2024-07-14 09:49:42 +00:00
|
|
|
},
|
|
|
|
2, http.StatusTemporaryRedirect,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"insert room restriction error",
|
2024-07-14 09:59:43 +00:00
|
|
|
[]postData{
|
|
|
|
{key: "first_name", value: "John"},
|
|
|
|
{key: "last_name", value: "Smith"},
|
|
|
|
{key: "email", value: "john@smith.com"},
|
|
|
|
{key: "phone", value: "1234"},
|
|
|
|
{key: "room_id", value: "100"},
|
|
|
|
{key: "start_date", value: "2050-01-01"},
|
|
|
|
{key: "end_date", value: "2050-01-02"},
|
2024-07-14 09:49:42 +00:00
|
|
|
},
|
|
|
|
100, http.StatusTemporaryRedirect,
|
|
|
|
},
|
|
|
|
}
|
2024-07-13 14:47:23 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
func TestRepository_PostMakeReservation(t *testing.T) {
|
|
|
|
for _, test := range postMakeReservationTests {
|
|
|
|
var reqBody string
|
|
|
|
if len(test.reservationInfo) > 0 {
|
2024-07-14 09:59:43 +00:00
|
|
|
reqBody = fmt.Sprintf("%s=%s", test.reservationInfo[0].key,
|
|
|
|
test.reservationInfo[0].value)
|
2024-07-14 09:49:42 +00:00
|
|
|
for _, element := range test.reservationInfo[1:] {
|
2024-07-14 09:59:43 +00:00
|
|
|
reqBody = fmt.Sprintf("%s&%s=%s", reqBody, element.key,
|
|
|
|
element.value)
|
2024-07-14 09:49:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
layout := "2006-01-02"
|
|
|
|
sd, _ := time.Parse(layout, "2050-01-01")
|
|
|
|
ed, _ := time.Parse(layout, "2050-01-02")
|
|
|
|
reservation := models.Reservation{
|
|
|
|
RoomID: test.roomID,
|
|
|
|
StartDate: sd,
|
|
|
|
EndDate: ed,
|
|
|
|
}
|
2024-07-13 14:47:23 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
req, _ := http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody))
|
|
|
|
ctx := getCtx(req)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
if test.roomID == 0 {
|
|
|
|
session.Put(ctx, "reservation", nil)
|
|
|
|
} else {
|
|
|
|
session.Put(ctx, "reservation", reservation)
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(Repo.PostMakeReservation)
|
|
|
|
handler.ServeHTTP(rr, req)
|
2024-07-13 14:47:23 +00:00
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
if rr.Code != test.expectedStatusCode {
|
2024-07-14 09:59:43 +00:00
|
|
|
fmt.Printf("for %s, reservation handler returned response code: got %d, wanted %d\n",
|
2024-07-14 09:49:42 +00:00
|
|
|
test.name, rr.Code, test.expectedStatusCode)
|
|
|
|
}
|
2024-07-13 14:47:23 +00:00
|
|
|
}
|
2024-07-13 14:35:17 +00:00
|
|
|
}
|
|
|
|
|
2024-07-14 09:49:42 +00:00
|
|
|
// }}}
|
|
|
|
// {{{ Test Helpers
|
|
|
|
|
2024-07-13 14:35:17 +00:00
|
|
|
func getCtx(req *http.Request) context.Context {
|
|
|
|
ctx, err := session.Load(req.Context(), req.Header.Get("X-Session"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
2024-07-14 09:49:42 +00:00
|
|
|
|
|
|
|
// }}}
|