-
Notifications
You must be signed in to change notification settings - Fork 86
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
Refactor Task Controller Tests with helpers #448
Conversation
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.
This is looking good 👍
assert json["data"] == [] | ||
[task_1, task_2] = insert_pair(:task) | ||
|
||
conn |
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.
Looks like the indentation is off here.
|
||
assert json["data"]["id"] | ||
attrs = @valid_attrs |> Map.merge(%{project: project, user: current_user}) | ||
assert conn |> request_create(attrs) |> json_response(201)] |
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.
Looks like there is a trailing bracket here.
@amyschools What difficulty are you having with the tests? Would you mind fixing the extra bracket on line 142? Can't really see which tests are failing when the app doesn't compile. |
@begedin it should compile now. I'm getting a 403 error for three of the tests:
and a new error saying key :task is not recognized that I thought I had solved yesterday on
|
@amyschools Looks like you didn't push up, or at least, I'm not seeing the changes. I'll try to reply best I can. For the update test, we have the following: @tag authenticated: :admin # an admin user is performing this test
test "updates and renders chosen resource when data is valid", %{conn: conn, current_user: current_user} do
task = insert(:task, user: current_user)
# you're not sending the task to `request_update`, so it doesn't do anything
assert conn |> request_update(@valid_attrs) |> json_response(200)
end
@tag :authenticated # notice the different tag. a regular user is performing
test "renders 422 when data is invalid", %{conn: conn, current_user: current_user} do
assert conn |> request_update(@invalid_attrs) |> json_response(422)
end For the first test, it's tagged to authenticate as admin. You create a task, but then create the version of the Option ARemove the Option BKeep the task line, tag as assert conn |> request_update(@valid_attrs) |> json_response(200) My vote would be option B, since as a side-effect, it's testing a more complex case of authorization. For the second test, as you can see, you're not authenticating as admin, which is why it's failing. If you go with option B, then the solution here would be to basically copy everything from the first test and just replace @tag :authenticated
test "renders 422 when data is invalid", %{conn: conn, current_user: current_user} do
task = insert(:task, user: current_user)
assert conn |> request_update(task, @invalid_attrs) |> json_response(422)
end For create, the problem is similar, but not quite the same
In order to get it working, you mimic the valid request approach: project = insert(:project)
attrs = @valid_attrs |> Map.merge(%{project: project, user: current_user})
assert conn |> request_create(attrs) |> json_response(201)] The difference being, you replace project = insert(:project)
attrs = @invalid_attrs |> Map.merge(%{project: project, user: current_user})
assert conn |> request_create(attrs) |> json_response(422) It's a bit complicated, but basically, in all our cases, we run authorization before we validate the record. That means that, to test validation error responses, you want to set up your request so authorization passes, but validation fails after. On some controllers, that isn't even possible, but in this one, it is, albeit it gets a bit complicated. It might make many things simpler, but I'm not sure. For your create and update valid request tests, it seems to me like they should work, from the changes here. However, it may be you altered something, so I'll take another look when you push your changes up. @joshsmith Writing all this, maybe we should at one point look into our process being something like:
|
@begedin Thank you so much!! That explanation was super helpful. All the tests are passing now so maybe one last check if you have time? |
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.
Just some indentation problems that need fixing. Looks good otherwise.
conn | ||
|> request_index | ||
|> json_response(200) | ||
|> assert_ids_from_response([task_1.id, task_2.id]) |
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.
Indentation here is bad. Which editor are you using, and does it have an .editorconfig
plugin? Should prevent this from happening.
Feel free to mention it on slack. Someone can guide you through configuring an editor when we get the chance.
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.
just installed the editorconfig package for atom!
assert json["data"]["id"] | ||
assert Repo.get_by(Task, @valid_attrs) | ||
task = insert(:task, user: current_user) | ||
assert conn |> request_update(task, @valid_attrs) |> json_response(200) |
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.
More indentation problems here.
@amyschools this had a review with changes that need made. |
pushed to the wrong branch! should be good now. The 'close pull request' button is very close to the comment button... |
@amyschools awesome, rebase, squash, and merge. |
812ec03
to
a860dad
Compare
What's in this PR?
Add api_case.ex helpers to Task controller tests.
Stuck on a fix to make two tests pass: update: create and update: renders 422 error.
References
Closes #442
Progress on: #413