Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. defmodule Bookmarker.BookmarkControllerTest do
  2. use Bookmarker.ConnCase
  3.  
  4. alias Bookmarker.Bookmark
  5. alias Bookmarker.User
  6. import Plug.Conn
  7. @valid_attrs %{description: "some content", title: "some content", url: "some content"}
  8. @invalid_attrs %{}
  9.  
  10. @session_opts Plug.Session.init [
  11. store: :cookie,
  12. key: "_test",
  13. encryption_salt: "abcdefgh",
  14. signing_salt: "abcdefgh"
  15. ]
  16.  
  17. setup do
  18. conn = conn(:get, "/")
  19. |> Map.put(:secret_key_base, String.duplicate("a", 64))
  20. |> Plug.Session.call(@session_opts)
  21. |> fetch_session
  22.  
  23. {:ok, user} = %User{
  24. email: "user@example.com",
  25. name: "User For Test",
  26. crypted_password: Comeonin.Bcrypt.hashpwsalt("123456")}
  27. |> Repo.insert
  28.  
  29. {:ok, user: user, conn: conn}
  30. end
  31.  
  32. test "lists all entries on index", %{conn: conn, user: user} do
  33. conn = put_session(conn, :current_user, %{id: user.id, name: user.name, email: user.email})
  34. conn = get conn, bookmark_path(conn, :index)
  35. assert html_response(conn, 200) =~ "Listing bookmarks"
  36. end
  37.  
  38. test "renders form for new resources", %{conn: conn} do
  39. conn = get conn, bookmark_path(conn, :new)
  40. assert html_response(conn, 200) =~ "New bookmark"
  41. end
  42.  
  43. test "creates resource and redirects when data is valid", %{conn: conn, user: user} do
  44. conn = post conn, bookmark_path(conn, :create), bookmark: @valid_attrs
  45. assert redirected_to(conn) == bookmark_path(conn, :index)
  46. assert Repo.get_by(Bookmark, @valid_attrs)
  47. end
  48.  
  49. test "does not create resource and renders errors when data is invalid", %{conn: conn, user: user} do
  50. conn = put_session(conn, :current_user, %{id: user.id, name: user.name, email: user.email})
  51. conn = post conn, bookmark_path(conn, :create), bookmark: @invalid_attrs
  52. assert html_response(conn, 200) =~ "New bookmark"
  53. end
  54.  
  55. test "shows chosen resource", %{conn: conn} do
  56. bookmark = Repo.insert! %Bookmark{}
  57. conn = get conn, bookmark_path(conn, :show, bookmark)
  58. assert html_response(conn, 200) =~ "Show bookmark"
  59. end
  60.  
  61. test "renders page not found when id is nonexistent", %{conn: conn, user: user} do
  62. conn = put_session(conn, :current_user, %{id: user.id, name: user.name, email: user.email})
  63. assert_error_sent 404, fn ->
  64. get conn, bookmark_path(conn, :show, -1)
  65. end
  66. end
  67.  
  68. test "renders form for editing chosen resource", %{conn: conn} do
  69. bookmark = Repo.insert! %Bookmark{}
  70. conn = get conn, bookmark_path(conn, :edit, bookmark)
  71. assert html_response(conn, 200) =~ "Edit bookmark"
  72. end
  73.  
  74. test "updates chosen resource and redirects when data is valid", %{conn: conn} do
  75. bookmark = Repo.insert! %Bookmark{}
  76. conn = put conn, bookmark_path(conn, :update, bookmark), bookmark: @valid_attrs
  77. assert redirected_to(conn) == bookmark_path(conn, :show, bookmark)
  78. assert Repo.get_by(Bookmark, @valid_attrs)
  79. end
  80.  
  81. test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do
  82. bookmark = Repo.insert! %Bookmark{}
  83. conn = put conn, bookmark_path(conn, :update, bookmark), bookmark: @invalid_attrs
  84. assert html_response(conn, 200) =~ "Edit bookmark"
  85. end
  86.  
  87. test "deletes chosen resource", %{conn: conn} do
  88. bookmark = Repo.insert! %Bookmark{}
  89. conn = delete conn, bookmark_path(conn, :delete, bookmark)
  90. assert redirected_to(conn) == bookmark_path(conn, :index)
  91. refute Repo.get(Bookmark, bookmark.id)
  92. end
  93. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement