auth_test_support v0.0.6 AuthTestSupport
A collection of common funcitonality to use in your Phoenix test suites.
use AuthTestSupport in your test files.
use is necessary for authenticate 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 the session with the given credentials
Authorizes a connection with an account
Refute account is authorized
Macros
Macro that generates a test for asserting that RESTful actions require authorization
Functions
Assert that the current connection is authenticated as a given account
Will run the following assertions:
- assert that
:account_idvalue in the session is notniland is equal to theaccount’s primary key value - assert that
:account_typevalue in the sesion is notniland is equal to theaccount’s struct
The original conn will be returned.
Authenticate the session with the given credentials
This function assumes that the session creation path is session_path and is using post.
Feel free to override this function.
Authorizes a connection with an account
Equivalent to running:
Plug.Conn.assign(conn, :account, account)
This function differs from authentication_as/2 as that will run the actual authentication whereas this function
simply assigns to the conn. The database is not hit, no encryption checks are run.
Macros
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:
:rolestakes an keyword list of role names. Keyword values can be a function reference that to manipulate theconnobject:onlyonly the actions in the keyword list given. Keyword values can be a map for passing custom params to the action:exceptall actions (index, show, create, update, destroy) except those in the keyword list. Keyword value behave similiar toonly
Examples
require_authorization :profile_path
require_authorization :profile_path, roles: [:no_auth, auth: &auth_conn/1]
defp auth_conn(conn) do
authenticate(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.