BUG: Get approles results in error 500
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
Seems like the error comes from calling fetch_flash without initialising a session. Changed in f17d45669c74f0be185e1eb7bb6dead4a1c0f683
That's annoying ... 😕 Need to add a regression test for this and fix it. 👍
@nelsonic Any reason we're calling fetch_flash on our API routes?
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)
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.
Trying to fix this but seeing the following error instead:
** (ArgumentError) cannot fetch key "_format" from conn.params because they were not fetched
Fixed in #131 (please review)