-
Notifications
You must be signed in to change notification settings - Fork 86
Refactor Category Controller Tests #429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,102 +1,81 @@ | ||
| defmodule CodeCorps.CategoryControllerTest do | ||
| use CodeCorps.ApiCase | ||
| use CodeCorps.ApiCase, resource_name: :category | ||
|
|
||
| alias CodeCorps.Category | ||
|
|
||
| @valid_attrs %{description: "You want to improve software tools and infrastructure.", name: "Technology"} | ||
| @valid_attrs %{name: "Technology"} | ||
| @invalid_attrs %{name: nil} | ||
|
|
||
| def request_create(conn, attrs) do | ||
| path = conn |> category_path(:create) | ||
| payload = json_payload(:category, attrs) | ||
| conn |> post(path, payload) | ||
| end | ||
|
|
||
| def request_update(conn, attrs) do | ||
| category = insert(:category) | ||
| payload = json_payload(:category, attrs) | ||
| path = conn |> category_path(:update, category) | ||
|
|
||
| conn |> put(path, payload) | ||
| end | ||
| describe "index" do | ||
| test "lists all entries on index", %{conn: conn} do | ||
| [category_1, category_2] = insert_pair(:category) | ||
|
|
||
| test "lists all entries on index", %{conn: conn} do | ||
| conn = get conn, category_path(conn, :index) | ||
| assert json_response(conn, 200)["data"] == [] | ||
| conn | ||
| |> request_index | ||
| |> json_response(200) | ||
| |> assert_ids_from_response([category_1.id, category_2.id]) | ||
| end | ||
| end | ||
|
|
||
| describe "show" do | ||
| test "shows chosen resource", %{conn: conn} do | ||
| category = insert(:category) | ||
|
|
||
| path = conn |> category_path(:show, category) | ||
| data = conn |> get(path) |> json_response(200) |> Map.get("data") | ||
|
|
||
| assert data["id"] == category.id |> Integer.to_string | ||
| assert data["type"] == "category" | ||
|
|
||
| assert data["attributes"]["name"] == category.name | ||
| assert data["attributes"]["slug"] == category.slug | ||
| assert data["attributes"]["description"] == category.description | ||
| conn | ||
| |> request_show(category) | ||
| |> json_response(200) | ||
| |> Map.get("data") | ||
| |> assert_result_id(category.id) | ||
| end | ||
|
|
||
| test "renders page not found when id is nonexistent", %{conn: conn} do | ||
| path = conn |> category_path(:show, -1) | ||
| assert conn |> get(path) |> json_response(404) | ||
| test "renders 404 when id is nonexistent", %{conn: conn} do | ||
| assert conn |> request_show(:not_found) |> json_response(404) | ||
| end | ||
| end | ||
|
|
||
| describe "create" do | ||
| @tag authenticated: :admin | ||
| test "creates and renders resource when data is valid", %{conn: conn} do | ||
| response = conn |> request_create(@valid_attrs) |> json_response(201) | ||
|
|
||
| assert response["data"]["id"] | ||
| assert response["data"]["attributes"]["slug"] == "technology" | ||
| assert Repo.get_by(Category, @valid_attrs) | ||
| assert conn |> request_create(@valid_attrs) |> json_response(201) | ||
| end | ||
|
|
||
| @tag authenticated: :admin | ||
| test "does not create resource and renders errors when data is invalid", %{conn: conn} do | ||
| response = conn |> request_create(@invalid_attrs) |> json_response(422) | ||
|
|
||
| assert response["errors"] != %{} | ||
| test "renders 422 when data is invalid", %{conn: conn} do | ||
| assert conn |> request_create(@invalid_attrs) |> json_response(422) | ||
| end | ||
|
|
||
| test "does not create resource and renders 401 when not authenticated", %{conn: conn} do | ||
| assert conn |> request_create(@valid_attrs) |> json_response(401) | ||
| test "renders 401 when not authenticated", %{conn: conn} do | ||
| assert conn |> request_create |> json_response(401) | ||
| end | ||
|
|
||
| @tag :authenticated | ||
| test "does not create resource and renders 403 when not authorized", %{conn: conn} do | ||
| assert conn |> request_create(@valid_attrs) |> json_response(403) | ||
| test "renders 403 when not authorized", %{conn: conn} do | ||
| assert conn |> request_create |> json_response(403) | ||
| end | ||
| end | ||
|
|
||
| describe "update" do | ||
| @tag authenticated: :admin | ||
| test "updates and renders chosen resource when data is valid", %{conn: conn} do | ||
| response = conn |> request_update(@valid_attrs) |> json_response(200) | ||
|
|
||
| category = Repo.get_by(Category, @valid_attrs) | ||
| assert category | ||
| assert response["data"]["id"] == "#{category.id}" | ||
| assert conn |> request_update(@valid_attrs) |> json_response(200) | ||
| end | ||
|
|
||
| @tag authenticated: :admin | ||
| test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do | ||
| response = conn |> request_update(@invalid_attrs) |> json_response(422) | ||
|
|
||
| assert response["errors"] != %{} | ||
| test "renders 422 when data is invalid", %{conn: conn} do | ||
| assert conn |> request_update(@invalid_attrs) |> json_response(422) | ||
| end | ||
|
|
||
| test "does not update resource and renders 401 when not authenticated", %{conn: conn} do | ||
| assert conn |> request_update(@valid_attrs) |> json_response(401) | ||
| test "renders 401 when not authenticated", %{conn: conn} do | ||
| assert conn |> request_update |> json_response(401) | ||
| end | ||
|
|
||
| @tag :authenticated | ||
| test "does not update resource and renders 403 when not authorized", %{conn: conn} do | ||
| assert conn |> request_update(@valid_attrs) |> json_response(403) | ||
| test "renders 403 when not authorized", %{conn: conn} do | ||
| assert conn |> request_update |> json_response(403) | ||
| end | ||
|
|
||
| @tag authenticated: :admin | ||
| test "renders 404 when id is nonexistent", %{conn: conn} do | ||
| assert conn |> request_update(:not_found) |> json_response(404) | ||
| end | ||
|
|
||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the organization membership update test:
organization = insert(:organization)membership = insert(:organization_membership, organization: organization, role: "pending")insert(:organization_membership, organization: organization, member: current_user, role: "owner")Should I change it to look more like this one? Looks like the request_update helper has a default_record.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, because you do need someone who's an organization admin to approve, right?