aboutsummaryrefslogtreecommitdiff
path: root/test/zendex/user_test.exs
blob: a1cee505faf1e8bbe413077658d1e8fb27d0f713 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
defmodule Zendex.UserTest do
  use ExUnit.Case, async: false

  @base_url "http://test.zendesk.com"

  setup do
    %{conn: Zendex.Connection.setup("http://test.zendesk.com", "User", "Passw")}
  end

  setup_all do
    :meck.new(HTTPoison)
    on_exit fn -> :meck.unload end
    :ok
  end

  test "list users", %{conn: conn} 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(conn)

    assert expected == actual
  end

  test "showing a user", %{conn: conn} do
    expected = %{"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"}}

    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", %{conn: conn} 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(conn, [6,67])

    assert expected == actual
  end

  test "getting related info on a user", %{conn: conn} do
    expected = %{"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}}

    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", %{conn: conn} 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(conn, %{user: %{name: "Roger", email: "roger@dodger.com"}})

    assert expected == actual
  end

  test "deleting a user", %{conn: conn} do
    expected = %{"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"}}

    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
end