-
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
Showing
1 changed file
with
117 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,10 @@ package handlers | |
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
@@ -110,15 +110,16 @@ func TestRepository_Reservation(t *testing.T) { | |
} | ||
|
||
func TestRepository_PostReservation(t *testing.T) { | ||
reqBody := "start_date=2050-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-02") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1") | ||
|
||
req, _ := http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody)) | ||
postedData := url.Values{} | ||
postedData.Add("start_date", "2050-01-01") | ||
postedData.Add("end_date", "2050-01-02") | ||
postedData.Add("first_name", "Abneed") | ||
postedData.Add("last_name", "Rodriguez") | ||
postedData.Add("email", "[email protected]") | ||
postedData.Add("phone", "55-555-5555") | ||
postedData.Add("room_id", "1") | ||
|
||
req, _ := http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode())) | ||
ctx := getCtx(req) | ||
req = req.WithContext(ctx) | ||
|
||
|
@@ -150,15 +151,16 @@ func TestRepository_PostReservation(t *testing.T) { | |
} | ||
|
||
// test for invalid start date | ||
reqBody = "start_date=invalid" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-2") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody)) | ||
postedData = url.Values{} | ||
postedData.Add("start_date", "invalid") | ||
postedData.Add("end_date", "2050-01-02") | ||
postedData.Add("first_name", "Abneed") | ||
postedData.Add("last_name", "Rodriguez") | ||
postedData.Add("email", "[email protected]") | ||
postedData.Add("phone", "55-555-5555") | ||
postedData.Add("room_id", "1") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode())) | ||
ctx = getCtx(req) | ||
req = req.WithContext(ctx) | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
@@ -173,15 +175,16 @@ func TestRepository_PostReservation(t *testing.T) { | |
} | ||
|
||
// test for invalid end date | ||
reqBody = "start_date=2050-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=invalid") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody)) | ||
postedData = url.Values{} | ||
postedData.Add("start_date", "2050-01-01") | ||
postedData.Add("end_date", "invalid") | ||
postedData.Add("first_name", "Abneed") | ||
postedData.Add("last_name", "Rodriguez") | ||
postedData.Add("email", "[email protected]") | ||
postedData.Add("phone", "55-555-5555") | ||
postedData.Add("room_id", "1") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode())) | ||
ctx = getCtx(req) | ||
req = req.WithContext(ctx) | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
@@ -196,15 +199,16 @@ func TestRepository_PostReservation(t *testing.T) { | |
} | ||
|
||
// test for invalid room id | ||
reqBody = "start_date=2050-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-02") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=invalid") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody)) | ||
postedData = url.Values{} | ||
postedData.Add("start_date", "2050-01-01") | ||
postedData.Add("end_date", "2050-01-02") | ||
postedData.Add("first_name", "Abneed") | ||
postedData.Add("last_name", "Rodriguez") | ||
postedData.Add("email", "[email protected]") | ||
postedData.Add("phone", "55-555-5555") | ||
postedData.Add("room_id", "invalid") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode())) | ||
ctx = getCtx(req) | ||
req = req.WithContext(ctx) | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
@@ -219,15 +223,16 @@ func TestRepository_PostReservation(t *testing.T) { | |
} | ||
|
||
// test for invalid data | ||
reqBody = "start_date=2050-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-02") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=A") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody)) | ||
postedData = url.Values{} | ||
postedData.Add("start_date", "2050-01-01") | ||
postedData.Add("end_date", "2050-01-02") | ||
postedData.Add("first_name", "A") | ||
postedData.Add("last_name", "Rodriguez") | ||
postedData.Add("email", "[email protected]") | ||
postedData.Add("phone", "55-555-5555") | ||
postedData.Add("room_id", "1") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode())) | ||
ctx = getCtx(req) | ||
req = req.WithContext(ctx) | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
@@ -242,15 +247,16 @@ func TestRepository_PostReservation(t *testing.T) { | |
} | ||
|
||
// test for failure to insert reservation into database | ||
reqBody = "start_date=2050-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-02") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=2") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody)) | ||
postedData = url.Values{} | ||
postedData.Add("start_date", "2050-01-01") | ||
postedData.Add("end_date", "2050-01-02") | ||
postedData.Add("first_name", "Abneed") | ||
postedData.Add("last_name", "Rodriguez") | ||
postedData.Add("email", "[email protected]") | ||
postedData.Add("phone", "55-555-5555") | ||
postedData.Add("room_id", "2") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode())) | ||
ctx = getCtx(req) | ||
req = req.WithContext(ctx) | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
@@ -265,15 +271,16 @@ func TestRepository_PostReservation(t *testing.T) { | |
} | ||
|
||
// test for failure to insert restriction into database | ||
reqBody = "start_date=2050-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end_date=2050-01-02") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "first_name=Abneed") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "last_name=Rodriguez") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "[email protected]") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "phone=+5218671487159") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1000") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(reqBody)) | ||
postedData = url.Values{} | ||
postedData.Add("start_date", "2050-01-01") | ||
postedData.Add("end_date", "2050-01-02") | ||
postedData.Add("first_name", "Abneed") | ||
postedData.Add("last_name", "Rodriguez") | ||
postedData.Add("email", "[email protected]") | ||
postedData.Add("phone", "55-555-5555") | ||
postedData.Add("room_id", "1000") | ||
|
||
req, _ = http.NewRequest("POST", "/make-reservation", strings.NewReader(postedData.Encode())) | ||
ctx = getCtx(req) | ||
req = req.WithContext(ctx) | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
@@ -302,11 +309,12 @@ func TestRepository_PostAvailability(t *testing.T) { | |
// first case -- rooms are not available | ||
*****************************************/ | ||
// create our request body | ||
reqBody := "start=2050-01-02" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2050-01-02") | ||
postedData := url.Values{} | ||
postedData.Add("start", "2050-01-02") | ||
postedData.Add("end", "2050-01-02") | ||
|
||
// create our request | ||
req, _ := http.NewRequest("POST", "/search_availability", strings.NewReader(reqBody)) | ||
req, _ := http.NewRequest("POST", "/search_availability", strings.NewReader(postedData.Encode())) | ||
|
||
// get the context with session | ||
ctx := getCtx(req) | ||
|
@@ -335,11 +343,12 @@ func TestRepository_PostAvailability(t *testing.T) { | |
*****************************************/ | ||
// this time, we specify a start date before 2040-01-01, which will give us | ||
// a non-empty slice, indicating that rooms are available | ||
reqBody = "start=2040-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2040-01-02") | ||
postedData = url.Values{} | ||
postedData.Add("start", "2040-01-01") | ||
postedData.Add("end", "2050-01-02") | ||
|
||
// create our request | ||
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(reqBody)) | ||
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(postedData.Encode())) | ||
|
||
// get the context with session | ||
ctx = getCtx(req) | ||
|
@@ -395,9 +404,10 @@ func TestRepository_PostAvailability(t *testing.T) { | |
// fourth case -- start date in wrong format | ||
*****************************************/ | ||
// this time, we specify a start date in the wrong format | ||
reqBody = "start=invalid" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2040-01-02") | ||
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(reqBody)) | ||
postedData = url.Values{} | ||
postedData.Add("start", "invalid") | ||
postedData.Add("end", "2040-01-02") | ||
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(postedData.Encode())) | ||
|
||
// get the context with session | ||
ctx = getCtx(req) | ||
|
@@ -425,9 +435,10 @@ func TestRepository_PostAvailability(t *testing.T) { | |
// fifth case -- end date in wrong format | ||
*****************************************/ | ||
// this time, we specify a start date in the wrong format | ||
reqBody = "start=2040-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "invalid") | ||
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(reqBody)) | ||
postedData = url.Values{} | ||
postedData.Add("start", "2040-01-01") | ||
postedData.Add("end", "invalid") | ||
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(postedData.Encode())) | ||
|
||
// get the context with session | ||
ctx = getCtx(req) | ||
|
@@ -456,9 +467,10 @@ func TestRepository_PostAvailability(t *testing.T) { | |
*****************************************/ | ||
// this time, we specify a start date of 2060-01-01, which will cause | ||
// our testdb repo to return an error | ||
reqBody = "start=2060-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2060-01-02") | ||
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(reqBody)) | ||
postedData = url.Values{} | ||
postedData.Add("start", "2060-01-01") | ||
postedData.Add("end", "2060-01-02") | ||
req, _ = http.NewRequest("POST", "/search-availability", strings.NewReader(postedData.Encode())) | ||
|
||
// get the context with session | ||
ctx = getCtx(req) | ||
|
@@ -488,12 +500,13 @@ func TestRepository_AvailabilityJSON(t *testing.T) { | |
// first case -- rooms are not available | ||
*****************************************/ | ||
// create our request body | ||
reqBody := "start=2050-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2050-01-02") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1") | ||
postedData := url.Values{} | ||
postedData.Add("start", "2050-01-01") | ||
postedData.Add("end", "2050-01-02") | ||
postedData.Add("room_id", "1") | ||
|
||
// create request | ||
req, _ := http.NewRequest("POST", "/search-availability-json", strings.NewReader(reqBody)) | ||
req, _ := http.NewRequest("POST", "/search-availability-json", strings.NewReader(postedData.Encode())) | ||
|
||
// get context with session | ||
ctx := getCtx(req) | ||
|
@@ -526,15 +539,16 @@ func TestRepository_AvailabilityJSON(t *testing.T) { | |
} | ||
|
||
/***************************************** | ||
// second case -- rooms not available | ||
// second case -- rooms available | ||
*****************************************/ | ||
// create our request body | ||
reqBody = "start=2040-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2040-01-02") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1") | ||
postedData = url.Values{} | ||
postedData.Add("start", "2040-01-01") | ||
postedData.Add("end", "2040-01-02") | ||
postedData.Add("room_id", "1") | ||
|
||
// create our request | ||
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(reqBody)) | ||
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(postedData.Encode())) | ||
|
||
// get the context with session | ||
ctx = getCtx(req) | ||
|
@@ -602,10 +616,11 @@ func TestRepository_AvailabilityJSON(t *testing.T) { | |
// fourth case -- database error | ||
*****************************************/ | ||
// create our request body | ||
reqBody = "start=2060-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2060-01-02") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1") | ||
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(reqBody)) | ||
postedData = url.Values{} | ||
postedData.Add("start", "2060-01-01") | ||
postedData.Add("end", "2060-01-02") | ||
postedData.Add("room_id", "1") | ||
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(postedData.Encode())) | ||
|
||
// get the context with session | ||
ctx = getCtx(req) | ||
|
@@ -639,12 +654,13 @@ func TestRepository_AvailabilityJSON(t *testing.T) { | |
// fifth case -- start date not valid | ||
*****************************************/ | ||
// create our request body | ||
reqBody = "start=invalid" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=2050-01-02") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1") | ||
postedData = url.Values{} | ||
postedData.Add("start", "invalid") | ||
postedData.Add("end", "2060-01-02") | ||
postedData.Add("room_id", "1") | ||
|
||
// create request | ||
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(reqBody)) | ||
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(postedData.Encode())) | ||
|
||
// get context with session | ||
ctx = getCtx(req) | ||
|
@@ -679,12 +695,13 @@ func TestRepository_AvailabilityJSON(t *testing.T) { | |
// sixth case -- end date not valid | ||
*****************************************/ | ||
// create our request body | ||
reqBody = "start=2050-01-01" | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "end=invalid") | ||
reqBody = fmt.Sprintf("%s&%s", reqBody, "room_id=1") | ||
postedData = url.Values{} | ||
postedData.Add("start", "2050-01-01") | ||
postedData.Add("end", "invalid") | ||
postedData.Add("room_id", "1") | ||
|
||
// create request | ||
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(reqBody)) | ||
req, _ = http.NewRequest("POST", "/search-availability-json", strings.NewReader(postedData.Encode())) | ||
|
||
// get context with session | ||
ctx = getCtx(req) | ||
|