Elixir Phoenix: Creating An App With Tests (Part 5: Setting a Current User)

Brian Emory
Brian Emory | Web Developer
3 min readMay 19, 2017

Updated: January 16th, 2019

Current user (GitHub)

Now that we can sign in, how do we tell we are signed in? It would be great if there was some sort of greeting on the navbar or maybe the navbar’s “Sign in with Google” changed to “Sign out” because we were already signed in. We will add that functionality next.

First things first, we will want to write a test to check our navbar for the proper links whether a user is signed in or not. Before we can do that, we need to set up a test_helpers file to allow us to quickly create users and videos for our tests. We will do this by adding to our test/support directory a file called test_helpers.ex (this is not to be mistaken for the test_helper file that is already in the test directory).

Inside our new test_helpers file we will create a fixture function that will create a user for us (1f18dc6).

test/support/test_helpers.ex

Next, we will import our TestHelpers to the DataCase module and the ConnCase module (0d9a519). This will ensure our fixture is available for our tests.

test/support/conn_case.ex
test/support/data_case.ex

We will add a test to our navigation_test.exs. The added test will ensure we show our “Sign out” link when we are signed in (59af37f).

test/catcasts_web/templates/layout/navigation_test.exs

If we run mix test now, our new test will be failing. We will now create our set_user plug to get the failed test passing.

In our lib/catcasts_web directory, we will create a new directory called plugs. Inside the plugs directory, we will create a file called set_user.ex.

lib/catcasts_web/plugs/set_user.ex

With our create action in our session_controller, we have |> put_session(:user_id, user.id). This puts the user.id of our user into the session when we sign them in. We access the session in call/2 and grab the user.id. Unless we already have an assigned user, we look in our database to find the user, and assign our user to conn. To make use of this in our application, we need to tell our pipeline :browser in our router.ex to use our plug (d0a7daa).

lib/catcasts_web/router.ex

Now that we have access to our user, we can conditionally show navbar items or text. In our _navigation.html.eex, we are going to replace our “Sign in with Google” link with a conditional that will show different links depending on if we are signed in or not. We are also going to add a little message that welcomes the user by their first name.

Here is the entirety of our new _navigation.html.eex (0ac23f5).

lib/catcasts_web/templates/layout/_navigation.html.eex

If your server is running and you are signed in, you will see that this will break our page because the sign out link does not exist. We will first update our session_controller_test and add a test for signing out a user (a813f99).

test/catcasts_web/controllers/session_controller_test.exs

Running mix test gives us an error stating ** (UndefinedFunctionError) function CatcastsWeb.SessionController.request/2 is undefined or private. This is telling us that the route does not exist. We will add get "/signout", SessionController, :delete inside our router.ex in the scope "/auth" block (ac4a0a3).

lib/catcasts_web/router.ex

Running mix test again tells us that our SessionController does not have a delete/2 function. Let’s open up our SessionController and take care of adding it after our create/2 function (7ced98b).

lib/catcasts_web/controllers/session_controller.ex

If we run mix test one more time we will see that all of our tests are passing!

Follow me on Twitter @thebrianemory. Follow me here, click the hands below to show some appreciation, leave a comment, and get in touch!

--

--

Brian Emory
Brian Emory | Web Developer

Backend Software Engineer (Ruby/Elixir). Giraffe-like qualities. I enjoy video games, bad movies, hard ciders, and pizza.