From 1bfac92a35eb752c4ed6cc4a510d11786d1a01c8 Mon Sep 17 00:00:00 2001 From: Steven Blowers Date: Tue, 22 Nov 2016 19:39:45 +0000 Subject: finishing off using meck. had to delete doctests, as they don't work with meck --- config/config.exs | 2 - config/test.exs | 2 - lib/http_client/in_memory.ex | 117 ------------------------------------------- lib/zendex/user.ex | 113 +++-------------------------------------- test/zendex/search_test.exs | 2 +- test/zendex/user_test.exs | 49 ++++++++++++++++-- 6 files changed, 53 insertions(+), 232 deletions(-) delete mode 100644 lib/http_client/in_memory.ex 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 25f1def..0000000 --- a/lib/http_client/in_memory.ex +++ /dev/null @@ -1,117 +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/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/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/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/test/zendex/search_test.exs b/test/zendex/search_test.exs index d1e2851..a5b6405 100644 --- a/test/zendex/search_test.exs +++ b/test/zendex/search_test.exs @@ -1,5 +1,5 @@ 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")} diff --git a/test/zendex/user_test.exs b/test/zendex/user_test.exs index f61ef13..5d07c73 100644 --- a/test/zendex/user_test.exs +++ b/test/zendex/user_test.exs @@ -1,14 +1,26 @@ 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(@base_url, "User1", "pass")] + end + + setup_all do + :meck.new(HTTPoison) + on_exit fn -> :meck.unload end + :ok end test "list users", context do expected = "users" + + stub = fn("#{@base_url}/api/v2/users.json", _) -> + %HTTPoison.Response{body: Poison.encode!(expected)} + end + :meck.expect(HTTPoison, :get!, stub) + actual = Zendex.User.list(context[:conn]) assert expected == actual @@ -49,6 +61,12 @@ defmodule Zendex.UserTest do "user_fields" => %{"customer_complaint" => nil}, "tags" => [], "updated_at" => "2016-10-28T21:08:23Z"}} + + 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(context[:conn], 295204) assert expected == actual @@ -57,6 +75,12 @@ defmodule Zendex.UserTest do test "showing many users", context do expected = %{"users" => [%{"id" => 6, "name" => "Kiki Segal"}, %{"id" => 67, "name" => "Sarpedon Baumgartner"}]} + + 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(context[:conn], [6,67]) assert expected == actual @@ -73,6 +97,12 @@ defmodule Zendex.UserTest do "topic_comments" => 116, "topics" => 5, "votes" => 2001}} + + 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(context[:conn], 649267) assert expected == actual @@ -80,6 +110,13 @@ defmodule Zendex.UserTest do test "creating a user", context do expected = %{"user" => %{"id" => 1234, "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(context[:conn], %{user: %{name: "Roger", email: "roger@dodger.com"}}) assert expected == actual @@ -120,6 +157,12 @@ defmodule Zendex.UserTest do "user_fields" => %{"customer_complaint" => nil}, "tags" => [], "updated_at" => "2016-10-28T21:08:23Z"}} + + 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(context[:conn], 49043) assert expected == actual -- cgit v1.2.1