Skip to content

Commit

Permalink
Merge pull request #357 from vidmantas/test-segment-analytics-tracking
Browse files Browse the repository at this point in the history
Test Segment tracking after user creation
  • Loading branch information
joshsmith committed Oct 17, 2016
2 parents 88839d9 + 2368542 commit 593f659
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ config :code_corps, allowed_origins: ["http://localhost:4200"]
config :guardian, Guardian,
secret_key: "e62fb6e2746f6b1bf8b5b735ba816c2eae1d5d76e64f18f3fc647e308b0c159e"

config :code_corps, :analytics, CodeCorps.Analytics.InMemoryAPI
config :code_corps, :analytics, CodeCorps.Analytics.TestAPI

config :code_corps, :icon_color_generator, CodeCorps.RandomIconColor.TestGenerator

Expand Down
18 changes: 18 additions & 0 deletions lib/code_corps/analytics/test_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule CodeCorps.Analytics.TestAPI do
@moduledoc """
In-memory interface to simulate calling out to the Segment API,
sending back itself a message with passed parameters - they're used for assertions.
Each function should have the same signature as `CodeCorps.Analytics.SegmentAPI` and simply return `nil`.
"""

def identify(user_id, traits) do
send self(), {:identify, user_id, traits}
nil
end

def track(user_id, event_name, properties) do
send self(), {:track, user_id, event_name, properties}
nil
end
end
40 changes: 40 additions & 0 deletions test/controllers/user_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ defmodule CodeCorps.UserControllerTest do
assert user.id == slugged_route.user_id
end

test "calls segment tracking after user is created", %{conn: conn} do
conn = post conn, user_path(conn, :create), %{
"meta" => %{},
"data" => %{
"type" => "user",
"attributes" => Map.put(@valid_attrs, :password, "password"),
"relationships" => relationships
}
}
id = json_response(conn, 201)["data"]["id"]

assert_received {:track, id, "Signed Up", %{}}
end

test "does not create resource and renders errors when data is invalid", %{conn: conn} do
conn = post conn, user_path(conn, :create), %{
"meta" => %{},
Expand Down Expand Up @@ -137,6 +151,32 @@ defmodule CodeCorps.UserControllerTest do
assert user.biography == "Just a test user"
end

test "tracks authentication & update profile events in Segment", %{conn: conn} do
user = insert(:user)
attrs = Map.put(@valid_attrs, :password, "password")

params = %{
"meta" => %{},
"data" => %{
"type" => "user",
"id" => user.id,
"attributes" => attrs,
"relationships" => relationships
}
}

path = user_path(conn, :update, user)

conn =
conn
|> authenticate(user)
|> put(path, params)

id = json_response(conn, 200)["data"]["id"]
assert_received {:identify, id, attrs}
assert_received {:track, id, "Updated Profile", %{}}
end

test "does not update when authorized as different user", %{conn: conn} do
[user, another_user] = insert_pair(:user)

Expand Down

0 comments on commit 593f659

Please sign in to comment.