Skip to content
Permalink
Browse files

Add chat service tests

  • Loading branch information...
nmohoric committed May 12, 2018
1 parent 2035081 commit aaf29df7e65f2f825411851ddb3f4b82b18b6da5
@@ -1,14 +1,14 @@
defmodule Cog.Command.Service.Chat do
require Logger

alias Cog.Chat.Adapter

def send_message(destination, message) do
{:ok, provider} = Cog.Util.Misc.chat_provider_module

case build_target(provider, destination) do
{:ok, target} ->
Cog.Chat.Adapter.send("slack", target, message, %{originating_room_id: target.id})
Cog.Chat.Adapter.send(provider, target, message, %{originating_room_id: target.id})
{:error, reason} ->
Logger.debug("Invalid message destination: #{inspect destination}")
{:error, reason}
@@ -17,19 +17,19 @@ defmodule Cog.Command.Service.Chat do

defp build_target(provider, "@" <> handle) do
case Adapter.lookup_user(provider, handle) do
{:ok, %{id: user_id}} ->
{:ok, %{id: user_id}} ->
{:ok, %{provider: provider, id: user_id, name: handle, is_dm: true}}
_ ->
{:error, :unknown_handle}
end
end
defp build_target(provider, "#" <> room) do
case Adapter.lookup_room(provider, name: "##{room}") do
{:ok, %{id: room_id}} ->
{:ok, %{id: room_id}} ->
{:ok, %{provider: provider, id: room_id, name: room, is_dm: false}}
_ ->
{:error, :unknown_room}
end
end
end
defp build_target(_provider, _destination) do
{:error, :invalid_destination}
@@ -0,0 +1,50 @@
defmodule Cog.V1.ChatServiceControllerTest do
use Cog.ConnCase

@moduletag :services
@endpoint Cog.ServiceEndpoint

@path "/v1/services/chat/1.0.0"

alias Cog.Command.Service.Tokens

setup do
# This makes the test process look like a pipeline executor,
# because the token will be registered to it.
token = Tokens.new
conn = tokened_connection(token)
{:ok, [conn: conn]}
end

defp tokened_connection(token) do
build_conn()
|> Plug.Conn.put_req_header("content-type", "application/json")
|> Plug.Conn.put_req_header("authorization", "pipeline #{token}")
end

test "requests without a token are denied" do
conn = post(build_conn(), @path <> "/send_message")
assert response(conn, 401)
end

test "sending message to an unknown user is an error", %{conn: conn} do
conn = post(conn, @path <> "/send_message", Poison.encode!(%{destination: "@fake_user", message: "taco"}))
assert %{"error" => "Unable to find chat user for @fake_user"} == json_response(conn, 404)
end

test "sending message to an unknown room is an error", %{conn: conn} do
conn = post(conn, @path <> "/send_message", Poison.encode!(%{destination: "#fake_room", message: "taco"}))
assert %{"error" => "Unable to find chat room for #fake_room"} == json_response(conn, 404)
end

test "sending message to an invalid destination is an error", %{conn: conn} do
conn = post(conn, @path <> "/send_message", Poison.encode!(%{destination: "definitely_fake", message: "taco"}))
assert %{"error" => "Invalid chat destination URI definitely_fake"} == json_response(conn, 404)
end

test "sending message to a good message results in a success", %{conn: conn} do
conn = post(conn, @path <> "/send_message", Poison.encode!(%{destination: "#ci_bot_testing", message: "taco"}))
assert %{"status" => "sent"} == json_response(conn, 200)
end

end
@@ -28,6 +28,7 @@ defmodule Cog.Chat.Test.Provider do
provider: @provider_name,
email: handle}
end
def lookup_user("fake_user"), do: {:error, :unknown_user}
def lookup_user(handle) do
%Cog.Chat.User{id: handle,
first_name: handle,
@@ -49,6 +50,7 @@ defmodule Cog.Chat.Test.Provider do
provider: @provider_name,
is_dm: false}
end
def lookup_room({:name, "#fake_room"}), do: {:error, :unknown_room}
def lookup_room({:name, name}) do
%Cog.Chat.Room{id: name,
name: name,

0 comments on commit aaf29df

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.