auth icon indicating copy to clipboard operation
auth copied to clipboard

BUG: Get approles results in error 500

Open t0mhaines opened this issue 5 years ago • 7 comments

When trying to run tests for Smart Home Security Server, I started getting error 500 from Auth.

Heroku logging spits out the following:

2020-10-01T15:00:03.600090+00:00 app[web.1]: Request: GET /approles/<Client ID>
2020-10-01T15:00:03.600090+00:00 app[web.1]: ** (exit) an exception was raised:
2020-10-01T15:00:03.600091+00:00 app[web.1]:     ** (ArgumentError) session not fetched, call fetch_session/2
2020-10-01T15:00:03.600091+00:00 app[web.1]:         (plug 1.10.4) lib/plug/conn.ex:1570: Plug.Conn.get_session/1
2020-10-01T15:00:03.600091+00:00 app[web.1]:         (plug 1.10.4) lib/plug/conn.ex:1553: Plug.Conn.get_session/2
2020-10-01T15:00:03.600092+00:00 app[web.1]:         (phoenix 1.5.4) lib/phoenix/controller.ex:1342: Phoenix.Controller.fetch_flash/2
2020-10-01T15:00:03.600092+00:00 app[web.1]:         (auth 1.3.1) AuthWeb.Router.api/2
2020-10-01T15:00:03.600093+00:00 app[web.1]:         (auth 1.3.1) lib/auth_web/router.ex:1: AuthWeb.Router.__pipe_through2__/1
2020-10-01T15:00:03.600093+00:00 app[web.1]:         (phoenix 1.5.4) lib/phoenix/router.ex:347: Phoenix.Router.__call__/2
2020-10-01T15:00:03.600093+00:00 app[web.1]:         (auth 1.3.1) lib/auth_web/endpoint.ex:1: AuthWeb.Endpoint.plug_builder_call/2
2020-10-01T15:00:03.600094+00:00 app[web.1]:         (auth 1.3.1) lib/auth_web/endpoint.ex:1: AuthWeb.Endpoint.call/2

t0mhaines avatar Oct 01 '20 15:10 t0mhaines

Seems like the error comes from calling fetch_flash without initialising a session. Changed in f17d45669c74f0be185e1eb7bb6dead4a1c0f683

t0mhaines avatar Oct 01 '20 15:10 t0mhaines

That's annoying ... 😕 Need to add a regression test for this and fix it. 👍

nelsonic avatar Oct 01 '20 15:10 nelsonic

@nelsonic Any reason we're calling fetch_flash on our API routes?

t0mhaines avatar Oct 01 '20 15:10 t0mhaines

Without the :fetch_flash we get the following failing tests:

  1) test GET /personroles/:person_id/:client_id returns 401 if client_id is invalid (AuthWeb.ApiControllerTest)
     test/auth_web/controllers/api_controller_test.exs:111
     ** (ArgumentError) flash not fetched, call fetch_flash/2
     code: |> get("/personroles/1/invalid")
     stacktrace:
       (phoenix 1.5.4) lib/phoenix/controller.ex:1409: Phoenix.Controller.get_flash/1
       (phoenix 1.5.4) lib/phoenix/controller.ex:1423: Phoenix.Controller.get_flash/2
       (auth 1.3.1) lib/auth_web/templates/layout/app.html.eex:18: AuthWeb.LayoutView."app.html"/1
       (phoenix 1.5.4) lib/phoenix/view.ex:472: Phoenix.View.render_to_iodata/3
       (phoenix 1.5.4) lib/phoenix/controller.ex:776: Phoenix.Controller.render_and_send/4
       (auth 1.3.1) lib/auth_web/controllers/api_controller.ex:1: AuthWeb.ApiController.action/2
       (auth 1.3.1) lib/auth_web/controllers/api_controller.ex:1: AuthWeb.ApiController.phoenix_controller_pipeline/2
       (phoenix 1.5.4) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
       (auth 1.3.1) lib/auth_web/endpoint.ex:1: AuthWeb.Endpoint.plug_builder_call/2
       (auth 1.3.1) lib/auth_web/endpoint.ex:1: AuthWeb.Endpoint.call/2
       (phoenix 1.5.4) lib/phoenix/test/conn_test.ex:225: Phoenix.ConnTest.dispatch/5
       test/auth_web/controllers/api_controller_test.exs:115: (test)

..16:08:37.774 request_id=FjnmzqivY4AHsB4AAA7i [error] GET /approles/invalid > msg:invalid AUTH_API_KEY/client_id please check, status_id:401


  2) test GET /approles/:client_id returns 401 if client_id is invalid (AuthWeb.ApiControllerTest)
     test/auth_web/controllers/api_controller_test.exs:28
     ** (ArgumentError) flash not fetched, call fetch_flash/2
     code: |> get("/approles/invalid")
     stacktrace:
       (phoenix 1.5.4) lib/phoenix/controller.ex:1409: Phoenix.Controller.get_flash/1
       (phoenix 1.5.4) lib/phoenix/controller.ex:1423: Phoenix.Controller.get_flash/2
       (auth 1.3.1) lib/auth_web/templates/layout/app.html.eex:18: AuthWeb.LayoutView."app.html"/1
       (phoenix 1.5.4) lib/phoenix/view.ex:472: Phoenix.View.render_to_iodata/3
       (phoenix 1.5.4) lib/phoenix/controller.ex:776: Phoenix.Controller.render_and_send/4
       (auth 1.3.1) lib/auth_web/controllers/api_controller.ex:1: AuthWeb.ApiController.action/2
       (auth 1.3.1) lib/auth_web/controllers/api_controller.ex:1: AuthWeb.ApiController.phoenix_controller_pipeline/2
       (phoenix 1.5.4) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
       (auth 1.3.1) lib/auth_web/endpoint.ex:1: AuthWeb.Endpoint.plug_builder_call/2
       (auth 1.3.1) lib/auth_web/endpoint.ex:1: AuthWeb.Endpoint.call/2
       (phoenix 1.5.4) lib/phoenix/test/conn_test.ex:225: Phoenix.ConnTest.dispatch/5
       test/auth_web/controllers/api_controller_test.exs:32: (test)

nelsonic avatar Oct 01 '20 15:10 nelsonic

I think the problem is we are attempting to respond to an API request with an HTML response which has the "flash" ... Will try and refactor it now.

nelsonic avatar Oct 01 '20 15:10 nelsonic

Trying to fix this but seeing the following error instead:

** (ArgumentError) cannot fetch key "_format" from conn.params because they were not fetched

nelsonic avatar Oct 01 '20 15:10 nelsonic

Fixed in #131 (please review)

nelsonic avatar Oct 01 '20 17:10 nelsonic