Compare commits
2 Commits
1c46c5a64b
...
0f17b3405e
Author | SHA1 | Date | |
---|---|---|---|
0f17b3405e | |||
f67aed4942 |
@ -80,13 +80,15 @@ func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
|
|||||||
// filled with the info when sent back.
|
// filled with the info when sent back.
|
||||||
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 {
|
||||||
helpers.ServerError(w, errors.New("cannot get reservation from session"))
|
m.App.Session.Put(r.Context(), "error", "can't get reservation from session")
|
||||||
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
room, err := m.DB.GetRoomById(res.RoomID)
|
room, err := m.DB.GetRoomById(res.RoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helpers.ServerError(w, err)
|
m.App.Session.Put(r.Context(), "error", "can't find room")
|
||||||
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"go-udemy-web-1/internal/models"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,30 +18,29 @@ var theTests = []struct {
|
|||||||
name string
|
name string
|
||||||
url string
|
url string
|
||||||
method string
|
method string
|
||||||
params []postData
|
|
||||||
expectedStatusCode int
|
expectedStatusCode int
|
||||||
}{
|
}{
|
||||||
{"home", "/", "GET", []postData{}, http.StatusOK},
|
{"home", "/", "GET", http.StatusOK},
|
||||||
{"about", "/about", "GET", []postData{}, http.StatusOK},
|
{"about", "/about", "GET", http.StatusOK},
|
||||||
{"gq", "/generals-quarters", "GET", []postData{}, http.StatusOK},
|
{"gq", "/generals-quarters", "GET", http.StatusOK},
|
||||||
{"ms", "/majors-suite", "GET", []postData{}, http.StatusOK},
|
{"ms", "/majors-suite", "GET", http.StatusOK},
|
||||||
{"sa", "/availability", "GET", []postData{}, http.StatusOK},
|
{"sa", "/availability", "GET", http.StatusOK},
|
||||||
{"contact", "/contact", "GET", []postData{}, http.StatusOK},
|
{"contact", "/contact", "GET", http.StatusOK},
|
||||||
{"ma", "/make-reservation", "GET", []postData{}, http.StatusOK},
|
|
||||||
{"post-search-avail", "/availability", "POST", []postData{
|
// {"post-search-avail", "/availability", "POST", []postData{
|
||||||
{key: "start", value: "2020-01-01"},
|
// {key: "start", value: "2020-01-01"},
|
||||||
{key: "end", value: "2020-01-02"},
|
// {key: "end", value: "2020-01-02"},
|
||||||
}, http.StatusOK},
|
// }, http.StatusOK},
|
||||||
{"post-search-avail-json", "/availability-json", "POST", []postData{
|
// {"post-search-avail-json", "/availability-json", "POST", []postData{
|
||||||
{key: "start", value: "2020-01-01"},
|
// {key: "start", value: "2020-01-01"},
|
||||||
{key: "end", value: "2020-01-02"},
|
// {key: "end", value: "2020-01-02"},
|
||||||
}, http.StatusOK},
|
// }, http.StatusOK},
|
||||||
{"make-reservation", "/make-reservation", "POST", []postData{
|
// {"make-reservation", "/make-reservation", "POST", []postData{
|
||||||
{key: "first_name", value: "John"},
|
// {key: "first_name", value: "John"},
|
||||||
{key: "last_name", value: "Smith"},
|
// {key: "last_name", value: "Smith"},
|
||||||
{key: "email", value: "me@here.com"},
|
// {key: "email", value: "me@here.com"},
|
||||||
{key: "phone", value: "12345"},
|
// {key: "phone", value: "12345"},
|
||||||
}, http.StatusOK},
|
// }, http.StatusOK},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandlers(t *testing.T) {
|
func TestHandlers(t *testing.T) {
|
||||||
@ -48,7 +49,6 @@ func TestHandlers(t *testing.T) {
|
|||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
for _, e := range theTests {
|
for _, e := range theTests {
|
||||||
if e.method == "GET" {
|
|
||||||
resp, err := ts.Client().Get(ts.URL + e.url)
|
resp, err := ts.Client().Get(ts.URL + e.url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
@ -57,19 +57,68 @@ func TestHandlers(t *testing.T) {
|
|||||||
if resp.StatusCode != e.expectedStatusCode {
|
if resp.StatusCode != e.expectedStatusCode {
|
||||||
t.Errorf("for %s, expected %d but got %d", e.name, e.expectedStatusCode, resp.StatusCode)
|
t.Errorf("for %s, expected %d but got %d", e.name, e.expectedStatusCode, resp.StatusCode)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
values := url.Values{}
|
|
||||||
for _, x := range e.params {
|
|
||||||
values.Add(x.key, x.value)
|
|
||||||
}
|
|
||||||
resp, err := ts.Client().PostForm(ts.URL+e.url, values)
|
|
||||||
if err != nil {
|
|
||||||
t.Log(err)
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if resp.StatusCode != e.expectedStatusCode {
|
|
||||||
t.Errorf("for %s, expected %d but got %d", e.name, e.expectedStatusCode, resp.StatusCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRepository_Reservation(t *testing.T) {
|
||||||
|
reservation := models.Reservation{
|
||||||
|
RoomID: 1,
|
||||||
|
Room: models.Room{
|
||||||
|
ID: 1,
|
||||||
|
RoomName: "General's Quarters",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("GET", "/make-reservation", nil)
|
||||||
|
ctx := getCtx(req)
|
||||||
|
|
||||||
|
req = req.WithContext(ctx)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
session.Put(ctx, "reservation", reservation)
|
||||||
|
|
||||||
|
handler := http.HandlerFunc(Repo.MakeReservation)
|
||||||
|
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if rr.Code != http.StatusOK {
|
||||||
|
t.Errorf("Reservation handler returned response code: got %d, wanted %d",
|
||||||
|
rr.Code, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// test case where reservation is not in session (reset everything)
|
||||||
|
req, _ = http.NewRequest("GET", "/make-reservation", nil)
|
||||||
|
ctx = getCtx(req)
|
||||||
|
req = req.WithContext(ctx)
|
||||||
|
rr = httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
if rr.Code != http.StatusTemporaryRedirect {
|
||||||
|
t.Errorf("Reservation handler returned response code: got %d, wanted %d",
|
||||||
|
rr.Code, http.StatusTemporaryRedirect)
|
||||||
|
}
|
||||||
|
|
||||||
|
// test with non-existant room
|
||||||
|
req, _ = http.NewRequest("GET", "/make-reservation", nil)
|
||||||
|
ctx = getCtx(req)
|
||||||
|
req = req.WithContext(ctx)
|
||||||
|
rr = httptest.NewRecorder()
|
||||||
|
reservation.RoomID = 100
|
||||||
|
session.Put(ctx, "reservation", reservation)
|
||||||
|
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
if rr.Code != http.StatusTemporaryRedirect {
|
||||||
|
t.Errorf("Reservation handler returned response code: got %d, wanted %d",
|
||||||
|
rr.Code, http.StatusTemporaryRedirect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alexedwards/scs/v2"
|
"github.com/alexedwards/scs/v2"
|
||||||
@ -26,7 +27,7 @@ var (
|
|||||||
session *scs.SessionManager
|
session *scs.SessionManager
|
||||||
)
|
)
|
||||||
|
|
||||||
func getRoutes() http.Handler {
|
func TestMain(m *testing.M) {
|
||||||
gob.Register(models.Reservation{})
|
gob.Register(models.Reservation{})
|
||||||
// change this to true when in production
|
// change this to true when in production
|
||||||
app.InProduction = false
|
app.InProduction = false
|
||||||
@ -57,6 +58,10 @@ func getRoutes() http.Handler {
|
|||||||
|
|
||||||
render.NewRenderer(&app)
|
render.NewRenderer(&app)
|
||||||
|
|
||||||
|
os.Exit(m.Run())
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRoutes() http.Handler {
|
||||||
mux := chi.NewMux()
|
mux := chi.NewMux()
|
||||||
|
|
||||||
mux.Use(middleware.Recoverer)
|
mux.Use(middleware.Recoverer)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package dbrepo
|
package dbrepo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"go-udemy-web-1/internal/models"
|
"go-udemy-web-1/internal/models"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -33,5 +34,9 @@ func (m *testDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([]mode
|
|||||||
// GetRoomById gets a room by id
|
// GetRoomById gets a room by id
|
||||||
func (m *testDBRepo) GetRoomById(id int) (models.Room, error) {
|
func (m *testDBRepo) GetRoomById(id int) (models.Room, error) {
|
||||||
var room models.Room
|
var room models.Room
|
||||||
|
|
||||||
|
if id > 2 {
|
||||||
|
return room, errors.New("Deliberate error")
|
||||||
|
}
|
||||||
return room, nil
|
return room, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user