aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Blowers <shdblowers@users.noreply.github.com>2016-11-22 19:54:09 (GMT)
committerGitHub <noreply@github.com>2016-11-22 19:54:09 (GMT)
commit088cd593dfa638655bb77923197a50e1c4a7a336 (patch)
treed8562b66308c0dbedbf667b91c20678031343c49
parent5c296447ad55a5e3f3e8503ed19befe106e3261b (diff)
parent5473dacfe2da5ca3dbe38360b49be7f1611d092b (diff)
downloadzendex-088cd593dfa638655bb77923197a50e1c4a7a336.tar.gz
zendex-088cd593dfa638655bb77923197a50e1c4a7a336.tar.xz
Merge pull request #2 from shdblowers/meck
Meck
-rw-r--r--config/config.exs2
-rw-r--r--config/test.exs2
-rw-r--r--lib/http_client/in_memory.ex138
-rw-r--r--lib/zendex/search.ex3
-rw-r--r--lib/zendex/ticket.ex5
-rw-r--r--lib/zendex/user.ex113
-rw-r--r--mix.exs1
-rw-r--r--mix.lock23
-rw-r--r--test/zendex/search_test.exs29
-rw-r--r--test/zendex/ticket_test.exs36
-rw-r--r--test/zendex/user_test.exs73
11 files changed, 131 insertions, 294 deletions
diff --git a/config/config.exs b/config/config.exs
index dd0aa56..c9c59bb 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -1,5 +1,3 @@
use Mix.Config
-config :zendex, :http_client, HTTPoison
-
import_config "#{Mix.env}.exs"
diff --git a/config/test.exs b/config/test.exs
index b2a3ed8..d2d855e 100644
--- a/config/test.exs
+++ b/config/test.exs
@@ -1,3 +1 @@
use Mix.Config
-
-config :zendex, :http_client, Zendex.HttpClient.InMemory
diff --git a/lib/http_client/in_memory.ex b/lib/http_client/in_memory.ex
deleted file mode 100644
index 4c43211..0000000
--- a/lib/http_client/in_memory.ex
+++ /dev/null
@@ -1,138 +0,0 @@
-defmodule Zendex.HttpClient.InMemory do
- @moduledoc """
- Allows testing of the Zendex project by mocking out calls to an actual Zendesk
- API.
- """
-
- @base_url "http://test.zendesk.com"
-
- def get!(@base_url <> "/api/v2/tickets.json",
- [{"Authorization", _authentication}]) do
- fake_response("ticket")
- end
-
- def get!(@base_url <> "/api/v2/search.json?query=requester%3AJimbob+type%3Aticket",
- [{"Authorization", _authentication}]) do
- fake_response(["Jimbob Ticket 1", "Jimbob Ticket 2"])
- end
-
- def get!(@base_url <> "/api/v2/search.json?query=requester%3AReginald+type%3Aticket&sort_by=created_at&sort_order=desc",
- [{"Authorization", _authentication}]) do
- fake_response(["Reginald Ticket 1", "Reginald Ticket 2"])
- end
-
- def get!(@base_url <> "/api/v2/users.json",
- [{"Authorization", _authentication}]) do
- fake_response("users")
- end
-
- def get!("#{@base_url}/api/v2/users/295204.json",
- [{"Authorization", _authentication}]) do
- fake_response(%{"user" => %{"ticket_restriction" => nil,
- "chat_only" => false,
- "shared_phone_number" => nil,
- "notes" => "",
- "phone" => nil,
- "organization_id" => 11129520411,
- "last_login_at" => "2016-10-28T21:08:23Z",
- "moderator" => true,
- "shared" => false,
- "id" => 295204,
- "role" => "admin",
- "external_id" => nil,
- "shared_agent" => false,
- "photo" => nil,
- "verified" => true,
- "active" => true,
- "locale_id" => 1,
- "suspended" => false,
- "created_at" => "2015-05-28T09:12:45Z",
- "name" => "Nikolao Aikema",
- "restricted_agent" => false,
- "locale" => "en-US",
- "details" => "",
- "alias" => nil,
- "url" => "https://test.zendesk.com/api/v2/users/295204.json",
- "custom_role_id" => nil,
- "email" => "nikolao.aikema@test.com",
- "signature" => nil,
- "two_factor_auth_enabled" => nil,
- "time_zone" => "London",
- "only_private_comments" => false,
- "user_fields" => %{"customer_complaint" => nil},
- "tags" => [],
- "updated_at" => "2016-10-28T21:08:23Z"}})
- end
-
- def get!("#{@base_url}/api/v2/users/show_many.json?ids=6,67",
- [{"Authorization", _authentication}]) do
- fake_response(%{users: [%{id: 6, name: "Kiki Segal"},
- %{id: 67, name: "Sarpedon Baumgartner"}]})
- end
-
- def get!("#{@base_url}/api/v2/users/649267/related.json",
- [{"Authorization", _authentication}]) do
- fake_response(%{"user_related" => %{"assigned_tickets" => 12,
- "ccd_tickets" => 5,
- "entry_subscriptions" => 1,
- "forum_subscriptions" => 3,
- "organization_subscriptions" => 1,
- "requested_tickets" => 7,
- "subscriptions" => 6,
- "topic_comments" => 116,
- "topics" => 5,
- "votes" => 2001}})
- end
-
- def post!(@base_url <> "/api/v2/tickets.json",
- "{\"ticket\":{}}",
- [{"Authorization", _authentication}, {"Content-Type", "application/json"}]) do
- fake_response("Ticket created successfully!")
- end
-
- def post!(@base_url <> "/api/v2/users.json",
- "{\"user\":{\"name\":\"Roger\",\"email\":\"roger@dodger.com\"}}",
- [{"Authorization", _authentication}, {"Content-Type", "application/json"}]) do
- fake_response(%{user: %{id: 1234, name: "Roger", email: "roger@dodger.com"}})
- end
-
- def delete!("#{@base_url}/api/v2/users/49043.json",
- [{"Authorization", _authentication}]) do
- fake_response(%{"user" => %{"ticket_restriction" => nil,
- "chat_only" => false,
- "shared_phone_number" => nil,
- "notes" => "",
- "phone" => nil,
- "organization_id" => 149043,
- "last_login_at" => "2016-10-28T21:08:23Z",
- "moderator" => true,
- "shared" => false,
- "id" => 49043,
- "role" => "admin",
- "external_id" => nil,
- "shared_agent" => false,
- "photo" => nil,
- "verified" => true,
- "active" => false,
- "locale_id" => 1,
- "suspended" => false,
- "created_at" => "2015-05-28T09:12:45Z",
- "name" => "Rian Hawkins",
- "restricted_agent" => false,
- "locale" => "en-US",
- "details" => "",
- "alias" => nil,
- "url" => "https://test.zendesk.com/api/v2/users/49043.json",
- "custom_role_id" => nil,
- "email" => "rian.hawkins@test.com",
- "signature" => nil,
- "two_factor_auth_enabled" => nil,
- "time_zone" => "London",
- "only_private_comments" => false,
- "user_fields" => %{"customer_complaint" => nil},
- "tags" => [],
- "updated_at" => "2016-10-28T21:08:23Z"}})
- end
-
- defp fake_response(body), do: %{body: Poison.encode!(body)}
-end
diff --git a/lib/zendex/search.ex b/lib/zendex/search.ex
index ec9ea28..f920666 100644
--- a/lib/zendex/search.ex
+++ b/lib/zendex/search.ex
@@ -6,7 +6,6 @@ defmodule Zendex.Search do
alias Zendex.CommonHelpers
@url "/api/v2/search.json?query="
- @http_client Application.get_env(:zendex, :http_client)
@doc """
Search Zendesk.
@@ -18,7 +17,7 @@ defmodule Zendex.Search do
sort_string = create_sort_string(sort_by, sort_order)
"#{connection.base_url}#{@url}#{search_string}#{sort_string}"
- |> @http_client.get!(CommonHelpers.get_headers(connection.authentication))
+ |> HTTPoison.get!(CommonHelpers.get_headers(connection.authentication))
|> CommonHelpers.decode_response
end
diff --git a/lib/zendex/ticket.ex b/lib/zendex/ticket.ex
index 9232d6e..2aaf7ae 100644
--- a/lib/zendex/ticket.ex
+++ b/lib/zendex/ticket.ex
@@ -7,7 +7,6 @@ defmodule Zendex.Ticket do
alias Zendex.CommonHelpers
@url "/api/v2/tickets.json"
- @http_client Application.get_env(:zendex, :http_client)
@doc """
List all tickets.
@@ -15,7 +14,7 @@ defmodule Zendex.Ticket do
@spec list(Zendex.Connection.t) :: map
def list(connection) do
"#{connection.base_url}#{@url}"
- |> @http_client.get!(CommonHelpers.get_headers(connection.authentication))
+ |> HTTPoison.get!(CommonHelpers.get_headers(connection.authentication))
|> CommonHelpers.decode_response
end
@@ -25,7 +24,7 @@ defmodule Zendex.Ticket do
@spec create(Zendex.Connection.t, map) :: map
def create(connection, ticket) do
"#{connection.base_url}#{@url}"
- |> @http_client.post!(Poison.encode!(ticket),
+ |> HTTPoison.post!(Poison.encode!(ticket),
CommonHelpers.get_headers(connection.authentication,
%{content_type: :json}))
|> CommonHelpers.decode_response
diff --git a/lib/zendex/user.ex b/lib/zendex/user.ex
index 9112b7f..719d2ab 100644
--- a/lib/zendex/user.ex
+++ b/lib/zendex/user.ex
@@ -6,7 +6,6 @@ defmodule Zendex.User do
alias Zendex.CommonHelpers
@url "/api/v2/users"
- @http_client Application.get_env(:zendex, :http_client)
@doc """
List all users.
@@ -14,58 +13,17 @@ defmodule Zendex.User do
@spec list(Zendex.Connection.t) :: map
def list(connection) do
"#{connection.base_url}#{@url}.json"
- |> @http_client.get!(CommonHelpers.get_headers(connection.authentication))
+ |> HTTPoison.get!(CommonHelpers.get_headers(connection.authentication))
|> CommonHelpers.decode_response
end
@doc """
Show a specific user, given their id.
-
- ## Examples
-
- iex> conn = Zendex.Connection.setup("http://test.zendesk.com", "ZendeskUser", "Password1")
- %{authentication: "WmVuZGVza1VzZXI6UGFzc3dvcmQx", base_url: "http://test.zendesk.com"}
- iex> Zendex.User.show(conn, 295204)
- %{"user" => %{"ticket_restriction" => nil,
- "chat_only" => false,
- "shared_phone_number" => nil,
- "notes" => "",
- "phone" => nil,
- "organization_id" => 11129520411,
- "last_login_at" => "2016-10-28T21:08:23Z",
- "moderator" => true,
- "shared" => false,
- "id" => 295204,
- "role" => "admin",
- "external_id" => nil,
- "shared_agent" => false,
- "photo" => nil,
- "verified" => true,
- "active" => true,
- "locale_id" => 1,
- "suspended" => false,
- "created_at" => "2015-05-28T09:12:45Z",
- "name" => "Nikolao Aikema",
- "restricted_agent" => false,
- "locale" => "en-US",
- "details" => "",
- "alias" => nil,
- "url" => "https://test.zendesk.com/api/v2/users/295204.json",
- "custom_role_id" => nil,
- "email" => "nikolao.aikema@test.com",
- "signature" => nil,
- "two_factor_auth_enabled" => nil,
- "time_zone" => "London",
- "only_private_comments" => false,
- "user_fields" => %{"customer_complaint" => nil},
- "tags" => [],
- "updated_at" => "2016-10-28T21:08:23Z"}}
-
"""
@spec show(Zendex.Connection.t, integer) :: map
def show(connection, id) do
"#{connection.base_url}#{@url}/#{id}.json"
- |> @http_client.get!(CommonHelpers.get_headers(connection.authentication))
+ |> HTTPoison.get!(CommonHelpers.get_headers(connection.authentication))
|> CommonHelpers.decode_response
end
@@ -77,34 +35,17 @@ defmodule Zendex.User do
ids = Enum.join(ids, ",")
"#{connection.base_url}#{@url}/show_many.json?ids=#{ids}"
- |> @http_client.get!(CommonHelpers.get_headers(connection.authentication))
+ |> HTTPoison.get!(CommonHelpers.get_headers(connection.authentication))
|> CommonHelpers.decode_response
end
@doc """
Show information relating to the user, example: number of assigned tickets.
-
- ## Examples
-
- iex> conn = Zendex.Connection.setup("http://test.zendesk.com", "ZendeskUser", "Password1")
- %{authentication: "WmVuZGVza1VzZXI6UGFzc3dvcmQx", base_url: "http://test.zendesk.com"}
- iex> Zendex.User.related_information(conn, 649267)
- %{"user_related" => %{"assigned_tickets" => 12,
- "ccd_tickets" => 5,
- "entry_subscriptions" => 1,
- "forum_subscriptions" => 3,
- "organization_subscriptions" => 1,
- "requested_tickets" => 7,
- "subscriptions" => 6,
- "topic_comments" => 116,
- "topics" => 5,
- "votes" => 2001}}
-
"""
@spec related_information(Zendex.Connection.t, integer) :: map
def related_information(connection, id) do
"#{connection.base_url}#{@url}/#{id}/related.json"
- |> @http_client.get!(CommonHelpers.get_headers(connection.authentication))
+ |> HTTPoison.get!(CommonHelpers.get_headers(connection.authentication))
|> CommonHelpers.decode_response
end
@@ -114,7 +55,7 @@ defmodule Zendex.User do
@spec create(Zendex.Connection.t, map) :: map
def create(connection, user) do
"#{connection.base_url}#{@url}.json"
- |> @http_client.post!(Poison.encode!(user),
+ |> HTTPoison.post!(Poison.encode!(user),
CommonHelpers.get_headers(connection.authentication,
%{content_type: :json}))
|> CommonHelpers.decode_response
@@ -122,53 +63,11 @@ defmodule Zendex.User do
@doc """
Delete a user.
-
- ## Examples
-
- iex> conn = Zendex.Connection.setup("http://test.zendesk.com", "ZendeskUser", "Password1")
- %{authentication: "WmVuZGVza1VzZXI6UGFzc3dvcmQx", base_url: "http://test.zendesk.com"}
- iex> Zendex.User.delete(conn, 49043)
- %{"user" => %{"ticket_restriction" => nil,
- "chat_only" => false,
- "shared_phone_number" => nil,
- "notes" => "",
- "phone" => nil,
- "organization_id" => 149043,
- "last_login_at" => "2016-10-28T21:08:23Z",
- "moderator" => true,
- "shared" => false,
- "id" => 49043,
- "role" => "admin",
- "external_id" => nil,
- "shared_agent" => false,
- "photo" => nil,
- "verified" => true,
- "active" => false,
- "locale_id" => 1,
- "suspended" => false,
- "created_at" => "2015-05-28T09:12:45Z",
- "name" => "Rian Hawkins",
- "restricted_agent" => false,
- "locale" => "en-US",
- "details" => "",
- "alias" => nil,
- "url" => "https://test.zendesk.com/api/v2/users/49043.json",
- "custom_role_id" => nil,
- "email" => "rian.hawkins@test.com",
- "signature" => nil,
- "two_factor_auth_enabled" => nil,
- "time_zone" => "London",
- "only_private_comments" => false,
- "user_fields" => %{"customer_complaint" => nil},
- "tags" => [],
- "updated_at" => "2016-10-28T21:08:23Z"}}
-
-
"""
@spec show(Zendex.Connection.t, integer) :: map
def delete(connection, id) do
"#{connection.base_url}#{@url}/#{id}.json"
- |> @http_client.delete!(CommonHelpers.get_headers(connection.authentication))
+ |> HTTPoison.delete!(CommonHelpers.get_headers(connection.authentication))
|> CommonHelpers.decode_response
end
diff --git a/mix.exs b/mix.exs
index 245d700..30f508f 100644
--- a/mix.exs
+++ b/mix.exs
@@ -24,6 +24,7 @@ defmodule Zendex.Mixfile do
defp deps do
[{:httpoison, "~> 0.9"},
{:poison, "~> 2.2"},
+ {:meck, "~> 0.8.4", only: :test},
{:ex_doc, ">= 0.0.0", only: :dev},
{:credo, "~> 0.4", only: :dev},
{:dialyxir, "~> 0.3", only: :dev},
diff --git a/mix.lock b/mix.lock
index 123d113..6267e20 100644
--- a/mix.lock
+++ b/mix.lock
@@ -1,16 +1,17 @@
-%{"bunt": {:hex, :bunt, "0.1.6", "5d95a6882f73f3b9969fdfd1953798046664e6f77ec4e486e6fafc7caad97c6f", [:mix], []},
+%{"bunt": {:hex, :bunt, "0.1.6", "5d95a6882f73f3b9969fdfd1953798046664e6f77ec4e486e6fafc7caad97c6f", [], []},
"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], []},
- "credo": {:hex, :credo, "0.5.1", "2395862b94628cadf0f5c68975c1440393f425b955f1e70ce1aea267e00187a1", [:mix], [{:bunt, "~> 0.1.6", [hex: :bunt, optional: false]}]},
- "dialyxir": {:hex, :dialyxir, "0.3.5", "eaba092549e044c76f83165978979f60110dc58dd5b92fd952bf2312f64e9b14", [:mix], []},
- "earmark": {:hex, :earmark, "1.0.2", "a0b0904d74ecc14da8bd2e6e0248e1a409a2bc91aade75fcf428125603de3853", [:mix], []},
+ "credo": {:hex, :credo, "0.5.3", "0c405b36e7651245a8ed63c09e2d52c2e2b89b6d02b1570c4d611e0fcbecf4a2", [:mix], [{:bunt, "~> 0.1.6", [hex: :bunt, optional: false]}]},
+ "dialyxir": {:hex, :dialyxir, "0.4.0", "53ac3014bb4aef647728a697052b4db3a84c6742de7aab0e0a1c863ea274007b", [:mix], []},
+ "earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.14.3", "e61cec6cf9731d7d23d254266ab06ac1decbb7651c3d1568402ec535d387b6f7", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
"excoveralls": {:hex, :excoveralls, "0.5.7", "5d26e4a7cdf08294217594a1b0643636accc2ad30e984d62f1d166f70629ff50", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
- "hackney": {:hex, :hackney, "1.6.3", "d489d7ca2d4323e307bedc4bfe684323a7bf773ecfd77938f3ee8074e488e140", [:rebar3, :mix], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
- "httpoison": {:hex, :httpoison, "0.9.2", "a211a8e87403a043c41218e64df250d321f236ac57f786c6a0ccf3e9e817c819", [:mix], [{:hackney, "~> 1.6.0", [hex: :hackney, optional: false]}]},
- "idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
+ "hackney": {:hex, :hackney, "1.6.3", "d489d7ca2d4323e307bedc4bfe684323a7bf773ecfd77938f3ee8074e488e140", [:mix, :rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
+ "httpoison": {:hex, :httpoison, "0.10.0", "4727b3a5e57e9a4ff168a3c2883e20f1208103a41bccc4754f15a9366f49b676", [:mix], [{:hackney, "~> 1.6.3", [hex: :hackney, optional: false]}]},
+ "idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [], []},
"jsx": {:hex, :jsx, "2.8.0", "749bec6d205c694ae1786d62cea6cc45a390437e24835fd16d12d74f07097727", [:mix, :rebar], []},
- "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
- "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
- "poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
- "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:rebar, :make], []}}
+ "meck": {:hex, :meck, "0.8.4", "59ca1cd971372aa223138efcf9b29475bde299e1953046a0c727184790ab1520", [:make, :rebar], []},
+ "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], []},
+ "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [], []},
+ "poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [], []},
+ "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}}
diff --git a/test/zendex/search_test.exs b/test/zendex/search_test.exs
index a7102b6..bb1f358 100644
--- a/test/zendex/search_test.exs
+++ b/test/zendex/search_test.exs
@@ -1,23 +1,38 @@
defmodule Zendex.SearchTest do
- use ExUnit.Case, async: true
+ use ExUnit.Case, async: false
setup do
- [conn: Zendex.Connection.setup("http://test.zendesk.com", "User", "Passw")]
+ %{conn: Zendex.Connection.setup("http://test.zendesk.com", "User", "Passw")}
end
- test "doing a search", context do
+ setup_all do
+ :meck.new(HTTPoison)
+ on_exit fn -> :meck.unload end
+ :ok
+ end
+
+ test "doing a search", %{conn: conn} do
expected = ["Jimbob Ticket 1", "Jimbob Ticket 2"]
- actual = Zendex.Search.query(context[:conn],
- %{type: "ticket", requester: "Jimbob"})
+ stub = fn("http://test.zendesk.com/api/v2/search.json?query=requester%3AJimbob+type%3Aticket", _headers) ->
+ %HTTPoison.Response{body: Poison.encode!(expected)}
+ end
+ :meck.expect(HTTPoison, :get!, stub)
+
+ actual = Zendex.Search.query(conn, %{type: "ticket", requester: "Jimbob"})
assert actual == expected
end
- test "doing a search with sorting", context do
+ test "doing a search with sorting", %{conn: conn} do
expected = ["Reginald Ticket 1", "Reginald Ticket 2"]
- actual = Zendex.Search.query(context[:conn],
+ stub = fn("http://test.zendesk.com/api/v2/search.json?query=requester%3AReginald+type%3Aticket&sort_by=created_at&sort_order=desc", _headers) ->
+ %HTTPoison.Response{body: Poison.encode!(expected)}
+ end
+ :meck.expect(HTTPoison, :get!, stub)
+
+ actual = Zendex.Search.query(conn,
%{type: "ticket", requester: "Reginald"},
"created_at",
"desc")
diff --git a/test/zendex/ticket_test.exs b/test/zendex/ticket_test.exs
index 6bc4436..2b5a3ce 100644
--- a/test/zendex/ticket_test.exs
+++ b/test/zendex/ticket_test.exs
@@ -1,16 +1,38 @@
defmodule Zendex.TicketTest do
- use ExUnit.Case, async: true
+ use ExUnit.Case, async: false
setup do
- [conn: Zendex.Connection.setup("http://test.zendesk.com", "User", "Passw")]
+ %{conn: Zendex.Connection.setup("http://test.zendesk.com", "User", "Passw")}
end
- test "getting tickets", context do
- assert "ticket" == Zendex.Ticket.list(context[:conn])
+ setup_all do
+ :meck.new(HTTPoison)
+ on_exit fn -> :meck.unload end
+ :ok
end
- test "creating a ticket", context do
- assert "Ticket created successfully!" ==
- Zendex.Ticket.create(context[:conn], %{"ticket": %{}})
+ test "getting tickets", %{conn: conn} do
+ expected = "ticket"
+
+ stub = fn("http://test.zendesk.com/api/v2/tickets.json", _headers) ->
+ %HTTPoison.Response{body: Poison.encode!(expected)}
+ end
+ :meck.expect(HTTPoison, :get!, stub)
+
+ assert expected == Zendex.Ticket.list(conn)
+ end
+
+ test "creating a ticket", %{conn: conn} do
+ expected = "Ticket created successfully!"
+
+ stub = fn("http://test.zendesk.com/api/v2/tickets.json",
+ "{\"ticket\":{\"title\":\"HELP!\"}}",
+ _headers) ->
+ %HTTPoison.Response{body: Poison.encode!(expected)}
+ end
+ :meck.expect(HTTPoison, :post!, stub)
+
+ assert expected ==
+ Zendex.Ticket.create(conn, %{"ticket": %{"title": "HELP!"}})
end
end
diff --git a/test/zendex/user_test.exs b/test/zendex/user_test.exs
index f61ef13..a1cee50 100644
--- a/test/zendex/user_test.exs
+++ b/test/zendex/user_test.exs
@@ -1,20 +1,32 @@
defmodule Zendex.UserTest do
- use ExUnit.Case, async: true
+ use ExUnit.Case, async: false
- doctest Zendex.User
+ @base_url "http://test.zendesk.com"
setup do
- [conn: Zendex.Connection.setup("http://test.zendesk.com", "User1", "pass")]
+ %{conn: Zendex.Connection.setup("http://test.zendesk.com", "User", "Passw")}
end
- test "list users", context do
+ setup_all do
+ :meck.new(HTTPoison)
+ on_exit fn -> :meck.unload end
+ :ok
+ end
+
+ test "list users", %{conn: conn} do
expected = "users"
- actual = Zendex.User.list(context[:conn])
+
+ stub = fn("#{@base_url}/api/v2/users.json", _) ->
+ %HTTPoison.Response{body: Poison.encode!(expected)}
+ end
+ :meck.expect(HTTPoison, :get!, stub)
+
+ actual = Zendex.User.list(conn)
assert expected == actual
end
- test "showing a user", context do
+ test "showing a user", %{conn: conn} do
expected = %{"user" => %{"ticket_restriction" => nil,
"chat_only" => false,
"shared_phone_number" => nil,
@@ -49,20 +61,32 @@ defmodule Zendex.UserTest do
"user_fields" => %{"customer_complaint" => nil},
"tags" => [],
"updated_at" => "2016-10-28T21:08:23Z"}}
- actual = Zendex.User.show(context[:conn], 295204)
+
+ stub = fn("#{@base_url}/api/v2/users/295204.json", _) ->
+ %HTTPoison.Response{body: Poison.encode!(expected)}
+ end
+ :meck.expect(HTTPoison, :get!, stub)
+
+ actual = Zendex.User.show(conn, 295204)
assert expected == actual
end
- test "showing many users", context do
+ test "showing many users", %{conn: conn} do
expected = %{"users" => [%{"id" => 6, "name" => "Kiki Segal"},
%{"id" => 67, "name" => "Sarpedon Baumgartner"}]}
- actual = Zendex.User.show_many(context[:conn], [6,67])
+
+ stub = fn("#{@base_url}/api/v2/users/show_many.json?ids=6,67", _) ->
+ %HTTPoison.Response{body: Poison.encode!(expected)}
+ end
+ :meck.expect(HTTPoison, :get!, stub)
+
+ actual = Zendex.User.show_many(conn, [6,67])
assert expected == actual
end
- test "getting related info on a user", context do
+ test "getting related info on a user", %{conn: conn} do
expected = %{"user_related" => %{"assigned_tickets" => 12,
"ccd_tickets" => 5,
"entry_subscriptions" => 1,
@@ -73,19 +97,32 @@ defmodule Zendex.UserTest do
"topic_comments" => 116,
"topics" => 5,
"votes" => 2001}}
- actual = Zendex.User.related_information(context[:conn], 649267)
+
+ stub = fn("#{@base_url}/api/v2/users/649267/related.json", _) ->
+ %HTTPoison.Response{body: Poison.encode!(expected)}
+ end
+ :meck.expect(HTTPoison, :get!, stub)
+
+ actual = Zendex.User.related_information(conn, 649267)
assert expected == actual
end
- test "creating a user", context do
+ test "creating a user", %{conn: conn} do
expected = %{"user" => %{"id" => 1234, "name" => "Roger", "email" => "roger@dodger.com"}}
- actual = Zendex.User.create(context[:conn], %{user: %{name: "Roger", email: "roger@dodger.com"}})
+
+ stub = fn("#{@base_url}/api/v2/users.json", _, _) ->
+ %HTTPoison.Response{body: Poison.encode!(expected)}
+ end
+ :meck.expect(HTTPoison, :post!, stub)
+
+
+ actual = Zendex.User.create(conn, %{user: %{name: "Roger", email: "roger@dodger.com"}})
assert expected == actual
end
- test "deleting a user", context do
+ test "deleting a user", %{conn: conn} do
expected = %{"user" => %{"ticket_restriction" => nil,
"chat_only" => false,
"shared_phone_number" => nil,
@@ -120,7 +157,13 @@ defmodule Zendex.UserTest do
"user_fields" => %{"customer_complaint" => nil},
"tags" => [],
"updated_at" => "2016-10-28T21:08:23Z"}}
- actual = Zendex.User.delete(context[:conn], 49043)
+
+ stub = fn("#{@base_url}/api/v2/users/49043.json", _) ->
+ %HTTPoison.Response{body: Poison.encode!(expected)}
+ end
+ :meck.expect(HTTPoison, :delete!, stub)
+
+ actual = Zendex.User.delete(conn, 49043)
assert expected == actual
end