-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #407 from code-corps/406-refactor-preview-controll…
…er-test Refactor Preview Controller Test with Helpers
- Loading branch information
Showing
2 changed files
with
22 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,24 @@ | ||
defmodule CodeCorps.PreviewControllerTest do | ||
use CodeCorps.ApiCase | ||
use CodeCorps.ApiCase, resource_name: :preview | ||
|
||
alias CodeCorps.Preview | ||
|
||
defp build_payload do | ||
%{"data" => %{"type" => "preview", "attributes" => %{markdown: "A **strong** element"}}} | ||
end | ||
@valid_attrs %{markdown: "A **strong** element"} | ||
|
||
describe "create" do | ||
@tag :authenticated | ||
test "creates and renders resource, with body containing markdown rendered to html", %{conn: conn, current_user: current_user} do | ||
payload = build_payload |> put_relationships(current_user) | ||
path = conn |> preview_path(:create) | ||
json = conn |> post(path, payload) |> json_response(201) | ||
|
||
id = json["data"]["id"] |> String.to_integer | ||
|
||
assert id | ||
|
||
attributes = json["data"]["attributes"] | ||
|
||
assert attributes["body"] == "<p>A <strong>strong</strong> element</p>\n" | ||
assert attributes["markdown"] == "A **strong** element" | ||
|
||
preview = Preview |> Repo.get(id) | ||
|
||
assert preview.body == "<p>A <strong>strong</strong> element</p>\n" | ||
assert preview.markdown == "A **strong** element" | ||
test "creates and renders resource when data is valid", %{conn: conn, current_user: current_user} do | ||
attrs = @valid_attrs |> Map.merge(%{user: current_user}) | ||
assert conn |> request_create(attrs) |> json_response(201) | ||
end | ||
|
||
@tag :authenticated | ||
test "it assigns current user as owner of preview, if available", %{conn: conn, current_user: current_user} do | ||
payload = build_payload |> put_relationships(current_user) | ||
path = conn |> preview_path(:create) | ||
json = conn |> post(path, payload) |> json_response(201) | ||
|
||
|
||
id = json["data"]["id"] |> String.to_integer | ||
|
||
preview = Preview |> Repo.get(id) | ||
|
||
assert preview.user_id == current_user.id | ||
test "does not create resource, and responds with 401 when unauthenticated", %{conn: conn} do | ||
assert conn |> request_create(@valid_attrs) |> json_response(401) | ||
end | ||
|
||
test "does not create resource, and responds with 401 when unauthenticated", %{conn: conn} do | ||
payload = build_payload | ||
path = conn |> preview_path(:create) | ||
assert conn |> post(path, payload) |> json_response(401) | ||
@tag :authenticated | ||
test "does not update resource and renders 403 when not authorized", %{conn: conn} do | ||
assert conn |> request_create(@valid_attrs) |> json_response(403) | ||
end | ||
end | ||
|
||
end |