Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. defmodule PhoenixApi.QuoteControllerTest do
  2. use PhoenixApi.ConnCase
  3.  
  4. alias PhoenixApi.Quote
  5. alias PhoenixApi.User
  6. alias PhoenixApi.Session
  7.  
  8. @valid_attrs %{author: "some content", quote: "some content"}
  9. @invalid_attrs %{}
  10.  
  11. setup %{conn: conn} do
  12. user = create_user("cristiano")
  13. session = create_session(user)
  14.  
  15. conn = conn
  16. |> put_req_header("accept", "application/json")
  17. |> put_req_header("authorization", "Token token=\"#{session.token}\"")
  18. {:ok, conn: conn, current_user: user}
  19. end
  20.  
  21. def create_user(%{name: name}) do
  22. Repo.insert! %User{ email: "#{name}.codelab@gmail.com", name: String.capitalize(name), password: "qwe@rty#$&"}
  23. end
  24.  
  25. def create_session(user) do
  26. Session.create_changeset(%Session{user_id: user.id}, %{}) |> Repo.insert!
  27. end
  28.  
  29. def create_quote(%{author: _author, user_id: _user_id} = options) do
  30. Quote.changeset(%Quote{}, options) |> Repo.insert!
  31. end
  32.  
  33. test "lists all entries on index", %{conn: conn, current_user: current_user} do
  34. create_quote(%{author: "Stephen Chbosky", quote: "We accept the love we think we deserve.", user_id: current_user.id})
  35.  
  36. other_user = create_user("otte")
  37. create_quote(%{author: "Francoise Sagan", quote: "Art must take reality by surprise.", user_id: other_user.id})
  38.  
  39. conn = get conn, quote_path(conn, :index)
  40.  
  41. assert Enum.count(json_response(conn, 200)["data"]) == 1
  42. assert %{"author" => "Stephen Chbosky"} = hd(json_response(conn, 200)["data"])
  43. end
  44.  
  45. test "create
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement