Skip to content

Commit d63d59a

Browse files
authored
Merge pull request #7 from tacataca/R6-Chapter_3_revisions
Some fixes and suggestions for chapter 3
2 parents 792a922 + 79bfe75 commit d63d59a

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

rails6/en/chapter03-presenting-users.adoc

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ We will modify a little bit this migration in order to add some database validat
7878
We will therefore add two additional constraints:
7979

8080
- email is mandatory: we use the property `null: false`.
81-
- email must be unique: we use the `unique: true` property
81+
- email must be unique: we add an index for the email column with property `unique: true`.
8282
- password is mandatory: we use the property `null: false`.
8383

8484
The migration thus becomes:
@@ -88,7 +88,8 @@ The migration thus becomes:
8888
----
8989
# ...
9090
create_table :users do |t|
91-
t.string :email, unique: true, null: false
91+
t.string :email, null: false
92+
t.index :email, unique: true
9293
t.string :password_digest, null: false
9394
# ...
9495
end
@@ -181,7 +182,7 @@ end
181182
[source,ruby]
182183
----
183184
# ...
184-
test 'user with unvalid email should be unvalid' do
185+
test 'user with invalid email should be invalid' do
185186
user = User.new(email: 'test', password_digest: 'test')
186187
assert_not user.valid?
187188
end
@@ -193,7 +194,7 @@ end
193194
[source,ruby]
194195
----
195196
# ...
196-
test 'user with taken email should be unvalid' do
197+
test 'user with taken email should be invalid' do
197198
other_user = users(:one)
198199
user = User.new(email: other_user.email, password_digest: 'test')
199200
assert_not user.valid?
@@ -267,7 +268,7 @@ end
267268

268269
In addition, this method will add a `User#password` attribute that will be automatically hashed and saved in the `User#password_digest` attribute.
269270

270-
Let's try this right now in the Rails console. Open a console with `console rails`:
271+
Let's try this right now in the Rails console. Open a console with `rails console`:
271272

272273
[source,ruby]
273274
----
@@ -342,7 +343,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
342343
test "should show user" do
343344
get api_v1_user_url(@user), as: :json
344345
assert_response :success
345-
# on teste que la réponse contient le courriel
346+
# Test to ensure response contains the correct email
346347
json_response = JSON.parse(self.response.body)
347348
assert_equal @user.email, json_response['email']
348349
end
@@ -356,6 +357,7 @@ Then simply add the action to our controller. It is extremely simple:
356357
.app/controllers/api/v1/users\_controller.rb
357358
----
358359
class Api::V1::UsersController < ApplicationController
360+
# GET /users/1
359361
def show
360362
render json: User.find(params[:id])
361363
end
@@ -409,6 +411,14 @@ $ git add . && git commit -m "Adds show action the users controller"
409411

410412
So we finally have a resource to test. We have several solutions to test it. The first one that comes to mind is the use of cURL, which is integrated in almost all Linux distributions. So let's try it:
411413

414+
First initialize the rails server on a new terminal.
415+
[source,bash]
416+
----
417+
$ rails s
418+
----
419+
420+
Then switch back to your other terminal and run:
421+
412422
[source,bash]
413423
----
414424
$ curl http://localhost:3000/api/v1/users/1
@@ -449,8 +459,8 @@ end
449459

450460
That's a lot of code. Don't worry I'll explain everything:
451461

452-
* In the first test we check the creation of a user by sending a valid POST request. Then, we checked that an additional user exists in the database and that the HTTP code of the response is `created`
453-
* In the first test we check that the user is not created using an email already used. Then, we check that the HTTP code of the response is `created`
462+
* In the first test we check the creation of a user by sending a valid POST request. Then, we checked that an additional user exists in the database and that the HTTP code of the response is `created` (status code 201)
463+
* In the second test we check that the user is not created using an email already used. Then, we check that the HTTP code of the response is `unprocessable_entity` (status code 422)
454464

455465
At that point, the tests must fail (as we expected):
456466

@@ -538,7 +548,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
538548
assert_response :success
539549
end
540550
541-
test "should not update other user" do
551+
test "should not update user when invalid params are sent" do
542552
patch api_v1_user_url(@user), params: { user: { email: 'bad_email', password: '123456' } }, as: :json
543553
assert_response :unprocessable_entity
544554
end
@@ -564,18 +574,18 @@ Then we implement the update action on the user controller and run our tests:
564574
----
565575
class Api::V1::UsersController < ApplicationController
566576
before_action :set_user, only: %i[show update]
567-
577+
578+
# GET /users/1
568579
def show
569580
render json: @user
570581
end
571582
572583
# ...
573584
574-
def create
575-
@user = User.new(user_params)
576-
577-
if @user.save
578-
render json: @user, status: :created
585+
# PATCH/PUT /users/1
586+
def update
587+
if @user.update(user_params)
588+
render json: @user, status: :ok
579589
else
580590
render json: @user.errors, status: :unprocessable_entity
581591
end
@@ -638,6 +648,7 @@ class Api::V1::UsersController < ApplicationController
638648
before_action :set_user, only: %i[show update destroy]
639649
# ...
640650
651+
# DELETE /users/1
641652
def destroy
642653
@user.destroy
643654
head 204

0 commit comments

Comments
 (0)