auth_test_support v0.0.4 AuthTestSupport

A collection of common funcitonality to use in your Phoenix test suites.

use AuthTestSupport in your test files.

use is necessary for sign_in and it will import the remaining functions. If you’d like to use another of the other functions in isolation feel free to import them specifically.

Summary

Functions

Assert that the current connection is authenticated as a given account

Authenticate a conn for a specific account

Sign in to the session

Macros

Macro that generates a test for asserting that RESTful actions require authorization

Functions

assert_authenticated_as(conn, account)

Assert that the current connection is authenticated as a given account

Will run the following assertions:

  1. assert that :account_id value in the session is not nil and is equal to the account’s primary key value
  2. assert that :account_type value in the sesion is not nil and is equal to the account’s struct
authenticate_as(conn, account)

Authenticate a conn for a specific account

Will setup the session on a conn object for a given account.

This function is different than sign_in/2 as it will simply set the session on the conn whereas sign_in/2 will step through the process of making the application API requests.

sign_in(conn, creds)

Sign in to the session

This function assumes that the session creation path is session_path and is using post.

Feel free to override this function.

Macros

require_authorization(path_helper, opts \\ [])

Macro that generates a test for asserting that RESTful actions require authorization

The assertion being run will expect that unauthorized route access will return a 401

Options:

  • :roles takes an keyword list of role names. Keyword values can be a function reference that to manipulate the conn object
  • :only only the actions in the keyword list given. Keyword values can be a map for passing custom params to the action
  • :except all actions (index, show, create, update, destroy) except those in the keyword list. Keyword value behave similiar to only

Examples

require_authorization :profile_path
require_authorization :profile_path, roles: [:no_auth, auth: &auth_conn/1]

defp auth_conn(conn) do
  sign_in(conn, username: "user@example.com", password: "password")
end

require_authorization :profile_path, only: [create: %{foo: "bar"}]

Each call to require_authorization only generates a single test, not multiple tests. This saves on compilation time.