SeatJson

Simple API testing DSL for Elixir/Phoenix, with Guardian integration

Prerequirements

defmodule MyApp.ApiTest do
  use ExUnit.Case, async: false
  use MyApp.ConnCase
  use SeatJson
  
  # YOUR TESTS GO HERE
end

Example

api :get, "/api/test",                                returns: {200, %{"test" => "ok"}} 
api :post, "/api/nonexistent",                        returns: {404, %{}} 
api :post, "/api/test/", [params: %{"test" => "ok"}], returns: {200, %{"test" => "ok"}} 

More advanced tests

api :get, "api/for_users_only", [as: :none],    returns: {401, %{}} 
api :get, "api/for_users_only", [as: :default], returns: {200, %{}} 
api :get, "api/for_users_only", [as: :admin],   returns: {200, %{}} 

Authenthication example

def insert_new_user(login, password) do
  Database.insert_user(login, password)
end

@tag call_before: {:insert_new_user, ["login", "password"]}
api :post, "/auth/login", [
  as: :none,
  info: "Allows user to log in",
  params:
  %{
    "email" => "email@example.com",
    "password" => "somepassword",
    "password_confirmation" => "somepassword",
    "name" => "Krzysztof Wende",
  }
],
returns: {200, %{"user" => %{"email" => _, "inserted_at" => _, "name" => _}}} 

Documentation

Use SeatJson

use SeatJson

SeatJson.__using__/1 takes one parameter auth: fn {tag, auth} Where tag is whatever atom You'd like to use, and auth is Guardian object This macro maps to Guardian.Plug.sign_in(conn, auth.user, :token, perms: %{default: Guardian.Permissions.available(level)}) If you don't provide :auth you can't use authenthication

api/4

macro api(method, url, opts \\ [], returns: match, guards: assertion)

Arguments:

Installation

Add seat_json to your list of dependencies in mix.exs:

def deps do
  [{:seat_json, "~> 0.0.1"}]
end