From 1797af9fb6f05b18da6c438c9a79ff9bc2ed753d Mon Sep 17 00:00:00 2001 From: Joseph Spurrier Date: Tue, 7 Mar 2017 23:23:19 -0500 Subject: [PATCH] Consolidate controllers to one package --- controller/{login => }/login.go | 10 +++--- controller/{login => }/login_test.go | 42 +++++++++------------- controller/{register => }/register.go | 10 +++--- controller/{register => }/register_test.go | 42 +++++++++------------- controller/util.go | 10 ++++++ lib/boot/route.go | 7 ++-- 6 files changed, 57 insertions(+), 64 deletions(-) rename controller/{login => }/login.go (82%) rename controller/{login => }/login_test.go (65%) rename controller/{register => }/register.go (82%) rename controller/{register => }/register_test.go (63%) create mode 100644 controller/util.go diff --git a/controller/login/login.go b/controller/login.go similarity index 82% rename from controller/login/login.go rename to controller/login.go index a20de67..3b85d0e 100644 --- a/controller/login/login.go +++ b/controller/login.go @@ -1,4 +1,4 @@ -package login +package controller import ( "fmt" @@ -8,14 +8,14 @@ import ( "github.com/josephspurrier/gocleanarchitecture/lib/view" ) -// Handler represents the services required for this controller. -type Handler struct { +// LoginHandler represents the services required for this controller. +type LoginHandler struct { UserService domain.UserService ViewService view.Service } // Index displays the logon screen. -func (h *Handler) Index(w http.ResponseWriter, r *http.Request) { +func (h *LoginHandler) Index(w http.ResponseWriter, r *http.Request) { // Handle 404. if r.URL.Path != "/" { w.WriteHeader(http.StatusNotFound) @@ -32,7 +32,7 @@ func (h *Handler) Index(w http.ResponseWriter, r *http.Request) { } // Store handles the submission of the login information. -func (h *Handler) Store(w http.ResponseWriter, r *http.Request) { +func (h *LoginHandler) Store(w http.ResponseWriter, r *http.Request) { // Don't continue if required fields are missing. for _, v := range []string{"email", "password"} { if len(r.FormValue(v)) == 0 { diff --git a/controller/login/login_test.go b/controller/login_test.go similarity index 65% rename from controller/login/login_test.go rename to controller/login_test.go index 5add57e..31c8982 100644 --- a/controller/login/login_test.go +++ b/controller/login_test.go @@ -1,4 +1,4 @@ -package login_test +package controller import ( "net/http" @@ -6,21 +6,13 @@ import ( "net/url" "testing" - "github.com/josephspurrier/gocleanarchitecture/controller/login" "github.com/josephspurrier/gocleanarchitecture/database" "github.com/josephspurrier/gocleanarchitecture/domain" "github.com/josephspurrier/gocleanarchitecture/lib/view" ) -// AssertEqual throws an error if the two values are not equal. -func AssertEqual(t *testing.T, actualValue interface{}, expectedValue interface{}) { - if actualValue != expectedValue { - t.Errorf("\n got: %v\nwant: %v", actualValue, expectedValue) - } -} - -// TestIndex ensures the index function returns a 200 code. -func TestIndex(t *testing.T) { +// TestLoginIndex ensures the index function returns a 200 code. +func TestLoginIndex(t *testing.T) { // Set up the request. w := httptest.NewRecorder() r, err := http.NewRequest("GET", "/", nil) @@ -29,16 +21,16 @@ func TestIndex(t *testing.T) { } // Call the handler. - h := new(login.Handler) - h.ViewService = view.New("../../view", "tmpl") + h := new(LoginHandler) + h.ViewService = view.New("../view", "tmpl") h.Index(w, r) // Check the output. AssertEqual(t, w.Code, http.StatusOK) } -// TestStoreMissingRequiredField ensures required fields should be entered. -func TestStoreMissingRequiredFields(t *testing.T) { +// TestLoginStoreMissingRequiredField ensures required fields should be entered. +func TestLoginStoreMissingRequiredFields(t *testing.T) { // Set up the request. w := httptest.NewRecorder() r, err := http.NewRequest("POST", "/", nil) @@ -47,18 +39,18 @@ func TestStoreMissingRequiredFields(t *testing.T) { } // Call the handler. - h := new(login.Handler) + h := new(LoginHandler) db := new(database.MockService) h.UserService = database.NewUserService(db) - h.ViewService = view.New("../../view", "tmpl") + h.ViewService = view.New("../view", "tmpl") h.Index(w, r) // Check the output. AssertEqual(t, w.Code, http.StatusBadRequest) } -// TestStoreAuthenticateOK ensures login can be successful. -func TestStoreAuthenticateOK(t *testing.T) { +// TestLoginStoreAuthenticateOK ensures login can be successful. +func TestLoginStoreAuthenticateOK(t *testing.T) { // Set up the request. w := httptest.NewRecorder() r, err := http.NewRequest("POST", "/", nil) @@ -73,10 +65,10 @@ func TestStoreAuthenticateOK(t *testing.T) { r.Form.Add("password", "Pa$$w0rd") // Call the handler. - h := new(login.Handler) + h := new(LoginHandler) db := new(database.MockService) h.UserService = database.NewUserService(db) - h.ViewService = view.New("../../view", "tmpl") + h.ViewService = view.New("../view", "tmpl") // Create a new user. u := new(domain.User) @@ -90,8 +82,8 @@ func TestStoreAuthenticateOK(t *testing.T) { AssertEqual(t, w.Code, http.StatusOK) } -// TestStoreAuthenticateFail ensures login can fail. -func TestStoreAuthenticateFail(t *testing.T) { +// TestLoginStoreAuthenticateFail ensures login can fail. +func TestLoginStoreAuthenticateFail(t *testing.T) { // Set up the request. w := httptest.NewRecorder() r, err := http.NewRequest("POST", "/", nil) @@ -106,10 +98,10 @@ func TestStoreAuthenticateFail(t *testing.T) { r.Form.Add("password", "BadPa$$w0rd") // Call the handler. - h := new(login.Handler) + h := new(LoginHandler) db := new(database.MockService) h.UserService = database.NewUserService(db) - h.ViewService = view.New("../../view", "tmpl") + h.ViewService = view.New("../view", "tmpl") // Create a new user. u := new(domain.User) diff --git a/controller/register/register.go b/controller/register.go similarity index 82% rename from controller/register/register.go rename to controller/register.go index 4ebaf8f..cdfa076 100644 --- a/controller/register/register.go +++ b/controller/register.go @@ -1,4 +1,4 @@ -package register +package controller import ( "fmt" @@ -8,14 +8,14 @@ import ( "github.com/josephspurrier/gocleanarchitecture/lib/view" ) -// Handler represents the services required for this controller. -type Handler struct { +// RegisterHandler represents the services required for this controller. +type RegisterHandler struct { UserService domain.UserService ViewService view.Service } // Index displays the register screen. -func (h *Handler) Index(w http.ResponseWriter, r *http.Request) { +func (h *RegisterHandler) Index(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { h.Store(w, r) return @@ -25,7 +25,7 @@ func (h *Handler) Index(w http.ResponseWriter, r *http.Request) { } // Store adds a user to the database. -func (h *Handler) Store(w http.ResponseWriter, r *http.Request) { +func (h *RegisterHandler) Store(w http.ResponseWriter, r *http.Request) { // Don't continue if required fields are missing. for _, v := range []string{"firstname", "lastname", "email", "password"} { if len(r.FormValue(v)) == 0 { diff --git a/controller/register/register_test.go b/controller/register_test.go similarity index 63% rename from controller/register/register_test.go rename to controller/register_test.go index fa3050c..0e84af9 100644 --- a/controller/register/register_test.go +++ b/controller/register_test.go @@ -1,4 +1,4 @@ -package register_test +package controller import ( "net/http" @@ -6,20 +6,12 @@ import ( "net/url" "testing" - "github.com/josephspurrier/gocleanarchitecture/controller/register" "github.com/josephspurrier/gocleanarchitecture/database" "github.com/josephspurrier/gocleanarchitecture/lib/view" ) -// AssertEqual throws an error if the two values are not equal. -func AssertEqual(t *testing.T, actualValue interface{}, expectedValue interface{}) { - if actualValue != expectedValue { - t.Errorf("\n got: %v\nwant: %v", actualValue, expectedValue) - } -} - -// TestIndex ensures the index function returns a 200 code. -func TestIndex(t *testing.T) { +// TesttRegisterIndex ensures the index function returns a 200 code. +func TestRegisterIndex(t *testing.T) { // Set up the request. w := httptest.NewRecorder() r, err := http.NewRequest("GET", "/", nil) @@ -28,16 +20,16 @@ func TestIndex(t *testing.T) { } // Call the handler. - h := new(register.Handler) - h.ViewService = view.New("../../view", "tmpl") + h := new(RegisterHandler) + h.ViewService = view.New("../view", "tmpl") h.Index(w, r) // Check the output. AssertEqual(t, w.Code, http.StatusOK) } -// TestStoreCreateOK ensures register can be successful. -func TestStoreCreateOK(t *testing.T) { +// TesttRegisterStoreCreateOK ensures register can be successful. +func TesttRegisterStoreCreateOK(t *testing.T) { // Set up the request. w := httptest.NewRecorder() r, err := http.NewRequest("POST", "/", nil) @@ -54,10 +46,10 @@ func TestStoreCreateOK(t *testing.T) { r.Form.Add("password", "Pa$$w0rd") // Call the handler. - h := new(register.Handler) + h := new(RegisterHandler) db := new(database.MockService) h.UserService = database.NewUserService(db) - h.ViewService = view.New("../../view", "tmpl") + h.ViewService = view.New("../view", "tmpl") h.Index(w, r) // Check the output. @@ -69,8 +61,8 @@ func TestStoreCreateOK(t *testing.T) { AssertEqual(t, w.Code, http.StatusInternalServerError) } -// TestStoreCreateNoFieldFail ensures register can fail with no fields. -func TestStoreCreateNoFieldFail(t *testing.T) { +// TestRegisterStoreCreateNoFieldFail ensures register can fail with no fields. +func TesttRegisterStoreCreateNoFieldFail(t *testing.T) { // Set up the request. w := httptest.NewRecorder() r, err := http.NewRequest("POST", "/", nil) @@ -79,19 +71,19 @@ func TestStoreCreateNoFieldFail(t *testing.T) { } // Call the handler. - h := new(register.Handler) + h := new(RegisterHandler) db := new(database.MockService) h.UserService = database.NewUserService(db) - h.ViewService = view.New("../../view", "tmpl") + h.ViewService = view.New("../view", "tmpl") h.Index(w, r) // Check the output. AssertEqual(t, w.Code, http.StatusBadRequest) } -// TestStoreCreateOneMissingFieldFail ensures register can fail with one missing +// TesttRegisterStoreCreateOneMissingFieldFail ensures register can fail with one missing // field. -func TestStoreCreateOneMissingFieldFail(t *testing.T) { +func TesttRegisterStoreCreateOneMissingFieldFail(t *testing.T) { // Set up the request. w := httptest.NewRecorder() r, err := http.NewRequest("POST", "/", nil) @@ -108,10 +100,10 @@ func TestStoreCreateOneMissingFieldFail(t *testing.T) { r.Form.Add("password", "Pa$$w0rd") // Call the handler. - h := new(register.Handler) + h := new(RegisterHandler) db := new(database.MockService) h.UserService = database.NewUserService(db) - h.ViewService = view.New("../../view", "tmpl") + h.ViewService = view.New("../view", "tmpl") h.Index(w, r) // Check the output. diff --git a/controller/util.go b/controller/util.go new file mode 100644 index 0000000..6d748b7 --- /dev/null +++ b/controller/util.go @@ -0,0 +1,10 @@ +package controller + +import "testing" + +// AssertEqual throws an error if the two values are not equal. +func AssertEqual(t *testing.T, actualValue interface{}, expectedValue interface{}) { + if actualValue != expectedValue { + t.Errorf("\n got: %v\nwant: %v", actualValue, expectedValue) + } +} diff --git a/lib/boot/route.go b/lib/boot/route.go index ea9c6db..1a57cda 100644 --- a/lib/boot/route.go +++ b/lib/boot/route.go @@ -3,8 +3,7 @@ package boot import ( "net/http" - "github.com/josephspurrier/gocleanarchitecture/controller/login" - "github.com/josephspurrier/gocleanarchitecture/controller/register" + "github.com/josephspurrier/gocleanarchitecture/controller" ) // LoadRoutes returns a handler with all the routes. @@ -23,7 +22,7 @@ func (s *Service) LoadRoutes() http.Handler { // AddLogin registers the login handlers. func (s *Service) AddLogin(mux *http.ServeMux) { // Create handler. - h := new(login.Handler) + h := new(controller.LoginHandler) // Assign services. h.UserService = s.UserService @@ -36,7 +35,7 @@ func (s *Service) AddLogin(mux *http.ServeMux) { // AddRegister registers the register handlers. func (s *Service) AddRegister(mux *http.ServeMux) { // Create handler. - h := new(register.Handler) + h := new(controller.RegisterHandler) // Assign services. h.UserService = s.UserService