udemy-go-web-1/internal/handlers/handlers_test.go

367 lines
9.2 KiB
Go

package handlers
import (
"context"
"encoding/json"
"go-udemy-web-1/internal/models"
"log"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
"time"
)
type postData struct {
key string
value string
}
var theTests = []struct {
name string
url string
method string
expectedStatusCode int
}{
{"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},
// {"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},
}
func TestHandlers(t *testing.T) {
routes := getRoutes()
ts := httptest.NewTLSServer(routes)
defer ts.Close()
for _, e := range theTests {
resp, err := ts.Client().Get(ts.URL + e.url)
if err != nil {
t.Log(err)
t.Fatal(err)
}
if resp.StatusCode != e.expectedStatusCode {
t.Errorf("for %s, expected %d but got %d\n", e.name, e.expectedStatusCode, resp.StatusCode)
}
}
}
// {{{ Make Reservation Tests
var makeReservationTests = []struct {
name string
roomID int
expectedStatusCode int
}{
{"ok", 1, http.StatusOK},
{"no session", 0, http.StatusTemporaryRedirect},
{"non-existant room", 100, http.StatusTemporaryRedirect},
}
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)
handler.ServeHTTP(rr, req)
if rr.Code != test.expectedStatusCode {
t.Errorf("for %s, reservation handler returned response code: got %d, wanted %d\n",
test.name, rr.Code, http.StatusOK)
}
}
}
// }}}
// {{{ PostMakeReservation tests
var postMakeReservationTests = []struct {
name string
reservationInfo []postData
expectedStatusCode int
}{
{
"ok",
[]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"},
},
http.StatusSeeOther,
},
{
"no_session",
[]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"},
},
http.StatusTemporaryRedirect,
},
{"no_post_data", []postData{}, http.StatusOK},
{
"missing first name",
[]postData{
{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"},
},
http.StatusOK,
},
{
"wrong first name",
[]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: "1"},
{key: "start_date", value: "2050-01-01"},
{key: "end_date", value: "2050-01-02"},
},
http.StatusOK,
},
{
"wrong email",
[]postData{
{key: "first_name", value: "John"},
{key: "last_name", value: "Smith"},
{key: "email", value: "john@smith"},
{key: "phone", value: "1234"},
{key: "room_id", value: "1"},
{key: "start_date", value: "2050-01-01"},
{key: "end_date", value: "2050-01-02"},
},
http.StatusOK,
},
{
"insert reservation error",
[]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"},
},
http.StatusTemporaryRedirect,
},
{
"insert room restriction error",
[]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"},
},
http.StatusTemporaryRedirect,
},
}
func TestRepository_PostMakeReservation(t *testing.T) {
for _, test := range postMakeReservationTests {
roomID := 1
reqBody := url.Values{}
if len(test.reservationInfo) > 0 {
for _, element := range test.reservationInfo {
reqBody.Add(element.key, element.value)
if element.key == "room_id" {
roomID, _ = strconv.Atoi(element.value)
}
}
}
layout := "2006-01-02"
sd, _ := time.Parse(layout, "2050-01-01")
ed, _ := time.Parse(layout, "2050-01-02")
reservation := models.Reservation{
RoomID: roomID,
StartDate: sd,
EndDate: ed,
}
req, _ := http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody.Encode()))
ctx := getCtx(req)
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()
if roomID == 0 {
session.Put(ctx, "reservation", nil)
} else {
session.Put(ctx, "reservation", reservation)
}
handler := http.HandlerFunc(Repo.PostMakeReservation)
handler.ServeHTTP(rr, req)
if rr.Code != test.expectedStatusCode {
t.Errorf("for %s, reservation handler returned response code: got %d, wanted %d\n",
test.name, rr.Code, test.expectedStatusCode)
}
}
}
// }}}
// {{{ Test AvailabilityJSON
var availabilityJSONTests = []struct {
name string
queryInfo []postData
expectedStatusJSON jsonResponse
}{
{"ok", []postData{
{key: "start", value: "2050-01-01"},
{key: "end", value: "2050-01-02"},
{key: "room_id", value: "1"},
}, jsonResponse{
OK: true,
Message: "",
StartDate: "2050-01-01",
EndDate: "2050-01-02",
RoomID: "1",
}},
{"not available", []postData{
{key: "start", value: "2050-01-01"},
{key: "end", value: "2050-01-02"},
{key: "room_id", value: "2"},
}, jsonResponse{
OK: false,
Message: "",
StartDate: "2050-01-01",
EndDate: "2050-01-02",
RoomID: "2",
}},
{"wrong start date", []postData{
{key: "start", value: "2050-01"},
{key: "end", value: "2050-01-02"},
{key: "room_id", value: "1"},
}, jsonResponse{
OK: false,
Message: "Wrong startDate",
}},
{"wrong end date", []postData{
{key: "start", value: "2050-01-01"},
{key: "end", value: "wrong"},
{key: "room_id", value: "1"},
}, jsonResponse{
OK: false,
Message: "Wrong endDate",
}},
{"wrong room id", []postData{
{key: "start", value: "2050-01-01"},
{key: "end", value: "2050-01-02"},
{key: "room_id", value: "x"},
}, jsonResponse{
OK: false,
Message: "Wrong roomID",
}},
{"not available", []postData{
{key: "start", value: "2050-01-01"},
{key: "end", value: "2050-01-02"},
{key: "room_id", value: "100"},
}, jsonResponse{
OK: false,
Message: "Error connecting to database",
}},
}
func Test_AvailabilityJSON(t *testing.T) {
for _, test := range availabilityJSONTests {
reqBody := url.Values{}
for _, element := range test.queryInfo {
reqBody.Add(element.key, element.value)
}
req, _ := http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody.Encode()))
ctx := getCtx(req)
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(Repo.AvailabilityJSON)
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("for %s, reservation handler returned response code: got %d, wanted %d\n",
test.name, rr.Code, http.StatusOK)
}
var j jsonResponse
err := json.Unmarshal(rr.Body.Bytes(), &j)
if err != nil {
t.Errorf("for %s, failed to parse json", test.name)
}
if j != test.expectedStatusJSON {
expected, _ := json.MarshalIndent(test.expectedStatusJSON, "", " ")
t.Errorf("for %s, returned json is wrong, expected: %s, returned: %s",
test.name, expected, rr.Body.String())
}
}
}
// }}}
// {{{ Test Helpers
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
}
// }}}