Unable to use variables to do/end block when generating series of tests using macro

Guys I am trying to create a macro that will generate a series of tests but I am stuck on passing the variable inside the do end block This is the macro:

defmacro test_authz(
             {:<-, _, [account_var, users]},
             {:<-, _, [action_var, actions]},
             context_var,
             do: block
           ) do
    account_var = Macro.escape(account_var)
    action_var = Macro.escape(action_var)
    context_var = Macro.escape(context_var)

    quote bind_quoted: [
            account_var: account_var,
            users: users,
            action_var: action_var,
            actions: actions,
            context_var: context_var,
            block: block
          ] do
      for action <- actions do
        for user <- users do
          test "#{action} action is accessible by #{user}", var!(context_var) = ctx do
            var!(account_var) = ctx.user[unquote(user)]
            var!(action_var) = unquote(action)
            unquote(block)
          end
        end
      end
    end
  end

This is how the test is written:

test_authz account <- [:employer_with_view_permission, :client, :vessel_account],
               action <- [:index],
               ctx do
      IO.inspect(account)
    end

But I am getting this error, undefined function account/0 (there is no such import). Same as with when I inspect action and ctx variables inside the do/end block. What do you think am I missing?

Solved! I solved it using ExUnit.Case.register_test to remove the complexity of quoting inside test api which is also a macro under the hood. Here’s the updated code

defmacro test_authz(
             {:<-, _, [account_var, users]},
             {:<-, _, [action_var, actions]},
             context_var,
             do: block
           ) do
    account_var = Macro.escape(account_var)
    action_var = Macro.escape(action_var)
    context_var = Macro.escape(context_var)
    block = Macro.escape(block, unquote: true)
    %{module: mod, file: file, line: line} = __CALLER__

    quote bind_quoted: [
            account_var: account_var,
            users: users,
            action_var: action_var,
            actions: actions,
            context_var: context_var,
            block: block,
            mod: mod,
            file: file,
            line: line
          ] do
      for action <- actions do
        for user <- users do
          message = "#{action} action is accessible by #{user}"
          name = ExUnit.Case.register_test(mod, file, line, :test, message, [])

          def unquote(name)(unquote(context_var) = ctx) do
            var!(unquote(account_var)) = ctx.user[unquote(user)]
            var!(unquote(action_var)) = unquote(action)
            unquote(block)
          end
        end
      end
    end
  end