-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5637a9
commit 2e499d0
Showing
4 changed files
with
400 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package handlers | |
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
|
@@ -982,3 +983,327 @@ func getCtx(req *http.Request) context.Context { | |
|
||
return ctx | ||
} | ||
|
||
// loginTests is the data for the Login handler tests | ||
var loginTests = []struct { | ||
name string | ||
email string | ||
expectedStatusCode int | ||
expectedHTML string | ||
expectedLocation string | ||
}{ | ||
{ | ||
"valid-credentials", | ||
"[email protected]", | ||
http.StatusSeeOther, | ||
"", | ||
"/", | ||
}, | ||
{ | ||
"invalid-credentials", | ||
"[email protected]", | ||
http.StatusSeeOther, | ||
"", | ||
"/user/login", | ||
}, | ||
{ | ||
"invalid-data", | ||
"j", | ||
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 to find %s but did not", e.name, e.expectedHTML) | ||
} | ||
} | ||
} | ||
} | ||
|
||
var adminPostShowReservationTests = []struct { | ||
name string | ||
url string | ||
postedData url.Values | ||
expectedResponseCode int | ||
expectedLocation string | ||
expectedHTML string | ||
}{ | ||
{ | ||
name: "valid-data-from-new", | ||
url: "/admin/reservations/new/1/show", | ||
postedData: url.Values{ | ||
"full_name": {"Stan Smith"}, | ||
"email": {"[email protected]"}, | ||
"phone": {"555-555-5555"}, | ||
}, | ||
expectedResponseCode: http.StatusSeeOther, | ||
expectedLocation: "/admin/reservations-new", | ||
expectedHTML: "", | ||
}, | ||
{ | ||
name: "valid-data-from-all", | ||
url: "/admin/reservations/all/1/show", | ||
postedData: url.Values{ | ||
"full_name": {"Stan Smith"}, | ||
"email": {"[email protected]"}, | ||
"phone": {"555-555-5555"}, | ||
}, | ||
expectedResponseCode: http.StatusSeeOther, | ||
expectedLocation: "/admin/reservations-all", | ||
expectedHTML: "", | ||
}, | ||
{ | ||
name: "valid-data-from-cal", | ||
url: "/admin/reservations/calendar/1/show", | ||
postedData: url.Values{ | ||
"full_name": {"Stan Smith"}, | ||
"email": {"[email protected]"}, | ||
"phone": {"555-555-5555"}, | ||
"year": {"2024"}, | ||
"month": {"02"}, | ||
}, | ||
expectedResponseCode: http.StatusSeeOther, | ||
expectedLocation: "/admin/reservations-calendar?y=2024&m=02", | ||
expectedHTML: "", | ||
}, | ||
} | ||
|
||
// TestAdminPostShowReservation tests the AdminPostReservation handler | ||
func TestAdminPostShowReservation(t *testing.T) { | ||
for _, e := range adminPostShowReservationTests { | ||
var req *http.Request | ||
if e.postedData != nil { | ||
req, _ = http.NewRequest("POST", "/user/login", strings.NewReader(e.postedData.Encode())) | ||
} else { | ||
req, _ = http.NewRequest("POST", "/user/login", nil) | ||
} | ||
ctx := getCtx(req) | ||
req = req.WithContext(ctx) | ||
req.RequestURI = e.url | ||
|
||
// set the header | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
rr := httptest.NewRecorder() | ||
|
||
// call the handler | ||
handler := http.HandlerFunc(Repo.AdminPostShowReservation) | ||
handler.ServeHTTP(rr, req) | ||
|
||
if rr.Code != e.expectedResponseCode { | ||
t.Errorf("failed %s: expected code %d, but got %d", e.name, e.expectedResponseCode, 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 to find %s but did not", e.name, e.expectedHTML) | ||
} | ||
} | ||
} | ||
} | ||
|
||
var adminPostReservationCalendarTests = []struct { | ||
name string | ||
postedData url.Values | ||
expectedResponseCode int | ||
expectedLocation string | ||
expectedHTML string | ||
blocks int | ||
reservations int | ||
}{ | ||
{ | ||
name: "cal", | ||
postedData: url.Values{ | ||
"year": {time.Now().Format("2006")}, | ||
"month": {time.Now().Format("01")}, | ||
fmt.Sprintf("add_block_1_%s", time.Now().AddDate(0, 0, 2).Format("2006-01-2")): {"1"}, | ||
}, | ||
expectedResponseCode: http.StatusSeeOther, | ||
}, | ||
{ | ||
name: "cal-blocks", | ||
postedData: url.Values{}, | ||
expectedResponseCode: http.StatusSeeOther, | ||
blocks: 1, | ||
}, | ||
{ | ||
name: "cal-res", | ||
postedData: url.Values{}, | ||
expectedResponseCode: http.StatusSeeOther, | ||
reservations: 1, | ||
}, | ||
} | ||
|
||
func TestPostReservationCalendar(t *testing.T) { | ||
for _, e := range adminPostReservationCalendarTests { | ||
var req *http.Request | ||
if e.postedData != nil { | ||
req, _ = http.NewRequest("POST", "/admin/reservations-calendar", strings.NewReader(e.postedData.Encode())) | ||
} else { | ||
req, _ = http.NewRequest("POST", "/admin/reservations-calendar", nil) | ||
} | ||
ctx := getCtx(req) | ||
req = req.WithContext(ctx) | ||
|
||
now := time.Now() | ||
bm := make(map[string]int) | ||
rm := make(map[string]int) | ||
|
||
currentYear, currentMonth, _ := now.Date() | ||
currentLocation := now.Location() | ||
|
||
firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation) | ||
lastOfMonth := firstOfMonth.AddDate(0, 1, -1) | ||
|
||
for d := firstOfMonth; d.After(lastOfMonth) == false; d = d.AddDate(0, 0, 1) { | ||
rm[d.Format("2006-01-2")] = 0 | ||
bm[d.Format("2006-01-2")] = 0 | ||
} | ||
|
||
if e.blocks > 0 { | ||
bm[firstOfMonth.Format("2006-01-2")] = e.blocks | ||
} | ||
|
||
if e.reservations > 0 { | ||
rm[lastOfMonth.Format("2006-01-2")] = e.reservations | ||
} | ||
|
||
session.Put(ctx, "block_map_1", bm) | ||
session.Put(ctx, "reservation_map_1", rm) | ||
|
||
// set the header | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
rr := httptest.NewRecorder() | ||
|
||
// call the handler | ||
handler := http.HandlerFunc(Repo.AdminPostReservationsCalendar) | ||
handler.ServeHTTP(rr, req) | ||
|
||
if rr.Code != e.expectedResponseCode { | ||
t.Errorf("failed %s: expected code %d, but got %d", e.name, e.expectedResponseCode, rr.Code) | ||
} | ||
|
||
} | ||
} | ||
|
||
var adminProcessReservationTests = []struct { | ||
name string | ||
queryParams string | ||
expectedResponseCode int | ||
expectedLocation string | ||
}{ | ||
{ | ||
name: "process-reservation", | ||
queryParams: "", | ||
expectedResponseCode: http.StatusSeeOther, | ||
expectedLocation: "", | ||
}, | ||
{ | ||
name: "process-reservation-back-to-cal", | ||
queryParams: "?y=2024&m=02", | ||
expectedResponseCode: http.StatusSeeOther, | ||
expectedLocation: "", | ||
}, | ||
} | ||
|
||
func TestAdminProcessReservation(t *testing.T) { | ||
for _, e := range adminProcessReservationTests { | ||
req, _ := http.NewRequest("GET", fmt.Sprintf("/admin/process-reservation/cal/1/do%s", e.queryParams), nil) | ||
ctx := getCtx(req) | ||
req = req.WithContext(ctx) | ||
|
||
rr := httptest.NewRecorder() | ||
|
||
handler := http.HandlerFunc(Repo.AdminProcessReservation) | ||
handler.ServeHTTP(rr, req) | ||
|
||
if rr.Code != http.StatusSeeOther { | ||
t.Errorf("failed %s: expected code %d, but got %d", e.name, e.expectedResponseCode, rr.Code) | ||
} | ||
} | ||
} | ||
|
||
var adminDeleteReservationTests = []struct { | ||
name string | ||
queryParams string | ||
expectedResponseCode int | ||
expectedLocation string | ||
}{ | ||
{ | ||
name: "delete-reservation", | ||
queryParams: "", | ||
expectedResponseCode: http.StatusSeeOther, | ||
expectedLocation: "", | ||
}, | ||
{ | ||
name: "delete-reservation-back-to-cal", | ||
queryParams: "?y=2024&m=02", | ||
expectedResponseCode: http.StatusSeeOther, | ||
expectedLocation: "", | ||
}, | ||
} | ||
|
||
func TestAdminDeleteReservation(t *testing.T) { | ||
for _, e := range adminDeleteReservationTests { | ||
req, _ := http.NewRequest("GET", fmt.Sprintf("/admin/process-reservation/cal/1/do%s", e.queryParams), nil) | ||
ctx := getCtx(req) | ||
req = req.WithContext(ctx) | ||
|
||
rr := httptest.NewRecorder() | ||
|
||
handler := http.HandlerFunc(Repo.AdminDeleteReservation) | ||
handler.ServeHTTP(rr, req) | ||
|
||
if rr.Code != http.StatusSeeOther { | ||
t.Errorf("failed %s: expected code %d, but got %d", e.name, e.expectedResponseCode, rr.Code) | ||
} | ||
} | ||
} |
Oops, something went wrong.