Skip to content
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

Update Dependencies > Fix Failing Tests #83

Closed
2 tasks done
nelsonic opened this issue Jul 14, 2020 · 17 comments
Closed
2 tasks done

Update Dependencies > Fix Failing Tests #83

nelsonic opened this issue Jul 14, 2020 · 17 comments
Assignees
Labels
chore a tedious but necessary task often paying technical debt enhancement New feature or enhancement of existing functionality in-progress An issue or pull request that is being worked on by the assigned person T25m Time Estimate 25 Minutes

Comments

@nelsonic
Copy link
Member

nelsonic commented Jul 14, 2020

3 of our dependencies are out-of-date because we published new versions of fields and auth_plugand a new version ofplug_cowboy` was released. https://libraries.io/hex/auth
image

Todo

@nelsonic nelsonic added enhancement New feature or enhancement of existing functionality chore a tedious but necessary task often paying technical debt T25m Time Estimate 25 Minutes labels Jul 14, 2020
@nelsonic nelsonic self-assigned this Jul 14, 2020
@nelsonic nelsonic added the in-progress An issue or pull request that is being worked on by the assigned person label Jul 14, 2020
@nelsonic
Copy link
Member Author

auth_plug had a breaking change where AuthPlug.session_options/0 was removed in https://github.com/dwyl/auth_plug/pull/20/files#diff-1560d1759954bb80cc81c6769976610fL68
image

So now we need to a bit of refactoring in auth to not invoke the unavailable function.
image

@th0mas
Copy link
Collaborator

th0mas commented Jul 14, 2020 via email

@nelsonic
Copy link
Member Author

@th0mas not really. it was just a way of keeping the auth App consistent with any app using auth_plug. ♻️
I figured that by using auth_plug in auth I would immediately know if there was a breaking change. 💭
In this case, there is, so now we get to do a bit more refactoring. 🔍 😉
The starting point is: dwyl/auth_plug_example#4
I'm happy to do this as it's a low-hanging fruit (chore) that I have all the environment variables setup for.
I will assign the PR(s) to you though. 👍

@nelsonic
Copy link
Member Author

After I made the same update to the lib/auth_web/endpoint.ex as in https://github.com/dwyl/auth_plug_example/pull/6/files#r454414361 the project compiles.
Unfortunately, 12 tests are failing with a similar-looking error:

  1) test delete apikey deletes chosen apikey (AuthWeb.ApikeyControllerTest)
     test/auth_web/controllers/apikey_controller_test.exs:204
     ** (ArgumentError) session not fetched, call fetch_session/2
     code: conn = AuthPlug.create_jwt_session(conn, person)
     stacktrace:
       (plug 1.10.3) lib/plug/conn.ex:1568: Plug.Conn.get_session/1
       (plug 1.10.3) lib/plug/conn.ex:1727: Plug.Conn.put_session/2
       (auth_plug 1.2.0) lib/auth_plug.ex:92: AuthPlug.create_session/3
       test/auth_web/controllers/apikey_controller_test.exs:206: (test)

Looking into it now.

@nelsonic nelsonic changed the title Update Dependencies Update Dependencies > Fix Failing Tests Jul 16, 2020
@nelsonic
Copy link
Member Author

It's not just the tests that fail on localhost, when I run the App it compiles but then Google Auth Fails:
image

If I downgrade auth_plug 1.2.0 => 1.1.1 and (and revert the change in endpoint.ex)
then re-run the tests they all pass (as expected).
But the Google Auth still fails:
auth-cowboy-error
🤦

From the stack trace I suspect this is a Cowboy issue (using the incorrect version of TLS).
So that's something else I will need to look at once the tests are passing with auth_plug@v1.2.0. 🙄

Note: I want to get onto RBAC ASAP as I know it's blocking progress in dwyl/smart-home-auth-server#1
but I want to have a solid foundation (known working state) before adding anything new.

@nelsonic
Copy link
Member Author

Still getting the error ... but this looks like a promising line of enquiry: https://elixirforum.com/t/erlang-v-23-0-and-httpoison-error/31615/7

@nelsonic
Copy link
Member Author

On heroku the error is even less helpful/friendly:
image

@nelsonic
Copy link
Member Author

Ok after updating all the underlying dependencies, and making a few tweeks to the code,
we are back at a known state with Auth.
image

image

image

image

However I still need to update to the latest version of auth_plug again and continue debugging that.

@nelsonic
Copy link
Member Author

OK, back to auth_plug@1.2.0 and debugging the error:

 12) test admin/2 show welcome page (AuthWeb.AuthControllerTest)
     test/auth_web/controllers/auth_controller_test.exs:16
     ** (ArgumentError) session not fetched, call fetch_session/2
     code: conn = AuthPlug.create_jwt_session(conn, Map.merge(data, %{id: person.id}))
     stacktrace:
       (plug 1.10.3) lib/plug/conn.ex:1568: Plug.Conn.get_session/1
       (plug 1.10.3) lib/plug/conn.ex:1727: Plug.Conn.put_session/2
       (auth_plug 1.2.0) lib/auth_plug.ex:92: AuthPlug.create_session/3
       test/auth_web/controllers/auth_controller_test.exs:25: (test)

.......

Finished in 17.8 seconds
1 property, 45 tests, 12 failures

The session needs to be started at the beginning of the request lifecycle to avoid seeing the error:

** (ArgumentError) session not fetched, call fetch_session/2

That's why we had:

plug Plug.Session, AuthPlug.session_options()

I'm going to keep digging. ⏳

@nelsonic
Copy link
Member Author

Ok, after a bit of digging, it turns out a new helper method was added to Plug.Test which we can use:
init_test_session/2 discovered via: phoenixframework/phoenix#861 (comment)

If we update a test from:

test "lists all apikeys", %{conn: conn} do
  person = Auth.Person.get_person_by_email(@email)
  conn = AuthPlug.create_jwt_session(conn, %{email: @email, id: person.id})
  conn = get(conn, Routes.apikey_path(conn, :index))
  assert html_response(conn, 200) =~ "Auth API Keys"
end

To:

test "lists all apikeys", %{conn: conn} do
  person = Auth.Person.get_person_by_email(@email)
  conn = conn
  |> init_test_session(foo: "bar") # auth/issues/83
  |> AuthPlug.create_jwt_session(%{email: @email, id: person.id})
  |> get(Routes.apikey_path(conn, :index))
  assert html_response(conn, 200) =~ "Auth API Keys"
end

It passes!! 🎉
It's an extra line of code (in each of the 12 tests) but I'm fine with that because it reduces the number of steps to setup auth_plug as noted in: https://github.com/dwyl/auth_plug/pull/20/files#diff-04c6e90faac2675aa89e2176d2eec7d8

Going to make the changes to all the failing tests and see how far I get. 🤞

@nelsonic
Copy link
Member Author

Applied the update to all 12 tests across 2 files. Works: 🙌

image

@th0mas
Copy link
Collaborator

th0mas commented Jul 17, 2020

init_test_session is how we setup the session in auth_plug as well:

    setup %{endpoint: endpoint} do
      test_conn =
        conn(:get, "endpoint")
        |> init_test_session(%{})

      {:ok, conn: test_conn}

@th0mas
Copy link
Collaborator

th0mas commented Jul 17, 2020

To reduce LOC in tests, you can move init_test_session/1 to the conn_case test helper, this is the strategy used in dwyl/smart_home_auth:

https://github.com/dwyl/smart-home-auth-server/blob/420ff63a009a0dbed7a17b23ea9f05ef85fbed9e/test/support/conn_case.ex#L43

@nelsonic
Copy link
Member Author

@nelsonic
Copy link
Member Author

@th0mas putting init_test_session/1 in the conn_case is much nicer. Thanks! 👍

@nelsonic
Copy link
Member Author

@th0mas if you don't mind, please review/merg the PR: #84 Thanks! 🌻

th0mas added a commit that referenced this issue Jul 17, 2020
@th0mas
Copy link
Collaborator

th0mas commented Jul 17, 2020

Done & merged

@th0mas th0mas closed this as completed Jul 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore a tedious but necessary task often paying technical debt enhancement New feature or enhancement of existing functionality in-progress An issue or pull request that is being worked on by the assigned person T25m Time Estimate 25 Minutes
Projects
None yet
Development

No branches or pull requests

2 participants