Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
ybkuroki committed Jul 19, 2020
1 parent 94bc921 commit 20ecca6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
17 changes: 12 additions & 5 deletions controller/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@ import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/ybkuroki/go-webapp-sample/config"
"github.com/ybkuroki/go-webapp-sample/model"
"github.com/ybkuroki/go-webapp-sample/service"
"github.com/ybkuroki/go-webapp-sample/session"
)

// GetLoginStatus is
var dummyAccount = model.NewAccountWithPlainPassword("test", "test", model.NewAuthority("Admin"))

// GetLoginStatus returns the status of login.
func GetLoginStatus() echo.HandlerFunc {
return func(c echo.Context) error {
return c.JSON(http.StatusOK, true)
}
}

// GetLoginAccount is
// GetLoginAccount returns the account data of logged in user.
func GetLoginAccount() echo.HandlerFunc {
return func(c echo.Context) error {
if !config.GetConfig().Extension.SecurityEnabled {
return c.JSON(http.StatusOK, dummyAccount)
}
return c.JSON(http.StatusOK, session.GetAccount(c))
}
}

// PostLogin is
// PostLogin is the method to login using username and password by http post.
func PostLogin() echo.HandlerFunc {
return func(c echo.Context) error {
username := c.FormValue("username")
Expand All @@ -31,7 +38,7 @@ func PostLogin() echo.HandlerFunc {
account := session.GetAccount(c)
if account == nil {
authenticate, a := service.AuthenticateByUsernameAndPassword(username, password)
if authenticate == true {
if authenticate {
_ = session.SetAccount(c, a)
_ = session.Save(c)
return c.JSON(http.StatusOK, a)
Expand All @@ -42,7 +49,7 @@ func PostLogin() echo.HandlerFunc {
}
}

// PostLogout is
// PostLogout is the method to logout by http post.
func PostLogout() echo.HandlerFunc {
return func(c echo.Context) error {
_ = session.SetAccount(c, nil)
Expand Down
3 changes: 2 additions & 1 deletion controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestGetLoginAccount(t *testing.T) {

router.ServeHTTP(rec, req)

account := model.NewAccountWithPlainPassword("test", "test", model.NewAuthority("Admin"))
assert.Equal(t, http.StatusOK, rec.Code)
assert.JSONEq(t, test.ConvertToString(&model.Account{ID: 1, Name: "test"}), rec.Body.String())
assert.JSONEq(t, test.ConvertToString(account), rec.Body.String())
}
7 changes: 5 additions & 2 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ func Init(e *echo.Echo, conf *config.Config) {

e.GET(controller.APIAccountLoginStatus, controller.GetLoginStatus())
e.GET(controller.APIAccountLoginAccount, controller.GetLoginAccount())
e.POST(controller.APIAccountLogin, controller.PostLogin())
e.POST(controller.APIAccountLogout, controller.PostLogout())

if conf.Extension.SecurityEnabled {
e.POST(controller.APIAccountLogin, controller.PostLogin())
e.POST(controller.APIAccountLogout, controller.PostLogout())
}

e.GET(controller.APIHealth, controller.GetHealthCheck())

Expand Down
6 changes: 3 additions & 3 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Init(e *echo.Echo, conf *config.Config) {
func AuthenticationMiddleware(conf *config.Config) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if hasAuthorization(c, conf) == false {
if !hasAuthorization(c, conf) {
return c.JSON(http.StatusUnauthorized, false)
}
if err := next(c); err != nil {
Expand Down Expand Up @@ -108,7 +108,7 @@ func GetValue(c echo.Context, key string) string {
sess := Get(c)
v := sess.Values[key]
data, result := v.(string)
if result == true && data != "null" {
if result && data != "null" {
return data
}
return ""
Expand All @@ -124,7 +124,7 @@ func GetAccount(c echo.Context) *model.Account {
v := GetValue(c, Account)
if v != "" {
a := &model.Account{}
json.Unmarshal([]byte(v), a)
_ = json.Unmarshal([]byte(v), a)
return a
}
return nil
Expand Down
3 changes: 3 additions & 0 deletions test/unittest_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ybkuroki/go-webapp-sample/logger"
"github.com/ybkuroki/go-webapp-sample/migration"
"github.com/ybkuroki/go-webapp-sample/repository"
"github.com/ybkuroki/go-webapp-sample/session"
)

// Prepare func is to prepare for unit test.
Expand All @@ -19,6 +20,7 @@ func Prepare() *echo.Echo {
conf.Database.Host = "file::memory:?cache=shared"
conf.Database.Migration = true
conf.Extension.MasterGenerator = true
conf.Extension.SecurityEnabled = false
conf.Log.Format = "${time_rfc3339} [${level}] ${remote_ip} ${method} ${uri} ${status}"
conf.Log.Level = 1
config.SetConfig(conf)
Expand All @@ -30,6 +32,7 @@ func Prepare() *echo.Echo {
migration.CreateDatabase(config.GetConfig())
migration.InitMasterData(config.GetConfig())

session.Init(e, config.GetConfig())
return e
}

Expand Down

0 comments on commit 20ecca6

Please sign in to comment.