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