github.com/lenfree/buffalo@v0.7.3-0.20170207163156-891616ea4064/examples/html-resource/actions/users_test.go (about)

     1  package actions_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gobuffalo/buffalo/examples/html-resource/actions"
     7  	"github.com/gobuffalo/buffalo/examples/html-resource/models"
     8  	"github.com/markbates/pop"
     9  	"github.com/markbates/willie"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func Test_UsersList(t *testing.T) {
    14  	r := require.New(t)
    15  
    16  	tx(func(tx *pop.Connection) {
    17  		w := willie.New(actions.App())
    18  		u := &models.User{
    19  			FirstName: "Mark",
    20  			LastName:  "Bates",
    21  			Email:     "mark@example.com",
    22  		}
    23  		r.NoError(tx.Create(u))
    24  
    25  		res := w.Request("/users").Get()
    26  		r.Equal(200, res.Code)
    27  
    28  		r.Contains(res.Body.String(), u.Email)
    29  	})
    30  }
    31  
    32  func Test_UsersShow(t *testing.T) {
    33  	r := require.New(t)
    34  
    35  	tx(func(tx *pop.Connection) {
    36  		w := willie.New(actions.App())
    37  
    38  		u := &models.User{
    39  			FirstName: "Mark",
    40  			LastName:  "Bates",
    41  			Email:     "mark@example.com",
    42  		}
    43  		r.NoError(tx.Create(u))
    44  
    45  		res := w.Request("/users/%d", u.ID).Get()
    46  		r.Equal(200, res.Code)
    47  
    48  		r.Contains(res.Body.String(), u.Email)
    49  	})
    50  }
    51  
    52  func Test_UsersCreate(t *testing.T) {
    53  	r := require.New(t)
    54  
    55  	tx(func(tx *pop.Connection) {
    56  		w := willie.New(actions.App())
    57  
    58  		ct, err := tx.Count("users")
    59  		r.NoError(err)
    60  		r.Equal(0, ct)
    61  
    62  		u := &models.User{
    63  			FirstName: "Mark",
    64  			LastName:  "Bates",
    65  			Email:     "mark@example.com",
    66  		}
    67  		res := w.Request("/users").Post(u)
    68  		r.Equal(301, res.Code)
    69  
    70  		ct, err = tx.Count("users")
    71  		r.NoError(err)
    72  		r.Equal(1, ct)
    73  	})
    74  }
    75  
    76  func Test_UsersCreate_HandlesErrors(t *testing.T) {
    77  	r := require.New(t)
    78  
    79  	tx(func(tx *pop.Connection) {
    80  		w := willie.New(actions.App())
    81  
    82  		ct, err := tx.Count("users")
    83  		r.NoError(err)
    84  		r.Equal(0, ct)
    85  
    86  		u := &models.User{}
    87  		res := w.Request("/users").Post(u)
    88  		r.Equal(422, res.Code)
    89  
    90  		r.Contains(res.Body.String(), "First Name can not be blank.")
    91  	})
    92  }
    93  
    94  func Test_UsersUpdate(t *testing.T) {
    95  	r := require.New(t)
    96  
    97  	tx(func(tx *pop.Connection) {
    98  		w := willie.New(actions.App())
    99  
   100  		u := &models.User{
   101  			FirstName: "Mark",
   102  			LastName:  "Bates",
   103  			Email:     "mark@example.com",
   104  		}
   105  		r.NoError(tx.Create(u))
   106  
   107  		res := w.Request("/users/%d", u.ID).Put(map[string]string{
   108  			"email": "bates@example.com",
   109  		})
   110  		r.Equal(301, res.Code)
   111  
   112  		r.NoError(tx.Reload(u))
   113  		r.Equal("bates@example.com", u.Email)
   114  	})
   115  }
   116  
   117  func Test_UsersUpdate_HandlesErrors(t *testing.T) {
   118  	r := require.New(t)
   119  
   120  	tx(func(tx *pop.Connection) {
   121  		w := willie.New(actions.App())
   122  
   123  		u := &models.User{
   124  			FirstName: "",
   125  			LastName:  "Bates",
   126  			Email:     "mark@example.com",
   127  		}
   128  		r.NoError(tx.Create(u))
   129  
   130  		res := w.Request("/users/%d", u.ID).Put(map[string]string{
   131  			"email": "bates@example.com",
   132  		})
   133  		r.Equal(422, res.Code)
   134  
   135  		r.Contains(res.Body.String(), "First Name can not be blank.")
   136  	})
   137  }
   138  
   139  func Test_UsersDestroy(t *testing.T) {
   140  	r := require.New(t)
   141  
   142  	tx(func(tx *pop.Connection) {
   143  		w := willie.New(actions.App())
   144  
   145  		u := &models.User{
   146  			FirstName: "Mark",
   147  			LastName:  "Bates",
   148  			Email:     "mark@example.com",
   149  		}
   150  		r.NoError(tx.Create(u))
   151  
   152  		ct, err := tx.Count("users")
   153  		r.NoError(err)
   154  		r.Equal(1, ct)
   155  
   156  		res := w.Request("/users/%d", u.ID).Delete()
   157  		r.Equal(301, res.Code)
   158  
   159  		ct, err = tx.Count("users")
   160  		r.NoError(err)
   161  		r.Equal(0, ct)
   162  	})
   163  }