How can I test a plug that calls an action_fallback controller? - model-view-controller

Hello I am currently trying to get this test pass
test "authentication plug should return 401 when not authenticated", %{conn: conn} do
conn = Map.put(conn, :params, %{})
conn = ChaacServerWeb.Plugs.Authentication.call(conn, nil)
assert json_response(conn, 401)["errors"] != %{}
end
Here is my plug
def call(conn, _) do
token = List.first(get_req_header(conn, "authorization"))
case Accounts.validate_token(conn.params["user_id"], token) do
{:ok, valid_token} -> conn
err ->
conn
|> halt()
|> ChaacServerWeb.FallbackController.call(err)
end
end
I get this error
1) test authentication plug should return 401 when not authenticated (ChaacServerWeb.AuthenticationTest) test/chaac_server_web/plugs/authentication_test.exs:26
** (RuntimeError) cannot render template :"401" because conn.params["_format"] is not set.
Please set `plug :accepts, ~w(html json ...)` in your pipeline.
code: conn = ChaacServerWeb.Plugs.Authentication.call(conn, nil)
stacktrace:
(phoenix) lib/phoenix/controller.ex:689: Phoenix.Controller.render/3
test/chaac_server_web/plugs/authentication_test.exs:28: (test)
I understand I need to call plug :accepts, [:json] somehow in my test setup but how do I do that? Thanks
(EDIT) My router has plug :accepts, [:json] in my pipeline

I just did
test "authentication plug should return 401 when not authenticated", %{conn: conn} do
conn = Map.put(conn, :params, %{"_format" => "json"})
conn = ChaacServerWeb.Plugs.Authentication.call(conn, nil)
assert json_response(conn, 401)["errors"] != %{}
end
Feels abit hacky but it works for me.

You can use put_req_header:
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end

Related

Absinthe - How to put_session in resolver function?

I'm using Absinthe and have a sign in mutation. When users send over valid credentials, I'd like to set a session cookie in the response via put_session.
The problem I'm facing is that I'm not able to access the conn from within a resolver function. That tells me that I'm not supposed to update the connection's properties from within a resolver.
Is it possible to do this with Absinthe? What are some alternative solutions?
It looks like one solution is:
In the resolver, resolve either an {:ok, _} or an {:error, _} as normal
Add middleware after the resolver to pattern match that resolution.value returned from step 1 and update the GraphQL context
Use the before_send feature of Absinthe (which has access to both the GraphQL context and the connection to put_session before sending a response
Code Example
Mutation:
mutation do
#desc "Authenticate a user."
field :login, :user do
arg(:email, non_null(:string))
arg(:password, non_null(:string))
resolve(&Resolvers.Accounts.signin/3)
middleware(fn resolution, _ ->
case resolution.value do
%{user: user, auth_token: auth_token} ->
Map.update!(
resolution,
:context,
&Map.merge(&1, %{auth_token: auth_token, user: user})
)
_ ->
resolution
end
end)
end
end
Resolver:
defmodule AppWeb.Resolvers.Accounts do
alias App.Accounts
def signin(_, %{email: email, password: password}, _) do
if user = Accounts.get_user_by_email_and_password(email, password) do
auth_token = Accounts.generate_user_session_token(user)
{:ok, %{user: user, auth_token: auth_token}}
else
{:error, "Invalid credentials."}
end
end
end
Router:
defmodule AppWeb.Router do
use AppWeb, :router
pipeline :api do
plug(:accepts, ["json"])
plug(:fetch_session)
end
scope "/" do
pipe_through(:api)
forward("/api", Absinthe.Plug,
schema: AppWeb.Schema,
before_send: {__MODULE__, :absinthe_before_send}
)
forward("/graphiql", Absinthe.Plug.GraphiQL,
schema: AppWeb.Schema,
before_send: {__MODULE__, :absinthe_before_send}
)
end
def absinthe_before_send(conn, %Absinthe.Blueprint{} = blueprint) do
if auth_token = blueprint.execution.context[:auth_token] do
put_session(conn, :auth_token, auth_token)
else
conn
end
end
def absinthe_before_send(conn, _) do
conn
end
end
Not sure why you want to use a session, can't this be solved using a bearer?
Please disregard the interfaces. :-)
Mutation.
object :user_token_payload do
field(:user, :user)
field(:token, :string)
end
object :login_user_mutation_response, is_type_of: :login_user do
interface(:straw_hat_mutation_response)
field(:errors, list_of(:straw_hat_error))
field(:successful, non_null(:boolean))
field(:payload, :user_token_payload)
end
Resolver.
def authenticate_user(args, _) do
case Accounts.authenticate_user(args) do
{:ok, user, token} -> MutationResponse.succeeded(%{user: user, token: token})
{:error, message} -> MutationResponse.failed(StrawHat.Error.new(message))
end
end
Now the client can pass along that token with the Authorization header, and pick it up with a plug.
defmodule MyAppWeb.Plugs.Context do
import Plug.Conn
alias MyApp.Admission
def init(opts), do: opts
def call(conn, _) do
case build_context(conn) do
{:ok, context} -> put_private(conn, :absinthe, %{context: context})
_ -> put_private(conn, :absinthe, %{context: %{}})
end
end
#doc """
Return the current user context based on the authorization header
"""
def build_context(conn) do
auth_header =
get_req_header(conn, "authorization")
|> List.first()
if auth_header do
"Bearer " <> token = auth_header
case Admission.get_token_by_hash(token) do
nil -> :error
token -> {:ok, %{current_user: token.user}}
end
else
:error
end
end
end
Then add the plug to your pipeline
plug(MyApp.Plugs.Context)
Then you can pick up the current user in your resolvers like so.
def create_note(%{input: input}, %{context: %{current_user: user}}) do
end

Elixir - Check if string is empty

I am playing with Elixir and Phoenix Framework for the first time after following this Tutorial..
I have a simple client/server app.
chat/lib/chat_web/room_channel.ex:
defmodule ChatWeb.RoomChannel do
use Phoenix.Channel
def join("room:lobby", _message, socket) do
{:ok, socket}
end
def join("room:" <> _private_room_id, _params, _socket) do
{:error, %{reason: "unauthorized"}}
end
def handle_in("new_msg", %{"body" => body}, socket) do
broadcast! socket, "new_msg", %{body: body}
{:noreply, socket}
end
end
I want to block empty incoming messages (body is empty string)
def handle_in("new_msg", %{"body" => body}, socket) do
# I guess the code should be here..
broadcast! socket, "new_msg", %{body: body}
{:noreply, socket}
end
How can I do that?
I want to block empty incoming messages (body is empty string)
You can add a guard clause for this. Either when body != "" or when byte_size(body) > 0
def handle_in("new_msg", %{"body" => body}, socket) when body != "" do
...
end
Now this function will only match if body is not "".
If you also want to handle empty body case, you can add two clauses like this (no need for the guard clause anymore since the second clause will never match if body is empty):
def handle_in("new_msg", %{"body" => ""}, socket) do
# broadcast error here
end
def handle_in("new_msg", %{"body" => body}, socket) do
# broadcast normal here
end
You can use answer proposed by #Dogbert, but to be 100% sure that string is not empty you can use wrap the broadcast! in the helper private function or just wrap into if or unless (negative if) expression.
unless String.trim(body) == "" do
broadcast! socket, "new_msg", %{body: body}
end
If you want to return an error message you try to use something more complex eg.:
if String.trim(body) != "" do
broadcast! socket, "new_msg", %{body: body}
else
broadcast! socket, "error_msg", %{body: "Body is empty"}
end

Ruby Gmail API with OAUTH2 responds with Invalid credentials (Failure) when logging in

I am trying to connect to the Gmail api using the Gmail for Ruby gem. I'm following this google oauth2 guide for installed applications.
I have set my app up in the Google Developer's Console, I am able to send a request with my client_id and client_secret to obtain an authorization code. I am then able to send a request with my authorization code to obtain an access token and a refresh token. I am able to successfully send a request to refresh my access token, and it returns a new access token.
The problem arises when I try to connect to Gmail. First I set an instance variable #gmail = Gmail.connect(:xoauth2, #email, #client.access_token). Then, I attempt to login with #gmail.login(true). However, when I try that, I get the following error:
Couldn't login to given Gmail account: caiden.robinson35#gmail.com (Invalid credentials (Failure)) (Gmail::Client::AuthorizationError)
I am at a loss here, everything suggests I'm executing the oauth2 flow correctly, except the fact that when it comes time to login, I get invalid credentials. When generating my authorization code, I specifically click my email and allow my app to have access. The API is also enabled in my developers console. Here is the full code:
class GmailClient
def initialize
load_client_info
#email = "caiden.robinson35#gmail.com"
load_and_set_oauth2_tokens
sign_in_gmail
binding.pry
end
private
def sign_in_gmail
binding.pry
#gmail = Gmail.connect(:xoauth2, #email, #client.access_token)
######################
# RIGHT HERE IS WHERE IT FAIL
######################
#gmail.login true
binding.pry
end
def load_client_info
gmail_credentials = YAML.load_file('config/gmail.yml')
#client_id = gmail_credentials["client_id"]
#client_secret = gmail_credentials["client_secret"]
#redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
end
def load_and_set_oauth2_tokens use_cached_tokens = true
if use_cached_tokens && File.exist?("config/tokens.yml")
token_hash = YAML.load_file('config/tokens.yml')
#authorization_code = { code: token_hash["authorization_code"],
is_cached: true }
#client = signet_client(token_hash)
#token_hash = #client.refresh!
else
if !(instance_variable_defined?("#authorization_code") && #authorization_code[:is_cached] == false)
retrieve_and_set_authorization_code
end
#token_hash = set_client_and_retrieve_oauth2_tokens
end
write_tokens_to_file
end
def retrieve_and_set_authorization_code
puts "Go to the following url to enable the gmail cli app:"
puts "https://accounts.google.com/o/oauth2/auth?scope=email&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=#{#client_id}"
print "Paste your authorization code here: "
#authorization_code = { code: gets.chomp,
is_cached: false }
end
def set_client_and_retrieve_oauth2_tokens
#client = signet_client
#client.fetch_access_token!
end
def signet_client token_hash = nil
client = Signet::OAuth2::Client.new(
client_id: #client_id,
client_secret: #client_secret,
redirect_uri: #redirect_uri,
scope: 'email',
token_credential_uri: 'https://www.googleapis.com/oauth2/v4/token'
)
if token_hash.present?
client.refresh_token = token_hash["refresh_token"]
else
client.authorization_uri = 'https://accounts.google.com/o/oauth2/auth'
client.code = #authorization_code[:code]
end
client
end
def write_tokens_to_file
if File.exist?("config/tokens.yml")
data = YAML.load_file "config/tokens.yml"
#token_hash.each { |k, v| data[k] = v }
File.open('config/tokens.yml', 'w') do |file|
YAML.dump(data, file)
end
else
File.open('config/tokens.yml', 'w') do |file|
#token_hash.each { |k, v| file.write("#{k}: #{v}\n") }
file.write("authorization_code: #{#authorization_code[:code]}\n")
end
end
end
end
If my question is lacking any info, please just ask, I am eager to solve this.
Scopes matter. Here are right ones:
scope: ['https://mail.google.com/', 'https://www.googleapis.com/auth/userinfo.email' #,'https://www.googleapis.com/auth/gmail.send' - if you'd like to send emails as well]

How to mock Net::HTTP::Post?

Yes, I know it is best to use webmock, but I would like to know how to mock this method in RSpec:
def method_to_test
url = URI.parse uri
req = Net::HTTP::Post.new url.path
res = Net::HTTP.start(url.host, url.port) do |http|
http.request req, foo: 1
end
res
end
Here is the RSpec:
let( :uri ) { 'http://example.com' }
specify 'HTTP call' do
http = mock :http
Net::HTTP.stub!(:start).and_yield http
http.should_receive(:request).with(Net::HTTP::Post.new(uri), foo: 1)
.and_return 202
method_to_test.should == 202
end
The test fails because with seems to be trying to match the NET::HTTP::Post object:
RSpec::Mocks::MockExpectationError: (Mock :http).request(#<Net::HTTP::Post POST>, {:foo=>"1"})
expected: 1 time
received: 0 times
Mock :http received :request with unexpected arguments
expected: (#<Net::HTTP::Post POST>, {:foo=>"1"})
got: (#<Net::HTTP::Post POST>, {:foo=>"1"})
How to match properly?
If you don't care about the exact instance, you could use the an_instance_of method:
http.should_receive(:request).with(an_instance_of(Net::HTTP::Post), foo: 1)
.and_return 202
Here it is with new syntax:
before do
http = double
allow(Net::HTTP).to receive(:start).and_yield http
allow(http).to \
receive(:request).with(an_instance_of(Net::HTTP::Get))
.and_return(Net::HTTPResponse)
end
And then in example:
it "http" do
allow(Net::HTTPResponse).to receive(:body)
.and_return('the actual body of response')
# here execute request
end
Very helpful if you need test external api library.
The following snippet works nice for me. It mocks a DELETE request.
mock_net_http = double("Net:HTTP")
mock_net_http_delete = double("Net::HTTP::Delete")
allow(Net::HTTP).to receive(:new).and_return(mock_net_http)
allow(Net::HTTP::Delete).to receive(:new).and_return(mock_net_http_delete)
expect(mock_net_http_delete).to receive(:set_form_data).with(hash_including(some_param: "some-value"))
expect(mock_net_http).to receive(:request).with(mock_net_http_delete)

Ruby URL.parse error

Here is my ruby program
require 'net/http'
require 'uri'
begin
url = URI.parse("http://google.com")
rescue Exception => err
p err
exit
end
http = Net::HTTP.new(url.host, url.port)
res = http.head("/")
p res.code
It works fine, however if I remove http:// from URL.parse(), It gives me this error:
/usr/lib/ruby/1.9.1/net/http.rb:1196:in `addr_port': undefined method `+' for nil:NilClass (NoMethodError) ...
from /usr/lib/ruby/1.9.1/net/http.rb:1094:in `request'
from /usr/lib/ruby/1.9.1/net/http.rb:860:in `head'
Is it the correct way to handle Exception ?
I know maybe the URL is not correct, but It should raise an exception URI::InvalidURIError instead of accepting and continue the program ?
If you say u = URI.parse('http://google.com'), you'll get a URI::HTTP back and the u.port will have a default of 80. If you say u = URI.parse('google.com'), you'll get a URI::Generic back with the u.port will be nil as will u.host.
So, when you do this:
url = URI.parse('google.com')
http = Net::HTTP.new(url.host, url.port)
You're really doing this:
http = Net::HTTP.new(nil, nil)
and Net::HTTP doesn't like that very much at all. You could try something like this instead:
if(str.to_s.empty?)
# complain loudly about a missing str
end
begin
url = URI.parse(str)
url = URI.parse('http://' + str) if !url.scheme
if(url.scheme != 'http' && url.scheme != 'https')
# more complaining about bad input
end
http = Net::HTTP.new(url.host, url.port)
#...
rescue URI::Error => e
# even yet more complaining
end
That sort of thing should bypass the exception completely and cover a few other things that you might be interested in.
You have to specifically catch URI::InvalidURIError, as it is not a descendant of Exception. See:
irb(main):002:0> URI::InvalidURIError.is_a?(Exception)
=> false
So the fix for your code would be:
begin
url = URI.parse("http://google.com")
rescue URI::InvalidURIError => err
p err
exit
end
The correct way is not to let any exception happen at all, but to check your conditions beforehand. Like this:
require 'net/http'
require 'uri'
begin
url = URI.parse("http://google.com")
rescue URI::InvalidURIError => err
p err
exit
end
if url.host && url.port
http = Net::HTTP.new(url.host, url.port)
res = http.head("/")
p res.code
else
p 'Error parsing url'
end

Resources