Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Bookmarker.BookmarkControllerTest do
- use Bookmarker.ConnCase
- alias Bookmarker.Bookmark
- alias Bookmarker.User
- import Plug.Conn
- @valid_attrs %{description: "some content", title: "some content", url: "some content"}
- @invalid_attrs %{}
- @session_opts Plug.Session.init [
- store: :cookie,
- key: "_test",
- encryption_salt: "abcdefgh",
- signing_salt: "abcdefgh"
- ]
- setup do
- conn = conn(:get, "/")
- |> Map.put(:secret_key_base, String.duplicate("a", 64))
- |> Plug.Session.call(@session_opts)
- |> fetch_session
- {:ok, user} = %User{
- email: "user@example.com",
- name: "User For Test",
- crypted_password: Comeonin.Bcrypt.hashpwsalt("123456")}
- |> Repo.insert
- {:ok, user: user, conn: conn}
- end
- test "lists all entries on index", %{conn: conn, user: user} do
- conn = put_session(conn, :current_user, %{id: user.id, name: user.name, email: user.email})
- conn = get conn, bookmark_path(conn, :index)
- assert html_response(conn, 200) =~ "Listing bookmarks"
- end
- test "renders form for new resources", %{conn: conn} do
- conn = get conn, bookmark_path(conn, :new)
- assert html_response(conn, 200) =~ "New bookmark"
- end
- test "creates resource and redirects when data is valid", %{conn: conn, user: user} do
- conn = post conn, bookmark_path(conn, :create), bookmark: @valid_attrs
- assert redirected_to(conn) == bookmark_path(conn, :index)
- assert Repo.get_by(Bookmark, @valid_attrs)
- end
- test "does not create resource and renders errors when data is invalid", %{conn: conn, user: user} do
- conn = put_session(conn, :current_user, %{id: user.id, name: user.name, email: user.email})
- conn = post conn, bookmark_path(conn, :create), bookmark: @invalid_attrs
- assert html_response(conn, 200) =~ "New bookmark"
- end
- test "shows chosen resource", %{conn: conn} do
- bookmark = Repo.insert! %Bookmark{}
- conn = get conn, bookmark_path(conn, :show, bookmark)
- assert html_response(conn, 200) =~ "Show bookmark"
- end
- test "renders page not found when id is nonexistent", %{conn: conn, user: user} do
- conn = put_session(conn, :current_user, %{id: user.id, name: user.name, email: user.email})
- assert_error_sent 404, fn ->
- get conn, bookmark_path(conn, :show, -1)
- end
- end
- test "renders form for editing chosen resource", %{conn: conn} do
- bookmark = Repo.insert! %Bookmark{}
- conn = get conn, bookmark_path(conn, :edit, bookmark)
- assert html_response(conn, 200) =~ "Edit bookmark"
- end
- test "updates chosen resource and redirects when data is valid", %{conn: conn} do
- bookmark = Repo.insert! %Bookmark{}
- conn = put conn, bookmark_path(conn, :update, bookmark), bookmark: @valid_attrs
- assert redirected_to(conn) == bookmark_path(conn, :show, bookmark)
- assert Repo.get_by(Bookmark, @valid_attrs)
- end
- test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do
- bookmark = Repo.insert! %Bookmark{}
- conn = put conn, bookmark_path(conn, :update, bookmark), bookmark: @invalid_attrs
- assert html_response(conn, 200) =~ "Edit bookmark"
- end
- test "deletes chosen resource", %{conn: conn} do
- bookmark = Repo.insert! %Bookmark{}
- conn = delete conn, bookmark_path(conn, :delete, bookmark)
- assert redirected_to(conn) == bookmark_path(conn, :index)
- refute Repo.get(Bookmark, bookmark.id)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement