Skip to content

Commit

Permalink
Initial tests for Nexmo adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
bigkevmcd committed Mar 19, 2018
1 parent e8f63d0 commit d400679
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/sms_blitz.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule SmsBlitz do
alias SmsBlitz.Adapters.{Plivo, Itagg, Twilio, Nexmo}

@spec send_sms(atom, SmsBlitz.Adapter.sms_params) :: SmsBlitz.Adapter.sms_result

@adapters %{
plivo: Plivo,
Expand All @@ -12,6 +11,7 @@ defmodule SmsBlitz do

def adapters(), do: Map.keys(@adapters)

@spec send_sms(atom, SmsBlitz.Adapter.sms_params) :: SmsBlitz.Adapter.sms_result
def send_sms(adapter_key, from: from, to: to, message: message) when is_binary(from) and is_binary(to) and is_binary(message) and is_atom(adapter_key) do

case Map.fetch(@adapters, adapter_key) do
Expand Down
56 changes: 32 additions & 24 deletions lib/sms_blitz/adapters/nexmo.ex
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
defmodule SmsBlitz.Adapters.Nexmo do

@behaviour SmsBlitz.Adapter
@base_uri "https://rest.nexmo.com/sms/json"

def authenticate({key, secret}) do
%{
uri: @base_uri,
uri: @base_uri,
auth: %{
key: key,
key: key,
secret: secret
}
}
end

def send_sms(%{uri: uri, auth: %{key: key, secret: secret}}, from: from, to: to, message: message) when is_binary(from) and is_binary(to) and is_binary(message) do
body = %{
from: from,
to: to,
text: message,
api_key: key,
api_secret: secret
} |> Poison.encode!
@spec send_sms(any, SmsBlitz.Adapter.sms_params()) :: SmsBlitz.Adapter.sms_result()
def send_sms(
%{uri: uri, auth: %{key: key, secret: secret}},
from: from,
to: to,
message: message
)
when is_binary(from) and is_binary(to) and is_binary(message) do
body =
%{
from: from,
to: to,
text: message,
api_key: key,
api_secret: secret
}
|> Poison.encode!()

{:ok, %{headers: headers, body: resp, status_code: status_code}} = HTTPoison.post(
uri,
body,
[{"Content-Type", "application/json"}]
)
HTTPoison.post(uri, body, [{"Content-Type", "application/json"}])
|> handle_response!
end

{:ok, %{"message-count" => msg_count, "messages" => [response_status]}} = Poison.decode(resp)
def handle_response!({:ok, %{headers: headers, body: resp, status_code: 200}}) do
{:ok, %{"message-count" => _, "messages" => [response_status]}} = Poison.decode(resp)

if response_status["status"] == "0" do
%{
id: response_status["message-id"],
id: response_status["message-id"],
result_string: "success",
status_code: response_status["status"]
status_code: response_status["status"]
}
else

{_key, trace_id} = Enum.find(headers, fn
({"X-Nexmo-Trace-Id", value}) -> true
(_) -> false
{_key, trace_id} =
Enum.find(headers, fn
{"X-Nexmo-Trace-Id", _} -> true
_ -> false
end)

%{
id: trace_id,
result_string: response_status["error-text"],
status_code: response_status["status"]
status_code: response_status["status"]
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sms_blitz/adapters/plivo.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SmsBlitz.Adapters.Plivo do

@behaviour SmsBlitz.Adapter
@base_uri "https://api.plivo.com/v1"

def authenticate({user_id, user_token}) do
Expand Down
58 changes: 58 additions & 0 deletions test/adapters/nexmo_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule SmsBlitz.Adapters.NexmoTest do
use ExUnit.Case
alias SmsBlitz.Adapters.Nexmo
import Mock
@config {"key", "secret"}

test "#authenticate" do
expected = %{
auth: %{
key: "key",
secret: "secret"
},
uri: "https://rest.nexmo.com/sms/json"
}

assert Nexmo.authenticate(@config) == expected
end

describe "#send_sms" do
test "failing to send an sms" do
response = %{
"message-count": 1,
messages: [
%{
status: "2",
"error-text": "Missing to param"
}
]
}

headers = [{"X-Nexmo-Trace-Id", "testing"}]

fake_response = %HTTPoison.Response{
status_code: 200,
body: Poison.encode!(response),
headers: headers
}

auth = Nexmo.authenticate(@config)

with_mock HTTPoison, post: fn _, _, _ -> {:ok, fake_response} end do
result =
Nexmo.send_sms(auth, from: "+4412345678910", to: "+4423456789101", message: "Testing")

assert result == %{id: "testing", result_string: "Missing to param", status_code: "2"}

assert called(
HTTPoison.post("https://rest.nexmo.com/sms/json", :_, [
{"Content-Type", "application/json"}
])
)
end
end

test "successful sending" do
end
end
end

0 comments on commit d400679

Please sign in to comment.