Skip to content

[USERSKILL]: refactor away ja_resource#926

Merged
begedin merged 4 commits intodevelopfrom
user-skill-controller-ja-resource
Sep 19, 2017
Merged

[USERSKILL]: refactor away ja_resource#926
begedin merged 4 commits intodevelopfrom
user-skill-controller-ja-resource

Conversation

@snewcomer
Copy link
Copy Markdown
Contributor

Fixes #916

@snewcomer snewcomer self-assigned this Sep 13, 2017
@snewcomer snewcomer requested a review from begedin September 13, 2017 03:39
@snewcomer snewcomer force-pushed the user-skill-controller-ja-resource branch 2 times, most recently from 72a2b0f to 9ae0ae9 Compare September 13, 2017 03:50
with %UserSkill{} = user_skill <- UserSkill |> Repo.get(id),
%User{} = current_user <- conn |> Guardian.Plug.current_resource,
{:ok, :authorized} <- current_user |> Policy.authorize(:delete, user_skill),
{:ok, %UserSkill{} = _user_skill} <- user_skill |> Repo.delete
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@begedin getting a test failure here

Not really familiar with this :track code. Lmk what you think here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've discovered a bug we've been creating on some controllers with this switch

We have a plug, SegmentPlugTracker, which registers an action right before the conn sends a response.

This action figures out what happened (in this case, that a UserSkill was deleted) and calls tracking code for that action.

However, the "figuring out" also relied on ja_resource and canary, so in this case, it got broken and the controller test found it. Unfortunately it seems not all our controller tests have this assert_received, so we may have turned off tracking in some cases. I'll make sure to check.

We probably want to make that explicit as well, to avoid further issues, so I'll create an issue to do this.

For know, to keep tracking working here, you'll need to do a conn |> Conn.assign(:user_skill, user_skill) |> conn |> send_resp(:no_content, "")

Copy link
Copy Markdown
Contributor

@begedin begedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work. The failed test is actually a good thing and you may have stopped us from causing bugs down the line.

with %UserSkill{} = user_skill <- UserSkill |> Repo.get(id),
%User{} = current_user <- conn |> Guardian.Plug.current_resource,
{:ok, :authorized} <- current_user |> Policy.authorize(:delete, user_skill),
{:ok, %UserSkill{} = _user_skill} <- user_skill |> Repo.delete
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've discovered a bug we've been creating on some controllers with this switch

We have a plug, SegmentPlugTracker, which registers an action right before the conn sends a response.

This action figures out what happened (in this case, that a UserSkill was deleted) and calls tracking code for that action.

However, the "figuring out" also relied on ja_resource and canary, so in this case, it got broken and the controller test found it. Unfortunately it seems not all our controller tests have this assert_received, so we may have turned off tracking in some cases. I'll make sure to check.

We probably want to make that explicit as well, to avoid further issues, so I'll create an issue to do this.

For know, to keep tracking working here, you'll need to do a conn |> Conn.assign(:user_skill, user_skill) |> conn |> send_resp(:no_content, "")

@joshsmith
Copy link
Copy Markdown
Contributor

@snewcomer perhaps we need some higher-level docs somewhere about the tracking?

@begedin
Copy link
Copy Markdown
Contributor

begedin commented Sep 14, 2017

@joshsmith Higher level docs are good, but I've also created #927 to avoid unclear cases like this one. I think we made far too many things implicit in the early days of this project.

@snewcomer
Copy link
Copy Markdown
Contributor Author

@begedin is this g2g?

@snewcomer snewcomer force-pushed the user-skill-controller-ja-resource branch from e7c3969 to bf6de49 Compare September 14, 2017 14:46
@begedin
Copy link
Copy Markdown
Contributor

begedin commented Sep 14, 2017

@snewcomer Squash the two commits together and then you can consider it merged. However, I'm hoping #805 gets merged first, since rebasing this one would be much easier, so don't worry, if the PR stays up for a bit. It's just waiting on that one.

@joshsmith
Copy link
Copy Markdown
Contributor

#805 is possibly not going to get merged quickly, pending what @begedin says in reply to review comments from today.

Copy link
Copy Markdown
Contributor

@begedin begedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We merged one of the two PRs, so rebasing the other became simple enough where we can get this merged to.

However, on a second review, I found a few things that need to be fixed.

def create?(%User{id: id}, %Changeset{changes: %{user_id: user_id}}), do: id == user_id
def create?(%User{}, %Changeset{}), do: false
def create?(%User{admin: true}, %{}), do: true
def create?(%User{id: id}, %{user_id: user_id}), do: id == user_id
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snewcomer The params we send here use strings as keys. Additionally, there's an issue with id values in the params map all being strings (this is what ember sends), so to fix the problem here you need to

  • replace this line with def create?(%User{id: id}, %{"user_id" => user_id}), do: id == user_id
  • update the policy test to match
  • add plug CodeCorpsWeb.Plug.IdsToIntegers into the controller, right after the DataToAttributes plug.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@begedin thats a great catch. Fixed.

test "returns true if user is creating their own record" do
user = insert(:user)
changeset = %UserSkill{} |> create_changeset(%{user_id: user.id})
user_skill = %UserSkill{user_id: user.id}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I explained above, this needs to be a params map isntead, looking like

params = %{"user_id" => user.id}

assert create?(user, params)

@snewcomer snewcomer force-pushed the user-skill-controller-ja-resource branch from bf6de49 to 28db06e Compare September 19, 2017 03:22
changeset = %UserSkill{} |> create_changeset(%{})

assert create?(user, changeset)
assert create?(user, %{"user_id" => user.id})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not using the changeset here anymore. You can likely also remove import CodeCorps.UserSkill, only: [create_changeset: 2] from the top of the file.

Sorry I didn't catch this on my previous pass.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops. fixed.

@snewcomer snewcomer force-pushed the user-skill-controller-ja-resource branch from 32f768b to 48e4933 Compare September 19, 2017 12:28
@begedin begedin merged commit f5a1d4f into develop Sep 19, 2017
@begedin begedin deleted the user-skill-controller-ja-resource branch September 19, 2017 12:39
@begedin
Copy link
Copy Markdown
Contributor

begedin commented Sep 19, 2017

Thank you @snewcomer. Great work 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants