aboutsummaryrefslogtreecommitdiff
path: root/test/exdatadog_test.exs
blob: 5e752ab353bec43846735506bb0e0e8c16bf8b2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
defmodule ExdatadogTest do
  @moduledoc """
  Provides tests for the Exdatadog module
  """
  use ExUnit.Case

  alias HTTPoison.Response
  import Exdatadog

  doctest Exdatadog

  setup_all do
    :meck.new(Poison, [:no_link])

    on_exit fn ->
      :meck.unload(Poison)
    end
  end

  test "auth_params using api_key" do
    assert auth_params(%{api_key: "1234"}) == [api_key: "1234"]
  end

  test "auth_params using api_key and app_key" do
    expected = [application_key: "abcd", api_key: "1234"]
    assert auth_params(%{api_key: "1234", app_key: "abcd"}) == expected
  end

  test "auth_params with no auth" do
    assert auth_params(%{}) == []
    assert auth_params(nil) == []
  end

  test "process_response with 200" do
    assert process_response(%Response{status_code: 200,
                                      headers: %{},
                                      body: "json"}) == {200, "json"}
    assert :meck.validate(Poison)
  end

  test "process_response with non-200" do
    assert process_response(%Response{status_code: 404,
                                      headers: %{},
                                      body: "json"}) == {404, "json"}

    assert :meck.validate(Poison)
  end

  test "process_resposne_body with nil body" do
    assert process_response_body("") == nil
  end

  test "process_response_body with content" do
    :meck.expect(Poison, :decode!, 1, :decoded_json)
    assert process_response_body("json") == :decoded_json
  end

  test "process_response with empty body" do
    assert process_response(%Response{status_code: 202,
                                      headers: %{},
                                      body: nil}) == {202, nil}
  end

end