Skip to content

Commit

Permalink
Merge pull request #64 from gobuffalo/task-signin
Browse files Browse the repository at this point in the history
Organizes routes a bit and avoids overriding index.html
  • Loading branch information
paganotoni authored Dec 19, 2019
2 parents 9565fb8 + 76db040 commit 2a8c9d8
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 59 deletions.
22 changes: 16 additions & 6 deletions genny/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,24 @@ func New(args []string) (*genny.Generator, error) {
gf, err = gogen.AddInsideBlock(
gf,
`if app == nil {`,
`//AuthMiddlewares`,
`app.Use(SetCurrentUser)`,
`app.Use(Authorize)`,
`app.GET("/users/new", UsersNew)`,
`app.POST("/users", UsersCreate)`,
`app.GET("/signin", AuthNew)`,
`app.POST("/signin", AuthCreate)`,
`app.DELETE("/signout", AuthDestroy)`,
`app.Middleware.Skip(Authorize, HomeHandler, UsersNew, UsersCreate, AuthNew, AuthCreate)`,
``,
`//Routes for Auth`,
`auth := app.Group("/auth")`,
`auth.GET("/", AuthLanding)`,
`auth.GET("/new", AuthNew)`,
`auth.POST("/", AuthCreate)`,
`auth.DELETE("/", AuthDestroy)`,
`auth.Middleware.Skip(Authorize, AuthLanding, AuthNew, AuthCreate)`,
``,
`//Routes for User registration`,
`users := app.Group("/users")`,
`users.GET("/new", UsersNew)`,
`users.POST("/", UsersCreate)`,
`users.Middleware.Remove(Authorize)`,
``,
)

return r.File(gf)
Expand Down
13 changes: 10 additions & 3 deletions genny/auth/templates/actions/auth.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"golang.org/x/crypto/bcrypt"
)

// AuthLanding shows a landing page to login
func AuthLanding(c buffalo.Context) error {
return c.Render(200, r.HTML("auth/landing.plush.html"))
}

