Skip to content

Commit

Permalink
Merge pull request #407 from code-corps/406-refactor-preview-controll…
Browse files Browse the repository at this point in the history
…er-test

Refactor Preview Controller Test with Helpers
  • Loading branch information
mackenziehicks committed Oct 31, 2016
2 parents f8a1fae + d19301e commit 197ab15
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 39 deletions.
12 changes: 12 additions & 0 deletions blueprint/api.apib
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ You can list all comments or retrieve individual comments, as well as create and

+ Attributes (Comment Response)

+ Response 401 (application/vnd.api+json; charset=utf-8)

+ Attributes (JSON Web Token Invalid Response)

+ Response 403 (application/vnd.api+json; charset=utf-8)

+ Attributes (Forbidden Response)

+ Response 422 (application/vnd.api+json; charset=utf-8)

+ Attributes (Unprocessable Entity Response)
Expand Down Expand Up @@ -623,6 +631,10 @@ Preview resources allow a user to create a temporary HTML preview for any kind o

+ Attributes (JSON Web Token Invalid Response)

+ Response 403 (application/vnd.api+json; charset=utf-8)

+ Attributes (Forbidden Response)

+ Response 422 (application/vnd.api+json; charset=utf-8)

+ Attributes (Unprocessable Entity Response)
Expand Down
49 changes: 10 additions & 39 deletions test/controllers/preview_controller_test.exs
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

0 comments on commit 197ab15

Please sign in to comment.