auth_test_support v0.0.1 AuthTestSupport

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

use AuthTestSupport in your test files.

Summary

Functions

Assert that the current connection is authenticated as a given 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
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.