// AuthNew loads the signin page
func AuthNew(c buffalo.Context) error {
c.Set("user", models.User{})
Expand All @@ -33,11 +38,13 @@ func AuthCreate(c buffalo.Context) error {

// helper function to handle bad attempts
bad := func() error {
c.Set("user", u)
verrs := validate.NewErrors()
verrs.Add("email", "invalid email/password")

c.Set("errors", verrs)
return c.Render(422, r.HTML("auth/new.plush.html"))
c.Set("user", u)

return c.Render(http.StatusUnauthorized, r.HTML("auth/new.plush.html"))
}

if err != nil {
Expand All @@ -57,7 +64,7 @@ func AuthCreate(c buffalo.Context) error {
c.Flash().Add("success", "Welcome Back to Buffalo!")

redirectURL := "/"
if redir, ok := c.Session().Get("redirectURL").(string); ok {
if redir, ok := c.Session().Get("redirectURL").(string); ok && redir != "" {
redirectURL = redir
}

Expand Down
110 changes: 68 additions & 42 deletions genny/auth/templates/actions/auth_test.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,92 @@ import (
"<%= app.PackagePkg %>/models"
)

func (as *ActionSuite) Test_Auth_New() {
res := as.HTML("/signin").Get()
as.Equal(200, res.Code)
as.Contains(res.Body.String(), "Sign In")
}

func (as *ActionSuite) Test_Auth_Create() {
func (as *ActionSuite) createUser() (*models.User, error) {
u := &models.User{
Email: "[email protected]",
Password: "password",
PasswordConfirmation: "password",
}

verrs, err := u.Create(as.DB)
as.NoError(err)
as.False(verrs.HasAny())

res := as.HTML("/signin").Post(u)
as.Equal(302, res.Code)
as.Equal("/", res.Location())
return u, err
}

func (as *ActionSuite) Test_Auth_Create_Redirect() {
u := &models.User{
Email: "[email protected]",
Password: "password",
PasswordConfirmation: "password",
}
verrs, err := u.Create(as.DB)
as.NoError(err)
as.False(verrs.HasAny())
func (as *ActionSuite) Test_Auth_Signin() {
res := as.HTML("/auth/").Get()
as.Equal(200, res.Code)
as.Contains(res.Body.String(), `<a href="/signin" class="btn btn-primary">Sign In</a>`)
}

as.Session.Set("redirectURL", "/some/url")
func (as *ActionSuite) Test_Auth_New() {
res := as.HTML("/auth/new").Get()
as.Equal(200, res.Code)
as.Contains(res.Body.String(), "Sign In")
}

res := as.HTML("/signin").Post(u)
func (as *ActionSuite) Test_Auth_SaveRedirect() {
res := as.HTML("/").Get()
as.Equal(302, res.Code)
as.Equal(res.Location(), "/some/url")

as.Equal("/auth", res.Location())
as.Equal(as.Session.Get("redirectURL"), "/")
}

func (as *ActionSuite) Test_Auth_Create_UnknownUser() {
u := &models.User{
Email: "[email protected]",
Password: "password",
func (as *ActionSuite) Test_Auth_Create() {
u, err := as.createUser()
as.NoError(err)

tcases := []struct {
Email string
Password string
Status int
RedirectURL string

Identifier string
}{
{u.Email, u.Password, http.StatusFound, "/", "Valid"},
{"[email protected]", "password", http.StatusUnauthorized, "", "Email Invalid"},
{u.Email, "invalidPassword", http.StatusUnauthorized, "", "Password Invalid"},
}
res := as.HTML("/signin").Post(u)
as.Equal(422, res.Code)
as.Contains(res.Body.String(), "invalid email/password")
}

func (as *ActionSuite) Test_Auth_Create_BadPassword() {
u := &models.User{
Email: "[email protected]",
Password: "password",
PasswordConfirmation: "password",
for _, tcase := range tcases {
as.Run(tcase.Identifier, func() {
res := as.HTML("/auth").Post(&models.User{
Email: tcase.Email,
Password: tcase.Password,
})

as.Equal(tcase.Status, res.Code)
as.Equal(tcase.RedirectURL, res.Location())
})
}
verrs, err := u.Create(as.DB)
}

func (as *ActionSuite) Test_Auth_Redirect() {
u, err := as.createUser()
as.NoError(err)
as.False(verrs.HasAny())

u.Password = "bad"
res := as.HTML("/signin").Post(u)
as.Equal(422, res.Code)
as.Contains(res.Body.String(), "invalid email/password")
tcases := []struct {
redirectURL interface{}
resultLocation string

identifier string
}{
{"/some/url", "/some/url", "RedirectURL defined"},
{nil, "/", "RedirectURL nil"},
{"", "/", "RedirectURL empty"},
}

for _, tcase := range tcases {
as.Run(tcase.identifier, func() {
as.Session.Set("redirectURL", tcase.redirectURL)

res := as.HTML("/auth").Post(u)

as.Equal(302, res.Code)
as.Equal(res.Location(), tcase.resultLocation)
})
}
}
1 change: 1 addition & 0 deletions genny/auth/templates/actions/users.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/pkg/errors"
)

//UsersNew renders the users form
func UsersNew(c buffalo.Context) error {
u := models.User{}
c.Set("user", u)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
<div class="auth-center">
<%= if (current_user) { %>
<h1><%= current_user.Email %></h1>
<a href="/signout" data-method="DELETE">Sign Out</a>
<%= linkTo(authPath(), {data-method: "DELETE"}){ %>Sign Out<% } %>
<% } else { %>
<a href="/signin" class="btn btn-primary">Sign In</a>
<a href="/users/new" class="btn btn-success">Register</a>
<%= linkTo(newAuthPath()){ %>Sign In<% } %>
<%= linkTo(newUsersPath()){ %>Register<% } %>
<% } %>
</div>

Loading

0 comments on commit 2a8c9d8

Please sign in to comment.