Closex.MockClient (closex v2.1.0)
This module is for testing, it allows you to stub requests to CloseIO.
It behaves like the standard client so you can drop it into your code via configuration in your testing environment, but it will return the test objects for each method.
It gets these test objects from json files, for more detail see the test/fixtures/*.json
files.
However, you can override the response of the mock client with json more specific to your domain:
# your_app/config/test.exs
config :yourapp,
closeio_client: Closex.MockClient,
...other configuration...
config :closex,
mock_client_fixtures_dir: Path.join([File.cwd!, "path", "to", "your", "fixtures"])
If you'd like to emulate not finding an object, pass in the Closex.MockClient.not_found_id
value.
For specific examples please see the documentation for the method you're using.
Link to this section Summary
Functions
Callback implementation for Closex.ClientBehaviour.create_contact/2
.
Creates a lead in CloseIO.
Creates an opportunity in CloseIO.
Stubs creation of a task for the given lead with the given text on Close.IO.
Callback implementation for Closex.ClientBehaviour.find_all_opportunities/2
.
Stubs finding leads in CloseIO.
Finds an opportunity in CloseIO.
Gets a lead from CloseIO.
Gets a lead custom field from CloseIO.
Gets the potential statuses of a lead from CloseIO.
Callback implementation for Closex.ClientBehaviour.get_opportunities/1
.
Gets an opportunity from CloseIO.
Gets the potential statuses of an opportunity from CloseIO.
Gets an organization from CloseIO.
Gets the users available in CloseIO.
Sends an email CloseIO.
Callback implementation for Closex.ClientBehaviour.update_contact/3
.
Update a lead in CloseIO.
Update an Opportunity in CloseIO.
Link to this section Functions
create_contact(map, _)
Callback implementation for Closex.ClientBehaviour.create_contact/2
.
create_lead(payload, opts \\ [])
Creates a lead in CloseIO.
Returns {:ok, lead}
examples
Examples
Closex.MockClient.create_lead(%{}) ...contents of test/fixtures/create_lead.json...
create_opportunity(payload, opts \\ [])
Creates an opportunity in CloseIO.
Returns {:ok, opportunity}
examples
Examples
Closex.MockClient.create_opportunity(%{}) ...contents of test/fixtures/create_opportunity.json...
create_task(lead_id, text, params \\ %{}, opts \\ [])
Stubs creation of a task for the given lead with the given text on Close.IO.
Returns {:ok, task} or
(possible errors are lead not found and HTTP timeout on using
not_found_idand
timeout_queryrespectively as
lead_id) By default, the task returned is the contents of
test/fixtures/task.json` but with the lead id and text provided in the arguments.
Any optional parameters permitted in the Close.IO API will be merged into the resulting task and all others will be omitted.
## Examples
> Closex.MockClient.create_task(Closex.MockClient.lead_id(), "I am task", %{})
...contents of test/fixtures/organization.json (with ID lead_id() and text "I am task")...
> Closex.MockClient.create_task(Closex.MockClient.lead_id(), "I am task", %{assigned_to: "busy user", foo: "bar"})
...contents of test/fixtures/organization.json (with ID lead_id(), text "I am task" and assigned_to "busy_user")...
iex> Closex.MockClient.create_task(Closex.MockClient.not_found_id(), "I am task", %{assigned_to: "busy user", foo: "bar"})
{:error, %{"errors" => [], "field-errors" => %{"lead" => "Object does not exist."}}}
iex> Closex.MockClient.create_task(Closex.MockClient.timeout_query(), "I am task", %{assigned_to: "busy user", foo: "bar"})
{:error, %HTTPoison.Error{id: nil, reason: :timeout}}
find_all_opportunities(term, limit \\ 10)
Callback implementation for Closex.ClientBehaviour.find_all_opportunities/2
.
find_leads(search_term, opts \\ [])
Stubs finding leads in CloseIO.
Returns {:ok, leads}
or {:error, error}
tuple.
examples
Examples
By default, this returns the contents of the find_one_lead.json
fixture.
> Closex.MockClient.find_leads("foo")
...contents of test/fixtures/find_one_lead.json...
Specific search results can be returned by storing a response in your
test/fixtures
directly which corresponds to the specific query. It must
be named find_leads_<search>.json
, otherwise we will use default. Note, we
turn spaces into underscores.
> Closex.MockClient.find_leads("some lead")
...contents of test/fixtures/find_leads_some_lead.json
Additional helper queries available are:
iex> Closex.MockClient.find_leads(Closex.MockClient.not_found_query())
{:ok, %{"data" => [], "has_more" => false, "total_results" => 0}}
> Closex.MockClient.find_leads(Closex.MockClient.multiple_results_query())
...contents of test/fixtures/find_multiple_leads.json...
iex> Closex.MockClient.find_leads(Closex.MockClient.timeout_query())
{:error, %HTTPoison.Error{id: nil, reason: :timeout}}
find_opportunities(search_term, opts \\ [])
Finds an opportunity in CloseIO.
Returns {:ok, opportunities}
.
You can hand in any search term you like and it will return an example search which finds one example lead.
You can also search for something you are not expecting to find with our not_found_id. This will provide the empty result set in find_no_opportunities.json.
We have provided an example search term to use when the search term doesn't matter to you.
examples
Examples
Closex.MockClient.find_opportunities("foo") ...contents of test/fixtures/find_one_opportunity.json...
iex> Closex.MockClient.find_opportunities(Closex.MockClient.not_found_query()) {:ok, %{"data" => [], "has_more" => false, "total_results" => 0}}
Closex.MockClient.find_opportunities(Closex.MockClient.multiple_results_query()) ...contents of test/fixtures/find_multiple_opportunities.json...
get_lead(id, opts \\ [])
Gets a lead from CloseIO.
Returns {:ok, lead}
.
You can hand in any lead id you like and it will return an example lead with that id.
We have provided an example id to use when the id doesn't matter to you.
examples
Examples
Closex.MockClient.get_lead(Closex.MockClient.lead_id()) ...contents of test/fixtures/lead.json...
iex> Closex.MockClient.get_lead(Closex.MockClient.not_found_id())
iex> Closex.MockClient.get_lead(Closex.MockClient.timeout_query()) {:error, %HTTPoison.Error{id: nil, reason: :timeout}}
get_lead_custom_field(id, opts \\ [])
Gets a lead custom field from CloseIO.
Returns {:ok, lead_custom_field}
.
You can hand in any custom field id you like and it will return an example custom field with that id.
We have provided an example id to use when the id doesn't matter to you.
examples
Examples
Closex.MockClient.get_lead_custom_field(Closex.MockClient.lead_custom_field_id()) ...contents of test/fixtures/lead_custom_field.json...
iex> Closex.MockClient.get_lead_custom_field(Closex.MockClient.not_found_id())
get_lead_statuses(opts \\ [])
Gets the potential statuses of a lead from CloseIO.
Returns {:ok, lead_statuses}
.
examples
Examples
Closex.MockClient.get_lead_statuses() ...contents of test/fixtures/lead_statuses.json...
get_opportunities(opts)
Callback implementation for Closex.ClientBehaviour.get_opportunities/1
.
get_opportunity(id, opts \\ [])
Gets an opportunity from CloseIO.
Returns {:ok, opportunity}
.
You can hand in any opportunity id you like and it will return an example opportunity with that id.
We have provided an example id to use when the id doesn't matter to you.
examples
Examples
Closex.MockClient.get_opportunity(Closex.MockClient.opportunity_id()) ...contents of test/fixtures/opportunity.json...
iex> Closex.MockClient.get_opportunity(Closex.MockClient.not_found_id())
get_opportunity_statuses(opts \\ [])
Gets the potential statuses of an opportunity from CloseIO.
Returns {:ok, opportunity_statuses}
.
examples
Examples
Closex.MockClient.get_opportunity_statuses() ...contents of test/fixtures/opportunity_statuses.json...
get_organization(id, opts \\ [])
Gets an organization from CloseIO.
Returns {:ok, organization}
.
You can hand in any organization id you like and it will return an example organization with that id.
We have provided an example id to use when the id doesn't matter to you.
examples
Examples
Closex.MockClient.get_organization(Closex.MockClient.organization_id()) ...contents of test/fixtures/organization.json...
iex> Closex.MockClient.get_organization(Closex.MockClient.not_found_id())
get_users(opts \\ [])
Gets the users available in CloseIO.
Returns {:ok, users}
.
examples
Examples
Closex.MockClient.get_users() ...contents of test/fixtures/users.json...
lead_custom_field_id()
lead_id()
multiple_results_query()
not_found_id()
not_found_query()
opportunity_id()
organization_id()
send_email(payload, opts \\ [])
Sends an email CloseIO.
Returns {:ok, email}
examples
Examples
Closex.MockClient.send_email(%{}) ...contents of test/fixtures/send_email.json...
timeout_query()
update_contact(id, map, _)
Callback implementation for Closex.ClientBehaviour.update_contact/3
.
update_lead(lead_id, payload, opts \\ [])
Update a lead in CloseIO.
Returns {:ok, lead}
.
You can hand in any lead id you like and it will return an example lead with that id, with your updates merged in. Any dates you hand in to be merged will be parsed to strings when they are returned.
We have provided an example id to use when the id doesn't matter to you.
examples
Examples
Closex.MockClient.update_lead(Closex.MockClient.lead_id(), %{}) ...contents of test/fixtures/organization.json...
iex> Closex.MockClient.update_lead(Closex.MockClient.not_found_id(), %{})
iex> Closex.MockClient.update_lead(Closex.MockClient.not_found_id(), %{"foo" => "bar"})
update_opportunity(opportunity_id, payload, opts \\ [])
Update an Opportunity in CloseIO.
Returns {:ok, opportunity}
.
You can hand in any opportunity id you like and it will return an example opportunity with that id, with your updates merged in.
We have provided an example id to use when the id doesn't matter to you.
examples
Examples
Closex.MockClient.update_opportunity(Closex.MockClient.opportunity_id(), %{}) ...contents of test/fixtures/organization.json...
iex> Closex.MockClient.update_opportunity(Closex.MockClient.not_found_id(), %{})
iex> Closex.MockClient.update_opportunity(Closex.MockClient.not_found_id(), %{"foo" => "bar"})