Adding a User
We are not done with all the user stories related to creating and maintaining courses. Far from it. But for the moment, we are going to focus on users.
We already touched on the need to have an administrator and we kind of brushed it off while working on the first course scenario.
Let’s have a look at that again and add a User class.
First some background.
The features that allow users to create accounts (and edit or delete their profiles) are called user management features. Allowing users to sign in and identify themselves is called authentication. Typically, we request an email address and a password to authenticate the user, so we can be sure whoever is signing in is the same person who created the account.
It’s important to distinguish authentication, which identifies a user, from authorization, which controls what a user is allowed to do.
This application will only have one type of user that will have access to its functionality apart from viewing content - that will be the Course Administrator. All other users that we will need information about are, of course the Course Participants, but that data will only be needed for processing, we will not allow them to actually log in to the application and perform any tasks. The ability to view and validate course certificates will not require a log in - this feature will be open to anonymous visitors.
Okay, having that i mind let's start writing some user stories:
Something like that. Basically, we want to create a mechanism for users to sign up and log in to the application. We also want to restrict access to some of the applications functionality for users that are logged in.
Let’s start with some acceptance tests (Cucumber
).
In your features
folder, create a file named user_maintenance.feature
. Write some headlines for the scenarios we will be working on:
Let’s add some steps to the Create an account
scenario:
Okay, looks pretty straight forward, right? Let’s run it. Remember how?
We are failing on the second step:
Let's add that link to the application.erb
(it is located in the lib/views/layouts
folder, remember?)
After every addition, keep on running cucumber
and see if you get a new error message.
The 'Register link' step should now pass and the next test should fail with an error message similar to the following:
In order to fix this, open up the features/support/paths.rb
file and add the route to users registration page:
And also add that route to you main controller, the `application.rb
The next step is to create a folder named users
as a subfolder to lib/views
and create a register.erb
file in that folder.
Place the following form_for
code in that file:
Okay, now when you run cucumber
again (as you do after every addition in order to keep track of the changing error messages), you should see a familiar error:
Remember what we did last time we saw a similar error? We created a Course
class. Now, we need to create a User
class.
In your lib
folder, add a file named user.rb
and add this simple class definition:
Also, don't forget to require that file in your application.rb
Great! If you run cucumber
now you will get the following error:
As we did before, now is a good time to switch from our acceptance tests (cucumber
) to writing some unit tests for our User
class using rspec
.
In the spec
folder, create a file named user_spec.rb
and add the following specs:
Run rspec
and see the tests fail.
Now open the lib/user.rb
file and define the properties:
Now when you run rspec
your tests should go all green. But when you return to cucumber
you should see:
That is, of course, because we have no method that actually creates the User
in our controller yet. Let's create that:
And that creates a User for us.
But we also want to give the user some sort of feedback that the account has been created, right? (That is the last step in the scenario we are working on)
In order to do that we need to enable sessions that can, among other things, be used to pass information between the controller and the view.
First, in your application.rb
, inside your class, add the following setting:
Then, in your application.erb
(the layout template), add this code that will display the message:
And, finally, in your main controller, on the post route, add:
That should do it for the user, right? Well, not quite... :wink:
Last updated