|
| 1 | +defmodule Cog.V1.ChatServiceControllerTest do |
| 2 | + use Cog.ConnCase |
| 3 | + |
| 4 | + @moduletag :services |
| 5 | + @endpoint Cog.ServiceEndpoint |
| 6 | + |
| 7 | + @path "/v1/services/chat/1.0.0" |
| 8 | + |
| 9 | + alias Cog.Command.Service.Tokens |
| 10 | + |
| 11 | + setup do |
| 12 | + # This makes the test process look like a pipeline executor, |
| 13 | + # because the token will be registered to it. |
| 14 | + token = Tokens.new |
| 15 | + conn = tokened_connection(token) |
| 16 | + {:ok, [conn: conn]} |
| 17 | + end |
| 18 | + |
| 19 | + defp tokened_connection(token) do |
| 20 | + build_conn() |
| 21 | + |> Plug.Conn.put_req_header("content-type", "application/json") |
| 22 | + |> Plug.Conn.put_req_header("authorization", "pipeline #{token}") |
| 23 | + end |
| 24 | + |
| 25 | + test "requests without a token are denied" do |
| 26 | + conn = post(build_conn(), @path <> "/send_message") |
| 27 | + assert response(conn, 401) |
| 28 | + end |
| 29 | + |
| 30 | + test "sending message to an unknown user is an error", %{conn: conn} do |
| 31 | + conn = post(conn, @path <> "/send_message", Poison.encode!(%{destination: "@fake_user", message: "taco"})) |
| 32 | + assert %{"error" => "Unable to find chat user for @fake_user"} == json_response(conn, 404) |
| 33 | + end |
| 34 | + |
| 35 | + test "sending message to an unknown room is an error", %{conn: conn} do |
| 36 | + conn = post(conn, @path <> "/send_message", Poison.encode!(%{destination: "#fake_room", message: "taco"})) |
| 37 | + assert %{"error" => "Unable to find chat room for #fake_room"} == json_response(conn, 404) |
| 38 | + end |
| 39 | + |
| 40 | + test "sending message to an invalid destination is an error", %{conn: conn} do |
| 41 | + conn = post(conn, @path <> "/send_message", Poison.encode!(%{destination: "definitely_fake", message: "taco"})) |
| 42 | + assert %{"error" => "Invalid chat destination URI definitely_fake"} == json_response(conn, 404) |
| 43 | + end |
| 44 | + |
| 45 | + test "sending message to a good message results in a success", %{conn: conn} do |
| 46 | + conn = post(conn, @path <> "/send_message", Poison.encode!(%{destination: "#ci_bot_testing", message: "taco"})) |
| 47 | + assert %{"status" => "sent"} == json_response(conn, 200) |
| 48 | + end |
| 49 | + |
| 50 | +end |
0 commit comments